Question

Java Programming Language Edit and modify from the given code Perform the exact same logic, except...

Java Programming Language

Edit and modify from the given code

  1. Perform the exact same logic, except . . .
  2. The numeric ranges and corresponding letter grade will be stored in a file.
  3. Need error checking for:
    1. Being able to open the text file.
    2. The data makes sense: 1 text line of data would be 100 = A.
  4. Read in all of the numeric grades and letter grades and stored them into an array or arraylist.
  5. Then do the same logic.
  6. The handing of the invalid numeric value should display the same message.
  7. The output for the grade letter should look the same.

----------------------------------------------------

//Given code

import java.util.*;


public class LetterGrades {
    public static void main(String[] args) {

        //Get the user input
        Scanner sc = new Scanner(System.in);

        //Array List
        ArrayList<Double> scores=new ArrayList<Double>();
        ArrayList<String> gradeLetters=new ArrayList<String>();
        String gradeLetter="";

        //Loop
        while(true)
        {
            System.out.print("Enter your numeric grade (enter -1 to end and view the result): ");
            double grade = sc.nextDouble();

            //End the program if user entered -1
            if(grade == -1)
            {
                System.out.print("The program ended");
                break;
            }

            //validating the grade between 0 and 100
            if(grade<0 || grade>100)//if user entered invalid input
            {
                while(true)//infinity loop to read input untill user enter valid input.
                {
                    //Displaying error message.
                    System.out.print("You entered inalid numeric grade.");
                    System.out.print("Please enter valid numeric grade from 0-100: ");
                    grade = sc.nextDouble();
                    //End the validating loop if user entered valid input.
                    if(grade>=0 && grade<=100)
                    {
                        break;
                    }
                }
            }
            scores.add(grade);
            //Convert numeric grade to letter grade
            if(grade>=93 && grade<=100)
                gradeLetter="A";
            else if(grade>=90 && grade<93)
                gradeLetter="A-";

            else if(grade>=87 && grade<90)
                gradeLetter="B+";
            else if(grade>=83 && grade<87)
                gradeLetter="B";
            else if(grade>=80 && grade<83)
                gradeLetter="B-";

            else if(grade>=73 && grade<80)
                gradeLetter="C+";
            else if(grade>=70 && grade<73)
                gradeLetter="C";

            else if(grade>=60 && grade<70)
                gradeLetter="D";

            else if(grade<60)
                gradeLetter="F";
            gradeLetters.add(gradeLetter);
        }

        for(int i=0;i<scores.size();i++)
        {
            System.out.println("\nScore :"+scores.get(i));
            System.out.println("Letter Grade :"+gradeLetters.get(i));

        }
    }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

import java.io.*;

import java.util.*;

public class LetterGrades {

public static void main(String[] args) throws FileNotFoundException {

// Get the user input

Scanner sc = new Scanner(System.in);

// Array List

ArrayList<Double> scores = new ArrayList<Double>();

ArrayList<String> gradeLetters = new ArrayList<String>();

String gradeLetter = "";

// Loop

while (true) {

System.out.print("Enter your numeric grade (enter -1 to end and view the result): ");

double grade = sc.nextDouble();

// End the program if user entered -1

if (grade == -1) {

System.out.print("The program ended");

break;

}

// validating the grade between 0 and 100

if (grade < 0 || grade > 100)// if user entered invalid input

{

while (true)// infinity loop to read input untill user enter valid input.

{

// Displaying error message.

System.out.print("You entered inalid numeric grade.");

System.out.print("Please enter valid numeric grade from 0-100: ");

grade = sc.nextDouble();

// End the validating loop if user entered valid input.

if (grade >= 0 && grade <= 100) {

break;

}

}

}

scores.add(grade);

// Convert numeric grade to letter grade

if (grade >= 93 && grade <= 100)

gradeLetter = "A";

else if (grade >= 90 && grade < 93)

gradeLetter = "A-";

else if (grade >= 87 && grade < 90)

gradeLetter = "B+";

else if (grade >= 83 && grade < 87)

gradeLetter = "B";

else if (grade >= 80 && grade < 83)

gradeLetter = "B-";

else if (grade >= 73 && grade < 80)

gradeLetter = "C+";

else if (grade >= 70 && grade < 73)

gradeLetter = "C";

else if (grade >= 60 && grade < 70)

gradeLetter = "D";

else if (grade < 60)

gradeLetter = "F";

gradeLetters.add(gradeLetter);

}

PrintWriter pw = new PrintWriter(new File("output.txt"));

for (int i = 0; i < scores.size(); i++) {

System.out.println("\nScore :" + scores.get(i));

System.out.println("Letter Grade :" + gradeLetters.get(i));

pw.println(scores.get(i)+" = "+gradeLetters.get(i));

}

pw.close();

}

}

Add a comment
Know the answer?
Add Answer to:
Java Programming Language Edit and modify from the given code Perform the exact same logic, except...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • Modify your PredictGPA.java file from Lab 2 to create a method called GPA that takes a...

    Modify your PredictGPA.java file from Lab 2 to create a method called GPA that takes a string argument (the grade) and returns the corresponding value (ie 4.0 for an A, 3.33 for a B+, etc). Use this method call to reduce the “redundant” code that you had in Lab 2. The program must otherwise still run correctly as to the directions in Lab 2 and compute the GPA after using the method calls to determine the appropriate grade values. Trying...

  • public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc...

    public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc = new Scanner(System.in);         String choice = "y";         while (choice.equalsIgnoreCase("y")) {             // get the input from the user             System.out.println("DATA ENTRY");             double monthlyInvestment = getDoubleWithinRange(sc,                     "Enter monthly investment: ", 0, 1000);             double interestRate = getDoubleWithinRange(sc,                     "Enter yearly interest rate: ", 0, 30);             int years = getIntWithinRange(sc,                     "Enter number of years: ", 0, 100);             System.out.println();            ...

  • I need this java program to contain methods that do some of the work that can...

    I need this java program to contain methods that do some of the work that can be modularized. It must logically be the same as this loop program but instead the logic will be modularized into Java Methods. Continue to take input for every employee in a company, and display their information until a sentinel value is entered, that exits the loop and ends the program. import java.util.Scanner; public class SalaryCalc { double Rpay = 0, Opay = 0; void...

  • IT Java code In Lab 8, we are going to re-write Lab 3 and add code...

    IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows:                 bmi = kilograms / (meters2)                 where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...

  • Below is a java program that inputs an integer grade and turns it into a letter...

    Below is a java program that inputs an integer grade and turns it into a letter grade. Update the below java code as follows and comment each line to explain what is happening: 1. Convert the if-else-if code block to a switch statement to solve the problem. 2. Use modulus to convert the grade input so that the range of grades are converted to one value. (comment the line) import java.util.Scanner; public class GradeLetterTest { public static void main(String[] args)...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

  • Can you help me rearrange my code by incorporating switch I'm not really sure how to...

    Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • Your code should contain appropriate validations and must focus on code optimization. Briefly, explain (150-200 words)...

    Your code should contain appropriate validations and must focus on code optimization. Briefly, explain (150-200 words) the logic of the program and how the code can be   optimized. Modify the code to repeat the program until the user enters “no”. (*I need the logical write out on how the program behaves when there is an input from the user. (I don't need the //comments). import java.util.Scanner; public class Q2 {     public static void main(String[] args)     {         Scanner...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT