Commit ac183567 authored by Anya Helene Bagge's avatar Anya Helene Bagge 🦆

f15

parent 2a49d3bd
package inf101.dyr;
import inf101.grid.Direction;
import inf101.grid.IPosition;
import inf101.grid.Position;
public class Dyr {
private double masse;
private IPosition pos;
private Direction dir;
public Dyr(double m) {
masse = m;
pos = Position.make(0, 0);
dir = Direction.NORTH;
}
public double getMass() {
return masse;
}
public IPosition getPosition() {
return pos;
}
public void move() {
pos = pos.go(dir);
}
public void hei() {
System.out.println("Hei, jeg er en " + toString() + " på posisjon " + pos + " og jeg veier " + masse + "g");
}
public String toString() {
return "dyr";
}
public void spis(double vekt) {
System.out.println("Jeg spiser som et dyr!");
this.masse += vekt/10;
}
}
package inf101.dyr;
import inf101.grid.Direction;
public class Kanin extends Dyr {
public Kanin() {
super(3500.0);
}
@Override
public String toString() {
return "kanin";
}
@Override
public void move() {
pos = pos.go(Direction.EAST);
}
public void spis(int vekt) {
System.out.println("Jeg spiser som en kanin!");
masse = masse + vekt;
}
}
package inf101.dyr;
public class MainDyr {
public static void main(String[] args) {
Dyr d = new Dyr(42);
Kanin k = new Kanin();
Ulv u = new Ulv();
Dyr v = new Ulv();
d.hei();
u.hei();
v.hei();
k.hei();
k.move();
u.move();
k.hei();
u.hei();
k.spis(15);
u.spis(30);
k.hei();
u.hei();
System.out.println();
Dyr dk = k;
Dyr du = u;
// Hvis spis() ikke finnes i Dyr, så må vi typekonvertere
// hvis vi vil kalle spis, selv om alle dyrene kan spise
// ((Ulv) du).spis(50);
du.spis(50);
du.hei();
u.spis(50);
u.hei();
}
}
package inf101.dyr;
public class Ulv extends Dyr {
public Ulv() {
super(30000.0);
}
@Override
public String toString() {
return "ulv";
}
@Override
public void spis(double vekt) {
System.out.println("GRRRROAAOAO");
super.spis(vekt);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment