public class Student implements Comparable<Student>
{
private String name;
private double gradePoints = 0;
private int units = 0;
private int index=-1;
public Student(String name)
{
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int i) {
index=i;
}
public Student(String name, double gpa, int units)
{
this.name = name;
this.units = units;
this.gradePoints = gpa * units;
}
public String getName()
{
return name;
}
public double gpa()
{
if(units > 0)
return gradePoints/units;
return 0;
}
public void addGrade(double gradePointsPerUnit, int units)
{
this.units += units;
this.gradePoints += gradePointsPerUnit * units;
}
public int compareTo(Student other) //Do not change this method. Ask me why if you like.
{
double difference = gpa() - other.gpa();
if(difference == 0) return 0;
if(difference > 0) return 14; //Do not hardcode 14, or -12, into your code.
return -12;
}
}
import java.util.ArrayList;
public class heapgang {
public static void main(String[] args) {
ArrayList<Student> x= new ArrayList<Student>();
Student a= new Student("bob",1,2);
Student b= new Student("arav",3,4);
x.add(a);
x.add(b);
x.set(0, b); // should change the first element in the arraylist to student named "arav"
}
}
At the end of my code, the first element is not change to be named arav. Why is it? I would think after the set method, both elements in the arraylist would be named "arav". I was looking all over stack overflow but I couldn't find a solution.
