8 Replies - 3576 Views - Last Post: 07 November 2011 - 05:41 PM Rate Topic: -----

#1 dedios09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Calculating Letter Grades

Posted 04 November 2011 - 09:59 AM

import java.util.Scanner;

/**
 * Students in a course take four exams and earn a letter grade (A, A-, B+, B,
 * B-, C+, C, C-, D+, D, D-, or F) for each of them. The course grade is
 * determined by dropping the lowest grade and averaging the three remaining
 * grades. To average grades, first convert them to number grades, using the
 * usual scheme A = 4.0, A- = 3.7, B+ = 3.3, . . ., D- = 0.7, F = 0. Then
 * compute their average and convert it back to the closest letter grade. For
 * example, an average of 3.51 would be an A-.
 *
 * Your task is to read inputs of
 * the form: letterGrade1 letterGrade2 letterGrade3 letterGrade4
 * For example, A- B+ C A
 *
 * For each input line, your output should be letterGrade where the
 * letter grade is the grade earned in the course, as just described.
 * For example, A
 *
 * The end of inputs will be indicated by a letterGrade1 field of -.
 *
 * @author smitra
 *
 */

public class grades {

	/**
	 * The program starts executing in the main method. It keeps calling processALine
	 * method until it returns false.
	 *
	 * @param args
	 */
	public static void main(String[] args) {
	Scanner in = new Scanner(System.in);
	System.out.print("Please enter four letter grades: ");
	
	double letterGrade1;
	double letterGrade2;
	double letterGrade3;
	double letterGrade4;
	
	letterGrade1 =  in.nextDouble();
	letterGrade2 =  in.nextDouble();
	letterGrade3 =  in.nextDouble();
	letterGrade4 =  in.nextDouble();
	
	
	
	

	}

	/**
	 * Processes one line of input.
	 * @return true if the sentinel (the - character) was not encountered
	 */
	static boolean processALine(Scanner in)
	{	if (letterGrade1 != "-") 
		return true;

		return false; // this line is just a placeholder
	}


	/**
	 * Returns the smallest of four numbers.
	 *
	 * @param a   a number
	 * @param b   a number
	 * @param c   a number
	 * @param d   a number
	 * @return the smallest of a, b, c, and d
	 */
	public static double min(double a, double b, double c, double d)
	{
		// TODO: write code for this method

		return 0; // this line is just a placeholder
	}


	/**
	 * Converts a number to the nearest letter grade using the
	 * usual scheme A = 4.0, A- = 3.7, B+ = 3.3, . . ., D- = 0.7, F = 0.
	 *
	 * For example, if x is the number grade, then we have:
	 * 2.5  x < 2.85: B-
	 * 2.85  x < 3.15: B
	 * 3.15  x < 3.5: B+
	 *
	 * Similarly, for other numbers.
	 *
	 * @param x a number between 0 and 4
	 * @return the nearest letter grade
	 */
	public static String numberToGrade(double x) {

		if (n == "A") return "4.0";
		if (n == "A-") return "3.7";
		if (n == "B+") return "3.3";
		if (n == "B") return "3.1";
		if (n == "B-") return "2.8";
		if (n == "C+") return "2.4 ";
		if (n == "C") return "2.0 ";
		if (n == "C-") return "1.7 ";
		if (n == "D+") return "1.3 ";
		if (n == "D") return "1.0 ";
		if (n == "D-") return "0.7";
		if (n == "F") return "0.0";

		return ""; // this line is just a placeholder

	}


	/**
	 * Converts a letter grade to a number using the
	 * usual scheme A = 4.0, A- = 3.7, B+ = 3.3, . . ., D- = 0.7, F = 0.
	 *
	 * @param grade a letter grade (A+, A, A-, . . ., D-, F)
	 * @return the equivalent number grade
	 */
	public static double gradeToNumber(String grade)
	{
		if (n == "A") return "4.0";
		

		return 0; // this line is just a placeholder

	}


} // end of class Grades


Can someone help me out. Am i on the right track when it comes to the main method? Also going into the boolean method i'm not sure what i'm asked to do..any help would be appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Calculating Letter Grades

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1132
  • View blog
  • Posts: 2,490
  • Joined: 05-May 05

Re: Calculating Letter Grades

Posted 04 November 2011 - 10:47 AM

Firstly, you should be reading in the data as strings, not floating point integers. You have

letterGrade1 =  in.nextDouble();
	letterGrade2 =  in.nextDouble();
	letterGrade3 =  in.nextDouble();
	letterGrade4 =  in.nextDouble();



yet you said in the program requirements that an example input was

Quote

* Your task is to read inputs of
* the form: letterGrade1 letterGrade2 letterGrade3 letterGrade4
* For example, A- B+ C A


So you should be using Scanner.next.

In processALine, which should be called processLine, or something more meaningful, like isExitSymbol, your want to see if the string is a dash. It's formal parameter should be a String, not a Scanner. And as for the implementation (the code inside the block), using a conditional is correct, but you shouldn't use != on strings. That's not semantically correct. Use String.equals.

numberToGrade is incorrect.

public static String numberToGrade(double x) {

		if (n == "A") return "4.0";



Semantically (and syntactically), it doesn't even make sense. Firstly, where has n been declared? Secondly, assuming that you meant

if (x == "A") return "4.0";


your trying to compare x, which is a double, to a string.

public static double numberToGrade(double x) {

		if (x == 4.0) return "A";


Was This Post Helpful? 1
  • +
  • -

#3 dedios09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Re: Calculating Letter Grades

Posted 04 November 2011 - 11:11 AM

View Postblackcompe, on 04 November 2011 - 10:47 AM, said:

Firstly, you should be reading in the data as strings, not floating point integers. You have

letterGrade1 =  in.nextDouble();
	letterGrade2 =  in.nextDouble();
	letterGrade3 =  in.nextDouble();
	letterGrade4 =  in.nextDouble();



yet you said in the program requirements that an example input was

Quote

* Your task is to read inputs of
* the form: letterGrade1 letterGrade2 letterGrade3 letterGrade4
* For example, A- B+ C A


So you should be using Scanner.next.

In processALine, which should be called processLine, or something more meaningful, like isExitSymbol, your want to see if the string is a dash. It's formal parameter should be a String, not a Scanner. And as for the implementation (the code inside the block), using a conditional is correct, but you shouldn't use != on strings. That's not semantically correct. Use String.equals.

numberToGrade is incorrect.

public static String numberToGrade(double x) {

		if (n == "A") return "4.0";



Semantically (and syntactically), it doesn't even make sense. Firstly, where has n been declared? Secondly, assuming that you meant

if (x == "A") return "4.0";


your trying to compare x, which is a double, to a string.

public static double numberToGrade(double x) {

		if (x == 4.0) return "A";



public static double min(double a, double b, double c, double d)
	{
		if ((a < b && a < c) || (a < d));
		return a;
		
		if ((b < a && b < c) || (b < d));
		return b;
		
		if ((c < a && c < B)/> ||(c < d));
		return c;
		
		if ((d < a && d < b && d < c));
		return d;

		return 0; // this line is just a placeholder
	}


	/**
	 * Converts a number to the nearest letter grade using the
	 * usual scheme A = 4.0, A- = 3.7, B+ = 3.3, . . ., D- = 0.7, F = 0.
	 *
	 * For example, if x is the number grade, then we have:
	 * 2.5  x < 2.85: B-
	 * 2.85  x < 3.15: B
	 * 3.15  x < 3.5: B+
	 *
	 * Similarly, for other numbers.
	 *
	 * @param x a number between 0 and 4
	 * @return the nearest letter grade
	 */
	public static String numberToGrade(double x) {

		if (x == 4.0) return "A";
		if (x == 3.7) return "A-";
		if (x == 3.3) return "B+";
		if (x == 3.1) return "B";
		if (x == 2.8) return "B-";
		if (x == 2.4) return "C+";
		if (x == 2.0) return "C ";
		if (x == 1.7) return "C- ";
		if (x == 1.3) return "D+ ";
		if (x == 1.0) return "D ";
		if (x == 0.7) return "D-";
		if (x == 0.0) return "F";

		return ""; // this line is just a placeholder

	}


	/**
	 * Converts a letter grade to a number using the
	 * usual scheme A = 4.0, A- = 3.7, B+ = 3.3, . . ., D- = 0.7, F = 0.
	 *
	 * @param grade a letter grade (A+, A, A-, . . ., D-, F)
	 * @return the equivalent number grade
	 */
	public static double gradeToNumber(String grade)
	{
		if (x == "A+") return 4.0;
		if (n == "A-") return 3.7;
		if (n == "B+") return 3.3;
		if (n == "B") return 3.1;
		if (n == "B-") return 2.8;
		if (n == "C+") return 2.4;
		if (n == "C") return 2.0;
		if (n == "C-") return 1.7;
		if (n == "D+") return 1.3;
		if (n == "D") return 1.0;
		if (n == "D-") return 0.7;
		if (n == "F") return 0.0;
		

		return 0; // this line is just a placeholder

	}


} // end of class Grades


Okay i changed all the 'n' values to x and that seemed to work, also for the boolean method am i doing that correctly cause its asking to return the smallest of those 4.
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1132
  • View blog
  • Posts: 2,490
  • Joined: 05-May 05

Re: Calculating Letter Grades

Posted 04 November 2011 - 12:32 PM

This about right. Those logical ORs need to be logical ANDs. And it should be less than or equal. If a, b, c, and d are the same none of your conditions are true.

 if(a <= b && a <= c && a <= d)

This post has been edited by blackcompe: 04 November 2011 - 12:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Calculating Letter Grades

Posted 04 November 2011 - 12:39 PM

You should also consider changing all of those if's to an else if statements.. dont want it to check 20 times if it is the first 1.
Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1132
  • View blog
  • Posts: 2,490
  • Joined: 05-May 05

Re: Calculating Letter Grades

Posted 04 November 2011 - 01:06 PM

A better way of calculating min would be:


public class Main {

	public static void main( String[] args ) {
		System.out.println( min( 23, 5345, -9, 29 ) );
	}

	static int min( int a, int b, int c, int d ) {
		int e = min( a, b );
		int f = min( c, d );
		return min( e, f );
	}

	static int min( int a, int b ) {
		return ( a < b ) ? a : b;
	}
}


This post has been edited by blackcompe: 04 November 2011 - 01:07 PM

Was This Post Helpful? 0
  • +
  • -

#7 AMZDeCoder  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 102
  • Joined: 04-November 11

Re: Calculating Letter Grades

Posted 04 November 2011 - 01:39 PM

Dear dedios09,

Following is the solution I have developed for your problem, it should work perfectly, for all the cases. Do try it out, also are you going to input the cases manually or automated ???

I have made use of maps, and look-up techniques and so the code is very efficient in execution. The code you have developed is not very efficient it seems and has a lot of unnecessary functions which i tried to avoid. Do let me know if you are satisfied with my solution.

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

/**
 * 
 */

/**
 * @author ameya Software Consultant at http://aknsoftwaresolutions.110mb.com/
 * 
 * 
 */
public class Grades {

	private final static HashMap gradeMap = new HashMap();

	private final static HashMap reverseGradeMap = new HashMap();

	static {
		gradeMap.put("A", 4.0);
		gradeMap.put("A-", 3.7);
		gradeMap.put("B+", 3.3);
		gradeMap.put("B", 3.1);
		gradeMap.put("B-", 2.8);
		gradeMap.put("C+", 2.4);
		gradeMap.put("C", 2.0);
		gradeMap.put("C-", 1.7);
		gradeMap.put("D+", 1.3);
		gradeMap.put("D", 1.0);
		gradeMap.put("D-", 0.7);
		gradeMap.put("F", 0.0);

		reverseGradeMap.put(4.0, "A");
		reverseGradeMap.put(3.7, "A-");
		reverseGradeMap.put(3.3, "B+");
		reverseGradeMap.put(3.1, "B");
		reverseGradeMap.put(2.8, "B-");
		reverseGradeMap.put(2.4, "C+");
		reverseGradeMap.put(2.0, "C");
		reverseGradeMap.put(1.7, "C-");
		reverseGradeMap.put(1.3, "D+");
		reverseGradeMap.put(1.0, "D");
		reverseGradeMap.put(0.7, "D-");
		reverseGradeMap.put(0.0, "F");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		// Read the letter grades.
		String[] letterGradesArray = new String[4];
		letterGradesArray[0] = in.next();
		letterGradesArray[1] = in.next();
		letterGradesArray[2] = in.next();
		letterGradesArray[3] = in.next();
		Double[] numericGradesArray = new Double[4];

		// Convert them to numeric grades from the lookup table.
		for (int i = 0; i < letterGradesArray.length; i++) {
			numericGradesArray[i] = (Double) gradeMap.get(letterGradesArray[i]);
		}
		// Sort in ascending order.
		Arrays.sort(numericGradesArray);
		// Now just take the average of last 3 elements and show the course
		// grade as output, that will eliminate the grade with the lowest score.
		Double courseGrade = (numericGradesArray[1] + numericGradesArray[2] + numericGradesArray[3]) / 3;
		// Now get the letter grade corresponding to the course grade, round up
		// the course grade first.

		courseGrade = Math.round(courseGrade * 10) / 10.0;

		// Now check if there in the map else keep incrementing by 0.1
		while (reverseGradeMap.get(courseGrade) == null) {
			courseGrade += 0.1;
		}
		// Print the final course grade.
		System.out.println(reverseGradeMap.get(courseGrade));
	}
}




Was This Post Helpful? 0
  • +
  • -

#8 123ISU  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-November 11

Re: Calculating Letter Grades

Posted 07 November 2011 - 05:31 PM

This is a homework assignment for the Iowa State University Computer Science 207 class for Fall 2011.

This is a homework assignment for the Iowa State University Computer Science 207 class for Fall 2011.
FYI I just had to google "static boolean processALine(Scanner in){}" to find this.
Was This Post Helpful? 0
  • +
  • -

#9 dedios09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Re: Calculating Letter Grades

Posted 07 November 2011 - 05:41 PM

View PostAMZDeCoder, on 04 November 2011 - 01:39 PM, said:

Dear dedios09,

Following is the solution I have developed for your problem, it should work perfectly, for all the cases. Do try it out, also are you going to input the cases manually or automated ???

I have made use of maps, and look-up techniques and so the code is very efficient in execution. The code you have developed is not very efficient it seems and has a lot of unnecessary functions which i tried to avoid. Do let me know if you are satisfied with my solution.

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

/**
 * 
 */

/**
 * @author ameya Software Consultant at http://aknsoftwaresolutions.110mb.com/
 * 
 * 
 */
public class Grades {

	private final static HashMap gradeMap = new HashMap();

	private final static HashMap reverseGradeMap = new HashMap();

	static {
		gradeMap.put("A", 4.0);
		gradeMap.put("A-", 3.7);
		gradeMap.put("B+", 3.3);
		gradeMap.put("B", 3.1);
		gradeMap.put("B-", 2.8);
		gradeMap.put("C+", 2.4);
		gradeMap.put("C", 2.0);
		gradeMap.put("C-", 1.7);
		gradeMap.put("D+", 1.3);
		gradeMap.put("D", 1.0);
		gradeMap.put("D-", 0.7);
		gradeMap.put("F", 0.0);

		reverseGradeMap.put(4.0, "A");
		reverseGradeMap.put(3.7, "A-");
		reverseGradeMap.put(3.3, "B+");
		reverseGradeMap.put(3.1, "B");
		reverseGradeMap.put(2.8, "B-");
		reverseGradeMap.put(2.4, "C+");
		reverseGradeMap.put(2.0, "C");
		reverseGradeMap.put(1.7, "C-");
		reverseGradeMap.put(1.3, "D+");
		reverseGradeMap.put(1.0, "D");
		reverseGradeMap.put(0.7, "D-");
		reverseGradeMap.put(0.0, "F");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		// Read the letter grades.
		String[] letterGradesArray = new String[4];
		letterGradesArray[0] = in.next();
		letterGradesArray[1] = in.next();
		letterGradesArray[2] = in.next();
		letterGradesArray[3] = in.next();
		Double[] numericGradesArray = new Double[4];

		// Convert them to numeric grades from the lookup table.
		for (int i = 0; i < letterGradesArray.length; i++) {
			numericGradesArray[i] = (Double) gradeMap.get(letterGradesArray[i]);
		}
		// Sort in ascending order.
		Arrays.sort(numericGradesArray);
		// Now just take the average of last 3 elements and show the course
		// grade as output, that will eliminate the grade with the lowest score.
		Double courseGrade = (numericGradesArray[1] + numericGradesArray[2] + numericGradesArray[3]) / 3;
		// Now get the letter grade corresponding to the course grade, round up
		// the course grade first.

		courseGrade = Math.round(courseGrade * 10) / 10.0;

		// Now check if there in the map else keep incrementing by 0.1
		while (reverseGradeMap.get(courseGrade) == null) {
			courseGrade += 0.1;
		}
		// Print the final course grade.
		System.out.println(reverseGradeMap.get(courseGrade));
	}
}





this is actually wonderful code and i'm learning new things from it, but is there anyway to adjust it for the methods that i've listed. I just don't really know what my professor is asking but anything close to it would help me out.

nevermind i've got this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1