array

advertisement
King Saud University
College of Computer & Information Science
CSC 111 All sections (boys & girls)
Lab_30-4
Passing parameters
Methods in depth
Constructors
Setters
Exercice 1:
Write a Java class named Rectangle to represent a rectangle, as shown in the UML class
diagram. The class contains:




Two private attributes named width (double) and height (double) that specify
the width and height of the rectangle.
A default constructor that reads the width and height of the rectangle from the user.
A method named getArea() that returns the area of the rectangle.
A method named getPerimeter() that returns the perimeter of the rectangle.
Write another Java class called TestRectangle with a main() method that will do the
following using the Rectangle class:


Create a number of rectangle objects, and for each object the program should do the
following
1. Input the data of each rectangle from the user.
2. Calculate and display the area and the perimeter of each rectangle.
The program should terminate when the user answers (‘N’) to the question (“Do you
want to create another rectangle (Y/N)?”
Rectangle
- width: double
- height: double
+ Rectangle()
+ getArea(): double
+ getPerimeter(): double
Exercice 2:
Write a Java class named Account to represent a bank account, as shown in the UML
class diagram. The class contains:



A private attribute named id (int)
A private attribute named balance (double)
A private attribute named annualInterestRate (double) that stores the annual
interest rate percentage.






A method named setId() to set the id attribute.
A method named setBalance() to set the balance field.
A method named setAnnualIntersetRate() to set the annualInterestRate field.
A method named getBalance() that returns the current balance.
A method named getMonthlyInterestRate() that returns the monthly interest rate.
A method named withdraw() that withdraws a specified amount (sent as a parameter)
from the account.
A method named deposit() that deposits a specified amount (sent as a parameter) to
the account.
A method named addAnnualInterest() that calculates and adds the annual interest
amount to the current balance.


Write another Java class called TestAccount with a main() method that will do the
following using the Account class:





Create an account object and ask the user to enter the information of the account.
Display the monthly interest rate of this account.
Withdraw S.R. 1000 from the account, if current balance permits, and display the new
balance. Otherwise, display the message “Not enough balance in account”
Deposit S.R. 5000 to the current account and display the new balance.
Add the annual interest amount to the current balance and display the new balance.
Account
-id : int
-balance: double
-annualInterestRate: double
+setId(int): void
+setBalance(double): void
+setAnnualIntersetRate(double): void
+getBalance(): double
+getMonthlyInterestRate(): double
+withdraw(double):void
+deposit(double):void
+addAnnualInterest():void
Exercice 3:
Write a java class named Triangle, as shown in the UML class diagram. The class
contains the following:
 3 private attributes named side1, side2 and side3, of type (int)
 A constructor that sets the 3 sides of the triangle to given values
 A method named isValid(), which returns true if the sum of any two sides is greater
than the third side.
 A method named area() which returns the area of the triangle, using the following
formula:
𝑠
= (𝑠𝑖𝑑𝑒1 + 𝑠𝑖𝑑𝑒2 + 𝑠𝑖𝑑𝑒3)/2
area= √(𝑠 ( 𝑠 − 𝑠𝑖𝑑𝑒1) (𝑠 − 𝑠𝑖𝑑𝑒2) (𝑠 − 𝑠𝑖𝑑𝑒3)
Hint: use the method Math.sqrt()
Write another class named TestTriangle with a main() method that will create a Triangle
object, initialize the data of the triangle to values entered by the user, and computes the
area if the input is valid. Otherwise, it displays that the input is not valid.
Triangle
-side1: int
-side2: int
-side3: int
+Triangle(int, int, int)
+isValid(): Boolean
+area(): double
Download