Simple ArrayList-JAVA Program

I am trying to solve this question:

Define a Student class with studentID attributes and labels in module 1030Y. The Student class must also contain a default constructor along with a constructor to initialize an object of type Student with custom values, mutator and accessors for each attribute and display method. Write a test program that stores an ArrayList Student object (with IDs in the range 701-799 and marks in the range 0.0-100.0). The program will allow the user to enter the student number and character for each student. After all are entered, the software will display the student IDs with the highest and lowest grades.

For some reason, the part where I try to get the lowest and highest grade from the students doesn't work.

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 work until the for loop.

+3


source to share


5 answers


Your problem is that you are going into infinite loops when you check using your while statement. Use expressions if

like:

for(int i=0; i<students.size(); i++)
    {
        if(students.get(i).getMark() < smallest)
        {

            smallest = students.get(i).getID();
        }

        if(students.get(i).getMark() > largest)
        {

            largest = students.get(i).getID();
        }
    }

      



However, this will leave you with a different problem as you are comparing mark

with id

. You need to check the value mark

on mark

and then assign id

. So:

int largestMark = 0;
inst smallestMark = 9999;
for(int i=0; i<students.size(); i++)
    {
        if(students.get(i).getMark() < smallestMark)
        {
            smallestMark = students.get(i).getMark();
            smallest = students.get(i).getID();
        }

        if(students.get(i).getMark() > largestMark)
        {
            largestMark = students.get(i).getMark();
            largest = students.get(i).getID();
        }
    }

      

+3


source


Using:

if(smallest > students.get(i).getMark())
{
    smallest = students.get(i).getMark();
}

if(largest < students.get(i).getMark())
{
    largest = students.get(i).getMark();
}

      



You were comparing labels and then assigning id. To track the student with the highest or lowest grade, you can add:

studentWithMostMarks = students.get(i).getId()

      

+1


source


The program will allow the user to enter the student number and character for each student. After all are entered, the software will display the student IDs with the highest and lowest grades.

while(smallest > students.get(i).getMark()) {
    smallest = students.get(i).getID();
}

while(largest < students.get(i).getMark()) {
  largest = students.get(i).getID();
}

      

You must assign your sign to the smallest or largest, not the identifier.

You can also track your index number students

with the highest or lowest marks, then get id and mark from your students

arraylist

+1


source


You need an additional variable:

Currently, you are storing your best / worst student id in smallest

/ largest

, whereas you are comparing it to mark

. You need something like:

smallestId;
smallestMark;//initialize biig
largestId;
largestMark;//initialize looow

      

+1


source


This piece of code is probably causing an infinite loop:

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();
    }
}

      

You have to change it:

for(int i=0; i<students.size(); i++)
{
    if(smallest > students.get(i).getMark())
    {
        smallest = students.get(i).getID();
    }

    if(largest < students.get(i).getMark())
    {
        largest = students.get(i).getID();
    }
}

      

+1


source







All Articles