Lab 4
Lab 4
Aim: In this lab, you will delve into the foundational aspects of Object-Oriented Programming.
Understanding these fundamental concepts is crucial as they form the building blocks for more
complex programming constructs in Java.
Objective: The primary objective of this lab is to provide you with a solid understanding of the
following key concepts:
Task 1:
What is wrong in the following code? Please rectify each and provide a separate source file.
Task 2:
Can you invoke an instance method or reference an instance variable from a static method? Can
you invoke a static method or reference a static variable from an instance method? What is
Task 3:
While exercising, you can use a heart-rate monitor to see that your heart rate stays within a safe
range suggested by your trainers and doctors. According to the American Heart Association
(AHA) (www.americanheart.org/presenter.jhtml?identifier=4736), the formula for calculating
your maximum heart rate in beats per minute is 220 minus your age in years.Your target heart
rate is a range that’s 50–85% of your maximum heart rate. [Note: These formulas are estimates
provided by the AHA. Maximum and target heart rates may vary based on the health, fitness and
gender of the individual. Always consult a physician or qualified health-care professional before
beginning or modifying an exercise program.] Create a class called HeartRates. The class
attributes should include the person’s first name, last name and date of birth (consisting of
separate attributes for the month, day and year of birth). The class also should include a method
that calculates and returns the person’s age (in years), a method that calculates and returns the
person’s maximum heart rate and a method that calculates and returns the person’s target heart
rate. Write a Java app that prompts for the person’s information, instantiates an object of class
HeartRates and prints the information from that object—including the person’s first name, last
name and date of birth—then calculates and prints the person’s age in (years), maximum heart
rate and target-heart-rate range.
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 attributes as instance variables— (think of a
hardware objects and define attributes accordingly). 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. Write a test app named InvoiceTest
that demonstrates class Invoice’s capabilities for two different hardware items.
Task 5:
Create a class called Employee that includes three instance variables—a first name (type String),
a last name (type String), monthly salary (double) and experience in years (int). Provide methods
to 1) display the details of employee 2) calculate the annual salary 3) display new monthly salary
after appraisal (appraisal is calculated by multiplying experience years with 10% of current
salary drawn) . Write a test app 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 using the appraisal method and display each Employee’s yearly
salary again.