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

Java

The document defines Person, Student, and Teacher classes with attributes and methods to represent people, students, and teachers. It demonstrates creating objects of these classes and using their methods like adding courses, grades, averages, and removing courses.

Uploaded by

Ss715609654
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Java

The document defines Person, Student, and Teacher classes with attributes and methods to represent people, students, and teachers. It demonstrates creating objects of these classes and using their methods like adding courses, grades, averages, and removing courses.

Uploaded by

Ss715609654
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

// Base class

class Person {
private String name;
private String address;

public Person(String name, String address) {


this.name = name;
this.address = address;
}

public String getName() {


return name;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}

public String toString() {


return name + " (" + address + ")";
}
}

// Derived class Student


class Student extends Person {
private int numCourses = 0;
private String[] courses = new String[10]; // assuming max 10 courses
private int[] grades = new int[10]; // assuming max 10 grades

public Student(String name, String address) {


super(name, address);
}

public void addCourseGrade(String course, int grade) {


courses[numCourses] = course;
grades[numCourses] = grade;
numCourses++;
}

public void printGrades() {


for (int i = 0; i < numCourses; i++) {
System.out.println(courses[i] + ": " + grades[i]);
}
}
public double getAverageGrade() {
double sum = 0;
if (numCourses == 0)
return sum;
for (int i = 0; i < numCourses; i++) {
sum += grades[i];
}
return sum / numCourses;
}

public String toString() {


return "Student: " + super.toString();
}
}

// Derived class Teacher


class Teacher extends Person {
private int numCourses = 0;
private String[] courses = new String[10]; // assuming max 10 courses

public Teacher(String name, String address) {


super(name, address);
}

public boolean addCourse(String course) {


for (int i = 0; i < numCourses; i++) {
if (courses[i].equals(course))
return false; // Course already exists
}
courses[numCourses] = course;
numCourses++;
return true;
}

public boolean removeCourse(String course) {


for (int i = 0; i < numCourses; i++) {
if (courses[i].equals(course)) {
// Remove the course by shifting courses
for (int j = i; j < numCourses - 1; j++) {
courses[j] = courses[j + 1];
}
numCourses--;
return true; // Course removed
}
}
return false; // Course does not exist
}
public String toString() {
return "Teacher: " + super.toString();
}
}

public class MyNewMain {


public static void main(String[] args) {

Student student1 = new Student("Mohammed", "123 Elm Street");


student1.addCourseGrade("Mathematics", 88);
student1.addCourseGrade("Science", 92);

System.out.println(student1);
student1.printGrades();
System.out.println("Average grade: " + student1.getAverageGrade());

Teacher teacher1 = new Teacher("Ahmed", "456 Maple Avenue");


boolean added = teacher1.addCourse("Mathematics");
if (added) {
System.out.println("Course added successfully.");
} else {
System.out.println("Course already exists.");
}

added = teacher1.addCourse("English");
if (added) {
System.out.println("Course added successfully.");
} else {
System.out.println("Course already exists.");
}

System.out.println(teacher1);

// Remove a course
boolean removed = teacher1.removeCourse("Mathematics");
if (removed) {
System.out.println("Course removed successfully.");
} else {
System.out.println("Course does not exist.");
}

removed = teacher1.removeCourse("Biology");
if (removed) {
System.out.println("Course removed successfully.");
} else {
System.out.println("Course does not exist.");
}
}
}

You might also like