Skip to content

Instantly share code, notes, and snippets.

@sn1p3r46
Last active September 26, 2016 18:18
Show Gist options
  • Save sn1p3r46/080ccbe2f6883115a1147078ab3535f0 to your computer and use it in GitHub Desktop.
Save sn1p3r46/080ccbe2f6883115a1147078ab3535f0 to your computer and use it in GitHub Desktop.
Basic Java class for java teaching proposes :)
// The class is visible from outside the package
// the class itself is not the object but is the
// description of its behaviour and its data structure.
// (The PROJECT)
public class Animal {
private String race = "Uknown";
private int age;
private float weight;
private String name;
private char sex;
// Constructor method useful to get an instance of the class (FACTORY)
// http://beginnersbook.com/2013/03/constructors-in-java/
// REMIND: No return type and not return within the costructor
public Animal (String r, int a, float w,String n, char s ) {
race = r;
age = a;
weight = w;
name = n;
sex = s;
}
/* Getters and Setters */
// Sets the value of our private field
public String setRace(String race){ //<<---------------//
String oldRace = this.race; //
this.race = race; //
return oldRace; //
} // Which is the difference between these two methods?
//
// Sets the value of our private field //
public void setRaceVoid(String race){ //<<---------------//
this.race = race;
}
// Getting the race value
public String getRace(){
return race;
}
// main method: always public, static and takes an array of strings as input
public static void main(String [] args) {
Animal animal1 = new Animal("Husky",8,23.4f,"Zoe",'F');
System.out.println( animal1.getRace() );
String oldRace = animal1.setRace("Puli");
System.out.println( oldRace );
System.out.println( animal1.getRace() );
animal1.setRaceVoid( oldRace );
System.out.println( animal1.getRace() );
}
}
// Without running the code, wich is the value of race at the end of the program?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment