ComProg2 Lesson 2 - Objects and Classes
ComProg2 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.
System.out.print(a.toString());
System.out.print(b.toString());
System.out.print(a.toString());
}
}
-----------------------------------------------------------------------------
//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: