Basic Java control structure issue. - java

I'm a college freshmen and I'm having trouble with my programming homework. The homework I got from my lecturer was for me to write a program in Java to take in a student's info, and allow the student to choose how many subject the student takes, and input marks and credit hours, followed by a formula to calculate the grade and subject grade points. In the end of the program, the program would be able to output the student info (name, ID etc) and the total subject grade point for all of the subjects entered, total credit hours for all the subjects, and the cumulative grade point average (CGPA).
However, I have three problems here
I have an issue with the loop that I have set in order to read how many subjects the user wants to key in.
When I tried to print "Grade = " + subjectGrade); my compiler says it hasn't been initialized. Same goes to the GradePoint and subjectCreditHour.
And I couldn't figure out how to get the program to calculate the Total Subject Grade Points, Total Credit Hours, and CGPA. Because depending on how many numbers of subjects the user wants, I can't figure out how to get the program to take in the user's input and sum them up together
My code goes like this :
package javaquiz1;
import java.util.Scanner;
/**
*
* #author jerem_000
*/
public class JavaQuiz1 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner (System.in);
String name;
int ID;
int tel;
String email;
int subjects;
String subjectName;
int subjectCreditHour;
int subjectMark;
String subjectGrade;
double GradePoint;
double subjectGradePoint;
double CGPA;
double totalSubjectGP;
int totalCreditHour;
System.out.print("Please input student's name : ");
name = input.nextLine();
System.out.print("Please input student's ID : ");
ID = input.nextInt();
System.out.print("Please input student's telephone number : ");
tel = input.nextInt();
System.out.print("Please input student's email : ");
email = input.next();
System.out.print("Please input number of subjects : ");
subjects = input.nextInt();
for (int i = 1; i >= subjects ; i++) { //I'm having an issue with this loop
System.out.println("Subject " + i + " : Please input the following"); //I placed the variable i there in order to make the program print something like "Subject 1 , Subject 2, Subject 3 etc". Depending on the user's number of subjects input
System.out.print("Subject name : ");
subjectName = input.next();
System.out.print("Credit Hour : ");
subjectCreditHour = input.nextInt();
System.out.print("Mark : ");
subjectMark = input.nextInt();
if ( subjectMark >= 80 ) {
subjectGrade = "A";
GradePoint = 4.0;
} else if (subjectMark < 80) {
subjectGrade = "B+";
GradePoint = 3.5;
} else if (subjectMark < 70) {
subjectGrade = "B";
GradePoint = 3.0;
} else if (subjectMark < 65) {
subjectGrade = "C+";
GradePoint = 2.5;
} else if (subjectMark < 55) {
subjectGrade = "C";
GradePoint = 2.0;
} else if (subjectMark < 50) {
subjectGrade = "D";
GradePoint = 1.0;
} else {
subjectGrade = "F";
GradePoint = 0.0;
}
}
System.out.println("Grade = " + subjectGrade);
System.out.println("Subject Grade Point = " + (GradePoint * subjectCreditHour)); //I'm having a problem with the subjectGrade, GradePoint, and subjectCreditHour, it says variable might have not been initialized
System.out.println("Name : " + name);
System.out.println("ID : " + ID);
System.out.println("Tel : " + tel);
System.out.println("email : " + email);
System.out.print("Total subject Grade Points = " );
System.out.print("Total Credit Hours = " );
System.out.print("Cumulative Grade Point Average ="); //On this 3 system.out.prints, I can't seem to think of a way to read the Grade Point, Total Credit Hours, and CGPA, and add them all together
}
}
I also have a sample output on how the program is supposed to be:
Please input student's name : James Cook
Please input student's ID : 0106578
Please input student's tel : 010783938
Please input student's e-mail : jcook#gmail.com
Please input number of subjects : 3
Subject 1 : Please input the following
Subject name : Fundamentals of Programming
Credit Hour : 4
Mark : 78
Grade : B+
Subject Grade Point : 14.0
Subject 2 : Please input the following
Subject name : English
Credit Hour : 3
Mark : 85
Grade : A
Subject Grade Point : 12.0
Subject 3 : Please input the following
Subject name : Computer Fundamentals
Credit Hour : 3
Mark : 78
Grade : B+
Subject Grade Point : 10.5
Name : James Cook
ID : 0106578
tel : 010783938
e-mail : jcook#gmail.com
Total subject Grade Point = 36.5
Total Credit Hours = 10
CGPA = 3.65
The formula to calculate the subject grade point is
subjectGradePoint = GradePoint * CreditHour
And the formula to calculate the CGPA (cumulative grade point average) is
CGPA = totalSubjectGP / totalCreditHours
Correction, criticism, advice will be welcomed for future improvement. Thanks in advance!

1) I have an issue with the loop that I have set in order to read how
many subjects the user wants to key in.
Already answered by #Nishan in a comment. Just replace for (int i=1;i >= subjects; i++) by for (int i=1;i <= subjects; i++).
2) When I tried to print "Grade = " + subjectGrade); my compiler says it
hasn't been initialized. Same goes to the GradePoint and
subjectCreditHour.
Already answered.
3) And I couldn't figure out how to get the program to calculate the
Total Subject Grade Points, Total Credit Hours, and CGPA. Because
depending on how many numbers of subjects the user wants, I can't
figure out how to get the program to take in the user's input and sum
them up together
You already are on the right way since you have the accumulators you need:
double subjectGradePoint = 0d;
double CGPA = 0d;
double totalSubjectGP = 0d;
int totalCreditHour = 0;
Whitin your loop and after nested if-else blocks, you need to update subjectGradePoint, totalSubjectGP and totalCreditHour variables in each iteration:
subjectGradePoint = GradePoint * CreditHour;
totalSubjectGP += subjectGradePoint;
totalCreditHour += CreditHour;
Finally, after your loop calculate CGPA:
CGPA = totalSubjectGP / totalCreditHour;

Java mandates method variables to be initialized before use,
just initialize String value and it shall work fine. Please refer to below code extract.
String subjectName=null;
int subjectCreditHour=0;
String subjectGrade=null;

java variable must intialized before use
import java.util.Scanner;
/**
*
* #author jerem_000
*/
public class JavaQuiz1 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner (System.in);
String name;
int ID;
int tel;
String email;
int subjects;
String subjectName;
int subjectCreditHour=0;
int subjectMark;
String subjectGrade="";
double GradePoint=0;
double subjectGradePoint=0;
double CGPA;
double totalSubjectGP;
int totalCreditHour;
System.out.print("Please input student's name : ");
name = input.nextLine();
System.out.print("Please input student's ID : ");
ID = input.nextInt();
System.out.print("Please input student's telephone number : ");
tel = input.nextInt();
System.out.print("Please input student's email : ");
email = input.next();
System.out.print("Please input number of subjects : ");
subjects = input.nextInt();
for (int i = 1; i >= subjects ; i++) { //I'm having an issue with this loop
System.out.println("Subject " + i + " : Please input the following"); //I placed the variable i there in order to make the program print something like "Subject 1 , Subject 2, Subject 3 etc". Depending on the user's number of subjects input
System.out.print("Subject name : ");
subjectName = input.next();
System.out.print("Credit Hour : ");
subjectCreditHour = input.nextInt();
System.out.print("Mark : ");
subjectMark = input.nextInt();
if ( subjectMark >= 80 ) {
subjectGrade = "A";
GradePoint = 4.0;
} else if (subjectMark < 80) {
subjectGrade = "B+";
GradePoint = 3.5;
} else if (subjectMark < 70) {
subjectGrade = "B";
GradePoint = 3.0;
} else if (subjectMark < 65) {
subjectGrade = "C+";
GradePoint = 2.5;
} else if (subjectMark < 55) {
subjectGrade = "C";
GradePoint = 2.0;
} else if (subjectMark < 50) {
subjectGrade = "D";
GradePoint = 1.0;
} else {
subjectGrade = "F";
GradePoint = 0.0;
}
}
System.out.println("Grade = " + subjectGrade);
System.out.println("Subject Grade Point = " + (GradePoint * subjectCreditHour)); //I'm having a problem with the subjectGrade, GradePoint, and subjectCreditHour, it says variable might have not been initialized
System.out.println("Name : " + name);
System.out.println("ID : " + ID);
System.out.println("Tel : " + tel);
System.out.println("email : " + email);
System.out.print("Total subject Grade Points = " );
System.out.print("Total Credit Hours = " );
System.out.print("Cumulative Grade Point Average ="); //On this 3 system.out.prints, I can't seem to think of a way to read the Grade Point, Total Credit Hours, and CGPA, and add them all together
}
}

Related

New to Java, and not sure what I'm doing wrong. My If statements aren't working

The bottom section is where it is stuffing up. When The if statements start after while, the product doesn't store the name of the product (but keeps what product it is and the cost for later - so the end result works).
The issue is, regardless of what is entered, the message still comes up "That isn't a valid product" even if it is. It doesn't affect the program, but does affect the output.
import java.util. * ;
public class Shopping {
String p1Name,
p2Name,
p3Name; // variables for storing product names
double p1UP,
p2UP,
p3UP; // variables for storing product unit prices
String c1Name,
c2Name; // variables for storing customer names
double c1DRate,
c2DRate; // variables for customer discount rates
double quant; //variables for unit quantity required
double sum; // unit and quantity
String custName; // Customer name
double discount1;
double discount2;
String product;
double cost = p1UP;
Scanner scan;
public void setProductData() {
System.out.println("Initializing product data");
System.out.print("Enter name of part 1 : ");
p1Name = scan.next();
System.out.print("Enter unit price of part 1 : ");
p1UP = scan.nextDouble();
System.out.print("Enter name of part 2 : ");
p2Name = scan.next();
System.out.print("Enter unit price of part 2 : ");
p2UP = scan.nextDouble();
System.out.print("Enter name of part 3 : ");
p3Name = scan.next();
System.out.print("Enter unit price of part : ");
p3UP = scan.nextDouble();
}
public void setCustomerData() {
System.out.println("Initializing customer data");
System.out.print("Enter name of customer 1 : ");
c1Name = scan.next();
System.out.print("Enter discount Rate for customer 1 : ");
c1DRate = scan.nextDouble();
System.out.print("Enter name of customer 2 : ");
c2Name = scan.next();
System.out.print("Enter discount Rate for customer 2 : ");
c2DRate = scan.nextDouble();
}
public void computeCost() {
// prompt and read product name, qty and customer name before computing and
// printing the cost taking into consideration customer specific discounts.
boolean discount1 = false;
boolean discount2 = false;
System.out.print("Please enter your name : ");
custName = scan.next();
if (custName.equalsIgnoreCase(c1Name)) {
discount1 = true;
}
if (custName.equalsIgnoreCase(c2Name)) {
discount2 = true;
}
// Determined if customer eligible for a discount
System.out.println("Our products and prices are as follows");
System.out.println(p1Name + " is priced at " + p1UP);
System.out.println(p2Name + " is priced at " + p2UP);
System.out.println(p3Name + " is priced at " + p3UP);
System.out.println("What product would you like");
//what product did they want to buy
product = scan.next();
while (! (product.equalsIgnoreCase(p1Name) || product.equalsIgnoreCase(p2Name) || product.equalsIgnoreCase(p3Name))) {
System.out.println("That isn't a valid product.");
product = scan.next();
}
if (product.equals(p1Name)) {
cost = p1UP;
}
if (product.equals(p2Name)) {
cost = p2UP;
}
if (product.equals(p3Name)) {
cost = p3UP;
}
else {
System.out.println("You have selected an incorrect product");
System.out.println("You have been assigned " + p1Name + " instead");
}
scan.nextLine();
I think you're having a mistake here.
I suppose you're trying to print the message "That isn't a valid product." if the input does not match any of p1Name, p2Name and p3Name, right?
So your while condition must be NOT EQUALS to p1Name AND p2Name, p3Name too.
So you can use this:
while (!(product.equalsIgnoreCase(p1Name) && !(product.equalsIgnoreCase(p2Name)) && !
(product.equalsIgnoreCase(p3Name)))) {
System.out.println("That isn't a valid product.");
product = scan.next();
}
use String.trim() maybe when you scan you scan also the space or whitespace character.
in ur while condition :
!(product.trim().equalsIgnoreCase(p1Name.trim()) || product.trim().equalsIgnoreCase(p2Name.trim()) || product.trim().equalsIgnoreCase(p3Name.trim()))
Ok, I worked it out.
I had if
not else if for the later statements.
my bad.
Thanks

Java For Loop not ending with Sentinel

I am creating a program that takes in employee payroll information and then displays the average and total afterwards. Everything seems to be working correctly except for the fact I cannot get the program to end when I enter the sentinel value of -1 for the hours worked so that the program can display the total and average of the employees. Any help is greatly appreciated.
import java.util.Scanner;
public class PayrollDo
{
public static void main(String[] args)
{
int hoursWorked = 0;
int grossPay = 0;
int empCounter = 0;
int total = 0;
Scanner keyboard = new Scanner(System.in);
do {
total = total + grossPay; // add gross to total
empCounter = empCounter + 1; // incriment counter
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextInt();
System.out.print("Enter hourly wage: ");
int hourlyWage = keyboard.nextInt();
//System.out.println("Grosspay is " + (hourlyWage * hoursWorked));
keyboard.nextLine();
System.out.println("Enter employee name ");
String name = keyboard.nextLine();
} while(hoursWorked != -1);
// if user entered at least one employee
if (empCounter != 0) {
// use number with decimal point to calculate average of employees
int average = (int) total / empCounter;
// display total and average (with two digits of precision)
System.out.printf("%nTotal of the %d employees entered is %d%n",
empCounter, total);
System.out.printf("Employee average is " + average);
}
else {
// no employees were entered, so output appropriate message
System.out.println("No employees were entered");
}
}
}
Test soon after entering the value whether it is -1 or not
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextInt();
if (hoursWorked == -1) break;
edit
I think you will also have trouble with Integer division http://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0
You could restructure your do loop to put the hoursWorked input last (otherwise you have to complete the entries) - and also, add a conditional to reject all entries that cycle, otherwise empCounter is off by one. And I think the total calculation needs reworking too:
do {
System.out.println("Enter employee name ");
String name = keyboard.nextLine();
System.out.print("Enter hourly wage: ");
int hourlyWage = keyboard.nextInt();
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextInt();
keyboard.nextLine();
if (hoursWorked != -1) {
total = total + (hourlyWage * hoursWorked); //
empCounter = empCounter + 1; // incriment counter
}
} while(hoursWorked != -1);
In cases like this it can be simpler to have a question like "More Employees? (y/n)" that is used to terminate the loop, using a boolean flag as the while terminator.

Simplified way to scan values in?

Current i am scanning in 4 exam score grades, each to their own variable that has been declared and initialized. I was wondering if there was better way i should be doing this to make it less clutters. Please bear with me though as i am currently in my first java class at uni and dont have any prior experience so i dont want the solution to be super complex, not yet at least but something easily understandable. Not sure if it helps/matters but i am using BlueJ as my IDE.
import java.util.*;
public class GradeCalculatorDriver
{
public static void main(String [] args){
String s1 = "student";//Declare student string variable;
String s2;
double exam1 = 0;//Declare and initalize variables.
double exam2 = 0;//Declare and initalize variables.
double exam3 = 0;//Declare and initalize variables.
double exam4 = 0;//Declare and initalize variables.
do{
Scanner input = new Scanner(System.in);
System.out.println("This program will calculate the average of 4 exam scores, and return the lowest, highest, and letter grade associated with the average");
System.out.println("Please enter the name of the student");//Request student name and scans it into a string.
String student = input.next();
System.out.println("Please enter one exam score, then hit enter and repeat for following grades.");//Request exam scores and scan them into variables.
exam1 = input.nextDouble();
exam2 = input.nextDouble();
exam3 = input.nextDouble();
exam4 = input.nextDouble();
double highest = GradeCalculator.high(exam1, exam2, exam3, exam4);//Calls highest grade method.
double lowest = GradeCalculator.low(exam1, exam2, exam3, exam4);//Calls lowest grade method.
double average = GradeCalculator.avg(exam1, exam2, exam3, exam4);//Calls average grade method.
char letterGrade = GradeCalculator.letter(exam1, exam2, exam3, exam4);//Calls letter grade method.
System.out.printf("The highest exam score is : %.2f " + highest);//Displays highest grade.
System.out.printf("The lowest exam score is : %.2f " + lowest);//Displays lowest grade.
System.out.printf("The average exam score is : %.2f " + average);//Displays average grade.
System.out.print("The letter grade is " + letterGrade);//Displays letter grade associated with average.
System.out.println("Would you like to enter another students grades? (yes/no)");//asks the user if they would like to calculate another factorial.
s2 = input.next();
} while (s2.equalsIgnoreCase("YES"));//checks to see what the users response was.
System.out.print("Thank you for using my program!");//Ending statement.
}
}
public class GradeCalculator{
public static double high(double exam1, double exam2, double exam3, double exam4){//Calculates the highest exam score entered.
double highest;//Declare and initilize variable.
highest = Math.max(Math.max(Math.max(exam1, exam2), exam3), exam4);
return highest;//Returns highest exam score.
}
public static double low(double exam1, double exam2, double exam3, double exam4){//Calculates the lowest exam score entered.
double lowest = 0;//Declare and initilize variable.
lowest = Math.min(Math.min(Math.min(exam1, exam2), exam3), exam4);
return lowest;//Returns highest exam score.
}
public static double avg (double exam1, double exam2, double exam3, double exam4){//Calculates the average exam score.
double average;//Declare variable.
double count = 4;//Declare and initilize variable.
average = ((exam1 + exam2 + exam3 + exam4) / count);
return average;//Returns average grade.
}
public static char letter (double exam1, double exam2, double exam3, double exam4){//Calculates the letterGrade based on the average exam score.
char letterGrade;//Declare and initilize variable.
double count = 4;//Declare and initilize variable.
double average = ((exam1 + exam2 + exam3 + exam4) / count);//declare and calculate average score.
if (average >= 90)
letterGrade = 'A';
else if (average >= 80)
letterGrade = 'B';
else if (average >= 70)
letterGrade = 'C';
else if (average >= 60)
letterGrade = 'D';
else
letterGrade = 'E';
return letterGrade;//Returns letter grade.
}
}
You can use a List<Double> scores = new ArrayList<>() to hold the exam score values and reduce code clutter. Then as you added new exam scores from user input, you would call scores.add(input.nextDouble()) for each input value.
Well, in my opinion, if this is your first kick at the cat (so to speak) then you're well on your way to being a successful Java programmer. As for reducing clutter, I suppose it depends upon what you have already learned, like the Java methods and statements already covered and are now at your disposal to create an application to accomplish the task at hand. There is really no way to know the extent of your learning and therefore the suggestions provided below should only be defined as mere suggestions:
There is nothing wrong with your current model other than a few minor errors which will generate a MissingFormatArgumentException and that is within the code lines:
System.out.printf("The highest exam score is : %.2f " + highest);
System.out.printf("The lowest exam score is : %.2f " + lowest);
System.out.printf("The average exam score is : %.2f " + average);
Any errors within your code should be repaired first. Each of the code lines above contains a fault and it's simply because there is no argument supplied for the format specifier (%.2f). This is because you placed a plus (+) character in place of where a comma separator character is to go so as to indicate the argument for %.2f to represent. In this case the + will append the following string or variable content to the string before it. The code should be:
System.out.printf("The highest exam score is : %.2f ", highest);
System.out.printf("The lowest exam score is : %.2f ", lowest);
System.out.printf("The average exam score is : %.2f ", average);
Or just utilize the String.format() method:
System.out.println("The lowest exam score is: " + String.format("%.2f",lowest));
System.out.println("The highest exam score is: " + String.format("%.2f", highest));
System.out.println("The average exam score is: " + String.format("%.2f", average));
Declaring Scanner should not be done upon every iteration of your do/while loop. Place this declaration above the do/while code block so as not to open unnecessary instances of Scanner.
Explaining application functionality does not need to be done upon each iteration of the do/while loop. Move this message above the do/while code block.
When prompting for User input don't use the Scanner#next() method unless you are expecting to receive string tokens. Sometimes it's just plain easier to use the Scanner#nextLine() method. In my opinion I feel it can give more flexibility and allows you a better opportunity to prevent possible exceptions (don't rely on exceptions if you can prevent them).
Believe it or not...sometimes comments can be the biggest source of code clutter. There is absolutely no need to comment the obvious. Yes, I might have done so but it's only so as to explain the code. It is expected to be deleted.
You can get rid of the code line: String s1 = "student";//Declare student string variable;. You don't seem to use this variable anywhere.
As already suggested an array or collection mechanism can be used to hold student scores however in the sense of reducing clutter, it won't for a mere 4 scores but it will for many more scores and therefore is a good way to handle this portion of the code. It is still a good way to go in any case if your course has already covered the use of Arrays and or ArrayLists. Not sure if your requirements are to utilize only what you've already learned. In any case....
To utilize an array to store Student scores you would also need to modify all your methods within the GradeCalculator Class. This in itself will remove parameter clutter since all you would need is a single parameter instead of 4 for each method. The methods in that class might look something like this if you want to use Arrays:
public class GradeCalculator {
public static double high(double[] exams) {//Calculates the highest exam score entered.
double highest = 0.0d;//Declare and initilize variable to 0.0
for (int i = 0; i < exams.length; i++) {
if (exams[i] > highest) {
highest = exams[i];
}
}
return highest;//Returns highest exam score.
}
public static double low(double[] exams) {//Calculates the lowest exam score entered.
double lowest = exams[0];
for (int i = 0; i < exams.length; i++) {
if (exams[i] < lowest) {
lowest = exams[i];
}
}
return lowest;//Returns lowest exam score.
}
public static double avg(double[] exams) {//Calculates the average exam score.
double average = 0.0d; //Declare variable.
double sum = 0.0d;
for (int i = 0; i < exams.length; i++) {
sum+= exams[i];
}
average = sum / exams.length;
return average; //Returns average grade.
}
//Calculates the letter-Grade based on the average exam score.
public static char letter(double average) {
char letterGrade;//Declare and initilize variable.
if (average >= 90) {
letterGrade = 'A';
}
else if (average >= 80) {
letterGrade = 'B';
}
else if (average >= 70) {
letterGrade = 'C';
}
else if (average >= 60) {
letterGrade = 'D';
}
else {
letterGrade = 'E';
}
return letterGrade; //Returns letter grade.
}
}
The letter() method only ever needed one parameter instead of the 4 you had forced to provide and that is the value that is returned from the average() method. You see, you don't need to calculate the average again within the letter() method since you already have a method that does, and has done that. Just pass the already calculated average to the letter() method.
To fill an array with User input it's best to carry out the task using a loop of some sort but then again, it depends upon exactly how you want the User to provide that information (like Test Scores). Perhaps you would like the User to supply all the scores from a single input by separating each score with a white-space:
Please enter all student scores (separated with a space):
65 112 75 88
The code for this might look something like this:
String s2;
boolean invalidEntry;
String LS = System.lineSeparator();
double[] exams; // Declare Array
Scanner input = new Scanner(System.in);
System.out.println("This program will calculate the average of any number of "
+ LS + "desired exam scores and return the lowest, highest, and "
+ LS + "letter grade associated with that determined average." + LS);
do {
s2 = "";
invalidEntry = false;
// Request Student Name
System.out.println("Please enter the name of the student (nothing to exit):");
String student = input.nextLine();
if (student.equals("")) { break; }
// Request exam scores
System.out.println("Please enter all student scores (each separated with a space): ");
String scores = input.nextLine();
// If nothing was supplied break out of loop
if (scores.equals("")) {
invalidEntry = true;
break;
}
String[] scoresArray = scores.split("\\s+"); // Split the input into single scores (regex "\\s+" is used to split on one or more whitespaces).
exams = new double[scoresArray.length]; // Initalize Array.
// Convert scores to double data type...
for (int i = 0; i < scoresArray.length; i++) {
/* Is the string value actually a numerical signed
or unsigned integer or double type value. Save
this Regular Expression. It's handy to have. */
if (scoresArray[i].matches("-?\\d+(\\.\\d+)?")) {
exams[i] = Double.parseDouble(scoresArray[i]);
}
else {
System.out.println("One or more of the Scores supplied are invalid! "
+ "Enter Student Scores again!" + LS);
invalidEntry = true;
break;
}
}
if (invalidEntry) { continue; } // Redo loop on Invalid Entry
double highest = high(exams);
double lowest = low(exams);
double average = avg(exams);
char letterGrade = letter(average);
System.out.println(LS + "Results For Student: " + student);
System.out.println("From " + String.format("%02d" , exams.length) +
" exam scores: " +
Arrays.toString(exams).replaceAll("[\\[\\]]", ""));
System.out.println("The lowest exam score is: " + String.format("%.2f",lowest));
System.out.println("The highest exam score is: " + String.format("%.2f", highest));
System.out.println("The average exam score is: " + String.format("%.2f", average));
System.out.println("The letter grade is: " + letterGrade + LS);
while (s2.equals("")) {
System.out.println("Would you like to enter another Students Scores? (yes/no)");
s2 = input.nextLine();
if (!s2.equalsIgnoreCase("yes") && !s2.equalsIgnoreCase("no")) {
System.out.println("Invalid Response! 'Yes' or 'No' only!");
s2 = "";
}
}
} while (s2.equalsIgnoreCase("yes") || invalidEntry);
System.out.println("Thank you for using my program!");
Sometimes it is just easier for the User to enter all the scores on one line as demonstrated within the above code however, you set the rules towards how your application is to function. If you want each score to be entered individually then so be it. The code might then look something like this:
String s2;
int indexCounter; // Declare an index counter for array.
boolean invalidEntry;
String LS = System.lineSeparator();
double[] exams; //Declare Array.
Scanner input = new Scanner(System.in);
System.out.println("This program will calculate the average of any number of "
+ LS + "desired exam scores and return the lowest, highest, and "
+ LS + "letter grade associated with that determined average." + LS);
do {
s2 = "";
indexCounter = 0; // Set index counter to 0
invalidEntry = false;
// Request Student Name
System.out.println("Please enter the name of the Student (nothing to exit):");
String student = input.nextLine();
if (student.equals("")) { break; }
// Get number of Scores to enter from User
int numberOfScores = 0;
while (numberOfScores == 0) {
System.out.println("Please enter the number of scores you wish to enter:");
String numScores = input.nextLine();
// Is the value supplied a String Integer value
if (!numScores.matches("\\d+")) {
System.out.println("Invalid number of scores provided! Try Again..." + LS);
continue;
}
numberOfScores = Integer.parseInt(numScores);
}
exams = new double[numberOfScores]; // Initialize Array
// Request exam scores
while (indexCounter < numberOfScores) {
System.out.println("Please enter exam score #" + (indexCounter + 1) +
" then hit enter: ");
exams[indexCounter] = input.nextDouble();
input.nextLine(); // Clear the scanner buffer
indexCounter++;
}
double highest = high(exams);
double lowest = low(exams);
double average = avg(exams);
char letterGrade = letter(average);
System.out.println(LS + "Results For Student: " + student);
System.out.println("From " + String.format("%02d" , exams.length) +
" exam scores: " +
Arrays.toString(exams).replaceAll("[\\[\\]]", ""));
System.out.println("The lowest exam score is: " + String.format("%.2f",lowest));
System.out.println("The highest exam score is: " + String.format("%.2f", highest));
System.out.println("The average exam score is: " + String.format("%.2f", average));
System.out.println("The letter grade is: " + letterGrade + LS);
while (s2.equals("")) {
System.out.println("Would you like to enter another Student's Scores? (yes/no)");
s2 = input.nextLine();
if (!s2.equalsIgnoreCase("YES") && !s2.equalsIgnoreCase("NO")) {
System.out.println("Invalid Response! 'Yes' or 'No' only!");
s2 = "";
}
}
} while (s2.equalsIgnoreCase("YES") || invalidEntry);
System.out.println("Thank you for using my program!");
You will have noticed that some of the code above takes advantage of Regular Expressions such as "\\s+", "\\d+", "[\\[\\]]", or even "-?\\d+(\\.\\d+)?". It's never to late to learn how to utilize Java's java.util.regex package for pattern matching. It can quickly become a close friend for you and once learned, you will find yourself using it all the time. To test Regular Expressions and to get explanations of what a particular expression does you can use the website RegEx101.com for example: this regex.

Java GradeCalculator - While loop not working correctly

I'm writing a program where a user enters a number of students in a class, the number of exams taken in the class, then enters each student's names and exam scores. The program then calculates that student's grade and assigns them a corresponding letter grade. Then finally, it adds their scores to a classSum, calculates the average class score and displays it.
This is what I have so far:
public class GradeCalculator {
public static void main(String[] args) {
int classSum = 0; // variable used to hold sum of entire classes exams
int classExams = 0; // variable used to hold number of exams taken by whole class
Scanner s = new Scanner(System.in);
System.out.println("Welcome to Gradecalculator!");
System.out.println("Please enter the number of students:");
int students = s.nextInt();
System.out.println("Please enter the number of exams:");
int exams = s.nextInt();
int i = 0;
int studentnumber = 1;
int sum = 0;
while (i < students) { // loop until it matches number of students entered above
i++;
sum = 0;
System.out.println("Enter student " + studentnumber++ + "'s name :");
String studentname = s.next();
System.out.println("Enter exam scores :");
int input = 0;
for (; input < exams; input++) {
int n = s.nextInt();
sum+=n;
if (n < 0) {
System.out.println("Invalid exam scores, reenter: "); //if one of the scores entered is negative, display message
}
}
double average = sum/exams; // assign letter grade based on average of exams
if (average <= 100 && average >= 90) {
System.out.println("Letter grade: A");
System.out.println(studentname + " gets 4 stars! ****");
} if (average <= 89 && average >= 80) {
System.out.println("Letter grade: B");
System.out.println(studentname + " gets 3 stars! ***");
} if (average <= 79 && average >= 70) {
System.out.println("Letter grade: C");
System.out.println(studentname + " gets 2 stars! **");
} if (average <= 69 && average >= 60) {
System.out.println("Letter grade: D");
System.out.println(studentname + " gets 1 star! *");
} if (average <= 59) {
System.out.println("Letter grade: F");
System.out.println(studentname + " gets 0 stars!");
}
classSum += sum; // add sum of this student's scores to the classSum
classExams += exams; // add exams taken by this student to amount of exams taken by whole class
}
int classAverage = classSum/classExams; // compute class average
System.out.println("Class statistics:");
System.out.println("\tAverage: " + classAverage);
}
}
And this is the output I get:
Welcome to Gradecalculator!
Please enter the number of students:
2
Please enter the number of exams:
3
Enter student 1's name :
sam
Enter exam scores :
80 80 80
Letter grade: B
sam gets 3 stars! ***
Enter student 2's name :
joe
Enter exam scores :
90 90 90
Class statistics:
Average: 125
As you can see, after the first student, entering the name and exam scores does not display the next student's letter grade as the loop is supposed to do. Also, the class average is not being calculated correctly. I'm at a bit of a loss here with where I'm going wrong.
You have to set sum to 0 each turn of the loop
while (i < students) { // loop until it matches number of students entered above
i++;
sum = 0;
System.out.println("Enter student " + studentnumber++ + "'s name :"); String studentname = s.next();
// [...]
You do not reset (assign zero to) sum inside the loop. This means that after Sam, when it's Joe's turn, his scores get added on top of sam's! For this reason, you have to reset sum before the score reading and adding loop.
This is also why it didn't print anything about Joe. Since you basically have the sum of the scores of six exams divided by only three, you get a result that's more than a hundred, and that is not covered by any of your if statements.

I am not getting the right result, not sure what I am missing (improper logic, missing steps?)

I am trying to get this problem to work right, what it does it ask for some scores to be entered and then it is supposed to show the name of the person with the highest score. I am getting the last score entered as the highest score, the problem with that is the last score entered is not going to necessarily be the highest one entered. Any ideas on how I can fix this would be appreciated. This is homework and just so nobody says "use a list or array" we have not covered that in class and thus are not supposed to use it for this problem.
public static void main(String[] args)
{
// variables
Scanner input = new Scanner(System.in);
int count = 0;
int numStudents;
double grade = 0, highestGrade = 0;
String name = "", highName = "";
String numGrades =
JOptionPane.showInputDialog
("How many student grades are you entering: ");
numStudents = Integer.parseInt(numGrades);
//for(int count = 0; count < numStudents; count++)
while(count < numStudents)
{
// prompt for the user to enter grades
String inputName =
JOptionPane.showInputDialog("Enter a student name: ");
name = inputName;
//name = input.next(inputName);
String inputGrade =
JOptionPane.showInputDialog("What is that students grade: ");
grade = Double.parseDouble(inputGrade);
//grade = input.nextDouble();
count++;
//if(grade < highestGrade)
if(highestGrade > grade)
{
name = highName;
grade = highestGrade;
}
else
{
continue;
}
}
JOptionPane.showMessageDialog
(null, "The student with the highest score is " + name +
" with a grade of " + grade);
}
if(highestGrade > grade)
{
name = highName;
grade = highestGrade;
}
Since you want to find the highest grade and name, the above should be in reverse.
if(grade > highestGrade )
{
highName = name;
highestGrade = grade;
}
Also, print the found values:
JOptionPane.showMessageDialog
(null, "The student with the highest score is " + highName +
" with a grade of " + highestGrade);
This is backwards:
if(highestGrade > grade)
should be
if(highestGrade < grade)
or more understandably:
if(grade > highestGrade)
EDIT: This is also backwards...
name = highName;
grade = highestGrade;

Categories

Resources