sort by name ascending import javautil public class Activity

sort by name ascending

import java.util.*;

public class Activity7C

{

public static void main(String[] args) {

StudentList students = new StudentList();

students.add(new Student(8032, \"Casper\", 2.78));

students.add(new Student(3044, \"Sheena\", 3.92));

students.add(new Student(6170, \"Yolanda\", 4.26));

students.add(new Student(1755, \"Geordi\", 3.58));

students.printByNumber();

System.out.println(\"\ End of processing.\");

} }

class Student

{

private int number;

private String name;

private double gpa;

public Student(int number, String name, double gpa) {

this.number = number;

this.name = name;

this.gpa = gpa;

}

public int getNumber()

{

return number;

}

public double getGPA()

{

return gpa;

}

public boolean nameComesBefore(Student other)

{

return name.compareToIgnoreCase(other.name) < 0;

}

public String toString()

{ return number + \" \" + name + \" (\" + gpa + \")\";

}

}

class StudentList { private ArrayList list;

public StudentList() { list = new ArrayList();

}

public void add(Student student)

{

boolean done = false;

int pos;

// find the insertion point (this is just a linear search)

pos = list.size() - 1;

while (pos >= 0 && !done)

{

if (student.getNumber() > list.get(pos).getNumber())

{

done = true;

} else { pos--;

} }

list.add(pos + 1, student);

}

public void printByNumber()

{

System.out.println(\"\ Students ordered by number:\");

printStudents(list);

}

private void printStudents(ArrayList list)

{ System.out.println(\"\ List of all students:\ \");

for (int i = 0; i < list.size(); i++) { System.out.println(i + 1 + \": \" + list.get(i));

}

}

}

OUTPUT should be:

Students ordered by number:

List of all students:

1: 1755 Geordi (3.58)
2: 3044 Sheena (3.92)
3: 6170 Yolanda (4.26)
4: 8032 Casper (2.78)

End of processing.

Students ordered by name:

List of all students:

1. 8032 Casper (2.78)
2. 1755 Geordi (3.58)
3. 3044 Sheena (3.92)
4. 6170 Yolanda (4.26)

Solution

Activity7C.java


import java.util.*;
public class Activity7C
{
public static void main(String[] args) {
StudentList students = new StudentList();
students.add(new Student(8032, \"Casper\", 2.78));
students.add(new Student(3044, \"Sheena\", 3.92));
students.add(new Student(6170, \"Yolanda\", 4.26));
students.add(new Student(1755, \"Geordi\", 3.58));
students.printByNumber();
StudentList studentsName = new StudentList();
studentsName.addByName(new Student(8032, \"Casper\", 2.78));
studentsName.addByName(new Student(3044, \"Sheena\", 3.92));
studentsName.addByName(new Student(6170, \"Yolanda\", 4.26));
studentsName.addByName(new Student(1755, \"Geordi\", 3.58));
studentsName.printByName();

System.out.println(\"\ End of processing.\");
} }
class Student
{
private int number;
private String name;
private double gpa;
public Student(int number, String name, double gpa) {
this.number = number;
this.name = name;
this.gpa = gpa;
}
public int getNumber()
{
return number;
}
public double getGPA()
{
return gpa;
}
public boolean nameComesBefore(Student other)
{
return name.compareToIgnoreCase(other.name) > 0;
}
public String toString()
{ return number + \" \" + name + \" (\" + gpa + \")\";
}
}
class StudentList { private ArrayList list;
public StudentList() { list = new ArrayList();
}
public void add(Student student)
{
boolean done = false;
int pos;
// find the insertion point (this is just a linear search)
pos = list.size() - 1;
while (pos >= 0 && !done)
{
if (student.getNumber() > ((Student)list.get(pos)).getNumber())
{
done = true;
} else { pos--;
} }
list.add(pos + 1, student);
}
public void addByName(Student student)
{
boolean done = false;
int pos;
// find the insertion point (this is just a linear search)
pos = list.size() - 1;
while (pos >= 0 && !done)
{
if (student.nameComesBefore(((Student)list.get(pos))))
{
done = true;
} else { pos--;
} }
list.add(pos + 1, student);
}

public void printByNumber()
{
System.out.println(\"\ Students ordered by number:\");
printStudents(list);
}
public void printByName()
{
System.out.println(\"\ Students ordered by name:\");
printStudents(list);
}

private void printStudents(ArrayList list)
{ System.out.println(\"\ List of all students:\ \");
for (int i = 0; i < list.size(); i++) { System.out.println(i + 1 + \": \" + list.get(i));
}
}
}

Ouput:


Students ordered by number:

List of all students:

1: 1755 Geordi (3.58)
2: 3044 Sheena (3.92)
3: 6170 Yolanda (4.26)
4: 8032 Casper (2.78)

Students ordered by name:

List of all students:

1: 8032 Casper (2.78)
2: 1755 Geordi (3.58)
3: 3044 Sheena (3.92)
4: 6170 Yolanda (4.26)

End of processing.

sort by name ascending import java.util.*; public class Activity7C { public static void main(String[] args) { StudentList students = new StudentList(); students
sort by name ascending import java.util.*; public class Activity7C { public static void main(String[] args) { StudentList students = new StudentList(); students
sort by name ascending import java.util.*; public class Activity7C { public static void main(String[] args) { StudentList students = new StudentList(); students
sort by name ascending import java.util.*; public class Activity7C { public static void main(String[] args) { StudentList students = new StudentList(); students
sort by name ascending import java.util.*; public class Activity7C { public static void main(String[] args) { StudentList students = new StudentList(); students

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site