I'm trying to solve this question:
Define a class Student with attributes studentID and marks in the module 1030Y. The class Student must also contain a default constructor along with a constructor to initialize an object of type Student with user-defined values, mutator and accessor methods for each attribute and a display method. Write a test program that maintains an ArrayList of Student objects (with IDs in the range 701-799 and marks in the range 0.0 to 100.0). The program will allow the user to input the student id number and the mark for each student. After all the input has been done, the program will display the ids of the students with the highest and the lowest marks.
For some reason the part where i'm trying to get the lowest and highest mark of the students is not working.
Here's my codes:
Student.java:
package Number5;
public class Student {
private int studentID;
private float mark;
public Student()
{
studentID = 0;
mark = 0;
}
public Student(int id, float marks)
{
this.studentID = id;
this.mark = marks;
}
public void setID(int id)
{
this.studentID = id;
}
public int getID()
{
return studentID;
}
public void setMark(float marks)
{
this.mark = marks;
}
public float getMark()
{
return mark;
}
public void display()
{
System.out.println("Student ID: "+getID());
System.out.println("Marks: "+getMark());
}
}
testStudent.java:
package Number5;
import java.util.ArrayList;
import java.util.Scanner;
public class testStudent {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<Student>();
int id=0; float mark=0;
Scanner input = new Scanner(System.in);
do
{
System.out.print("Enter the student ID: ");
id = input.nextInt();
System.out.print("Enter the marks: ");
mark = input.nextFloat();
students.add(new Student(id,mark));
}
while(id != 0 || mark != 0);
int smallest = 9999, largest = -9999;
for(int i=0; i<students.size(); i++)
{
while(smallest > students.get(i).getMark())
{
smallest = students.get(i).getID();
}
while(largest < students.get(i).getMark())
{
largest = students.get(i).getID();
}
}
System.out.println("Smallest is "+smallest);
System.out.println("Largest is "+largest);
}
}
The program just stops after reading the user input. It doesn't even go until the for loop.
#define define undef