Assignment 1
Assignment 1
Exercises (Chapter 4)
1. Write an object oriented program to find area of circle using single as well as multiple
classes.
2. Create a class called Distance that includes two private data members feet (type int)
and inches (type float). Use a method setData to set values to instance variables.
Provide a method display that displays the feet and inches, a member function
addDistance for adding two distances, and a member function compareDistance for
comparing two distances. Implement your program in Java.
3. Create a class called Box with fields width, height and depth and methods to find
volume and surface area. Use suitable constructors. Implement the class to find volume
and surface area of two boxes.
4. Write a program that calculates percentage and GPA of 5 students. Each of the students
has following attributes: name, class, rollnum, and marks obtained in 5 subjects.
5. Create a class called Time with three attributes hours, minutes, and seconds. Use
appropriate constructor (s) to initialize instance variables. Also, use a display method
to display the time in hh:mm:ss format. Modify the class to add two time objects that
correctly results in addition of time.
6. Create a class called Invoice that a hardware store might use to represent an invoice for
an item sold at the store. 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 purchased (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), and then returns the amount as a double value. Write a test
application named InvoiceTest that demonstrates class Invoice's capabilities.
7. Create a class called Employee that includes three pieces of information as instance
variables a first name (type String), a last name (type String) and a monthly salary
(double). Your class should have a constructor that initializes the three instance
variables. Provide a set and a get method for each instance variable. Write a test
application named EmployeeTest that demonstrates class Employee's capabilities.
Create two Employee objects and display each object's yearly salary. Then give each
Employee a 10% raise and display each Employee's yearly salary again.
8. Create a class called Date that includes three pieces of information as instance variables
a month (type int), a day (type int) and a year (type int). Your class should have 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 (/). Write a test application named DateTest that demonstrates class Date's
capabilities.
Page 2