// Define Class with Constructor returning Object
public class HelloObject {
private String hello; // instance attribute or variable
public HelloObject() { // constructor
hello = "Hello, World!";
}
public String getHello() { // getter, returns value from inside the object
return this.hello; // return String from object
}
public static void main(String[] args) {
HelloObject ho = new HelloObject(); // Instance of Class (ho) is an Object via "new HelloObject()"
System.out.println(ho.getHello()); // Object allows reference to public methods and data
}
}
// IJava activation
HelloObject.main(null);
Hello, World!
// Define Class
public class HelloDynamic { // name the first letter of class as capitalized, note camel case
// instance variable have access modifier (private is most common), data type, and name
private String hello;
// constructor signature 1, public and zero arguments, constructors do not have return type
public HelloDynamic() { // 0 argument constructor
this.setHello("Chill!"); // using setter with static string
}
// constructor signature, public and one argument
public HelloDynamic(String hello) { // 1 argument constructor
this.setHello(hello); // using setter with local variable passed into constructor
}
// setter/mutator, setter have void return type and a parameter
public void setHello(String hello) { // setter
this.hello = hello; // instance variable on the left, local variable on the right
}
// getter/accessor, getter used to return private instance variable (encapsulated), return type is String
public String getHello() { // getter
return this.hello;
}
// public static void main(String[] args) is signature for main/drivers/tester method
// a driver/tester method is singular or called a class method, it is never part of an object
public static void main(String[] args) {
HelloDynamic hd1 = new HelloDynamic(); // no argument constructor
HelloDynamic hd2 = new HelloDynamic("Hello, Nighthawk Coding Society!"); // one argument constructor
System.out.println(hd1.getHello()); // accessing getter
System.out.println(hd2.getHello());
}
}
// IJava activation
HelloDynamic.main(null);
Chill!
Hello, Nighthawk Coding Society!
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.setName(name);
this.setAge(age);
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
if(age >= 0) {
this.age = age;
}
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public static void main(String[] args) {
Person p1 = new Person("Vishnu", 17);
System.out.println(p1.getName());
System.out.println(p1.getAge());
}
}
Person.main(null);
Vishnu
17
public class Dessert {
private String type;
private double cost;
public Dessert(String type, double cost) {
this.setType(type);
this.setCost(cost);
}
public void setType(String type) {
this.type = type;
}
public void setCost(double cost) {
this.cost = cost;
}
public String getType() {
return type;
}
public double getCost() {
return cost;
}
public static void main(String[] args) {
Dessert dessert1 = new Dessert("cake", 8.99);
System.out.println(dessert1.getType());
System.out.println(dessert1.getCost());
}
}
Dessert.main(null);
cake
8.99
public class Location {
private String city;
private String state;
private String zip;
public Location(String city, String state, String zip) {
this.setCity(city);
this.setState(state);
this.setZip(zip);
}
public void setCity(String city) {
this.city = city;
}
public void setState(String state) {
this.state = state;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public String getZip() {
return zip;
}
@Override
public String toString() {
return "City: " + city + ", State: " + state + ", Zip: " + zip;
}
public static void main(String[] args) {
Location loc = new Location("San Diego", "CA", "92127");
System.out.println(loc);
}
}
Location.main(null);
City: San Diego, State: CA, Zip: 92127