# Am I doing it right?

• October 17th, 2011, 01:57 PM
CSUTD
Am I doing it right?
I want to make sure I am doing this programming assignment the right way.

Question:
Code :

```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
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);   } }```
• October 17th, 2011, 03:03 PM
salnet
Re: Am I doing it right?
I would say:

Code :

```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);   } }```
• October 17th, 2011, 03:42 PM
CSUTD
Re: Am I doing it right?
Quote:

Originally Posted by salnet
I would say:

Code :

```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.
• October 17th, 2011, 03:46 PM
copeg
Re: Am I doing it right?
http://www.javaprogrammingforums.com...n-feeding.html
• October 17th, 2011, 08:48 PM
CSUTD
Re: Am I doing it right?
Code :

```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:
Code :

```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.
• October 17th, 2011, 09:26 PM
Junky
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.