// 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("Hello, World!"); // 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);
Hello, World!
Hello, Nighthawk Coding Society!
public class Pytho {
private int a;
private int b;
private int c;
public void pythoset( int a, int b, int c){ // Setter
this.a = a;
this.b = b;
this.c = c;
}
public Pytho(){
this.pythoset(3,4,5);
}
public Pytho(int a, int b, int c){
this.pythoset(a, b, c);
}
public String pythoget(){
int sqa = this.a * this.a;
int sqb = this.b * this.b;
int sqc = this.c * this.c;
int sumsq = sqa + sqb;
String correct = "This is a Pythagorean Triple";
String wrong = "This is not a Pythagorean Triple";
if(sumsq == sqc){
return correct;
}
else{
return wrong;
}
}
public static void main(String[] args){
Pytho tr1 = new Pytho();
Pytho tr2 = new Pytho(6,8,9);
System.out.println(tr1.pythoget());
System.out.println(tr2.pythoget());
}
}
Pytho.main(null)
This is a Pythagorean Triple
This is not a Pythagorean Triple