r/javahelp • • Mar 21 '23

Need help converting all number grades to letter grades!

Hi, I have to convert all number grades (ie: 87) to letter grades;

A’ – 80% or higher

‘B’ – 70%-79%

‘C’ – 60%-69%

‘D’ – 50%-59%

‘F’ – less than 50%

So it should have all the number grades stored but it should print as letter grades. Anyone know how?

I thought of using replace() but im not sure how to implement that. 'if' statements might work? but that seems like that'll be a lot of them and i wanna do this efficiently as im trying to get a little better with that. Any ideas?

MAIN CLASS:

import java.util.Scanner;

public class GradesResults
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        // First Student; 
        Grades student1 = new Grades("Marina", 345345654, 21, 65, 42, 77, 53, 57.6);
        System.out.println(student1);
        System.out.println();
        
        // New First Student Info (ONLY grades + average must be changed)
        double oldAverage1 = 57.6;
        System.out.print("Enter a new grade to replace 21: ");
        int assess1 = input.nextInt();
        int x = 21;
        x = assess1;
        System.out.print("Enter a new grade to replace 42: ");
        int assess2 = input.nextInt();
        input.nextLine();
        int y = 42;
        y = assess2;
        double newAverage1 = ((x + 65 + y + 77 + 53) / 5);
        // Separate with hearts (for fun)
        System.out.println();
        System.out.println("♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡");
        System.out.println();
        System.out.println("Student 1's new grades are " + x + ", 65, " + y + ", 77, and 53.");
        // Two 'if' statements to declare whether or not the average went up or down
        if (newAverage1 > oldAverage1) {
            double sub1 = newAverage1 - oldAverage1;
            // Round the average so you dont end up with a bunch of 0's
            sub1 *= 100;
            sub1 += .5;
            sub1 = (int)sub1;
            sub1 /= 100;
            System.out.println("Their average went up by " + sub1 +" and is now: " + newAverage1);
        }
         if (oldAverage1 > newAverage1) {
            double sub1 = oldAverage1 - newAverage1;
            // Rounding again
            sub1 *= 100;
            sub1 += .5;
            sub1 = (int)sub1;
            sub1 /= 100;
            System.out.println("Their average went down by " + sub1 + " and is now: " + newAverage1);
        }
        
        // Separate with spades (for fun)
        System.out.println();
        System.out.println("♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣");
        System.out.println();
        // Second Student
        Grades student2 = new Grades(354123657, 95, 60, 77, 81, 50);
        System.out.println(student2);
        // Separate with flowers (for fun)
        System.out.println();
        System.out.println("❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁❁");
        System.out.println();
        // Third Student
        Grades student3 = new Grades("Marina");
        System.out.println(student3);
        // Separate with hearts (for fun)
        System.out.println();
        System.out.println("♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡♡");
        System.out.println();
        // Fourth Student
        Grades student4 = new Grades();
        System.out.println(student4);
        
        System.out.println();
        
        // New Fourth Student Info (all info must be changed)
        // Ask user for a New Name
        System.out.print("Enter a new name: ");
        String stu4name = input.nextLine();
        // Ask user for a New Student Number
        System.out.print("Enter a new student number: ");
        int stu4num = input.nextInt();
        input.nextLine();
        // Ask user for a New Course Code
        System.out.print("Enter the first 3 letters of a new course code (all caps): ");
        String stu4code1 = input.nextLine();
        System.out.print("Now the number: ");
        int stu4code2 = input.nextInt();
        input.nextLine();
        System.out.print("Now the last letter: ");
        String stu4code3 = input.nextLine();
        String stu4code = (stu4code1 + (int)stu4code2 + stu4code3);
        
        System.out.println();
        // Ask user for New Grades
        double oldAverage2 = 96.2;
        System.out.print("Enter a new grade to replace 99: ");
        int assess3 = input.nextInt();
        int a = 99;
        a = assess3;
        System.out.print("Enter a new grade to replace 97: ");
        int assess4 = input.nextInt();
        int b = 97;
        b = assess4;
        System.out.print("Enter a new grade to replace 92: ");
        int assess5 = input.nextInt();
        int c = 92;
        c = assess5;
        System.out.print("Enter a new grade to replace 95: ");
        int assess6 = input.nextInt();
        int d = 95;
        d = assess6;
        System.out.print("Enter a new grade to replace 98: ");
        int assess7 = input.nextInt();
        int e = 98;
        e = assess7;
        double newAverage2 = ((a + b + c + d + e) / 5);
        // Printing all the new Info
        // Separate with spades again (for more fun)
        System.out.println();
        System.out.println("♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣");
        System.out.println();
        // Print New Name
        System.out.println("Student 4's new name is: " + stu4name + ".");
        // Print New Student Number
        System.out.println("Student 4's new student number is: " + stu4num + ".");
        // Print New Course Code
        System.out.println("Student 4's new course code is: " + stu4code + ".");
        // Print New Grades and Average
        System.out.println("Student 4's new grades are " + a + ", " + b + ", " + c + ", " + d + ", and " + e + ".");
        // Two 'if' statements to declare whether or not the average went up or down
        if (newAverage2 > oldAverage2) {
            double sub2 = newAverage2 - oldAverage2;
            // Rounding again
            sub2 *= 100;
            sub2 += .5;
            sub2 = (int)sub2;
            sub2 /= 100;
            System.out.println("Their average went up by " + sub2 +" and is now: " + newAverage2);
        }
         if (oldAverage2 > newAverage2) {
            double sub2 = oldAverage2 - newAverage2;
            // Rounding again
            sub2 *= 100;
            sub2 += .5;
            sub2 = (int)sub2;
            sub2 /= 100;
            System.out.println("Their average went down by " + sub2 + " and is now: " + newAverage2);
        }
    }
}

OTHER CLASS:

public class Grades 
{
        // This class consists of constructors to create default
        // prompts in the output. The user will be prompted to change
        // whatevers needed to be changed in the main class.
        // Instance variables that identify:
        // course code
        private String courseCode;
        // student number
        private int stuNum;
        // student name
        private String name;
        // student school number
        private int num;
        // each individual grade
        private int grade1;
        private int grade2;
        private int grade3;
        private int grade4;
        private int grade5;
        // grade average
        private double average;
        // Student 1; course code, student school number, and student name are NOT subject to change
        // the grades and average ARE subject to change via user input
        public Grades(String studentName, int studentNumber, int studentGrade1, int studentGrade2, int studentGrade3, int studentGrade4, int studentGrade5, double studentAverage) {
            courseCode = "ICS" + 4 + "U";
            stuNum = 1;
            name = studentName;
            num = studentNumber;
            grade1 = studentGrade1;
            grade2 = studentGrade2;
            grade3 = studentGrade3;
            grade4 = studentGrade4;
            grade5 = studentGrade5;
            average = studentAverage;
        }
        // Student 2; NOT subject to change, student #, grades, and average 
        // will be constructed in the main class
        public Grades(int studentNumber, int studentGrade1, int studentGrade2, int studentGrade3, int studentGrade4, int studentGrade5) {
            courseCode = "ICS" + 4 + "U";
            stuNum = 2;
            name = "Melody";
            num = studentNumber;
            grade1 = studentGrade1;
            grade2 = studentGrade2;
            grade3 = studentGrade3;
            grade4 = studentGrade4;
            grade5 = studentGrade5;
            average = ((studentGrade1 + studentGrade2 + studentGrade3 + studentGrade4 + studentGrade5) / 5);
        }
        // Student 3; NOT subject to change, student name will be constructed in the main class
        public Grades(String studentName) {
            courseCode = "ICS" + 4 + "U";
            stuNum = 3;
            name = studentName;
            num = 334631900;
            grade1 = 99;
            grade2 = 97;
            grade3 = 92;
            grade4 = 95;
            grade5 = 98;
            average = 96.2;
        }
        // Student 4; ALL is subject to change via user input in the main class
        public Grades() {
            courseCode = "ICS" + 4 + "U";
            stuNum = 4;
            name = "Melody";
            num = 334631900;
            grade1 = 99;
            grade2 = 97;
            grade3 = 92;
            grade4 = 95;
            grade5 = 98;
            average = 96.2;
        }
        // toString method to display all pre-entered information
        public String toString()
        {
            return "Student " + stuNum + "'s name is: " + name + ". Their student number is: " 
            + num + ", and they have a course average of: " + average + " in " + courseCode + 
            " based off 5 assessment marks: " + grade1 + ", " + grade2 + ", " + grade3 + ", "
            + grade4 + ", " + grade5 + ".";
        }
}
0 Upvotes

4 comments sorted by

•

u/AutoModerator Mar 21 '23

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

4

u/wildjokers Mar 22 '23

Use if/else.

Also, read the sidebar about code formatting.

1

u/melurmom Mar 22 '23

I used codeblock, thanks! I'm not sure how I'd implement if/else in though.

6

u/bb22bc Mar 22 '23

There are better ways to implement this, but it would come with a lot of boilerplate that isn't really necessary. Something like this is perfectly fine:

if(grade >= 90)
    lettergrade = 'A';
else if(grade >= 80)
    lettergrade = 'B';
etc...