Introduction to programing class. I am stuck on my assignment arrays.
I have an array assignment that I am stuck on. This is the assignment. This is my first time posting here, but I have gained alot of information from the other posts.
Create a program that asks the user how many students there are. Then, for each student, ask for a student id, the first name, last name, and score (must be between 0 and 100 to be valid). When all the data has been entered, calculate and display the average. Allow the user to search for a student by entering a student id. If the student id is valid and a student is found, display the student's full name, score, and deviation of the score from the average. If the student has the highest score, display a message indicating that. Continue asking the user to search for students until the user enters "quit", at which point the program should exit.
The following is the code I have so far. I am not sure how to search the arrays and also how to return all the information from the different arrays.
Code Java:
import java.util.Scanner;
public class Assn06 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// User input number of students
System.out.print("Enter the number of students");
int studentNumber=input.nextInt();
int sum = 0;
// Array for student ID
int[] studentid = new int[studentNumber];
String[] firstname = new String[studentNumber];
String[] lastname = new String[studentNumber];
int[] studentscore = new int[studentNumber];
int i;
for ( i = 0 ; i < studentNumber ; i++)
{
studentscore[i] = studentid[i];
firstname[i] = lastname[i];
System.out.printf("Enter student %d ID : ", i+1);
int studentId = input.nextInt();
studentid[i] = studentId;
System.out.printf("Enter student %d first name : ", i+1);
String studentFirstname = input.next();
firstname[i] = studentFirstname;
System.out.printf("Enter student %d last name : ", i+1);
String studentLastname = input.next();
lastname[i] = studentLastname;
System.out.printf("Enter student %d score : ", i+1);
int studentScore = input.nextInt();
studentid[i] = studentScore;
sum += studentScore;
}
//Calculate Average
int average = sum / studentNumber;
//Display Average
System.out.println("The student average is " + average);
// Search by student Id
System.out.printf("Search for student by entering ID: ");
int studentIdsearch = input.nextInt();
public static int linearSearch(studentid, studentIdsearch)
{
for (int j = 0 ; j < list.length; j++)
{
if (studentIdsearch == list[j])
return j;
}
}
}
}
Re: Introduction to programing class. I am stuck on my assignment arrays.
Hi. I understand you are in an intro class, and while your implementation will work for the purpose of this assignment, have you thought about using a separate class to hold all the information for a student? If you do not know what I am talking about, just forget everything I have said and read on..
You are off to a good start. You have this:
Code :
public static int linearSearch(studentid, studentIdsearch)
{
for (int j = 0 ; j < list.length; j++)
{
if (studentIdsearch == list[j])
return j;
}
}
Let us utilize this method for search. Start with the most basic criteria: Allow the user to search for a student by entering a student id. First, which array would you use to search for IDs? The array that holds IDs, of course: studentid! Now, how would you use that for-loop with studentid[]? When do you stop searching? What do you do when you find the ID? Utilize the linearSearch(.., ...) method -- it is all there for you; you just need to fill everything in!