Skip to content

Instantly share code, notes, and snippets.

@edenizk
Last active May 11, 2018 17:23
Show Gist options
  • Save edenizk/4790d602975cfd770094a191d817f6d8 to your computer and use it in GitHub Desktop.
Save edenizk/4790d602975cfd770094a191d817f6d8 to your computer and use it in GitHub Desktop.
outputing animals powers with classes
package com.example.deniz.app4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txtanimal = (TextView)findViewById(R.id.txtanimal);
TextView txtcat = (TextView)findViewById(R.id.txtcat);
TextView txtlion = (TextView)findViewById(R.id.txtlion);
TextView txtleo = (TextView)findViewById(R.id.txtleo);
TextView txtbird = (TextView)findViewById(R.id.txtbird);
animal animal1 = new animal("tiger", "orange", 80,40);
txtanimal.setText(animal1.toString());
cat cat1 = new cat ("Mia", "white", 100, 10, 4, true);
txtcat.setText(cat1.toString());
lion lion1 = new lion ("King of the forest", "golden", 90, 50 , 4 ,true , true);
txtlion.setText(lion1.toString());
leopard leopard1 = new leopard("Leo","Gold Black spots", 60, 70, 4,true,"Sharp");
txtleo.setText(leopard1.toString());
bird bird1 = new bird ("CivCiv", "Yellow", 1 , 5,false ,2);
txtbird.setText(bird1.toString());
}
}
package com.example.deniz.app4;
/**
* Created by deniz on 23/03/2018.
*/
public class animal {
private final String name;
private final String color;
private int power;
private int speed;
public animal (String name, String color, int power, int speed){
this.name = name;
this.color = color;
this.power = power;
this.speed = speed;
}
public String getName() {
return name;
}
public String getColor() {
return color;
}
public void setPower(int power){
this.power=power;
}
public int getPower(){
return power;
}
public void setSpeed(int speed){
this.speed=speed;
}
public int getSpeed(){
return speed;
}
public int value(int power, int speed){
int result;
return result = power * speed / 100;
}
@Override
public String toString() {
return String.format("%s: %s %s: %s Power: %d Speed: %d Value: %d ", "Name" , name, "Color", color, power, speed,value(power,speed));
}
}
package com.example.deniz.app4;
/**
* Created by deniz on 23/03/2018.
*/
public class bird extends animal {
private final boolean canfly;
private int wing;
public bird (String name, String color, int power, int speed,boolean canfly, int wing){
super(name , color , power, speed);
this.canfly = canfly;
this.wing = wing;
}
public boolean getCanfly() {
return canfly;
}
public int getWing() {
return wing;
}
public void setWing(int wing) {
this.wing = wing;
}
@Override
public String toString() {
return super.toString() + String.format("canfly?: %b wing(s): %d",canfly,wing);
}
}
package com.example.deniz.app4;
/**
* Created by deniz on 23/03/2018.
*/
public class cat extends animal {
private final int numberoflegs;
private boolean canhunt;
public cat(String name,String color, int power, int speed, int numberoflegs , boolean canhunt ){
super(name, color, power, speed);
this.numberoflegs = numberoflegs;
this.canhunt = canhunt;
}
public int getNumberoflegs() {
return numberoflegs;
}
public boolean getCanhunt() {
return canhunt;
}
public void setCanhunt(boolean canhunt) {
this.canhunt = canhunt;
}
/*
in order to use it change values private to protected
public String somevalue(){
return "name: " + name + " color: " + color + "power: " + power + "speed: " + speed;
}
*/
@Override
public String toString(){
return super.toString() + String.format(" Legs: %d canhunt?: %b ",numberoflegs,canhunt);
}
}
package com.example.deniz.app4;
/**
* Created by deniz on 23/03/2018.
*/
public class leopard extends cat {
private String claws = "sharp";
public leopard(String name, String color, int power, int speed, int numberoflegs, boolean canhunt, String claws){
super(name , color , power , speed , numberoflegs , canhunt);
this.claws = claws;
}
public String getClaw(){
return claws;
}
public void setClaws(String claws){
this.claws = claws;
}
@Override
public String toString() {
return super.toString() + String.format(claws);
}
}
package com.example.deniz.app4;
/**
* Created by deniz on 23/03/2018.
*/
public class lion extends cat {
private boolean brave;
public lion(String name, String color , int power, int speed, int numberoflegs, boolean canhunt, boolean brave){
super(name , color , power , speed , numberoflegs , canhunt);
this.brave = brave;
}
public boolean getBrave() {
return brave;
}
public void setBrave(boolean brave) {
this.brave = brave;
}
@Override
public String toString() {
return super.toString() + String.format("Braveness: ", brave);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment