๐Ÿ”’ Closed Create a java program

Status
Not open for further replies.
Solution
๐Ÿค–
Sure! Here's a Java program that creates a student inventory with sorting:

Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class StudentInventory {
    
    static class Student {
        String name;
        int id;
        double gpa;
        
        public Student(String name, int id, double gpa) {
            this.name = name;
            this.id = id;
            this.gpa = gpa;
        }
        
        public String getName() {
            return name;
        }
        
        public int getId() {
            return id;
        }
        
        public double getGpa() {
            return gpa;
        }
        
        @Override
        public...
Sure! Here's a Java program that creates a student inventory with sorting:

Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class StudentInventory {
    
    static class Student {
        String name;
        int id;
        double gpa;
        
        public Student(String name, int id, double gpa) {
            this.name = name;
            this.id = id;
            this.gpa = gpa;
        }
        
        public String getName() {
            return name;
        }
        
        public int getId() {
            return id;
        }
        
        public double getGpa() {
            return gpa;
        }
        
        @Override
        public String toString() {
            return "Name: " + name + ", ID: " + id + ", GPA: " + gpa;
        }
    }
    
    static ArrayList<Student> students = new ArrayList<>();
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String name;
        int id;
        double gpa;
        
        // Add students to the inventory
        System.out.println("Enter student information (name, id, gpa):");
        while (true) {
            System.out.print("> ");
            name = scanner.next();
            if (name.equals("done")) {
                break;
            }
            id = scanner.nextInt();
            gpa = scanner.nextDouble();
            students.add(new Student(name, id, gpa));
        }
        
        // Sort the students by name
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                return s1.getName().compareTo(s2.getName());
            }
        });
        
        // Print the sorted students
        System.out.println("Sorted by name:");
        for (Student student : students) {
            System.out.println(student);
        }
        
        // Sort the students by ID
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                return s1.getId() - s2.getId();
            }
        });
        
        // Print the sorted students
        System.out.println("Sorted by ID:");
        for (Student student : students) {
            System.out.println(student);
        }
        
        // Sort the students by GPA
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                return Double.compare(s2.getGpa(), s1.getGpa());
            }
        });
        
        // Print the sorted students
        System.out.println("Sorted by GPA:");
        for (Student student : students) {
            System.out.println(student);
        }
    }
}

This program creates a Student class with name, ID, and GPA fields. The program then prompts the user to enter student information and adds each student to an ArrayList. The program then sorts the students by name, ID, and GPA using the Collections.sort method and custom Comparator objects. Finally, the program prints the sorted students.
 
Solution
Status
Not open for further replies.

About this Thread

  • 3
    Replies
  • 415
    Views
  • 2
    Participants
Last reply from:
V_1 M

Trending Topics

Online now

Members online
1,099
Guests online
963
Total visitors
2,062

Forum statistics

Threads
2,274,767
Posts
28,958,095
Members
1,233,424
Latest member
Hahdjajaban
Back
Top