I am new to CS and I am doing this online java class which is a joke tbh. I say that because the books we got are $15 books, custom made that were used for a summer camp that was "taught" to kids. There is no where in the book that has actual java information, it is just a bunch of programs. I did a bit of searching around and I can't seem to find what I am looking for.
One thing we have to do is modify the program so it can display first and last name. I have looked and seen that you can do two strings to get first and last name. Is there something that will get the entire line in java?
package computegrades;
import java.util.Scanner;
public class ComputeGrades {
public static void main (String[] args) {
String[] names = getNames();
int[] scores = getScores(names);
double average = computeAverage(scores);
int highestIndex = getHighest(scores);
int lowestIndex = getLowest(scores);
System.out.format("Average = %3.2f\n", average);
System.out.println("Highest = " + names[highestIndex] + ": " + scores [highestIndex]);
System.out.println("Lowest = " + names[lowestIndex] + ": " + scores [lowestIndex]);
printLetterGrades(names, scores);
}
public static void printLetterGrades(String[] names, int[] scores) {
for (int i = 0; i < names.length; i++) {
if (scores[i] >= 90) {
System.out.println(names[i] + ": A");
} else if (scores[i] >= 80) {
System.out.println(names[i] + ": B");
} else if (scores[i] >= 70) {
System.out.println(names[i] + ": C");
} else if (scores[i] >= 60) {
System.out.println(names[i] + ": D");
} else {
System.out.println(names[i] + ": F");
}
}
}
public static String[] getNames() {
Scanner input = new Scanner(System.in);
System.out.println("How many students?");
int n = input.nextInt();
System.out.println("Please enter their first names:");
String[] names = new String[n];
for (int i = 0; i < names.length; i++) {
System.out.print("name " + (i + 1) + ": ");
names[i] = input.next();
}
System.out.println("Thank you.");
return names;
}
public static int[] getScores(String[] names) {
System.out.println("Now, please enter their scores:");
Scanner input = new Scanner(System.in);
int[] scores = new int[names.length];
for (int i = 0; i < names.length; i++) {
System.out.print(names[i] + "'s ");
System.out.print("score: ");
while(!input.hasNextInt()){
input.next();
System.out.println("Please enter an integer for the score");
System.out.print("scores: ");
}
scores[i] = input.nextInt();
}
return scores;
}
public static double computeAverage(int[] scores) {
double sum = 0;
for (int i = 0; i < scores.length; i++) {
sum += scores[i];
}
return sum / scores.length;
}
public static int getHighest(int[] scores) {
int highestIndex = Integer.MIN_VALUE;
int highestScore = Integer.MIN_VALUE;
for (int i = 0; i < scores.length; i++) {
if (scores[i] > highestScore) {
highestScore = scores[i];
highestIndex = i;
}
}
return highestIndex;
}
public static int getLowest(int[] scores) {
int lowestIndex = Integer.MAX_VALUE;
int lowestScore = Integer.MAX_VALUE;
for (int i = 0; i < scores.length; i++) {
if (scores[i] < lowestScore) {
lowestScore = scores[i];
lowestIndex = i;
}
}
return lowestIndex;
}
}