Results 1 to 4 of 4

Thread: weighted GPA

  1. #1
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Question weighted GPA

    Hello there,

    This is my first time posting here. I hope i've done it correctly. I'm writing java code for a weighted a GPA with user inputs. I keep getting the error that i have incompatible file types but i can't seem to fix it. Here's the code:

    Java Code:
    import java.util.*;
    
    public class GradePointAvg
    {
    	public static void main(String[] args) 
    	{
    		
    		Scanner keysIn = new Scanner(System.in);	
    
    		//variable declarations
    		double numCourses = 0;
    		double totalGrade = 0;
    		double totalCreditValue = 0;
    		char keepGoing = '\0';
    		
    	
    
    		//ask the user for their marks and ask if they want to keep going
    		while (keepGoing == 'Y')
    		{
    			try
    			{
    				//get grade - grade cannot be negative or more than 110%
    			String grade = getValidNumber(keysIn, "Enter numeric grade: ",
    				0, 110);
    
    			//add the grade to the total grade
    			grade += totalGrade;
    
    			//get grade value - value cannot be negative or more than 9
    			String value = getPoints(keysIn, "Enter Credit Value: ",
    				0, 9);
    
    			//add the value to the totalcredit value
    			value += totalCreditValue;
    
    			}
    			catch (InputMismatchException ex)
    			{
    				System.out.println(ex);
    			}
    			
    			System.out.print("Do you wish to add another? (Y/N) ");
    			keepGoing = Character.toUpperCase(keysIn.next().charAt(0));
    
    			//add one to the # of course recorded
    			numCourses++;
    
    			//output
    			double avgGrade = totalGrade / numCourses;
    			System.out.printf("\nAverage Grade: %.2f",
    				avgGrade);
    
    			double gpa  = totalCreditValue / numCourses;
    			System.out.printf("\nGPA: %.2f %n", gpa);
    		}
    	}
    	//method getValidNumber
    	public static Double getValidNumber(Scanner in, String prompt, 
    		double min, double max)
    		{
    		System.out.print(prompt);
    		Double value = in.nextDouble();
    		while (value <= min || value > max)
    		{
    			System.out.println("Grades must be between " +
    				min + " and " + max + ".");
    			System.out.print(prompt);
    			value = in.nextDouble();
    		}
    			return value;
    			}
    
    	//method getPoints
    	public static Double getPoints(Scanner in, String prompt, 
    		double min, double max)
    		{
    		System.out.print(prompt);
    		Double value = in.nextDouble();
    		while (value <= min || value > max)
    		{
    			System.out.println("Values must be between " +
    				min + " and " + max + ".");
    			System.out.print(prompt);
    			value = in.nextDouble();
    		}
    			return value;
    			
    	}
    }
    and the error it generates is:

    [error]
    C:\Users\mona\SchoolWork\prog10082java\Programs\La bs\Lab4\GradePointAvg.java:65: incompatible types
    found : java.lang.Double
    required: java.lang.String
    String grade = getValidNumber(keysIn, "Enter numeric grade: ",
    ^
    C:\Users\mona\SchoolWork\prog10082java\Programs\La bs\Lab4\GradePointAvg.java:72: incompatible types
    found : java.lang.Double
    required: java.lang.String
    String value = getPoints(keysIn, "Enter Credit Value: ",
    ^
    2 errors
    Press any key to continue . . .
    [/error]

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    I haven't checked your program logic, since you seemed confident in it.

    As for your type problems, it's because your method returns a Double, but you try to store it in a String. Also, instead of "Double", your type should be "double" (the primitive type), since Scanner.nextDouble returns a "double".
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Double and double are interchangeable as far as I know. Also, your logic is flimsy. I would suggest using a do-while loop instead of the while loop. something like this:

    [code]
    do{
    //code already inside the while loop
    }while (keepGoing=='Y')
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by Singing Boyo View Post
    Double and double are interchangeable as far as I know.
    You're right that since Java 5, the line between Double and double has become small, since the values are now boxed and unboxed automatically; however, the line is not completely gone.


    Quoted from Java Autoboxing Tutorial
    So when should you use autoboxing and unboxing? Use them only when there is an “impedance mismatch” between reference types and primitives, for example, when you have to put numerical values into a collection. It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code. An Integer is not a substitute for an int; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it.

    In the code presented, the use of Double serves no purpose, since all values are actually doubles. You should use a Double, for example, when storing doubles in a List, since a collection can only hold objects.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •