Can anyone help me this problem The detailed description of
Can anyone help me this problem?
The detailed description of this problem comes from the Programming Exercise P3.12 that is in the book (page 126).
You are to write a program that will take a grade as a String. This grade has the form of a letter (e.g. A, B, C, D, F) followed by an optional sign (e.g. +, -). The letter values are worth 4.0 grade points for an A, 3.0 for a B, 2.0 for a C, 1.0 for a D, and 0 for an F. The + sign add 0.3 to the grade points and the - sign subtracts 0.3. Note that an \"A+\" is still 4.0 points and an \"F+\" or \"F-\" is still 0 grade points.
Hints:
You can use the length() method of the String object to figure out how many characters are in your input.
You can use the charAt() method of the String object to extract a character from your string.
This problem should be solved by writing all your code in public static void main. You will want to implement the algorithm inside of the main method.
Using the test input, your output should look like:
Enter a letter grade: A+ The numeric value is 4.0
Getting Started
Like our last exercise, we are going to do this exercise by writing the source code that solves the problem first in Grade.java. Using the techniques shown on the web page titled How to Start Every Project in this Class create a source file called Grade.java. This is where your code will go. Replace the code in that file with the code in the grey box below:
package edu.sbcc.cs105;
public class Main {
public static float computeGradePoint(String letterGrade) {
// Put code to compute grade points from letter grade.
}
public static void main(String[] args) {
// Put code to read a letter grade from the user
}
}
Solution
I modified According to your requirement.Please check once.Thank You.
Grade.java
package org.students;
import java.util.Scanner;
public class Grade {
public static void main(String[] args)
{
//Declaring variables
String grade;
float val;
//Scanner class object is used to read the inputs entered by the user.
Scanner sc=new Scanner(System.in);
//getting the grade entered by the user
System.out.print(\"Enter a letter grade:\");
grade=sc.next();
//calling the method computeGradePoint() by passing the grade as input
val=computeGradePoint(grade);
//Displaying the numeric value of the grade
System.out.println(\"The Numeric value is :\"+val);
}
/* This method will calculate the numerical value of the grade
* Params : String grade
* Return : grade value of float type
*/
public static float computeGradePoint(String letterGrade) {
//Declaring local variable
float value=0;
if(letterGrade.length()==1)
{
if(letterGrade.equals(\"A\"))
value=4;
else if(letterGrade.equals(\"B\"))
value=3;
else if(letterGrade.equals(\"C\"))
value=2;
else if(letterGrade.equals(\"D\"))
value=1;
else if(letterGrade.equals(\"F\"))
value=0;
}
else if(letterGrade.length()==2)
{
//if the grade is A+ the numeric value is 4.0
if(letterGrade.charAt(0)==\'A\' && letterGrade.charAt(1)==\'+\')
value=4;
//if the grade is F+ or the numeric value is 0.0
else if((letterGrade.charAt(0)==\'F\' && letterGrade.charAt(1)==\'+\')|| (letterGrade.charAt(0)==\'F\' && letterGrade.charAt(1)==\'-\'))
value=0;
else if( letterGrade.charAt(0)==\'A\')
value=4;
if(letterGrade.charAt(1)==\'-\')
value-=0.3;
//If the grade is B+ or B- calculating the numeric value
else if(letterGrade.charAt(0)==\'B\')
{
value=3;
if(letterGrade.charAt(1)==\'+\')
value+=0.3;
else if(letterGrade.charAt(1)==\'-\')
value-=0.3;
}
//If the grade is C+ or C- calculating the numeric value
else if(letterGrade.charAt(0)==\'C\')
{
value=2;
if(letterGrade.charAt(1)==\'+\')
value+=0.3;
else if(letterGrade.charAt(1)==\'-\')
value-=0.3;
}
//If the grade is D+ or D- calculating the numeric value
else if(letterGrade.charAt(0)==\'D\')
{
value=1;
if(letterGrade.charAt(1)==\'+\')
value+=0.3;
else if(letterGrade.charAt(1)==\'-\')
value-=0.3;
}
}
return value;
}
}
___________________________________
Output1:
Enter a letter grade:A+
The Numeric value is :4.0
___________________________________
Output2:
Enter a letter grade:F+
The Numeric value is :0.0
___________________________________
Output3:
Enter a letter grade:B-
The Numeric value is :2.7
___________________________________
Output4:
Enter a letter grade:C+
The Numeric value is :2.3
___________Thank You


