0% found this document useful (0 votes)
73 views

ComProg2 Lesson 2 - Objects and Classes

This document discusses objects and classes in Java. It defines key terms like class, encapsulation, constructor, mutator, and accessor. It provides an example Person class with data fields for name, age, height, and weight. The Person class uses constructors, getter and setter methods, and a toString method to demonstrate working with object attributes and printing object details.

Uploaded by

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

ComProg2 Lesson 2 - Objects and Classes

This document discusses objects and classes in Java. It defines key terms like class, encapsulation, constructor, mutator, and accessor. It provides an example Person class with data fields for name, age, height, and weight. The Person class uses constructors, getter and setter methods, and a toString method to demonstrate working with object attributes and printing object details.

Uploaded by

Jofrank Riego
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lesson 2: Objects and Classes

I. Terminologies
a. Classes – the mechanism in Java that allows you to combine data and operations that
can be performed on that data.
b. Encapsulation – an object combines data and operations on that data in a single unit.

c. Constructor – has the same name as the class, and it executes automatically when an
object of that class is created.
d. Mutator – a method of a class that modifies the value(s) of one or more data
member(s).
e. Accessor – a method of a class that only accesses the value(s) of the data member(s).
f. Methods – a Java mechanism where operations in a program is implemented.

II. Sample Program

public class PersonSimulator


{
public static void main (String[] args)
{
Person a = new Person();
Person b = new Person("John Doe", 22, 166.0, 70.0);

System.out.print(a.toString());
System.out.print(b.toString());

//set a's attributes


a.setName("Magdalena");
a.setAge(14);
a.setHeight(140);
a.setWeight(35);

System.out.print(a.toString());
}
}

-----------------------------------------------------------------------------

public class Person


{
//instance variables
private String name;
private int age;
private double height, weight;

//set constructor
public Person() {
this.setName("No Name");
this.setAge(0);
this.setHeight(0.0);
this.setWeight(0.0);
New Era University
Computer Programming 2 Page 1
Lesson 2: Objects and Classes
}
public Person(String name, int age, double height, double weight) {
this.setName(name);
this.setAge(age);
this.setHeight(height);
this.setWeight(weight);
}
//set methods
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;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String toString() {
return "Name: " + name + "\nAge: " + age + "\nHeight: " + height
+
"\nWeight: " + weight + "\n-----------\n";
}
}

OUTPUT:

New Era University


Computer Programming 2 Page 2
Lesson 2: Objects and Classes

New Era University


Computer Programming 2 Page 3

You might also like