Professional Documents
Culture Documents
Java 3
Java 3
programming (OOP). This exercise involves creating a basic class for managing a
list of students. Each student will have a name and an age.
java
Copy code
class Student {
private String name;
private int age;
studentList.addStudent("Alice", 20);
studentList.addStudent("Bob", 22);
studentList.addStudent("Charlie", 21);
studentList.addStudent("David", 19);
studentList.addStudent("Eve", 23);
studentList.displayStudents();
}
}
In this exercise, we have a Student class to represent individual students with
their name and age. The StudentList class manages an array of students, with a
specified capacity. You can add students to the list, and the displayStudents
method will print their names and ages. The main method demonstrates how to create
a StudentList object, add students, and display their information.
This exercise combines the use of arrays and basic OOP principles in Java. You can
extend and modify it as needed to practice more advanced concepts.
User
use multiple funtions and methodes
ChatGPT
Sure, here's a modified version of the previous Java exercise that uses multiple
functions and methods. In this version, we have separate functions to add students,
display students, and find the average age of the students:
java
Copy code
class Student {
private String name;
private int age;
int sum = 0;
for (int i = 0; i < size; i++) {
sum += students[i].getAge();
}
return (double) sum / size;
}
studentList.addStudent("Alice", 20);
studentList.addStudent("Bob", 22);
studentList.addStudent("Charlie", 21);
studentList.addStudent("David", 19);
studentList.addStudent("Eve", 23);
System.out.println("Student List:");
studentList.displayStudents();