In: Computer Science
Create an Java application that uses a class to convert number grades to letter grades and another class for data validation.
Specifications
public void setNumber(int number) public int getNumber()
public String getLetter()
A 88-100
B 80-87
C 67-79
D 60-66
F <60
im having some issue getting the overloading of the three string functions to pass values and validate the required entry validation
public class Console {
// require the user to input a value
public static String getString(String prompt) {
return getString(prompt, false);
}
// check that an input has been entered if not return an error
public static String getString(String prompt, boolean required)
{
String choice = "";
Scanner scanner = new Scanner(System.in);
//repeat until a valid input is entered
while (true) {
System.out.print(prompt);
if (choice.isEmpty() && required == true) {
choice = scanner.next();
if (scanner.hasNext()) {
choice = scanner.next();
System.out.println("Error! y or n. Try again.");
}
} else{
}
scanner.nextLine(); // discard any other data entered on the
line
}
return getString(prompt,"y", "n");
}
// require one of two specified string values.
public static String getString(String prompt, String choiceOne,
String choiceTwo) {
String choice = "";
boolean isValid = false;
Scanner scanner = new Scanner(System.in);
//repeat until a valid input is entered
while (!isValid) {
System.out.print(prompt);
if (scanner.hasNext()) {
choice = scanner.next(); // read user entry
//check if valid input is entered, if yes set isValid to true
if (choice.equalsIgnoreCase(choiceOne) ||
choice.equalsIgnoreCase(choiceTwo)) {
isValid = true;
} else { //else display error message
System.out.println("Error! y or n. Try again.");
}
} else {
System.out.println("Error! y or n. Try again.");
}
scanner.nextLine(); // discard any other data entered on the
line
}
return choice;
}
public static int getInt(String prompt) {
int i = 0;
boolean isValid = false;
Scanner scanner = new Scanner(System.in);
while (!isValid) {
System.out.print(prompt);
if (scanner.hasNextInt()) {
i = scanner.nextInt();
isValid = true;
} else {
System.out.println("Error! Invalid integer. Try again.");
}
scanner.nextLine(); // discard any other data entered on the
line
}
return i;
}
}
import java.util.Scanner;
public class Grade {
private int grade;
Grade() {
grade = 0;
}
Grade(int marks) {
this.grade = marks;
}
public void setNumber(int number) {
this.grade = number;
}
public String getLetter() {
if (grade >= 88) {
return "A";
} else if (grade >= 80) {
return "B";
} else if (grade >= 67) {
return "C";
} else if (grade >= 60) {
return "D";
} else if (grade < 60) {
return "F";
}
return "";
}
public String toString() {
return "Letter Grade : " + getLetter();
}
}
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Welcome to the Letter Grade
Converter");
System.out.println();
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
int myGrade = Console.getInt("Enter numerical grade: ");
Grade grade = new Grade(myGrade);
System.out.println("Letter grade: " + grade.getLetter());
choice = Console.getString("Continue? (y/n):");
}
}
}
Explanation::
Code in JAVA:::
import java.util.Scanner;
public class Console {
// require the user to input a value
public static String getString(String prompt) {
return getString(prompt,
false);
}
// check that an input has been entered if not return an error
public static String getString(String prompt,
boolean required) {
return getString(prompt,"y",
"n");
}
// require one of two specified string values.
public static String getString(String prompt,
String choiceOne, String choiceTwo) {
String choice = "";
boolean isValid = false;
Scanner scanner = new
Scanner(System.in);
//repeat until a valid input is
entered
while (!isValid) {
System.out.print(prompt);
if
(scanner.hasNext()) {
choice = scanner.next(); // read user
entry
//check if valid input is entered, if yes set
isValid to true
if (choice.equalsIgnoreCase(choiceOne) ||
choice.equalsIgnoreCase(choiceTwo)) {
isValid = true;
} else { //else display error message
System.out.println("Error! y
or n. Try again.");
}
} else {
System.out.println("Error! y or n. Try
again.");
}
scanner.nextLine(); // discard any other data entered on the
line
}
return choice;
}
public static int getInt(String prompt) {
int i = 0;
boolean isValid = false;
Scanner scanner = new
Scanner(System.in);
while (!isValid) {
System.out.print(prompt);
if
(scanner.hasNextInt()) {
i = scanner.nextInt();
isValid = true;
} else
{
System.out.println("Error! Invalid integer. Try
again.");
}
scanner.nextLine(); // discard any other data entered on the
line
}
return i;
}
}
OUTPUT::
Please provide the feedback!!
Thank You!!