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

Lab4 Objects

The document describes two exercises involving object-oriented programming with classes. Exercise 1 involves a Rectangle class with attributes of height and width, and methods to set/get values and calculate area. Exercise 1b asks the reader to write a program that creates Rectangle objects, sets their values, and prints their details and identifies the larger by area. Exercise 2 involves an Account class with a balance attribute and methods to deposit, withdraw, and get the balance. The reader is asked to write a program that creates Account objects, deposits/withdraws amounts between them, and displays the new balances.

Uploaded by

anis_harris
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views6 pages

Lab4 Objects

The document describes two exercises involving object-oriented programming with classes. Exercise 1 involves a Rectangle class with attributes of height and width, and methods to set/get values and calculate area. Exercise 1b asks the reader to write a program that creates Rectangle objects, sets their values, and prints their details and identifies the larger by area. Exercise 2 involves an Account class with a balance attribute and methods to deposit, withdraw, and get the balance. The reader is asked to write a program that creates Account objects, deposits/withdraws amounts between them, and displays the new balances.

Uploaded by

anis_harris
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Objects (Tutorial)

Exercise 1a - Rectangle
Let’s say you need to write a program that enables you to calculate
the area of a rectangle, and there is already a Rectangle class
defined for you. The attribute and behaviour of objects of this
class is described in the following table.
Rectangle Description
- height : int •Height of rectangle
- width : int •Width of rectangle
+ Rectangle(h : int, w: int) •Create rectangle with given values of height
and width

+ getArea() : int Calculates and returns area of rectangle

You only need to write a client program that will interact with objects
of this class.
How do you write your program?
Exercise 1b - Rectangle
 Given the complete class interface of Rectangle is as follows:

Rectangle Description
- height : int •Height of rectangle
- width : int •Width of rectangle
+Rectangle() Create a Rectangle with height& height = 0

+ Rectangle(h : int, w: int) Create rectangle with given values of height and
width
+ setHeight(h : int) : void Set the height to the given value

+ setWidth(w : int) : void Set the width to the given value

+ getHeight() : int Returns height of rectangle

+ getWidth() : int Return width of rectangle

+ getArea() : int Calculates and returns area of rectangle


Exercise 1b - Rectangle
Write another client program that does the following:
 Creates a rectangle called rect1 with its height = 20, and width
= 10
 Creates another rectangle called rect2 with no initial value of
height and width
 Read 2 integers h and w from users, and assign these values
to height and width respectively
 Print the details of both rectangles ( height, width, and area)
 Identify which rectangle is bigger (the first rectangle or the
second one) according to the area
* Copy Rectangle.class, compile and run your program.
Exercise 2 - Account
 Given the class interface:
Account Description
- balance : double Amount of balance in an
account
+ Account() Create Account with initial
balance 0
+ Account( initBalance : double) Create account with initial
balance = initBalance
+ credit(amount : double) : void Adds an amount to the
balance
+ debit(amount : double) : void Debit an amount from the
balance
+ getbalance() : double Returns current balance
Exercise 2 - Account
Using the description of class Account, write a client
program in Java that does the following:

1. Creates two accounts acc1 and acc2 with initial balance


100.00 and 20.00 respectively,
2. Displays the balance of both accounts,
3. Receives an amount from the user and debit this amount
from acc1 (use class Scanner for the input)
4. Credits the same amount into acc2
5. Displays the new balance of both account.
* Copy Account.class, compile and run your program.

You might also like