0% found this document useful (0 votes)
26 views5 pages

Lab 4

This document outlines Lab 4 for the CS 212 Object Oriented Programming course, focusing on the introduction to classes and objects in Java. It includes tasks that require students to identify and correct errors in provided code, create classes for heart rate calculations, invoices, and employee management, and demonstrate their capabilities through test applications. The lab aims to solidify students' understanding of fundamental OOP concepts.

Uploaded by

sadax16031
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)
26 views5 pages

Lab 4

This document outlines Lab 4 for the CS 212 Object Oriented Programming course, focusing on the introduction to classes and objects in Java. It includes tasks that require students to identify and correct errors in provided code, create classes for heart rate calculations, invoices, and employee management, and demonstrate their capabilities through test applications. The lab aims to solidify students' understanding of fundamental OOP concepts.

Uploaded by

sadax16031
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/ 5

Department of Computing

CS 212: Object Oriented Programming


BESE15-AB
Lab 4: Introduction to Classes and Objects
Date: 24-02-2025
Instructor: Ms. Mehwish Kiran
Lab Engineer: Ms. Shakeela Bibi

CS 212: Object Oriented Programming Page 1


Lab 04: Introduction to Classes and Objects

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:

• Concept of Objects and Classes


Tools/ Software: VS Code, NetBeans, Eclipse etc.

Task 1:

What is wrong in the following code? Please rectify each and provide a separate source file.

public class Test1


{
public static void main(String[] args
{
Test1 t1 = new Test1(5);
}
}
public class Test2
{
public static void main(String[] args)
{
Test2 t2 = new Test2();
t2.x();
}
}
public class Test3
{
Public void method1()
{
Circle c;
System.out.println(“Radius is ” + c.getRadius());
c = new Circle();

CS 212: Object Oriented Programming Page 2


}
}
public class Test4
{
public static void main(String[] args)
{
C c = new C(5.0);
System.out.println(c.value);
}
}
class C
{
int value = 2;
}
public class Circle
{
private double radius = 1.0;
/** Find the area of this circle */
public double getArea()
{
return radius * radius * Math.PI;
}
}
public class Test5
{
public static void main(String[] args)
{
Circle myCircle = new Circle();
System.out.println("Radius is "+ myCircle.radius);
}
}

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

CS 212: Object Oriented Programming Page 3


wrong in the following code? Please correct it.

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.

CS 212: Object Oriented Programming Page 4


Task 4:

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.

CS 212: Object Oriented Programming Page 5

You might also like