The NetBeans IDE keeps putting the error, saying that it cannot find the symbol IO. Cannot do any input or output operations with IO is since "IO" is considered as a symbol. Cannot find out why this is so.
Here is the very simple program i tried to run :-
public class CourseGrade
{
public static void main(String[] args)
{
int examWeight = 70; // Percentage weight given to examination
int labWeight = 20; // Percentage weight given to lab work
int hwWeight = 10; // Percentage weight given to homework
assignment
double examScore; // Examination score obtained by student
double labScore; // Lab score obtained by student
double hwScore; // Homework score obtained by student
double finalGrade; // Final grade obtained by student
// Ask student to input scores for exam, lab and nomework
IO.output("Enter your exam grade: ");
examScore = IO.inputDouble( );
IO.output("Enter your lab grade: ");
labScore = IO.inputDouble( );
IO.output("Enter your homework grade: ");
hwScore = IO.inputDouble( );
// Computer final grade as the weighted sum of exam, lab and
homework scores
examScore = examScore * (examWeight / 100.0);
labScore = labScore * (labWeight / 100.0);
hwScore = hwScore * (hwWeight / 100.0);
finalGrade = examScore + labScore + hwScore;
// Output the final grade
IO.outputln("Your final grade is " + finalGrade);
}
}
This is the Error that i got:-
error: cannot find symbol
IO.output("Enter the exam score :- \n");
symbol: variable IO
location: class CourseGrade
I got this output for each of the IO that i used.
What is causing this ?
Related
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.
This question already has answers here:
Random printing order for System.out & System.err calls [duplicate]
(3 answers)
System.out.println and System.err.println out of order
(7 answers)
Race between System.out and System.err in java [duplicate]
(3 answers)
Closed 4 years ago.
I added in the System.err.flush(); and System.out.flush(); however the problem still exists. It appears as if these printf statements are being output at a random unorthodox order.
When the (absoluteFinalGrade % 1) ranges from 0.45 to 0.49, I would like the output to be printed in red. This is because since I am given hundreds of lines of output, I would like the important ones to stand out. However, system.err.printf seems to be glitching my output in an unorthodox way. Is it possible to use a different method to make my text standout?
import java.util.Scanner;
public class FinalChemistryGradeCalculator {
public static void main (String [] args) {
Scanner input = new Scanner (System.in);
Scanner input2 = new Scanner (System.in);
double termGrade;
double examinationGrade;
double examinationMarkScore;
double examinationMarkTotal;
double lowestTestGrade;
double lowestTestMarkScore;
double lowestTestMarkTotal;
double initialFinalGrade;
double absoluteFinalGrade;
System.out.printf("Input the term grade.%n");
termGrade = input.nextDouble();
System.out.printf("%nThe term grade is %.2f.%n", termGrade);
System.out.printf("%nInput the lowest test mark score.");
lowestTestMarkScore = input.nextDouble();
System.out.printf("%nInput the lowest test mark total.");
lowestTestMarkTotal = input.nextDouble();
lowestTestGrade = (lowestTestMarkScore/lowestTestMarkTotal) * 100;
System.out.printf("%nThe lowest test grade is %.2f.%n", lowestTestGrade);
System.out.printf("%nInput the examination mark total.");
examinationMarkTotal = input.nextDouble();
for (examinationMarkScore = 0; examinationMarkScore <= examinationMarkTotal; examinationMarkScore += 0.5) {
examinationGrade = (examinationMarkScore/examinationMarkTotal) * 100;
initialFinalGrade = (termGrade * 0.7) + (examinationGrade * 0.3);
if (lowestTestGrade < examinationGrade) {
absoluteFinalGrade = initialFinalGrade - (lowestTestGrade * 0.05) + (examinationGrade * 0.05);
}
else {
absoluteFinalGrade = initialFinalGrade;
}
if (absoluteFinalGrade % 1 >= 0.45 && absoluteFinalGrade % 1 <= 0.49) {
System.err.printf("%n%n--------------------------------------------------------------------------------------------------------------");
System.err.printf("%nTerm Grade: %.2f ||||| Examination Grade: %.2f (%.1f/%.1f) ||||| Lowest Test Grade: %.2f (%.1f/%.1f)", termGrade, examinationGrade, examinationMarkScore, examinationMarkTotal, lowestTestGrade, lowestTestMarkScore, lowestTestMarkTotal);
System.err.printf("%nInitial Final Grade: %.2f ||||| Absolute Final Grade: %.2f", initialFinalGrade, absoluteFinalGrade);
System.err.printf("%n--------------------------------------------------------------------------------------------------------------");
System.err.flush();
}
else {
System.out.printf("%n%n--------------------------------------------------------------------------------------------------------------");
System.out.printf("%nTerm Grade: %.2f ||||| Examination Grade: %.2f (%.1f/%.1f) ||||| Lowest Test Grade: %.2f (%.1f/%.1f)", termGrade, examinationGrade, examinationMarkScore, examinationMarkTotal, lowestTestGrade, lowestTestMarkScore, lowestTestMarkTotal);
System.out.printf("%nInitial Final Grade: %.2f ||||| Absolute Final Grade: %.2f", initialFinalGrade, absoluteFinalGrade);
System.out.printf("%n--------------------------------------------------------------------------------------------------------------");
System.out.flush();
}
}
}
}
You can use jansi:
System.out.println( ansi().eraseScreen().render("#|red Hello|# #|green
World|#") );
This example is quoted from Jansi Github README.
Note that this won't probably work from Eclipse or any IDE wrapper.
I am a bit confused, I have a little program I wrote for a school assignment, the constraints I am required to work with is that no method can contain more than 15 lines of code, and we cannot use any for next loops.
What I cant seem to figure out is why I am able to pass an integer to my rounding method and it even returns the rounded value, but then it jumps back to my if / then statements (which it shouldnt be doing) and does not pass the variable.
It is a very rudimentary program I know...my coding skills are not very good, but any help would be appreciated.
The variables I need passed back are testscore and GPA, those need to go back to the main method so they can be stored to new variables and finally pushed off into the results method.
Im still pretty new to coding and the community and how things work...
import java.util.*;
public class Admit {
public static void main(String[] Args) {
Scanner console = new Scanner(System.in);
Introduction();
double testscore = 0;
double GPA = 0;
int Student = 1;
ACTSATScores(1,0,console);
double StudentOneTestScore = testscore;
GPAInfo(0,console);
double StudentOneGPAScore = GPA;
Student = 2;
ACTSATScores(2,0,console);
double StudentTwoTestScore = testscore;
GPAInfo(0,console);
double StudentTwoGPAScore = GPA;
DisplayResults(StudentOneTestScore,StudentOneGPAScore,StudentTwoTestScore,StudentTwoGPAScore);
}
public static void Introduction() {
System.out.println("This program compares two applicants to");
System.out.println("determine which one seems like the stronger");
System.out.println("applicant. For each candidate I will need");
System.out.println("either SAT or ACT scores plus a weighted GPA.");
System.out.println();
}
public static double ACTSATScores(int Student,double testscore,Scanner console) {
System.out.println("Information for applicant #" + Student + ":");
System.out.print("do you have 1) SAT scores or 2) ACT scores? ");
int ACTSAT = console.nextInt();
if ( ACTSAT == 1) {
SAT(Student,testscore,console);
}
if ( ACTSAT == 2) {
ACT(Student,testscore,console);
}
return testscore;
}
public static double SAT(int Student,double testscore,Scanner console) {
System.out.print("SAT math? ");
int SATMath = console.nextInt();
System.out.print("SAT critical reading? ");
int SATReading = console.nextInt();
System.out.print("SAT writing? ");
int SATWriting = console.nextInt();
testscore = ( ( 2 * SATMath + SATReading + SATWriting ) / 32);
System.out.println("exam score = " + roundNumber(testscore));
return ( ( 2 * SATMath + SATReading + SATWriting ) / 32);
}
public static double ACT(int Student,double testscore,Scanner console) {
System.out.print("ACT English? ");
int ACTEnglish = console.nextInt();
System.out.print("ACT math? ");
int ACTMath = console.nextInt();
System.out.print("ACT reading? ");
int ACTReading = console.nextInt();
System.out.print("ACT science? ");
int ACTScience = console.nextInt();
testscore = ( ( 2 * ACTMath + ACTEnglish + ACTReading + ACTScience ) / 1.8 );
System.out.println("exam score = " + roundNumber(testscore));
return testscore;
}
public static double GPAInfo(double GPA,Scanner console) {
System.out.print("overall GPA? ");
double OverallGPA = console.nextDouble();
System.out.print("max GPA? ");
double MaxGPA = console.nextDouble();
System.out.print("Transcript Multiplier? ");
double TranscriptMultiplier = console.nextDouble();
GPA = ( OverallGPA / MaxGPA * 100 * TranscriptMultiplier );
System.out.println("GPA score = " + roundNumber(GPA));
return GPA;
}
public static double roundNumber(double number) {
return (Math.round(number * 10)) / 10.0;
}
public static double DisplayResults(double StudentOneTestScore, double StudentOneGPAScore, double StudentTwoTestScore, double StudentTwoGPAScore) {
System.out.println("First applicant overall score = " + StudentOneTestScore + StudentOneGPAScore);
System.out.println("Second applicant overall score = " + StudentTwoTestScore + StudentTwoGPAScore);
if ( StudentOneTestScore + StudentOneGPAScore > StudentTwoTestScore + StudentTwoGPAScore ) {
System.out.println("The first applicant seems to be better");
}
else if ( StudentOneTestScore + StudentOneGPAScore < StudentTwoTestScore + StudentTwoGPAScore ) {
System.out.println("The second applicant seems to be better");
}
else {
System.out.println("The two applicants seem to be equal");
}
return StudentOneTestScore;
}
}
Expected output:
This program compares two applicants to
determine which one seems like the stronger
applicant. For each candidate I will need
either SAT or ACT scores plus a weighted GPA.
Information for applicant #1:
do you have 1) SAT scores or 2) ACT scores? 1
SAT math? 450
SAT critical reading? 530
SAT writing? 490
exam score = 60.0
overall GPA? 3.4
max GPA? 4.0
Transcript Multiplier? 0.9
GPA score = 76.5
Information for applicant #2:
do you have 1) SAT scores or 2) ACT scores? 2
ACT English? 25
ACT math? 20
ACT reading? 18
ACT science? 15
exam score = 54.4
overall GPA? 3.3
max GPA? 4.0
Transcript Multiplier? 0.95
GPA score = 78.4
First applicant overall score = 136.5
Second applicant overall score = 132.8
The first applicant seems to be better
My output
This program compares two applicants to
determine which one seems like the stronger
applicant. For each candidate I will need
either SAT or ACT scores plus a weighted GPA.
Information for applicant #1:
do you have 1) SAT scores or 2) ACT scores? 1
SAT math? 450
SAT critical reading? 530
SAT writing? 490
exam score = 60.0
overall GPA? 3.4
max GPA? 4.0
Transcript Multiplier? 0.9
GPA score = 76.5
Information for applicant #2:
do you have 1) SAT scores or 2) ACT scores? 2
ACT English? 25
ACT math? 20
ACT reading? 18
ACT science? 15
exam score = 54.4
overall GPA? 3.3
max GPA? 4.0
Transcript Multiplier? 0.95
GPA score = 78.4
First applicant overall score = 0.00.0
Second applicant overall score = 0.00.0
The two applicants seem to be equal
Corrected Code for anyone that needs it
import java.util.*;
public class Admit {
public static void main(String[] Args) {
Scanner console = new Scanner(System.in);
Introduction();
int Student = 1;
double StudentOneTestScore = ACTSATScores(1,console);
double StudentOneGPAScore = GPAInfo(0,console);
Student = 2;
double StudentTwoTestScore = ACTSATScores(1,console);
double StudentTwoGPAScore = GPAInfo(0,console);
DisplayResults(StudentOneTestScore,StudentOneGPAScore,StudentTwoTestScore,StudentTwoGPAScore);
}
public static void Introduction() {
System.out.println("This program compares two applicants to");
System.out.println("determine which one seems like the stronger");
System.out.println("applicant. For each candidate I will need");
System.out.println("either SAT or ACT scores plus a weighted GPA.");
System.out.println();
}
public static double ACTSATScores(int Student,Scanner console) {
double testscore = 0;
System.out.println("Information for applicant #" + Student + ":");
System.out.print(" do you have 1) SAT scores or 2) ACT scores? ");
int ACTSAT = console.nextInt();
if ( ACTSAT == 1) {
testscore = SAT(console);
}
if ( ACTSAT == 2) {
testscore = ACT(console);
}
return testscore;
}
public static double SAT(Scanner console) {
System.out.print(" SAT math? ");
int SATMath = console.nextInt();
System.out.print(" SAT critical reading? ");
int SATReading = console.nextInt();
System.out.print(" SAT writing? ");
int SATWriting = console.nextInt();
double testscore = ( ( 2 * SATMath + SATReading + SATWriting ) / 32);
System.out.println(" exam score = " + roundNumber(testscore));
return testscore;
}
public static double ACT(Scanner console) {
System.out.print(" ACT English? ");
int ACTEnglish = console.nextInt();
System.out.print(" ACT math? ");
int ACTMath = console.nextInt();
System.out.print(" ACT reading? ");
int ACTReading = console.nextInt();
System.out.print(" ACT science? ");
int ACTScience = console.nextInt();
double testscore = ( ( 2 * ACTMath + ACTEnglish + ACTReading + ACTScience ) / 1.8 );
System.out.println(" exam score = " + roundNumber(testscore));
return testscore;
}
public static double GPAInfo(double GPA,Scanner console) {
System.out.print(" overall GPA? ");
double OverallGPA = console.nextDouble();
System.out.print(" max GPA? ");
double MaxGPA = console.nextDouble();
System.out.print(" Transcript Multiplier? ");
double TranscriptMultiplier = console.nextDouble();
GPA = ( OverallGPA / MaxGPA * 100 * TranscriptMultiplier );
System.out.println(" GPA score = " + roundNumber(GPA));
System.out.println();
return GPA;
}
public static double roundNumber(double number) {
return (Math.round(number * 10)) / 10.0;
}
public static double finalScore(double TestScore, double GPAScore) {
return TestScore + GPAScore;
}
public static double DisplayResults(double StudentOneTestScore, double StudentOneGPAScore, double StudentTwoTestScore, double StudentTwoGPAScore) {
double StudentOneScore = finalScore(StudentOneTestScore,StudentOneGPAScore);
double StudentTwoScore = finalScore(StudentTwoTestScore,StudentTwoGPAScore);
System.out.println("First applicant overall score = " + roundNumber(StudentOneScore)); //StudentOneTestScore + StudentOneGPAScore);
System.out.println("Second applicant overall score = " + roundNumber(StudentTwoScore)); //StudentTwoTestScore + StudentTwoGPAScore);
if ( StudentOneTestScore + StudentOneGPAScore > StudentTwoTestScore + StudentTwoGPAScore ) {
System.out.println("The first applicant seems to be better");
} else if ( StudentOneTestScore + StudentOneGPAScore < StudentTwoTestScore + StudentTwoGPAScore ) {
System.out.println("The second applicant seems to be better");
} else {
System.out.println("The two applicants seem to be equal");
}
return StudentOneTestScore;
}
}
It seems you have a problem when using methods, returning types and inmutable types.
In the main section:
double testscore = 0;
double GPA = 0;
int Student = 1;
ACTSATScores(1,0,console);
double StudentOneTestScore = testscore;
GPAInfo(0,console);
double StudentOneGPAScore = GPA;
Student = 2;
ACTSATScores(2,0,console);
double StudentTwoTestScore = testscore;
You are assigning those variables the value "testscore". But you really never change that value, and the way you wrote the methods makes me believe that you are expecting testscore to change inside the methods.
Try printing the value of testscore in the main to see that it does not change its value (it should be always 0 in that main). This might be what you are looking for:
double StudentOneTestScore = ACTSATScores(1,0,console);
I am new to Java and I am trying to code a "Grading" system. Basically, when the code is ran, a window opens, it asks a question, you enter a number value, press enter, it loads another question, same process as before, hit enter, and repeat for the final time. At the end it takes those 3 numbers, does math and then displays a message. I have the basic code, I am just having issues with the popup window.
public class PopUp {
public static void main(String[] args) {
// Constant grade worth percentage
final int examWeight = 55;
final int hwWeight = 20;
final int activityWeight = 25;
// Current grades
double examGrade = 65.8;
double hwGrade = 55.3;
double activityGrade = 29.5;
double finalGrade;
// Math to figuring out final grade.
examGrade = examGrade * (examWeight / 100.0);
hwGrade = hwGrade * (hwWeight / 100.0);
activityGrade = activityGrade * (activityWeight / 100.0);
finalGrade = examGrade + activityGrade + hwGrade;
//round thingy
double rounded = Math.round(finalGrade *10)/10.0;
// What you made
if (rounded>=90.0 && rounded<100.0) {
System.out.println("You made an A");
}
if (rounded>=85.0 && rounded<92.9) {
System.out.println("You made a B");
}
if (rounded>=75.0 && rounded<84.9) {
System.out.println("You made a C");
}
if (rounded>=67.0 && rounded<74.9) {
System.out.println("You made a D");
}
if (rounded<=59.9) {
System.out.println("You are a fucking failure");
}
System.out.println("Your final grade is: " + rounded);
}
}
I need THAT code to display the information like this in a NEW WINDOW:
So basically:
Question one: 98.8 ENTER
Question two: 76.9 ENTER
Question three: 87.6 ENTER
Result: 93.3 END
In BlueJ the result I am trying to replicate in Eclipse is as follows:
// Ask student to input scores for exam, lab and nomework
IO.output("Enter your exam grade: ");
examScore = IO.inputDouble( );
IO.output("Enter your lab grade: ");
labScore = IO.inputDouble( );
IO.output("Enter your homework grade: ");
hwScore = IO.inputDouble( );
Then the very end would be
// Output the final grade
IO.outputln("Your final grade is " + finalGrade);
http://prntscr.com/9hicp6 -- Proof that it is a new window.
If you want a popup windows maybe this will help you:
String grade = JOptionsPane.showInputDialog("Enter your grade:");
Good luck.
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
}
}