0% found this document useful (0 votes)
17 views2 pages

Assignment 1

The document contains a series of programming exercises focused on object-oriented programming in Java. It includes tasks such as creating classes for geometric shapes, handling distances, invoices, employees, dates, savings accounts, and bank accounts, each with specific attributes and methods. The exercises aim to reinforce concepts like encapsulation, constructors, and method implementation in Java.

Uploaded by

nelp.erlicense
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)
17 views2 pages

Assignment 1

The document contains a series of programming exercises focused on object-oriented programming in Java. It includes tasks such as creating classes for geometric shapes, handling distances, invoices, employees, dates, savings accounts, and bank accounts, each with specific attributes and methods. The exercises aim to reinforce concepts like encapsulation, constructors, and method implementation in Java.

Uploaded by

nelp.erlicense
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/ 2

Page 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

9. Create class SavingsAccount. Use a static variable annualInterestRate to store


the annual interest rate for all account holders. Each object of the class contains a
private instance variable savingsBalance indicating the amount the saver currently
has on deposit. Provide method calculateMonthlyInterest to calculate the monthly
interest by multiplying the savingsBalance by annualInterestRate divided by 12.
This interest should be added to savingsBalance. Provide a static method
modifyInterestRate that sets the annualInterestRate to a new value. Write a
program to test class SavingsAccount. Instantiate two savingsAccount objects,
saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Set
annualInterestRate to 4%, then calculate the monthly interest and print the new
balances for both savers. Then set the annualInterestRate to 5%, calculate the next
month's interest and print the new balances for both savers.
10. Design a class to represent a bank account. Include the following members:
Data members:
Name of the depositor
Account number
Type of account
Balance amount in the account
Methods:
To assign initial vales
To deposit an amount
To withdraw an amount after checking balance
To display the name and balance
11. Modify the program of question 10 to incorporate a constructor to provide initial
values.

You might also like