Training, Open Source Computer Languages

This is page http://www.wellho.net/share/javainherit2.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Java Interitance Example - with abstract, arrays, etc

This is the second of two examples - the first is called Javainherit - that shows classes and inheritance in Java, as demonstrated bring our recent training course in Melksham.

Anitest.java


public class Anitest {

public static void main(String [] args) {

Animal [] beasts = new Animal[4];
beasts[0] = new Farm("Cow",100,20);
beasts[1] = new Farm("Cockerel",4,2,2);
beasts[2] = new Pet("Cat",5,300);
beasts[3] = new Myth("Thestral",90);

for (int bn=0; bn<beasts.length; bn++) {
int ew = beasts[bn].getweight();
int ev = beasts[bn].getvalue();
String et = beasts[bn].getbreed();
int el = beasts[bn].getlegs();
System.out.println(et + " which weighs " + ew +
" worth " + ev + " legs: "+el);
}

Animal best = beasts[2].morevaluable(beasts[0]);
int mv = best.getvalue();
System.out.println("More valuable is worth "+ mv);

Animal bestest = Animal.mostvaluable(beasts);
// Would have been a good idea to output the result ;-)

}}


Myth.java


public class Myth extends Animal {
int vpk;

public Myth (String breed, int weight) {
super(breed, weight);
}

public Myth (String breed, int weight, int legs) {
super(breed, weight, legs);
}

public int getvalue() {
return 0;
}

}


Pet.java


public class Pet extends Animal {
int v;

public Pet (String breed, int weight, int v) {
super(breed, weight);
this.v = v;
}

public int getvalue() {
return v;
}
}

Farm.java


public class Farm extends Animal {
int vpk;

public Farm (String breed, int weight, int vpk) {
super(breed, weight);
this.vpk = vpk;
}

public Farm (String breed, int weight, int vpk, int legs) {
super(breed, weight, legs);
this.vpk = vpk;
}

public int getvalue() {
return vpk * weight;
}
}


Animal.java


abstract public class Animal {
String breed;
int weight;
int legs;
abstract public int getvalue() ;

public Animal(String breed, int weight) {
mkobject(breed, weight, 4);
}

public Animal(String breed, int weight, int legs) {
mkobject(breed, weight, legs);
}

private void mkobject (String breed, int weight, int legs) {
this.breed = breed;
this.weight = weight;
this.legs = legs;
}

public String getbreed() {
return breed;
}

public int getweight() {
return weight;
}

public int getlegs() {
return legs;
}

public Animal morevaluable(Animal that) {
int val1 = this.getvalue();
int val2 = that.getvalue();
if (val1 > val2) return this;
return that;
}

public static Animal mostvaluable(Animal [] zoo) {
Animal mvsf = zoo[0];
for (int n=1; n<zoo.length; n++) {
mvsf = mvsf.morevaluable(zoo[n]);
}
return mvsf;
}
}

[J710]
OPTIONS AVAILABLE TO YOU
You may follow links in the text to explore other topics.
Extra link - Return to the HomePage of this share system

STATUS AND CONTROL
You are a guest
Enter name and password to


This is a page from Well House Consultant's shared information directory. Learn about our shared information system.

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/share/javainherit2.html • PAGE BUILT: Fri Apr 5 18:43:09 2013 • BUILD SYSTEM: wizard