Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Help me with different activities in Java program

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    21
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Help me with different activities in Java program

    Example 4-1

    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
    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
    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
    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


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    21
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: i am a student.. need help in my activities today

    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

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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.

    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!");
            }
     
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    xyldon27 (June 9th, 2009)

  6. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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..

    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");
            }
     
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. The Following User Says Thank You to JavaPF For This Useful Post:

    xyldon27 (June 9th, 2009)

  8. #6
    Junior Member
    Join Date
    Jun 2009
    Posts
    21
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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

  9. #7
    Junior Member
    Join Date
    Jun 2009
    Posts
    21
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: i am a student.. need help in my activities today

    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?

  10. #8
    Junior Member
    Join Date
    Jun 2009
    Posts
    21
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: i am a student.. need help in my activities today

     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

  11. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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:

    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]
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  12. The Following User Says Thank You to JavaPF For This Useful Post:

    xyldon27 (June 9th, 2009)

  13. #10
    Junior Member
    Join Date
    Jun 2009
    Posts
    21
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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..

  14. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: i am a student.. need help in my activities today

    Quote Originally Posted by xyldon27 View Post
    thanks.. i am very thankful for this site.. i will learn lots of things here..
    Glad I can help
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.