Example 4-1
Activity 4-1public class FourDashOne{ public static void main(String[] args){ int grade = 68; if( grade > 60 ){ System.out.println("Congratulations!"); System.out.println("You passed!"); } }
Modify Example 4-1 such that the value of int grade is through user input. (Remember BufferedReader?) Filename: ActivityFourDashOne.java
Example 4-2
Activity 4-2public class FourDashTwo{ public static void main(String[] args){ int grade = 68; if( grade > 60 ){ System.out.println("Congratulations!"); System.out.println("You passed!"); } else{ System.out.println("Sorry you failed"); } }
Modify Example 4-2 such that the value of int grade is through user input.
Filename: ActivityFourDashOne.java
Example 4-3
Activity 4-3public class FourDashThree{ public static void main(String[] args){ int grade = 68; if( grade > 90 ){ System.out.println("Very good!"); } else if( grade > 60 ){ System.out.println("Very good!"); } else{ System.out.println("Sorry you failed"); } }
Write an executable class that evaluates Richter magnitude scale. User inputs Richter magnitude. The program displays the description and the earthquake effects based on the table below:
Richter Magnitude Description Earthquake Effects
Less than 2.0 Micro Microearthquakes, not felt.
2.0 - 2.9 Minor Generally not felt, but recorded.
3.0 – 3.9 Minor Often felt, but rarely causes damage.
4.0 – 4.9 Light Noticeable shaking of indoor items, rattling noises.
5.0 – 5.9 Moderate Can cause major damage to poorly constructed buildings.
6.0 – 6.9 Strong Can be destructive in areas up to about 160 km across.
7.0 – 7.9 Major Can cause serious damage over large areas.
8.0 – 8.9 Great Can cause serious damage in areas several hundred miles across
9.0 – 9.9 Great Devastating in areas several thousand miles across.
10.0 + Epic Never reorded
Filename: ActivityFourDashThree.java
Example 4-4
Activity 4-4import java.io.*; public class FourDashFour{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); char choice; System.out.println(“Options: “); System.out.println(“[1] Addition “); System.out.println(“[2] Subtraction “); System.out.println(“[3] Multiplication “); System.out.println(“[4] Division “); System.out.print(“Choice: “); choice = br.readLine().charAt(0); switch(choice) { case ‘1’: System.out.println(“Addition”); break; case ‘2’: System.out.println(“Subtraction”); break; case ‘3’: System.out.println(“Multiplication”); break; case ‘4’: System.out.println(“Division”); break; default: System.out.println(“Invalid option”); } }
Complete the functionality of example 4-4.
After selecting an operation, the user inputs two integers and the program performs the appropriate operation and displays the result.
Filename: ActivityFourDashThree.java
Activity 4-5
Use while-loop to make Activity 4-3 iterative. Processing is to be carried out 5 times.
Filename: ActivityFourDashFive.java
Activity 4-6
Use do-while loop to make Activity 4-4 iterative. Include a fifth option:
[5] Exit. When the user chooses 5, the program displays “Thanks!”.
The program loops until the user chooses 5.
Activity 4-7
Use for-loop to make Activity 4-3 iterative. Processing is to be carried out 5 times.
Filename: ActivityFourDashSeven.java