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

Thread: Am I doing it right?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Am I doing it right?

    I want to make sure I am doing this programming assignment the right way.

    Question:
    Your history instructor gives three tests worth 50 points each. You can drop one of the first 
    two grades. Your final grade is the sum of the best of the first two grades and the third grade.
    Given three test grades, write a program that calculates the final letter grade using the following
    cut-off points. The output should list all three test grades, state what test was dropped and the
    final letter grade. 
     
    >= 90 A
    < 90, >= 80 B
    < 80, >= 70 C
    < 70, >= 60 D
    < 60 F
     
    For example, if your input is 45 15 25 (you do not need to format the input), 
     
    the output of your program should be very similar to:
     
    First test: 45
    Second test: 15
    Third test: 25
     
    After dropping test 2, the final grade is 70. The final letter grade is C.

    My Code
    package javaapplication28;
     
    import java.util.Scanner;
     
    public class JavaApplication28 
    {
     
        public static void main(String[] args) 
        {
            double firstTest, secondTest, thirdTest;
            double sum;
     
            Scanner keyboard = new Scanner (System.in);
     
            System.out.print("First Test: ");
            firstTest = keyboard.nextDouble();
            System.out.print("Second Test: ");
            secondTest = keyboard.nextDouble();
            System.out.print("Third Test: ");
            thirdTest = keyboard.nextDouble();
     
            if( firstTest > secondTest)
            {
                    sum = firstTest + thirdTest;
                    System.out.println("\nFirst Test:  " + firstTest);
                    System.out.println("Second Test: " + secondTest);
                    System.out.println("Third Test:  " + thirdTest);
                    System.out.print("\nAfter dropping test 2, The final grade is "
                            + sum + "." + "\n");
     
            }
            else if( secondTest > firstTest)
            {
                    sum = secondTest + thirdTest;
                    System.out.println("\nFirst Test:  " + firstTest);
                    System.out.println("Second Test: " + secondTest);
                    System.out.println("Third Test:  " + thirdTest);
                    System.out.print("\nAfter dropping test 1, The final grade is "
                            + sum + "." + "\n");
            }
     
            System.exit(0);
     
        }
    }
    Last edited by CSUTD; October 17th, 2011 at 02:00 PM.

  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Am I doing it right?

    I would say:

    package javaapplication28;
     
    import java.util.Scanner;
     
    public class Javaapplication28 
    {
     
        public static void main(String[] args) 
        {
            double firstTest, secondTest, thirdTest;
            double sum=0;
     
            Scanner keyboard = new Scanner (System.in);
     
            System.out.print("First Test: ");
            firstTest = keyboard.nextDouble();
            System.out.print("Second Test: ");
            secondTest = keyboard.nextDouble();
            System.out.print("Third Test: ");
            thirdTest = keyboard.nextDouble();
     
            System.out.println("\nFirst Test:  " + firstTest);
            System.out.println("Second Test: " + secondTest);
            System.out.println("Third Test:  " + thirdTest);
     
            if( firstTest >= secondTest)
            {
                System.out.print("After dropping test 2, The final grade is ");
                sum=firstTest+thirdTest;
     
            }
            else if( secondTest > firstTest)
            {
     
                 System.out.print("After dropping test 1, The final grade is ");    
                sum=secondTest+thirdTest;        
            }
     
            System.out.print(sum+". The final letter grade is ");
            if (sum >= 90)     System.out.print("A");
     
            if ((sum < 90)&&(sum >=80))  System.out.print("B");
     
            if ((sum < 80)&&(sum >=70))  System.out.print("C");
     
            if ((sum < 70)&&(sum >=60))  System.out.print("D");
     
            if (sum<60) System.out.print("F");
     
            System.out.println();
            System.exit(0);
     
        }
    }

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Am I doing it right?

    Quote Originally Posted by salnet View Post
    I would say:

    package javaapplication28;
     
    import java.util.Scanner;
     
    public class Javaapplication28 
    {
     
        public static void main(String[] args) 
        {
            double firstTest, secondTest, thirdTest;
            double sum=0;
     
            Scanner keyboard = new Scanner (System.in);
     
            System.out.print("First Test: ");
            firstTest = keyboard.nextDouble();
            System.out.print("Second Test: ");
            secondTest = keyboard.nextDouble();
            System.out.print("Third Test: ");
            thirdTest = keyboard.nextDouble();
     
            System.out.println("\nFirst Test:  " + firstTest);
            System.out.println("Second Test: " + secondTest);
            System.out.println("Third Test:  " + thirdTest);
     
            if( firstTest >= secondTest)
            {
                System.out.print("After dropping test 2, The final grade is ");
                sum=firstTest+thirdTest;
     
            }
            else if( secondTest > firstTest)
            {
     
                 System.out.print("After dropping test 1, The final grade is ");    
                sum=secondTest+thirdTest;        
            }
     
            System.out.print(sum+". The final letter grade is ");
            if (sum >= 90)     System.out.print("A");
     
            if ((sum < 90)&&(sum >=80))  System.out.print("B");
     
            if ((sum < 80)&&(sum >=70))  System.out.print("C");
     
            if ((sum < 70)&&(sum >=60))  System.out.print("D");
     
            if (sum<60) System.out.print("F");
     
            System.out.println();
            System.exit(0);
     
        }
    }
    I know how to do the rest of it...didn't need it done for me. I just wanted to know if the first part was correct.

  4. The Following User Says Thank You to CSUTD For This Useful Post:

    copeg (October 17th, 2011)

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default Re: Am I doing it right?

    Salnet, please read the forum rules as well as the following link:
    http://www.javaprogrammingforums.com...n-feeding.html

  6. #5
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Am I doing it right?

    package lab4_ex2;
     
    import java.util.Scanner;
     
    public class Lab4_Ex2
    {
     
         public static void main(String[] args) 
        {
            double firstTest,secondTest, thirdTest;
            double sum = 0;
            char grade;
     
            Scanner keyboard = new Scanner (System.in);
     
            System.out.print("First Test:  ");
            firstTest = keyboard.nextDouble();
            System.out.print("Second Test: ");
            secondTest = keyboard.nextDouble();
            System.out.print("Third Test:  ");
            thirdTest = keyboard.nextDouble();
     
            System.out.println("\nFirst Test:  " + firstTest);
            System.out.println("Second Test: " + secondTest);
            System.out.println("Third Test:  " + thirdTest);
     
           if( firstTest >= secondTest)
            {
                sum = firstTest + thirdTest;
                System.out.print("\nAfter dropping test 2, The final grade is " + 
                        sum + "." + " The final letter grade is " + grade );
            }
            else if( secondTest > firstTest)
            {
                sum = secondTest + thirdTest;
                System.out.print("\nAfter dropping test 1, The final grade is " + 
                        sum + "." + " The final letter grade is " + grade );
            }
     
            if ( sum >= 90 )
            {
                grade = 'A';
            }
            if ( sum < 90 && sum >= 80 )
            {
                grade = 'B';
            }
            else if ( sum < 80 && sum >= 70 )
            {
                grade = 'C';
            }
            else if ( sum < 70  && sum >= 60 )
            {
                grade = 'D';
            }
            else if( sum < 60 )
            {
                grade = 'F';
            }
        } // end main
    } // end class

    My issue is with this:
    if( firstTest >= secondTest)
            {
                sum = firstTest + thirdTest;
                System.out.print("\nAfter dropping test 2, The final grade is " + 
                        sum + "." + " The final letter grade is " + grade );

    my compiler is saying "variable grade may not have been initialized" but I initialized it at the top of the program.

  7. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Am I doing it right?

    Declaring a variable and initialising a variable are 2 different things. Initialising is giving a variable an initial value.
    Improving the world one idiot at a time!