Question

In: Computer Science

Create an Java application that uses a class to convert number grades to letter grades and...

Create an Java application that uses a class to convert number grades to letter grades and another class for data validation.

Specifications

  • The Grade class should have only one Instance Variable of type int.
  • Use a class named Grade to store the data for each grade. This class should include these three methods:

  public void setNumber(int number)   public int getNumber()

  public String getLetter()

  • The Grade class should have two constructors. The first one should accept no parameters and set the initial value of the number instance variable to zero. The second should accept an integer value and use it to set the initial value of the number instance variable.
  • The grading criteria are as follows:

A 88-100
B 80-87
C 67-79
D 60-66
F <60

  • Use the Console class presented in chapter 7 or an enhanced version of it to get and validate the user’s entries.
  • Overload the getString() method of the Console class to add the ability to require a string value, and to require one of two specified string values.
  • When the user responds to the Continue prompt, the application should only accept a value of “y” or “n”.
  • Use ch07_LineItem for concept of different classes and using Console class for application.

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):");
  
}
}
}

Solutions

Expert Solution

Explanation::

  • Change is in Console.java Code only
  • Output is provided at the end of the code.

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!!


Related Solutions

JAVA PROJECT Step 1: Create an application named YourInitials_Project 3 that uses a class to convert...
JAVA PROJECT Step 1: Create an application named YourInitials_Project 3 that uses a class to convert number grades to letter grades and another class for data validation. Make comments including your name, date, and project name as well as appropriate comments throughout your application that include the step number. Step 2: Create code to print Titles Step 3: Create a variable to hold user’s choice. Remember that all variables must begin with your initials and should be descriptive. Step 4:...
Create an application that uses a constructor and two different methods. JAVA
Create an application that uses a constructor and two different methods. JAVA
Write a program that translates a letter grade into a number grade. Letter grades are A,...
Write a program that translates a letter grade into a number grade. Letter grades are A, B, C, D, and F, possibly followed by + or -. Their numeric values are 4, 3, 2, 1 and 0. There is no F+ or F-. A “+” increases the numeric value by 0.3, a “–“ decreases it by 0.3. However, an A+ has value 4.0.4 pts Enter a Letter grade: B- The numeric value is 2.7 Your code with comments A screenshot...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
Create a Java windows application to manage a list of stocks 1. Add a class Stock...
Create a Java windows application to manage a list of stocks 1. Add a class Stock with the following fields: companyName, pricePerShare, numberOfShares (currently owned) and commission (this is the percent you pay a financial company when you purchase or sell stocks. Add constructor, getters and methods: ***purchaseShares (method that takes the number of shares purchased, updates the stock and return the cost of purchasing these shares make sure to include commission. ***sellShares (method that takes the number of shares...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java Swing GUI application in a new Netbeans project called FortuneTeller. Your project will have a FortuneTellerFrame.java class (which inherits from JFrame) and a java main class: FortuneTellerViewer.java. Your application should have and use the following components: Top panel: A JLabel with text “Fortune Teller” (or something similar!) and an ImageIcon. Find an appropriate non-commercial Fortune Teller image for your ImageIcon. (The JLabel has a...
Create a C++ program that will accept any number of grades for an exam. The grades...
Create a C++ program that will accept any number of grades for an exam. The grades will be input as 4 for an A, 3 for a B, 2 for a C, 1 for a D, and 0 for an F. After all grades have been entered, allow the user to enter -1 to exit. Output the number of grades in each category. Using arrays.
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls:...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls: A Label control with the following text displayed: "First Number:" A Label control with the following text displayed: "Second Number:" An empty TextField control beside the First Number label. An empty TextField control beside the Second Number label. Five buttons: Button 1 labeled + Button 2 labeled - Button 3 labeled * Button 4 labeled / Button 5 labeled = An empty Label control...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT