Help me with different activities in Java program
Example 4-1
Code :
public class FourDashOne{
public static void main(String[] args){
int grade = 68;
if( grade > 60 ){
System.out.println("Congratulations!");
System.out.println("You passed!");
}
}
Activity 4-1
Modify Example 4-1 such that the value of int grade is through user input. (Remember BufferedReader?) Filename: ActivityFourDashOne.java
Example 4-2
Code :
public 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");
}
}
Activity 4-2
Modify Example 4-2 such that the value of int grade is through user input.
Filename: ActivityFourDashOne.java
Example 4-3
Code :
public 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");
}
}
Activity 4-3
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
Code :
import 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”);
}
}
Activity 4-4
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
Re: i am a student.. need help in my activities today
Hello xyldon27. Welcome to the forums.
Please make sure you always post code within the code tags. I have done it for you this time.
Have you started any of this assignment yet? People won't normally give you the answers without you attempting it yourself first.
Please let us know where you are stuck on the first activity and we can take it from there..
Re: i am a student.. need help in my activities today
Code :
import java.io.*;
public class ActivityFourDashOne
{
public static void main(String[]args)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your grade: ");
String grade= br.readLine( );
int grade = 0;
if( grade > 75 ) {
System.out.println("You Passed Your grade is " + grade);
//System.out.println("Congratulations!");
//System.out.println("You passed!");
}
}
}
i can't compile it and i dont know what's missing or wrong
Re: i am a student.. need help in my activities today
The reason this code will not compile is because you have declared the variable 'grade' twice.
You have String grade and int grade. You can fix this by changing one of the variable names.
Once you have done that there is also one other problem. The BufferedReader requires you to handle the IOException. You can either add a try/catch block or simply throw the Exception.
Code :
import java.io.*;
public class ActivityFourDashOne {
public static void main(String[] args)[B] throws Exception[/B] {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your grade: ");
String grade = br.readLine();
[B]int myGrade[/B] = 0;
if ([B]myGrade[/B] > 75) {
System.out.println("You Passed Your grade is " + grade);
// System.out.println("Congratulations!");
// System.out.println("You passed!");
}
}
}
Re: i am a student.. need help in my activities today
I have edited the code to make it work as expected. You can go from here..
Code :
import java.io.*;
public class ActivityFourDashOne {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your grade: ");
String grade = br.readLine();
int myGrade = Integer.parseInt(grade);
if (myGrade > 75) {
System.out.println("You Passed Your grade is " + grade);
}
else{
System.out.println("Sorry you failed");
}
}
}
Re: i am a student.. need help in my activities today
thank you.. just got confused with the codes in my head,,, i'll proceed to the next activity
Re: i am a student.. need help in my activities today
Code :
import java.io.*;
public class ActivityFourDashThree {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your magnitude: ");
double magnitude = br.readLine();
double magnitude = Integer.parseint(magnitude);
if( magnitude>=10.0)
{
System.out.println("Epic");
System.out.println("Never recorded");
}
else if( magnitude>=9.0 && magnitude<=9.9)
{
System.out.println("Great");
System.out.println("Devastating in areas several thousand miles across.");
}
else if( magnitude>=8.0 && magnitude<=8.9)
{
System.out.println("Great");
System.out.println("Can cause serious damage in areas several hundred miles across");
}
else if( magnitude>=7.0 && magnitude<=7.9)
{
System.out.println("Major");
System.out.println("Can cause serious damage over large areas.");
}
else if( magnitude>=6.0 && magnitude<=6.9)
{
System.out.println("Strong");
System.out.println("Can be destructive in areas up to about 160 km across.");
}
else if( magnitude>=5.0 && magnitude<=5.9)
{
System.out.println("Moderate");
System.out.println("Can cause major damage to poorly constructed buildings. ");
}
else if( magnitude>=4.0 && magnitude<=4.9)
{
System.out.println("Light");
System.out.println("Noticeable shaking of indoor items, rattling noises. ");
}
else if( magnitude>=3.0 && magnitude<=3.9)
{
System.out.println("minor");
System.out.println("Often felt, but rarely causes damage.");
}
else if( magnitude>=2.0 && magnitude<=2.9)
{
System.out.println("minor");
System.out.println("Generally not felt, but recorded.");
}
else if( magnitude>=0 && magnitude<=2)
{
System.out.println("micro");
System.out.println("Microearthquakes, not felt.);
}
}
i can't find the error.. is double wrong? or i will use float?
Re: i am a student.. need help in my activities today
Code :
import java.io.*;
public class ActivityFourDashThree {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your magnitude: ");
double magnitude = br.readLine();
double magnitude = Integer.parseint(magnitude);
if( magnitude>=10.0)
{
System.out.println("Epic");
System.out.println("Never recorded");
}
else if( magnitude>=9.0 && magnitude<=9.9)
{
System.out.println("Great");
System.out.println("Devastating in areas several thousand miles across.");
}
else if( magnitude>=8.0 && magnitude<=8.9)
{
System.out.println("Great");
System.out.println("Can cause serious damage in areas several hundred miles across");
}
else if( magnitude>=7.0 && magnitude<=7.9)
{
System.out.println("Major");
System.out.println("Can cause serious damage over large areas.");
}
else if( magnitude>=6.0 && magnitude<=6.9)
{
System.out.println("Strong");
System.out.println("Can be destructive in areas up to about 160 km across.");
}
else if( magnitude>=5.0 && magnitude<=5.9)
{
System.out.println("Moderate");
System.out.println("Can cause major damage to poorly constructed buildings. ");
}
else if( magnitude>=4.0 && magnitude<=4.9)
{
System.out.println("Light");
System.out.println("Noticeable shaking of indoor items, rattling noises. ");
}
else if( magnitude>=3.0 && magnitude<=3.9)
{
System.out.println("minor");
System.out.println("Often felt, but rarely causes damage.");
}
else if( magnitude>=2.0 && magnitude<=2.9)
{
System.out.println("minor");
System.out.println("Generally not felt, but recorded.");
}
else if( magnitude>=0 && magnitude<=2)
{
System.out.println("micro");
System.out.println("Microearthquakes, not felt.");
}
}
}
this is 2nd try still errors
Re: i am a student.. need help in my activities today
There are several errors in your code. I have made the changes shown in bold:
Code :
import java.io.*;
public class ActivityFourDashThree {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your magnitude: ");
[B] String myMagnitude = br.readLine();[/B]
[B]double magnitude = Double.parseDouble(myMagnitude);[/B]
if( magnitude>=10.0)
{
System.out.println("Epic");
System.out.println("Never recorded");
}
else if( magnitude>=9.0 && magnitude<=9.9)
{
System.out.println("Great");
System.out.println("Devastating in areas several thousand miles across.");
}
else if( magnitude>=8.0 && magnitude<=8.9)
{
System.out.println("Great");
System.out.println("Can cause serious damage in areas several hundred miles across");
}
else if( magnitude>=7.0 && magnitude<=7.9)
{
System.out.println("Major");
System.out.println("Can cause serious damage over large areas.");
}
else if( magnitude>=6.0 && magnitude<=6.9)
{
System.out.println("Strong");
System.out.println("Can be destructive in areas up to about 160 km across.");
}
else if( magnitude>=5.0 && magnitude<=5.9)
{
System.out.println("Moderate");
System.out.println("Can cause major damage to poorly constructed buildings. ");
}
else if( magnitude>=4.0 && magnitude<=4.9)
{
System.out.println("Light");
System.out.println("Noticeable shaking of indoor items, rattling noises. ");
}
else if( magnitude>=3.0 && magnitude<=3.9)
{
System.out.println("minor");
System.out.println("Often felt, but rarely causes damage.");
}
else if( magnitude>=2.0 && magnitude<=2.9)
{
System.out.println("minor");
System.out.println("Generally not felt, but recorded.");
}
else if( magnitude>=0 && magnitude<=2)
{
System.out.println("micro");
[B]System.out.println("Microearthquakes, not felt.");[/B]
}
}
[B]}[/B]
Re: i am a student.. need help in my activities today
thanks.. i am very thankful for this site.. i will learn lots of things here..
Re: i am a student.. need help in my activities today
Quote:
Originally Posted by
xyldon27
thanks.. i am very thankful for this site.. i will learn lots of things here..
Glad I can help :D