Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Lab Task 3

Uploaded by

fahadraufhashmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab Task 3

Uploaded by

fahadraufhashmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

BS (Software Engineering) 2023

LAB 3: Classes and Objects, Properties, and Behaviour


Objectives
In this lab, you will dive into the fundamental concepts of object-oriented programming in
Java, specifically focusing on classes, objects, properties (attributes), and behavior (methods).
You will learn how to design and create classes, instantiate objects from those classes, define
properties to represent the state of objects, and implement methods to specify their behavior.

Theoretical Description
Classes and Objects: In Java, a class is a blueprint for creating objects. A class defines the
structure and characteristics (properties and behaviors) that its objects will have. Objects are
instances of classes, and they represent real-world entities or concepts.

Properties (Attributes): Properties are variables defined within a class that represent the state
or characteristics of objects. These properties are also referred to as attributes or fields. In this
lab, you will create a "Person" class with two properties: name and age. These properties
encapsulate information about a person.

Behavior (Methods): Behavior in object-oriented programming is defined through methods.


Methods are functions or procedures associated with a class that describe the actions or
operations an object can perform. In this lab, you will implement two methods: introduce and
birthday. The introduce method allows a person to introduce themselves, and the birthday
method increments a person's age.

Lab Task
Task 1: Creating a Class
• Create a new Java class named "Person."
• Define two properties within the "Person" class: name (a string) and age (an integer).
• Create a constructor that allows you to initialize these properties when an object is
created.

Task 2: Instantiating Objects

9
BS (Software Engineering) 2023

• In the main method of another class, create two objects of the "Person" class.
• Initialize their name and age properties using the constructor.
• Print out the details of these two persons.

Task 3: Adding Behavior


• Add a method named introduce to the "Person" class. This method should print a
message introducing the person by name and age.
• Call the introduce method for both persons you created in Task 2.

Task 4: Modifying Properties


• Add a method named birthday to the "Person" class. This method should increment
the person's age by 1.
• Call the birthday method for one of the persons and print their updated age.

Task 5: Class Documentation


• Add comments to your class, methods, and properties to document their functionality
and purpose.
• Explain the concept of encapsulation and how it helps maintain data integrity.

Code Help:
// Task 1: Creating a Class

public class Person {

private String name;

private int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

public void introduce() {

System.out.println("Hello, my name is " + name + " and I am " + age + "


years old.");

10
BS (Software Engineering) 2023

public void birthday() {

age++;

// Getter and Setter methods for encapsulation

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public int getAge() {

return age;

public void setAge(int age) {

this.age = age;

// Task 2: Instantiating Objects

public class Main {

public static void main(String[] args) {

Person person1 = new Person("Alice", 25);

Person person2 = new Person("Bob", 30);

person1.introduce();

person2.introduce();

11
BS (Software Engineering) 2023

person1.birthday();

System.out.println(person1.getName() + "'s new age: " + person1.getAge());

12

You might also like