Task 2 : Classes, Instance Attributes, Methods, and Data Hiding

Task Objectives
  1. Defining classes with instance attributes and methods
  2. Accessing instance attributes from methods
  3. How hide data from direct access
  4. Defining constructors and using them to initialize instance attributes
1. Make a Rectangle class with width and length attributes, both of type double. Define a method double calculateArea(), it should return area of the Rectangle. Define another method in Rectangle class: double calculatePerimeter(), that should return perimeter of the Rectangle. Create RectangleTest class, in its main method, create an instance of Rectangle class and initialize its attributes from user input. Then print the area and perimeter of the rectangle using methods defined in class.

2. Make BankAccount class with balance and name attributes of type double and String. Define public void deposit(double amount) and public void withdraw(double amount) methods. Deposit should increase the balance by passed value and withdraw should decrease the balance with passed amount.

Make BankAccountTest class. In main method create a new object of BankAccount class. Get balance and name of account holder from user input and initilize both object attributes. Then show this menu:

Press 1: To Deposit an amount
Press 2: To Withdraw an amount
Press 3: To View the current balance
Press 4: To Close this program

If user press 1: show following:
Enter the amount you want to desposit in your account >

For example. if user enters 500, call the desposit method of BankAccount object and pass 500 to it. Do same for option 2 but call the withdraw method. If user choose 3 from menu, print the current balance on CLI. If user press 4, close the program.

Below task are taken from Book exercise. Its 3.12 - 3.14.

3. (Invoice Class) Create a class called Invoice that a shop shall use to represent an invoice of a sold item. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being sold (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Call setter methods from the constructor to initialize instance attributes. Write a test app named InvoiceTest that demonstrates Invoice class capabilities.

4. (Employee Class) Create a class Employee that includes three instance variables -a first name (type String), a last name (type String) and a monthly salary (of type double). Provide two constructors that allows to create the object: first shall receive and initialize three instance variables and second shall receive and initialize first and last name only. Provide set and get methods for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Define a method raiseSalary(float percentage) in Employee class that shall raise the employee salary by given percentage. Give salary raise of 10% to both employees and print each employee yearly salary again.

5. (Date Class) Create a class called Date that includes three instance variables—a month (type int), a day (type int) and a year (type int). Provide a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set and a get method for each instance variable. Provide a method displayDate() that displays the month, day and year separated by forward slashes (/). Privide another method getMonthName(), that shall print the month name, if month instance attribute is not initialized, it shall print "Undefined". Write a test app named DateTest that demonstrates class Date’s capabilities.


  1. I myself is a great fan of java programming and just right now as I’ve gone through this post. It has really amazed me, I really like the concept of coding done in it.


Post a Comment