Java Tutorial #7 Extends + Vectors
Previous tutorial https://steemit.com/java/@nicetea/java-tutorial-6-fun-with-vectors
In this tutorial, we will create 2 classes. One more general SpaceObject and another one more specific Planet. Inside the main, we will be using both. Keep on reading to see how it will be used! :)
SpaceObject.java
public class SpaceObject {
private int diameter = 300;
private String name = "Unknown";
public SpaceObject() {
}
public SpaceObject(String name, int diameter) {
setName(name);
setDiameter(diameter);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDiameter() {
return diameter;
}
public void setDiameter(int diameter) {
this.diameter = diameter;
}
}
Understanding this class should be pretty obvious if you watched my last tutorials. Basically we are creating a SpaceObject class, with the attributes diameter and a name of the object. Then we will create the public constructor, as well as getters and setters.
Planet.java
public class Planet extends SpaceObject {
private float watermass;
public Planet(String name, int diameter, float watermass) {
super(name, diameter);
setWatermass(watermass);
}
public void setWatermass(float watermass) {
if (watermass < 0.0f)
this.watermass = 0.0f;
else if (watermass > 1.0f)
this.watermass = 1.0f;
else
this.watermass = watermass;
}
public float getWatermass() {
return watermass;
}
public float GetLandmass() {
return 1.0f - getWatermass();
}
}
This time, we have something new! You might have noticed the extends word next to the Planet class name. This means that the class Planet will be extended, by all the attributes and methods of SpaceObject. So if we are creating a planet, we will automatically use the code from SpaceObject! Great isn't it? This is a huge time safer when writing lots of code which will be used again and again.
So as most of the code should be understandable, besides the constructor, we will just dig into that. If you have any questions, just drop a comment below!
The constructor Planet(String name, int diameter, float watermass is using a new word super(). What super does, is calling the parent constructor. In our case, it's calling the constructor of SpaceObject with the parameters name, diameter.
ObjectVector.java
public class ObjectVector {
private static Vector<SpaceObject> wobjects = new Vector<SpaceObject>();
public static void fillVector() {
Random rn = new Random();
for (int i = 0; i < 5; i++) {
int d = rn.nextInt(10000) + 1000;
wobjects.add(new Planet("Planet " + (i + 1), d, rn.nextFloat()));
}
}
public static void main(String[] args) {
fillVector();
System.out.println(wobjects.get(0).getDiameter());
}
}
At first, we are creating a new empty SpaceObject Vector for later usage. The method fillVector() is used to fill our Vector<SpaceObject> with data. Inside, we will create a new Random object to gather randomized data. With the for loop, we are adding 5 new elements to wobjects. To be accurate, we are adding 5 Planets with randomized data to the SpaceObject Vector.
Inside the main method, we will call our previously written method, and printing out the contents of wobjects, to verify our work.
Output:
Stay tuned!
Hi my friend
I folow you. You also see my page if you would like folow me
thank you
Thanks :)