Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Need Help With My Sorting and Arrays

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help With My Sorting and Arrays

    Hello everyone, I still learning java so I'm still not familiar with how to call and how to change from different classes and using arrays but right now I'm trying to create a simple code like a school system where there show all the student name list with a grade in all the classes there are in using arrays object to store the data. I don't want to use ArrayList so NO ArrayList.

    and it seems that I don't know how to do it. I'm really stuck right now can anyone help me with my code.

    I WANT MY OUTPUT TO BE LIKE THIS:-

    === SCHOOL LISTS ===

    1. Class Thunder with Total grade: 146
    2. Class Lightning with Total grade: 103.9
    3. Class Sunrise with Total grade: 115.5

    --------------------
    Class: Sunrise
    Class quantity: 5

    Student1: James Shawn
    Grade: 75
    Student2: Ali Pole
    Grade: 30.5
    Student3: Tong Kim
    Grade: 10

    Total Grade: 115.5
    ------------------------
    Class: Thunder
    Class quantity: 5

    Student1: Mark Lee
    Grade: 65.5
    Student2: James Mic
    Grade: 20.4
    Student3: James Mic
    Grade: 18.0

    Total Grade: 103.9
    ------------------------
    Class: Lightning
    Class quantity: 5

    Student1: Luke Kim
    Grade: 90
    Student2: Noth Shawn
    Grade: 44
    Student3: Lex Wale
    Grade: 12

    Total Grade: 146

    Do you want to go back to the selection function?
    Press Y or N:
    //if yes they will go back to the functionSelection
    //if no they will ask users if they want to exit the systems.

    Do you want to exit (Y or N):

    //if yes they will exit the systems
    //if no they will go back to the functionSelection.

    ---------------------------------------------------------------------------------------------------------------------------------------

    The class should have an array class of 10 sizes but initialize with 5 pre-team set.
    and the student is only set to have 20 sizes (mean 20 students per class can't be more than the number set)

    BELOW ARE MY CODE:-

    STUDENT CLASS
    import java.util.Comparator;
     
    public class Student {
     
        private String studentName;
        private int grade;
     
        public Student(String studentName, int score) {
            this.studentName = studentName;
            this.grade = grade;
        }
     
        public String toString() {
            return "\nStudent Name: " + studentName + "\nGrade: " + grade;
        }
     
        public String getStudentName() {
            return studentName;
        }
     
        public int getGrade() {
            return grade;
        }
     
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }
     
        public void setGrade(int grade) {
            this.grade = grade;
        }   
    }
     
    //Sorting side
    class sortGrade implements Comparator<Student> {
     
            @Override
            public int compare(Student a, Student b) {
            return (a.getGrade() - b.getGrade());
        }
    }
    -----------------------------------------------------------------------
    CLASS CLASS
    public class Class {
     
        Student[] student;
        private String className;
        private int classQuantity;
        private int totalGrade = 0;
     
        //add student to the class
         public void addNewStudent(String studentName, double grade) {
     
        }
     
        public double calculateTotalGrade() {
     
            for (Student students : student) {
                totalGrade += students.getGrade();
            }
            return Math.round(totalGrade * 10.0) / 10.0;
        }
     
        public Class(Student[] student,
                String className, int classQuantity) {
     
            this.student = student;
            this.className = className;
            this.classQuantity = classQuantity;
        }
     
        public Class(String className, int classQuantity) {
     
            this.className = className;
            this.classQuantity = classQuantity;
        }
     
        public String toString() {
            String studentList = " ";
     
            for (int i = 0; i < student.length; i++) {
                studentList += "\n\tStudent " + (i + 1) + ": " + student[i].getStudentName()
                        + "\n\tGrade: " + student[i].getGrade();
            }
     
            return "\nClass Name: " + className
                    + "\nClass Quantity: " + classQuantity + "\n" + studentList;
        }
    }
    --------------------------------------------------------------------------------------
    MAIN DRIVER CLASS
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Scanner;
     
    public class MainDriver {
     
        private Object[] a;
     
        void printSort(Student[] sortList) {
            for (Object sort : a) {
                System.out.print(sort + " ");
            }
            System.out.println();
        }
     
        public static void main(String[] args) {
     
            int size = 10;
            Class[] classroom = new Class[size];
     
            Student[] studentList1 = {
                new Student("Tong Kim", 10),
                new Student("Ali Pole", 30),
                new Student("James Shawn", 75)};
     
            Student[] studentList2 = {
                new Student("James Mic", 20),
                new Student("Ho Kim", 18),
                new Student("Mark Lee", 65)};
     
            Student[] studentList3 = {
                new Student("Luke Kim", 90),
                 new Student("Noth Shawn", 44),
                 new Student("Lex Wale", 12)
            };
     
            classroom[0] = new Class(studentList1, "Sunrise", 20);
            classroom[1] = new Class(studentList2, "Thunder", 10);
            classroom[2] = new Class(studentList3, "Lightning", 5);
     
            Scanner input = new Scanner(System.in);
            boolean functionSelection = true;
     
           // Student[] SortList = studentList1;
     
            while (functionSelection) {
     
                System.out.println("=== SCHOOL SYSTEM === \n");
                System.out.println("[1] Add New Class");
                System.out.println("[2] Exit");
     
                System.out.print("\nChoose a function: ");
     
                //Check if it is other then number
                if (!input.hasNextInt()) {
                    System.out.println(" \nChoose the existing number");
                    input.nextLine();
                    continue;
                }
     
                int choice = input.nextInt();
     
                String newClassName, searchClass, newName, addName;
                switch (choice) {
                    case 1:
     
                        //Show School List
                        /*List all class, student, grade and the total grade
                        in ascending*/
     
                        System.out.println("=== SCHOOL LIST ===");
     
                       Student[] SortList = studentList1;
     
                       //Descending Order Arrays
     
                        Comparator<Student> descendingOrder;
                        descendingOrder = Collections.reverseOrder
                        (new sortGrade());
     
                        Arrays.sort(SortList, descendingOrder);
                        System.out.println(Arrays.toString(SortList) + "\n");
                    case 2: 
                        System.exit(0);
                        break;
                    default:
                        System.out.println(" \nChoose the existing number");
                }
            }
        }
    }

    Please help me with my code
    Last edited by eecanisha; October 21st, 2020 at 11:59 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help With My Sorting and Arrays

    What happens when you compile the program? Are there any errors? Please copy and paste them here.

    If there are no compiler errors, what happens when you execute the program? Please copy and paste here the console from when you execute the program. Be sure to add some comments on the lines that are not what you want to see.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help With My Sorting and Arrays

    I WANT MY OUTPUT TO BE LIKE THIS:-

    === SCHOOL LISTS ===

    1. Class Thunder with Total grade: 146
    2. Class Lightning with Total grade: 103.9
    3. Class Sunrise with Total grade: 115.5

    --------------------
    Class: Sunrise
    Class quantity: 5

    Student1: James Shawn
    Grade: 75
    Student2: Ali Pole
    Grade: 30.5
    Student3: Tong Kim
    Grade: 10

    Total Grade: 115.5
    ------------------------
    Class: Thunder
    Class quantity: 5

    Student1: Mark Lee
    Grade: 65.5
    Student2: James Mic
    Grade: 20.4
    Student3: James Mic
    Grade: 18.0

    Total Grade: 103.9
    ------------------------
    Class: Lightning
    Class quantity: 5

    Student1: Luke Kim
    Grade: 90
    Student2: Noth Shawn
    Grade: 44
    Student3: Lex Wale
    Grade: 12

    Total Grade: 146

    Do you want to go back to the selection function?
    Press Y or N:
    //if yes they will go back to the functionSelection
    //if no they will ask users if they want to exit the systems.

    Do you want to exit (Y or N):

    //if yes they will exit the systems
    //if no they will go back to the functionSelection.

    ---------------------------------------------------------------------------------------------------------------------------------------

    The class should have an array class of 10 sizes but initialize with 5 pre-team set.
    and the student is only set to have 20 sizes (mean 20 students per class can't be more than the number set)

    BELOW ARE MY CODE:-

    import java.util.Comparator;
     
    public class Student {
     
        private String studentName;
        private int grade;
     
        public Student(String studentName, int score) {
            this.studentName = studentName;
            this.grade = grade;
        }
     
        public String toString() {
            return "\nStudent Name: " + studentName + "\nGrade: " + grade;
        }
     
        public String getStudentName() {
            return studentName;
        }
     
        public int getGrade() {
            return grade;
        }
     
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }
     
        public void setGrade(int grade) {
            this.grade = grade;
        }   
    }
     
    //Sorting side
    class sortGrade implements Comparator<Student> {
     
            @Override
            public int compare(Student a, Student b) {
            return (a.getGrade() - b.getGrade());
        }
    }
    -----------------------------------------------------------------------
     
    public class Class {
     
        Student[] student;
        private String className;
        private int classQuantity;
        private int totalGrade = 0;
     
        //add student to the class
         public void addNewStudent(String studentName, double grade) {
     
        }
     
        public double calculateTotalGrade() {
     
            for (Student students : student) {
                totalGrade += students.getGrade();
            }
            return Math.round(totalGrade * 10.0) / 10.0;
        }
     
        public Class(Student[] student,
                String className, int classQuantity) {
     
            this.student = student;
            this.className = className;
            this.classQuantity = classQuantity;
        }
     
        public Class(String className, int classQuantity) {
     
            this.className = className;
            this.classQuantity = classQuantity;
        }
     
        public String toString() {
            String studentList = " ";
     
            for (int i = 0; i < student.length; i++) {
                studentList += "\n\tStudent " + (i + 1) + ": " + student[i].getStudentName()
                        + "\n\tGrade: " + student[i].getGrade();
            }
     
            return "\nClass Name: " + className
                    + "\nClass Quantity: " + classQuantity + "\n" + studentList;
        }
    }
    --------------------------------------------------------------------------------------
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Scanner;
     
    public class MainDriver {
     
        private Object[] a;
     
        void printSort(Student[] sortList) {
            for (Object sort : a) {
                System.out.print(sort + " ");
            }
            System.out.println();
        }
     
        public static void main(String[] args) {
     
            int size = 10;
            Class[] classroom = new Class[size];
     
            Student[] studentList1 = {
                new Student("Tong Kim", 10),
                new Student("Ali Pole", 30),
                new Student("James Shawn", 75)};
     
            Student[] studentList2 = {
                new Student("James Mic", 20),
                new Student("Ho Kim", 18),
                new Student("Mark Lee", 65)};
     
            Student[] studentList3 = {
                new Student("Luke Kim", 90),
                 new Student("Noth Shawn", 44),
                 new Student("Lex Wale", 12)
            };
     
            classroom[0] = new Class(studentList1, "Sunrise", 20);
            classroom[1] = new Class(studentList2, "Thunder", 10);
            classroom[2] = new Class(studentList3, "Lightning", 5);
     
            Scanner input = new Scanner(System.in);
            boolean functionSelection = true;
     
           // Student[] SortList = studentList1;
     
            while (functionSelection) {
     
                System.out.println("=== SCHOOL SYSTEM === \n");
                System.out.println("[1] Add New Class");
                System.out.println("[2] Exit");
     
                System.out.print("\nChoose a function: ");
     
                //Check if it is other then number
                if (!input.hasNextInt()) {
                    System.out.println(" \nChoose the existing number");
                    input.nextLine();
                    continue;
                }
     
                int choice = input.nextInt();
     
                String newClassName, searchClass, newName, addName;
                switch (choice) {
                    case 1:
     
                        //Show School List
                        /*List all class, student, grade and the total grade
                        in ascending*/
     
                        System.out.println("=== SCHOOL LIST ===");
     
                       Student[] SortList = studentList1;
     
                       //Descending Order Arrays
     
                        Comparator<Student> descendingOrder;
                        descendingOrder = Collections.reverseOrder
                        (new sortGrade());
     
                        Arrays.sort(SortList, descendingOrder);
                        System.out.println(Arrays.toString(SortList) + "\n");
                    case 2: 
                        System.exit(0);
                        break;
                    default:
                        System.out.println(" \nChoose the existing number");
                }
            }
        }
    }



    CURRENT OUTPUT

    === SCHOOL SYSTEM ===

    [1] Add New Class
    [2] Exit

    Choose a function: 1
    === SCHOOL LIST ===
    [
    Student Name: Tong Kim
    Grade: 0,
    Student Name: Ali Pole
    Grade: 0,
    Student Name: James Shawn
    Grade: 0]

    BUILD SUCCESSFUL (total time: 11 seconds)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help With My Sorting and Arrays

    Ok, that looks better.
    Starting with the first line in the desired out:
    === SCHOOL LISTS ===
    I do not see anything in the code that prints that line. Where should that line and the following lines be printed?

    Then looking at the current output I see a line that is NOT in the desired output:
    === SCHOOL SYSTEM ===
    Do you get any warnings or errors when you compile the program? I get a warning that MUST be fixed for the program to work properly. Copy and post here any compiler messages you need help with.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help With My Sorting and Arrays

    The code are in the functionSelection while loop

    while (functionSelection) {
     
                System.out.println("=== SCHOOL SYSTEM === \n");
                System.out.println("[1] Add New Class");
                System.out.println("[2] Exit");
     
                System.out.print("\nChoose a function: ");
     
                //Check if it is other then number
                if (!input.hasNextInt()) {
                    System.out.println(" \nChoose the existing number");
                    input.nextLine();
                    continue;
                }
     
                int choice = input.nextInt();
     
                String newClassName, searchClass, newName, addName;
                switch (choice) {
                    case 1:
     
                        //Show School List
                        /*List all class, student, grade and the total grade
                        in ascending*/
     
                        System.out.println("=== SCHOOL LIST ===");
     
                       Student[] SortList = studentList1;
     
                       //Descending Order Arrays
     
                        Comparator<Student> descendingOrder;
                        descendingOrder = Collections.reverseOrder
                        (new sortGrade());
     
                        Arrays.sort(SortList, descendingOrder);
                        System.out.println(Arrays.toString(SortList) + "\n");
                    case 2: 
                        System.exit(0);
                        break;
                    default:
                        System.out.println(" \nChoose the existing number");
                }
            }

    There no errors it show but that not the output i want because i can't show all the data in the list of the arrays

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help With My Sorting and Arrays

    There no errors
    No, I am talking about a warning from the compiler. You need to be sure that all warnings are turned on to be displayed when the code is compiled so that the warning messages are displayed. This warning is important for the code to work properly. The error causes the program to end too soon.


    I suggest that you work on one section at a time. Make the needed changes to print these lines:
    === SCHOOL LISTS ===

    1. Class Thunder with Total grade: 146
    2. Class Lightning with Total grade: 103.9
    3. Class Sunrise with Total grade: 115.5
    When those lines are printed, then move to the next set of lines.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help With My Sorting and Arrays

    I don't really know how to code that part

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help With My Sorting and Arrays

    Where is the data that is to be printed? What class has it? What array are those classes in?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help With My Sorting and Arrays

    the data for the team name are in the TEAM CLASS and the output should be in the MAIN DRIVE

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help With My Sorting and Arrays

    Ok can you add some print statements that print those values out like shown in the desired output?
    1. Class Thunder with Total grade: 146
    The parts in dark red should come from the class. The other Strings are just text to be printed between the data from the class.

    Where is the TEAM CLASS? I can not see it in the posted code? You should be sure to copy the correct names from the code when posting here so that it can be found in the code to know what you are talking about. Don't make up names. Use the names in the code.

    Also what is MAIN DRIVE? I can not see anything like that in the code.


    How much of the posted code did you write? How much was given to you?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help With My Sorting and Arrays

    oops sorry from CLASS CLASS and MAIN DRIVER. What is post code?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help With My Sorting and Arrays

    Spelling and case is important for communication so that there is no confusion.
    The class's name is Class, not CLASS. The main method is named main not MAIN.
    What is post code?
    posted code refers to the code that you have pasted into your inputs to this forum's thread.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help With My Sorting and Arrays

    I don't get it but i try to put it CLASS but I don't know how to call it because it keeps on giving me errors

    Class

    I add this in the class
       public String displayTeamWin() {
          return "\nClass: " + className+"with"+ calculateTotalGrade();
        }

    MainDriver

    I call it like this ( error underline on thedisplayTeamWin())
          System.out.println(classroom.displayTeamWin());

    Is this the right way to do it?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help With My Sorting and Arrays

    it keeps on giving me errors
    Please copy the full text and paste it here.
    System.out.println(classroom.displayTeamWin());
    Is the variable: classroom declared to hold a reference to the class with the displayTeamWin method?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Sorting BigInteger Arrays
    By WD3k in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 23rd, 2014, 11:21 PM
  2. Sorting parallel arrays
    By dynasty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 27th, 2013, 07:23 AM
  3. Arrays sorting
    By LeeDD in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 4th, 2013, 03:17 PM
  4. Need helpt with sorting arrays?
    By xion0374 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2012, 06:00 PM
  5. Sorting Arrays
    By Bryan29 in forum Collections and Generics
    Replies: 5
    Last Post: November 28th, 2011, 07:21 AM

Tags for this Thread