I am in a Computer Science Class and need help with the following Lab Assingment (WARNIG: this entry may be unintentionally long):
We must create a program that will do these three things:
(1) Allow the user to type in a student name the coorosponds with the student's exam score. The program will be able to accpet exam scores till the user types in the phrase "allDone".
(2) The code will determine which student has the highest score and give the name and the score to the user.
(3) Lastly, the code must print a sorted list of all the students and the corresponding scores listing the highest score first and the lowest score last. Here is a sample output:
Sample Input
Bob 82
Mary 90
James 87
alldone
Sample Output
Mary has the highest score. Her score is: 90.
Sorted List of Students listed bellow
Mary 90
James 87
Bob 82
Inline Code Example Here
In actually, I have done a simliar java code in our previous Computer Science class however it seems that we must use "One Dimensional Arrays" to solve this code so I can not use the exact approch that I used in the previous lab assignment. This is because I must use a bubble sort which works for an array only.
I am definetally not trying to get someone to type the code for me however I would like some help with the logic of the code I am doing though my main question is how to incorrparate a "bubble sort" to the code.
Here are the aspects of code that I need help with (I am trying to avoid typing the whole set of code due to plagiarism [I don't want it to look like I was allowing my code to be appropriated] and it might be to much code to look at. If I must type the whole program then I will if someone suggests that I do so):
Prior to this code being typed, I have initalized the varaible named "studentName" and "studentScore". This line of code allows the user to type in the name of student and the grade that goes with the name:
//Allows for name and score to be type into system.
for(count = 0; count < numberOfStudents; count++)
{
//Prompts user to type name of student.
System.out.println("Type in the name of student " + count + ": ");
//Allows name of student to be stored in array.
studentName [count] = input.nextLine();
//Prompts user to type grade of student.
System.out.println("Type in the score of student " + count + ": ");
//Allows grade of student to be assigned in array.
studentScore [count] = input.nextDouble();
}
When I run the code, the code stops at this loop and gives the following error:
Enter the number of students into the system:
2
Type in the name of student 0:
Type in the score of student 0:
Lane 90
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at chapterSeven.LabOne_Q1.main(LabOne_Q1.java:60)
In other words, how do I reformat this piece of code to allow the user to type in a name seporataly along with the corrospoindng exame score?
This is my "Bubble Sort" method shown in the code block below.
public static void bubbleSort(String [] name, double [] score)
{
double temp;
int counter;
int index = 0;
for(counter = 0; counter < score.length - 1 - counter; index++)
{
if(score[index] > score[index + 1])
{
temp = score[index];
score[index] = score[index + 1];
score[index + 1] = temp;
}
}
}
Looking at the way I have written this code, I think that the exam scores should be sorted and should print to the console if I get the lines of code written above fixed but the corrosponding names will not be printed since I did not use the String [] name array in the method. How would i fix this code to sort not only the scores but print the names that go along side the scores? Do I need another method to accpet the names?
This question is more straight forward. I want to know if I am calling the "Bubble Sort Method" correctly and if the sorted list will be printed since their is no "PrintLine" code for it to print to the console. Here is the code I am refering to:
System.out.println("Here is your Sorted List: ");
bubbleSort(studentName, studentScore);
This question may be less compicated then the others.
The instructions the instructor gave us for this assigment says that the code must stop once the user types in the words "alldone" just like in the Lab assingment from the first Computer Science class however, I typed my code so that the user can tell the computer how many students they want to type in. This will automaticlly stop the program from accepting anymore code so I would not nessicarly need the user to type "alldone".
This is what I typed in my code however it seems like it is not needed:
//Controls when the code will stop and start
boolean endProgram = false;
System.out.println("Are you done with program? If "
+ "so then type in the words \"allDone: \"");
if (studentName.equals("alldone"))
{
//Tells the program to end.
endProgram = true;
}
//Tells the computer what to do if user types in "alldone".
while(!endProgram)
{
//Tells program what to do if score typed in is bigger then the "Highest Score"
if(studentScore[count] > highScore)
{
//Will declare the student's name as the name with the highest score.
highGradeName = studentName[count];
//Will show the student's score since their score is the highest.
highScore = studentScore[count];
}
Should I just remove this? After all, it was in the instructions to include "alldone" but my code stops when the array is full (becasue the user gets to initialize the size of the array when the program says "Type in how many students: ").
For those who took the time to read this unfortunately long entry, I just want to say thanks. I hope someone can at least help with one of the four questions though some of the questions are not totally related to "Bubble Sorting".