0% found this document useful (0 votes)
15 views6 pages

Lab Task 010

The document contains two Java programs: one for a Zoo Management System and another for a School Management System. The Zoo Management System manages animals and enclosures, allowing for animal movement and information display, while the School Management System manages students and courses, enabling course enrollment and information display. Both systems utilize object-oriented programming principles with classes and methods to encapsulate functionality.

Uploaded by

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

Lab Task 010

The document contains two Java programs: one for a Zoo Management System and another for a School Management System. The Zoo Management System manages animals and enclosures, allowing for animal movement and information display, while the School Management System manages students and courses, enabling course enrollment and information display. Both systems utilize object-oriented programming principles with classes and methods to encapsulate functionality.

Uploaded by

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

LAB TASK 01

Question 01:
import java.util.ArrayList;
class Animal {
String name, species;
int age;
Enclosure currentEnclosure;

Animal(String name, String species, int age) {


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

void displayInfo() {
System.out.println("Animal: " + name + ", Species: " + species + ", Age: " + age + ", Enclosure: " + (currentEnclosure !=
null ? currentEnclosure.enclosureNumber : "None"));
}
}

class Enclosure {
int enclosureNumber;
String size, type;
ArrayList<Animal> animals = new ArrayList<>();

Enclosure(int enclosureNumber, String size, String type) {


this.enclosureNumber = enclosureNumber;
this.size = size;
this.type = type;
}

void addAnimal(Animal animal) {


animals.add(animal);
animal.currentEnclosure = this;
}

void removeAnimal(Animal animal) {


animals.remove(animal);
}

void displayEnclosureInfo() {
System.out.println("Enclosure " + enclosureNumber + " (" + type + ", " + size + ")");
System.out.println("Animals Inside:");
for (Animal a : animals) {
System.out.println(" - " + a.name + " (" + a.species + ")");
}
}
}

class Zoo {
ArrayList<Animal> animals = new ArrayList<>();
ArrayList<Enclosure> enclosures = new ArrayList<>();

void addAnimal(Animal animal) {


animals.add(animal);
}

void addEnclosure(Enclosure enclosure) {


enclosures.add(enclosure);
}

void moveAnimal(Animal animal, Enclosure newEnclosure) {


if (animal.currentEnclosure != null) {
animal.currentEnclosure.removeAnimal(animal);
}
newEnclosure.addAnimal(animal);
System.out.println(animal.name + " has been moved to Enclosure " + newEnclosure.enclosureNumber);
}

void displayAllAnimals() {
System.out.println("All Animals in the Zoo:");
for (Animal a : animals) {
a.displayInfo();
}
}

void displayAllEnclosures() {
System.out.println("All Enclosures:");
for (Enclosure e : enclosures) {
e.displayEnclosureInfo();
}
}
}

public class ZooManagementSystem {


public static void main(String[] args) {
Zoo zoo = new Zoo();
Enclosure enc1 = new Enclosure(1, "Large", "Open");
Enclosure enc2 = new Enclosure(2, "Medium", "Closed");
zoo.addEnclosure(enc1);
zoo.addEnclosure(enc2);
Animal lion = new Animal("Hong", "Lion", 5);
Animal tiger = new Animal("Kong", "Tiger", 4);
Animal elephant = new Animal("Dong", "Elephant", 8);
zoo.addAnimal(lion);
zoo.addAnimal(tiger);
zoo.addAnimal(elephant);
enc1.addAnimal(lion);
enc1.addAnimal(tiger);
enc2.addAnimal(elephant);
zoo.displayAllAnimals();
zoo.displayAllEnclosures();
zoo.moveAnimal(tiger, enc2);
zoo.displayAllAnimals();
zoo.displayAllEnclosures();
}
}
Question 02:
import java.util.ArrayList;
class Student {
String studentID;
String name;
int age;
ArrayList<String> enrolledCourses;

Student(String studentID, String name, int age) {


this.studentID = studentID;
this.name = name;
this.age = age;
this.enrolledCourses = new ArrayList<>();
}

public void enrollInCourse(String course) {


enrolledCourses.add(course);
System.out.println(name + " has enrolled in " + course);
}

public void dropCourse(String course) {


if (enrolledCourses.remove(course)) {
System.out.println(name + " has dropped " + course);
} else {
System.out.println(name + " is not enrolled in " + course);
}
}

public void displayStudentInfo() {


System.out.println("Student ID: " + studentID);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.print("Enrolled Courses: ");
if (enrolledCourses.isEmpty()) {
System.out.println("None");
} else {
for (String course : enrolledCourses) {
System.out.print(course + " ");
}
System.out.println();
}
System.out.println();
}
}

class Course {
String courseID;
String courseName;
int credits;

public Course(String courseID, String courseName, int credits) {


this.courseID = courseID;
this.courseName = courseName;
this.credits = credits;
}

public String getCourseName() {


return courseName;
}

public void displayCourseInfo() {


System.out.println("Course ID: " + courseID);
System.out.println("Course Name: " + courseName);
System.out.println("Credits: " + credits);
System.out.println();
}
}
class School {
ArrayList<Student> students;
ArrayList<Course> courses;

School() {
students = new ArrayList<>();
courses = new ArrayList<>();
}

public void addStudent(Student student) {


students.add(student);
}

public void addCourse(Course course) {


courses.add(course);
}

public void enrollStudentInCourse(Student student, Course course) {


if (courses.contains(course)) {
student.enrollInCourse(course.getCourseName());
} else {
System.out.println("Course " + course.getCourseName() + " does not exist.");
}
}
public void displayAllStudents() {
System.out.println("List of Students:");
for (Student student : students) {
student.displayStudentInfo();
}
}

public void displayAllCourses() {


System.out.println("List of Courses:");
for (Course course : courses) {
course.displayCourseInfo();
}
}
}

public class SchoolManagementSystem {

public static void main(String[] args) {


School school = new School();
Course math = new Course("MATH", "Mathematics", 3);
Course science = new Course("SCI", "Science", 4);
Course history = new Course("HIST", "History", 3);
school.addCourse(math);
school.addCourse(science);
school.addCourse(history);
Student student1 = new Student("S1", "Alliya", 20);
Student student2 = new Student("S2", "Ali", 22);
school.addStudent(student1);
school.addStudent(student2);
school.displayAllCourses();
school.enrollStudentInCourse(student1, math);
school.enrollStudentInCourse(student1, science);
school.enrollStudentInCourse(student2, history);
school.displayAllStudents();
student1.dropCourse(math.getCourseName());
student2.dropCourse(science.getCourseName());
school.displayAllStudents();
}

You might also like