Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I want to compare two objects in ArrayList by their specific property and print the result. But my code does not work properly or it's
I want to compare two objects in ArrayList by their specific property and print the result.
But my code does not work properly or it's wrong totally.
And it will be good to understand how and why
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Management ql = new Management();
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("1. Add student. ");
System.out.println("2. Edit student. ");
System.out.println("3. Show info. ");
System.out.println("4. Sort GPA o. ");
int choose;
System.out.println("Enter the selection :");
choose = Integer.parseInt(sc.nextLine());
switch(choose)
{
case 1:
{
ql.addStudent();
break;
}
case 2:
{
ql.editStudent();
break;
}
case 3:
{
ql.showStudent();
break;
}
case 4:
{
ql.sortStudentByGpa();
}
}
}
}
}
import java.util.Scanner;
public class Student implements Comparable
Scanner input = new Scanner(System.in);
public String studentName;
public int studentID;
public int GPA;
public int getGPA() {
return GPA;
}
public void setGPA(int gPA) {
GPA = gPA;
}
public Student(String studentName, int studentID, int gPA) {
super();
this.studentName = studentName;
this.studentID = studentID;
GPA = gPA;
}
public Student() {
// TODO Auto-generated constructor stub
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getStudentID() {
return studentID;
}
public int getStudentID(int i) {
return studentID;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public void inputInfo() {
System.out.println("Enter student's name : ");
studentName = input.nextLine();
System.out.println("Enter student's ID: ");
studentID = input.nextInt();
System.out.println("Enter student's GPA: ");
GPA = input.nextInt();
}
public void showInfo() {
toString();
}
public String toString() {
return "Student ID = [ " + studentID + " ]" + " , Name = [ " + studentName + " ]" + " GPA = [ " +
GPA + " ]";
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
Student isLess = (Student) o;
return this.GPA - isLess.GPA;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Management extends Student {
ArrayList school = new ArrayList();
public void addStudent() {
Student newPerson = new Student();
newPerson.inputInfo();
school.add(newPerson);
}
public void editStudent() {
int id;
System.out.println("Enter name or ID to edit : ");
id = input.nextInt();
for(int i=0; i
if(school.get(i).getStudentID() == id) {
Student editedStudent = new Student();
editedStudent.inputInfo();
school.set(i, editedStudent);
break;
}
}
}
public void sortStudentByName() {
Collections.sort(school ,new Comparator()
{
public int compare(Student st1,Student st2)
{
return st1.getStudentName().compareTo(st2.getStudentName());
}
});
}
public void showStudent() {
for(int i=0; i
System.out.println("Student number : [ " + (i+1) + school.get(i).toString());
}
}
public void deleteStudent(int id) {
int deleting = id;
for(int i=0; i
module-Info.java DStudent.jave Management.java Main.java 92 93 95 98 990 100 -1010 102 970 public void sortStudentByGpa() lections.sort(school, new Comparator Student>() public int compare(Student sti, Student st2) if(st1.getGPA() st2.getGPAO) return 1; else 115 return ; 122 stuch Nane 119 120 1210 public Management(String studentName, int student ID, int GPA) super(studentName, student ID, GPA); 123 // TODO Auto-generated constructor stub 124 } 125 126 public Management() { 2127 // TODO Auto-generated constructor stub 128 } gene 129 130 131 132 } Console Problems Debug Shell Main (Java Application) /Library/Java/Java Virtual Machines/idk-13.0.2.jdk/Contents/Home/bin/java (28 Oca 2020 16:47:54) 1. Add student. 2. Edit student. 3. Show info. 4. Sort GPA oc. Enter the selection : 1. Add student. 2. Edit student if(school.get(i).getStudentID() == deleting){
school.remove(i);
break;
}
}
}
/* public void sortStudentByName() {
}
*/
public void sortStudentByGpa()
{
Collections.sort(school, new Comparator()
{
public int compare(Student st1,Student st2)
{
if(st1.getGPA()
{
System.out.println(st1.getGPA() + "is less than" + st2.getGPA());
return -1;
}
else if(st1.getGPA() > st2.getGPA())
{
return 1;
}
else
{
return 0;
}
}
});
}
public Management(String studentName, int studentID, int gPA) {
super(studentName, studentID, gPA);
// TODO Auto-generated constructor stub
}
public Management() {
// TODO Auto-generated constructor stub
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started