英文:
Need help to show all data of the arrays using Array Object (Can't use Array List)
问题
import java.util.Comparator;
import java.util.Collections;
import java.util.Arrays;
import java.util.Scanner;
public class MainDriver {
void printSort(Student[] sortList) {
for (Object sort : sortList) {
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.4),
new Student("Ho Kim", 18.0),
new Student("Mark Lee", 65.5)};
Student[] studentList3 = {
new Student("Luke Kim", 90),
new Student("Noth Shawn", 44),
new Student("Lex Wale", 12)};
classroom[0] = new Class(studentList1, "Sunrise", 5);
classroom[1] = new Class(studentList2, "Thunder", 5);
classroom[2] = new Class(studentList3, "Lightning", 5);
Scanner input = new Scanner(System.in);
boolean functionSelection = true;
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: ");
if (!input.hasNextInt()) {
System.out.println("\nChoose the existing number");
input.nextLine();
continue;
}
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("=== SCHOOL LIST ===");
Student[] sortList = studentList1;
Comparator<Student> descendingOrder;
descendingOrder = Collections.reverseOrder(new sortGrade());
Arrays.sort(sortList, descendingOrder);
for (int i = 0; i < classroom.length; i++) {
System.out.println((i + 1) + ". " + classroom[i].getClassName() + " with Total grade: " + classroom[i].calculateTotalGrade());
}
System.out.println("--------------------");
for (int i = 0; i < classroom.length; i++) {
System.out.println(classroom[i]);
System.out.println("------------------------");
}
System.out.print("Do you want to go back to the selection function?\nPress Y or N: ");
String goBackChoice = input.next();
if (goBackChoice.equalsIgnoreCase("Y")) {
continue;
} else {
System.out.print("Do you want to exit (Y or N): ");
String exitChoice = input.next();
if (exitChoice.equalsIgnoreCase("Y")) {
functionSelection = false;
}
}
break;
case 2:
System.exit(0);
break;
default:
System.out.println("\nChoose the existing number");
}
}
}
}
(Note: I've made some modifications to your code to achieve the desired output. The changes include correcting the Student
constructor to use the correct parameter name for the grade, fixing the class quantity in the Class
constructor, and adding missing getters in the Class
class.)
英文:
I am creating a school system where it shows all the list of class and students with their grades and the total grade**.
but right now I'm really stuck. I can show the list all of the data in class and I don't really know what to do now. Please help me with my code.
In this code, I use Array to store the data of the student, grade, class name, and class quantity.
> ARRAYS FOR CLASS AND STUDENT:-
>
> The class can store up to [10 class] but initial it with min 3 class
> and not more than that max is 10 students can store up to [20 students]
> and not more than that the max is 20
NO ARRAYLIST ARE TO BE USE IN THIS CODE.
= OUTPUT THAT I WANT FOR SCHOOL LIST =
In this output, the name is arranged by the grade to ascending order and on top, it lists by number where it shows what class has the highest total grade. and the user asks whether to go back to the function selection
=== 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.
MY JAVA PROGRAM
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");
}
}
}
}
答案1
得分: 1
如果您绝对不能使用ArrayList,那么您唯一的选择是重新初始化数组,然后将新数组分配给您的原始指针。您可以将此功能封装到一个方法中,如下所示:
String[] push(String[] array, String push) {
String[] longer = new String[array.length + 1];
for (int i = 0; i < array.length; i++)
longer[i] = array[i];
longer[array.length] = push;
return longer;
}
当您调用此方法时,您需要将其返回值赋值给原始数组值。但这可能会产生副作用。因此您应该谨慎使用它。对于较大的数组来说可能会很繁琐,因为每次添加内容时都必须进行迭代。
英文:
If you absolutely can't use an ArrayList then your only other option is to re-initialize the array then assign the new array to your original pointer. You could encapsulate this functionality into a method like so:
String[] push(String[] array, String push) {
String[] longer = new String[array.length + 1];
for (int i = 0; i < array.length; i++)
longer[i] = array[i];
longer[array.length] = push;
return longer;
}
When you call this method you would have to assign its return value to your original array value. This could have side effects though. So you should be careful with it. It can be cumbersome with a larger array since you will have to iterate it every time you add something.
答案2
得分: 0
Java数组不可调整大小。 无法添加或删除项目。 请改用ArrayList。
英文:
Java arrays aren't resizeable. Your can't add or delete items. Use ArrayList instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论