Skip to content

Instantly share code, notes, and snippets.

@lyxal
Created May 16, 2021 05:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyxal/c336f0a7463764ad303ab8bb4278c9f4 to your computer and use it in GitHub Desktop.
Save lyxal/c336f0a7463764ad303ab8bb4278c9f4 to your computer and use it in GitHub Desktop.
public class Mammal {
private String name;
private int age;
private String noise = "whatever noise a generic mammal makes";
private float weight;
public static final int NAME = 1;
public static final int AGE = 2;
public static final int NOISE = 3;
public static final int WEIGHT = 4;
public Mammal(){
name = "Yesn't";
age = 0;
weight = 0;
}
public Mammal(String pName){
this(); // is SPARTA!!!
name = pName;
}
public Mammal(String pName, int pAge){
this(pName);
age = pAge;
}
public Mammal(String pName, int pAge, String pNoise){
this(pName, pAge);
noise = pNoise;
}
public Mammal(String pName, int pAge, String pNoise, float pWeight){
this(pName, pAge, pNoise);
weight = pWeight;
}
public void speak(){
System.out.println(noise);
}
public <Integer, T> void setAttribute(Integer attr, T value){
int temp = (int) attr;
switch (temp){
case NAME: name = (String) value; break;
case AGE: age = (int) value; break;
case NOISE: noise = String.valueOf(value); break;
case WEIGHT: weight = (float) value; break;
default: System.out.println("Invalid attr number");
}
}
public <T> T getAttribute(Integer attr){
int temp = (int) attr;
switch (temp){
case NAME: return name;
case AGE: return age;
case NOISE: return noise;
case WEIGHT: return weight;
default: return -1;
}
}
public void printInformation(){
System.out.printf("Name: %s\nAge: %d\nNoise: %s\nWeight: %.2f\n", name, age, noise, weight);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment