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

Programing 2 - Lab 03

sheet for programming 2 with java

Uploaded by

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

Programing 2 - Lab 03

sheet for programming 2 with java

Uploaded by

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

Alexandria National University

Faculty of Computer and Information


Lecturer: Dr. Amr AboHani, Dr. Ahmed Saleh, Dr. Ahmed Moustafa
Course: Programming 2
Lab #: 03
-------------------------------------------------------------------------------------------------------------------------------
Lab Objectives
1- Array of Objects
2- Class Diagram relationships
Part 1: In Lab (Discuss with students)
Question 1. Design a UML for class named BankAccount, Create a class
BankAccount with

a. Attributes like account number, balance, and owner name.


b. A no-arg constructor that creates a default BankAccount.
c. A constructor that creates a BankAccount with the specified account
number, balance, and owner name.
d. The accessor and mutator methods for the BankAccount fields.
e. A method named toString(), that returns String contains all BankAccount
information
f. Implement methods for deposit, withdrawal (with overdraft protection).
Then write a Java code for the described class. Finally, write a test program that
contains the main method and extra methods to perform the following operations:
a. A method to read array of objects from Bank accounts.
b. A method to get the sum of balanced in the array of Bank accounts.
c. A method to display all the bank accounts data.
Then use the previous methods in main to read 5 bank accounts from user.
Question 2. Design two classes named Student and Course. [Draw the class
Diagram]
The class Student contains:
• Private data fields named name, id, and a collection of Course-objects.
• The constructor to initialize the data fields.
• The setter and getter methods.
• A method named numOfCourses to determine the number of course a
student will register.

Page 1 of 4
Alexandria National University
Faculty of Computer and Information
Lecturer: Dr. Amr AboHani, Dr. Ahmed Saleh, Dr. Ahmed Moustafa
Course: Programming 2
Lab #: 03
-------------------------------------------------------------------------------------------------------------------------------
• A method named addCourse() to add courses registered by this student.
• A method named showData() to display the student information.
The class Course contains:
• Private data fields named crsName, code, and a collection of Student-
objects.
• The constructor to initialize the data fields.
• The setter and getter methods.
• A method named numOfStudents to determine the number of students that
will register this course.
• A method named addStudent to add student registered this course.
• A method named showData to display the course information.

Write a main program to test your classes.

Part 2: Students IN LAB.


Question 1. Consider a library management system.
We have two classes:
a- Book: This class represents a book with attributes like title, author, ISBN,
etc.
b- Library: This class represents a library with attributes like name, location,
and a collection of books (represented as an array).
The Library class composes the Book class. This means:
The Library class has an instance variable (member variable) that holds a collection
of Book objects.
The Book object exists independently but becomes part of the library object.
The lifecycle of the Book object is tied to the library object. If the Library object is
destroyed, the associated Book objects are also destroyed.

Question 2. Design two classes named Author and Book. [Draw the class
Diagram]
The class Book contains:
• The data fields name and price.
• The constructor to initialize the data fields.
• The setter and getter methods.

Page 2 of 4
Alexandria National University
Faculty of Computer and Information
Lecturer: Dr. Amr AboHani, Dr. Ahmed Saleh, Dr. Ahmed Moustafa
Course: Programming 2
Lab #: 03
-------------------------------------------------------------------------------------------------------------------------------
• A method named show that displays the book’s information.
The class Author contains:
• The data fields name, email, gender, and collection of Book-objects.
• The constructor to initialize the data fields.
• The setter and getter methods.
• A method named showdata that displays the author’s information and his/her
book information.
• A method named books_number that returns the number of books this author
has.
Write a main program to test your classes.

Part 3: HOMEWORK.
Question 1. Design a class named Circle to represent a Circle.
The class contains:
a. The data fields radius.
b. A no-arg constructor that circle with radius=1.
c. A constructor that constructs a circle with specified radius.
d. A method names getArea(), which return the area of the circle.
e. A method named getCir() that returns the circumference of the circle.
f. A method named compare(Circle c) that returns 0 if the class circle and circle
c has the same radius, return 1 if class radius is greater than circle c, and
return -1 if the class radius less than circle c.
g. A method named toString() that returns String contains all Circle information.
Then write a Java code for the described class. Finally, write a test program that
contains the main method and extra methods to perform the following operations:
A method to read array of objects from Circle.
A method to get the sum of areas of all circles in the array.
A method to get the sum of circumferences of all circles in the array.
A method to display all the Circles information.
A method to Compare all radius for all array objects and return a Boolean matrix
for the comparison, then display this matrix in main.

Page 3 of 4
Alexandria National University
Faculty of Computer and Information
Lecturer: Dr. Amr AboHani, Dr. Ahmed Saleh, Dr. Ahmed Moustafa
Course: Programming 2
Lab #: 03
-------------------------------------------------------------------------------------------------------------------------------

Question 2. Design two classes named Person and Car. [Draw the class Diagram]
The class Car contains:
• The data fields model and licenseNum.
• The constructor to initialize the data fields.
• The setter and getter methods.
• A method named show that displays the car’s information.
The class Person contains:
• The data fields name, idNum, and object of type Car.
• The constructor to initialize the data fields.
• The setter and getter methods.
• An Own method to own a car.
• A Disown method to disown his/her car.
• A method named showdata that displays the person’s information and his/her
car information.
Write a main program to test your classes.

Page 4 of 4

You might also like