简体   繁体   中英

Simple ArrayList program-JAVA

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.

Your problem appears to be the fact that you are entering into infinite loops when you check using your while statement. Use if statements like so:

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 is going to leave you with another problem, in that you are comparing the mark to the id . You need to check the value of mark against mark , then assign the id . like 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();
        }
    }

Use:

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 marks but then assigning id. To keep track of the student who had the most or least marks, you can add:

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

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.

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

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

You should assign your mark to smallest or largest, not Id.

You can also tracking the index number of your students which has highest or lowest marks, then get the id and mark from your students arraylist

You need an additional variable:

Currently you store your best/worst student id in smallest / largest , while you compare it with the mark . You need something like:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM