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

Lap Solution

Uploaded by

Hossam Bakr
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)
9 views

Lap Solution

Uploaded by

Hossam Bakr
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/ 10

Lap 5 oop solution

OCTOBER 17, 2024


Experiment 1: Student Class UML Diagram

Challenge : class diagram for the Book class

Experiment 2 : java code for advisor class


package studentinformation;

public class StudentInformation {

public static class Student {


// Private attributes
private int studentID;
private String studentName;
private float studentGPA;

// Constructor
public Student(int studentID, String studentName, float studentGPA) {
this.studentID = studentID;
this.studentName = studentName;
this.studentGPA = studentGPA;
}

// Overriding the toString() method to display student details


@Override
public String toString() {
return "ID: " + studentID + "\n"
+ "Name: " + studentName + "\n"
+ "GPA: " + studentGPA + "\n";
}
}

public static class Advisor {


// Private attributes
private int advisorID;
private String advisorName;
private String advisorDept;

// Constructor
public Advisor(int advisorID, String advisorName, String advisorDept) {
this.advisorID = advisorID;
this.advisorName = advisorName;
this.advisorDept = advisorDept;
}

// Overriding the toString() method to display advisor details


@Override
public String toString() {
return "ID: " + advisorID + "\n"
+ "Name: " + advisorName + "\n"
+ "Department: " + advisorDept + "\n";
}
}

public static void main(String[] args) {


// Creating Student objects
Student student1 = new Student(101, "Ahmed", 4.5f);
Student student2 = new Student(102, "Fatima", 4.7f);

// Creating Advisor objects


Advisor advisor1 = new Advisor(11, "Maytham", "Computer Science");
Advisor advisor2 = new Advisor(21, "Sara", "Information Systems");

// Printing student information


System.out.println("Students Information:");
System.out.println("----------------------");
System.out.println(student1.toString());
System.out.println("----------------------");
System.out.println(student2.toString());
System.out.println("----------------------");

// Printing advisor information


System.out.println("Advisors Information:");
System.out.println("----------------------");
System.out.println(advisor1.toString());
System.out.println("----------------------");
System.out.println(advisor2.toString());
}
}

Screenshot for the output :


Challenge 2 : Code for StudentInformation class

package studentinformation;

public class StudentInformation {

public static class Student {


private int studentID;
private String studentName;
private float studentGPA;

// Constructor
public Student(int studentID, String studentName, float studentGPA) {
this.studentID = studentID;
this.studentName = studentName;
this.studentGPA = studentGPA;
}

// Using String.format() in toString method


@Override
public String toString() {
return String.format("ID: %d\nName: %s\nGPA: %.1f\n", studentID,
studentName, studentGPA);
}
}

public static class Advisor {


private int advisorID;
private String advisorName;
private String advisorDept;

// Constructor
public Advisor(int advisorID, String advisorName, String advisorDept) {
this.advisorID = advisorID;
this.advisorName = advisorName;
this.advisorDept = advisorDept;
}

// Using String.format() in toString method


@Override
public String toString() {
return String.format("ID: %d\nName: %s\nDepartment: %s\n", advisorID,
advisorName, advisorDept);
}
}
public static void main(String[] args) {
Student student1 = new Student(101, "Ahmed", 4.5f);
Student student2 = new Student(102, "Fatima", 4.7f);

Advisor advisor1 = new Advisor(11, "Maytham", "Computer Science");


Advisor advisor2 = new Advisor(21, "Sara", "Information Systems");

// Printing student information


System.out.println("Students Information:");
System.out.println("----------------------");
System.out.println(student1);
System.out.println("----------------------");
System.out.println(student2);
System.out.println("----------------------");

// Printing advisor information


System.out.println("Advisors Information:");
System.out.println("----------------------");
System.out.println(advisor1);
System.out.println("----------------------");
System.out.println(advisor2);
}
}
Screenshot for the output :

Experiment 3 : drawing the association between classes


Challenge 3 : UML Diagram for Donor class

public class Donor {


// Private attributes
private int donorID;
private String donorName;
private int birthYear;
private String bloodGroup;
// Constructor
public Donor(int donorID, String donorName, int birthYear, String bloodGroup) {
this.donorID = donorID;
this.donorName = donorName;
this.birthYear = birthYear;
this.bloodGroup = bloodGroup;
}

// Setters
public void setDonorID(int donorID) {
this.donorID = donorID;
}

public void setDonorName(String donorName) {


this.donorName = donorName;
}

public void setBirthYear(int birthYear) {


this.birthYear = birthYear;
}

public void setBloodGroup(String bloodGroup) {


this.bloodGroup = bloodGroup;
}

// Getters
public int getDonorID() {
return donorID;
}

public String getDonorName() {


return donorName;
}

public int getBirthYear() {


return birthYear;
}

public String getBloodGroup() {


return bloodGroup;
}

// Overriding the toString() method to display donor details


@Override
public String toString() {
return String.format("Donor ID: %d\nName: %s\nBirth Year: %d\nBlood Group:
%s",
donorID, donorName, birthYear, bloodGroup);
}
public static void main(String[] args) {
// Create a Donor object
Donor donor1 = new Donor(1001, "John Doe", 1985, "O+");

// Print the donor information


System.out.println(donor1);
}
}

You might also like