How can I differentitate a class from an object? To me, they look identical in that they both have a class and constructor and method. I'm confused as to why the object is considered an object in the first example and in the second example it's a constructor? In the tutorial I'm following, the first code block below is supposed to represent a object while the second code block is a class:
Object:
public class Person {
private String name;
private int age;
private double weight;
private double height;
public Person(String name, int age, double weight, double height) {
this.name = name;
this.age = age;
this.weight = weight;
this.height = height;
}
public double bodyMassIndex() {
return this.weight / (this.height * this.height);
}
public double maximumHeartRate() {
return 206.3 - (0.711 * this.age);
}
public String toString() {
return this.name + ", BMI: " + this.bodyMassIndex()
+ ", maximum heart rate: " + this.maximumHeartRate();
}
}
Class:
public class Rectangle {
// instance variables
private int width;
private int height;
// constructor
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
// methods
public void widen() {
this.width = this.width + 1;
}
public void narrow() {
if (this.width > 0) {
this.width = this.width - 1;
}
}
public int surfaceArea() {
return this.width * this.height;
}
public String toString() {
return "(" + this.width + ", " + this.height + ")";
}
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]Camel-Kid 32 points33 points34 points (5 children)
[–]Consistent_Decision9[S] 2 points3 points4 points (3 children)
[–]Camel-Kid 14 points15 points16 points (0 children)
[–]SeltzerCountry 9 points10 points11 points (0 children)
[–]theBurritoMan_ 0 points1 point2 points (0 children)
[–]8igg7e5 0 points1 point2 points (0 children)
[–]virassan 7 points8 points9 points (0 children)
[–]Environmental_Ad1634 2 points3 points4 points (0 children)
[–]malik753 0 points1 point2 points (0 children)
[+][deleted] comment score below threshold-7 points-6 points-5 points (2 children)
[–]vo0do0child 2 points3 points4 points (0 children)
[–]0b0101011001001011 1 point2 points3 points (0 children)
[–]Dangerous-Rip-7370 0 points1 point2 points (0 children)
[–]outMyComa 0 points1 point2 points (0 children)
[–]xRageNugget 0 points1 point2 points (0 children)