3

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.

11
  • I don't know, you tell us. What could be wrong? Commented Apr 30, 2015 at 21:07
  • Obviously my codes could be wrong, right? If i knew exactly where i wouldn't be asking. Commented Apr 30, 2015 at 21:08
  • 2
    @RaGe #define define undef Commented Apr 30, 2015 at 21:13
  • 2
    @downvoter Can you explain why please? So that i don't make the same mistake hereafter. Is being a beginner in java wrong? Commented Apr 30, 2015 at 21:13
  • 1
    The problem in your code is not the ArrayList. Commented Apr 30, 2015 at 21:21

5 Answers 5

3

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();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

1

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

6 Comments

even though the while loop works in this case, I'd replace it with an if-statement as it makes the code more readable
thanks, agree. only pointed the wrong part, not optimize it yet.
@downvoter. can you explain why downvote? In my opinion, we need to point out OP's logic error and help him out, code optimization is making code more readable and works more efficient, here it is optional.
This doesn't solve the problem. You didn't change any of the code. It won't print out the ID with the highest or lowest mark. You are also comparing marks to ID's.
@BlackHatSamurai that's OP's code, not mine, I was pointing out where was not correct. My explanation is underneath
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.