0% found this document useful (0 votes)
13 views

Java 3

The document describes a Java exercise that uses object-oriented programming to create a StudentList class for managing student data. The StudentList class uses an array to store Student objects, each containing a name and age. Methods are defined to add students to the list, display student information, and calculate the average age. The main method demonstrates creating a StudentList, adding students, displaying them, and finding the average age.

Uploaded by

Mrabet Mustapha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Java 3

The document describes a Java exercise that uses object-oriented programming to create a StudentList class for managing student data. The StudentList class uses an array to store Student objects, each containing a name and age. Methods are defined to add students to the list, display student information, and calculate the average age. The main method demonstrates creating a StudentList, adding students, displaying them, and finding the average age.

Uploaded by

Mrabet Mustapha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sure, here's a simple Java exercise that uses arrays and object-oriented

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;

public Student(String name, int age) {


this.name = name;
this.age = age;
}

public String getName() {


return name;
}

public int getAge() {


return age;
}
}

public class StudentList {


private Student[] students;
private int size;
private int capacity;

public StudentList(int capacity) {


this.capacity = capacity;
this.students = new Student[capacity];
this.size = 0;
}

public void addStudent(String name, int age) {


if (size < capacity) {
students[size] = new Student(name, age);
size++;
} else {
System.out.println("Student list is full. Cannot add more students.");
}
}

public void displayStudents() {


for (int i = 0; i < size; i++) {
System.out.println("Name: " + students[i].getName() + ", Age: " +
students[i].getAge());
}
}

public static void main(String[] args) {


StudentList studentList = new StudentList(5);

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;

public Student(String name, int age) {


this.name = name;
this.age = age;
}

public String getName() {


return name;
}

public int getAge() {


return age;
}
}

public class StudentList {


private Student[] students;
private int size;
private int capacity;

public StudentList(int capacity) {


this.capacity = capacity;
this.students = new Student[capacity];
this.size = 0;
}

public void addStudent(String name, int age) {


if (size < capacity) {
students[size] = new Student(name, age);
size++;
} else {
System.out.println("Student list is full. Cannot add more students.");
}
}

public void displayStudents() {


for (int i = 0; i < size; i++) {
System.out.println("Name: " + students[i].getName() + ", Age: " +
students[i].getAge());
}
}

public double calculateAverageAge() {


if (size == 0) {
return 0.0;
}

int sum = 0;
for (int i = 0; i < size; i++) {
sum += students[i].getAge();
}
return (double) sum / size;
}

public static void main(String[] args) {


StudentList studentList = new StudentList(5);

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();

double averageAge = studentList.calculateAverageAge();


System.out.println("Average Age: " + averageAge);
}
}
In this updated version, we've added the calculateAverageAge method to calculate
the average age of the students. The main method demonstrates how to use this
method along with the addStudent and displayStudents methods. This exercise still
maintains the use of arrays and OOP principles while adding more functions and
methods for different tasks.

You might also like