5/2/2015ITK 1791 Shape {abstract} - ShapeName: String # setShapeName(newShapeName: String): void +...
-
Upload
anton-cart -
Category
Documents
-
view
217 -
download
0
Transcript of 5/2/2015ITK 1791 Shape {abstract} - ShapeName: String # setShapeName(newShapeName: String): void +...
04/18/23 ITK 179 1
Shape {abstract}
- ShapeName: String
# setShapeName(newShapeName: String): void
+ getShapeName(): String
+ getSurfaceArea(): double
+ getPerimeter(): double
Rectangle
- length: double
- height: double
+ setLength(newLength: double): void
+ getLength(): double
+ setHeight(newHeight: double): void
+ getHeight(): double
Circle
- radius: double
+ setRadius(newRadius: double): void
+ getRadius(): double
There are some detailsthat are not as important to the concepts (classes)
04/18/23 ITK 179 2
interface Object
Shape
CircleRectangle
RectanglePrism Cylinder
{abstract}ThreeD
{interface}
+ setDepth(double): void
+ getDepth(): double
+ getVolume(): double
implements
04/18/23 ITK 275 3
public abstract class Animal {
private int weight;
public Animal(int lb) { // constructor;
weight = lb;
}
public abstract String is_a(); // What animal is this;
public abstract String eats(); // the food it eats;
public abstract String sounds(); // the sound it makes;
public int Weight() { // return the weight;
return weight;
}
}
Animal
04/18/23 ITK 275 4
public abstract class WildAnimal extends Animal {
private String fHabit;
private String NativePlace=null;
public WildAnimal(String feedingHabit, int weight) {
super(weight);
this.fHabit = feedingHabit;
}
public String feedingHabit() { return fHabit;}
public void lives_in(String NativePlace) {
this.NativePlace = NativePlace;
}
public String lives_in() { return NativePlace;}
}
WildAnimal
04/18/23 ITK 275 5
public abstract class DomesticAnimal extends Animal{
private String purpose, my_name=null;
public DomesticAnimal(String purpose, int weight) {
super(weight);
this.purpose = purpose;
}
public String name() { return my_name; }
public void name(String name) {
my_name = name;
}
public String is_for() { return purpose; }
}
DomesticAnimal
04/18/23 ITK 275 6
public class Elephant extends WildAnimal{
public Elephant(int weight) {
super("herbivorous", weight);
}
public String is_a() {
return "elephant";
}
public String eats() {
return "banana";
}
public String sounds() {
return "enheeen";
}
}
Elephant
04/18/23 ITK 275 7
public abstract class BigCat extends WildAnimal{
public BigCat(int weight) {
super("carnivorous", weight);
}
}
BigCat
public class Tiger extends BigCat{ public Tiger(int weight) {
super(weight); }
public String is_a() { return "tiger"; }
public String sounds() { return "Grrr! Grrr! "; }
public String eats() { return "meat"; }
}
Tiger
04/18/23 ITK 275 8
public class Lion extends BigCat{
public Lion(int weight) {
super(weight);
}
public String is_a() {
return "lion";
}
public String sounds() {
return "Roarn! Roand!";
}
public String eats() {
return "meat";
}
}
Lion
04/18/23 ITK 275 9
public class Cow extends DomesticAnimal {
public Cow (int weight) {
super("milk",weight);
}
public String is_a() {
return "cow";
}
public String sounds() {
return "Mooo! Mooo!";
}
public String eats() {
return "grass";
}
}
Cow
04/18/23 ITK 275 10
public interface Pet {
public String address();
public String likes();
}
Pet
public class Dog extends DomesticAnimal implements Pet { private String my_address;
public Dog (String name, int weight) {super("pleasure",weight);this.name(name);
} public String is_a() { return "dog"; }
public String sounds() { return "Woof! Woof!"; }
public String likes() { return "bone"; }
public String eats() { return likes(); }
public String address() { return my_address; }
public String address(String add) { return my_address = add; }}
Dog
04/18/23 ITK 275 11
public class Cat extends DomesticAnimal implements Pet {
private String my_address;
public Cat (String name, int weight) {
super("pleasure",weight);
this.name(name);
}
public String is_a() { return "cat"; }
public String sounds() { return "Meow! Meow!"; }
public String likes() { return "fish"; }
public String eats() { return likes(); }
public String address() { return my_address; }
public String address(String add) { return my_address = add;}
}
Cat
04/18/23 ITK 275 12
public static void aboutThisDomesticAnimal(DomesticAnimal x) {
System.out.println(" Variable x is a " + x.is_a() +".");
System.out.println(x.name() + " is a " + x.Weight()+" ponds "+ x.is_a() + " for " + x.is_for() + "." );
}
public static void aboutThisWildAnimal(WildAnimal x) {
System.out.println(" Variable x is a " + x.is_a() +".");
System.out.println("This " + x.is_a() + " weights " +
x.Weight() + " pounds.");
System.out.println("The " + x.is_a() + " is a " +
x.feedingHabit()+" animal from " + x.lives_in()+".");
}
04/18/23 ITK 275 13
Animal animal[];
Dog lucky;
Cat mimi;
Cow daisy;
Tiger T;
Lion L;
Elephant E;
animal = new Animal[6];
animal[0] = lucky = new Dog("Lucky", 25);
lucky.address("Bloomington");
animal[1] = mimi = new Cat("Mimi", 15);
mimi.address("Normal");
animal[2] = daisy = new Cow(500);
daisy.name("Daisy");
animal[3] = T = new Tiger(300);
T.lives_in("Asia");
animal[4] = L = new Lion(400);
L.lives_in("Africa");
animal[5] = E = new Elephant(1200);
E.lives_in("Africa");
04/18/23 ITK 275 14
for (int i=0; i<animal.length;i++)
{
System.out.print("\n"+i+": ");
if (animal[i] instanceof DomesticAnimal) {
aboutThisDomesticAnimal((DomesticAnimal) animal[i]);
}
if (animal[i] instanceof WildAnimal) {
aboutThisWildAnimal((WildAnimal) animal[i]);
}
}
04/18/23 ITK 275 15
public interface Bird {
String color();
}
Bird
Cardinal
04/18/23 ITK 275 16
Canary