Beginner to programming and Java. How would I create a Java class that will conv
ID: 3919494 • Letter: B
Question
Beginner to programming and Java.
How would I create a Java class that will convert a number grade to a letter grade with the following information:
The user will enter a numeric grade from 0-100. The application will then display the corresponding letter grade. The application will also prompt the user if they wish to continue. If the user enters "Y" in either upper or lower case, the application will start over.
The grading criterion is:
A 90-100
B 80-89
C 70-79
D 60-69
F less than 60
Assuming user will enter the valid integer numbers.
Again, I'm a beginner, so please leave good comments with explanations. Thank you
Explanation / Answer
Ok, here is the code
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int number; char choice;
do
{
System.out.print("Please enter an interger in the range 0-100: ");
number = s.nextInt();
if(number>=90 && number<=100)
System.out.println("Grade is A");
else if(number>=80 && number<=89)
System.out.println("Grade is B");
else if(number>=70 && number<=79)
System.out.println("Grade is C");
else if(number>=60 && number<=69)
System.out.println("Grade is D");
else if(number<60)
System.out.println("Grade is F");
System.out.print("Do you wish to continue? ");
choice = s.next().charAt(0);
}
while(choice=='Y' || choice=='y');
}
}
Explanation :
Let us create a class named MyClass and start the main method like this..
public static void main(String args[])
Program execution starts from within the main block.
To use java functions and classes particular to our requirement, we should import them.
import java.util.*;
We need to use Scanner class and its methods for taking input, we used the above statement.
Variables Declaration:
int number; char choice;
number for entering in the range 0 - 100 and choice for entering y or n
Do-while loop:
Its a program construction which looks like this.
do
something
while condition..
i.e., it executes something once and checks if the condition provided in the while is true or not, if the conditionis true it executes something again and again until the condition is false.
So, according to our program... we asked the user for input and converted to grade and then asked if they wish to continue.. if their answer is Y or y, we execute the same again and again until user says no.
hence, our condition in the while statement is while(choice=='Y' || choice=='y');
Calculating the grade:
Inside do, we wrote this logic.
i.e., after getting the input number we check the number using if and else if statements..
if(number>=90 && number<=100)
System.out.println("Grade is A");
if the number doesn't satisfy the above condition, if doesn't print anything and goes to next statements in the sequence.. i.e.,
else if(number>=80 && number<=89)
System.out.println("Grade is B");
It processes the sequence and checks which range matches the given input number and prints accordingly.
After completing that, it asks for user choice
System.out.print("Do you wish to continue? ");
choice = s.next().charAt(0);
s.next().charAt(0) takes a single character from the input stream and then checks if it is y or Y and processes accordingly.
Now the program along with comments.
//import statement to use Scanner class
import java.util.*;
//class
public class MyClass {
//main method
public static void main(String args[]) {
//creating an object of Scanner class
Scanner s = new Scanner(System.in);
//declaring variables
int number; char choice;
//do something; here something is calculating the grade and asking user for their choice to continue or not
do
{
//prompting user to enter and interger
System.out.print("Please enter an integer in the range 0-100: ");
//taking input using Scanner object
number = s.nextInt();
//checking the conditions based on the range given and prints the grade
if(number>=90 && number<=100)
System.out.println("Grade is A");
else if(number>=80 && number<=89)
System.out.println("Grade is B");
else if(number>=70 && number<=79)
System.out.println("Grade is C");
else if(number>=60 && number<=69)
System.out.println("Grade is D");
else if(number<60)
System.out.println("Grade is F");
//asking if they wish to continue
System.out.print("Do you wish to continue? ");
//taking the input
choice = s.next().charAt(0);
}
//checking the input, goes to the do again if the choice is Y or y
while(choice=='Y' || choice=='y');
}
}
Sample Output:
Please enter an integer in the range 0-100: 90
Grade is A
Do you wish to continue? y
Please enter an integer in the range 0-100: 89
Grade is B
Do you wish to continue? n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.