Home > database >  Is it possible to create an array which contains objects from several classes that don't necess
Is it possible to create an array which contains objects from several classes that don't necess

Time:02-21

I currently have 7 different classes in my project, with 5 of those classes being related to each other (4 subclasses and 1 superclass) and the other two being related as well (1 subclass and 1 superclass). My assignment asks me to create an array which holds one object from each of these classes. My question is how can I create an array that can hold objects from unrelated classes? Here is a (much) shorter version of my code for example/clarity purposes:

//First superclass
package Plane;
public class Plane {
    
    
     private String brand;
     private double price; 
     private int horsepower;
    
     
    public Airplane() {
        brand = "example";
        price = 700000; 
        horsepower = 1750;
    }

    public Airplane(String airplaneBrand, double airplanePrice, int airplanePower) {
        this.brand = planeBrand;
        this.price = planePrice;
        this.horsepower = airplanePower;
    }
//Second superclass
package Boat;
public class Boat {

    private double weight;
    private double price;
    
    public Boat() {
        weight = 3475.6;
        price = 700000.99;
    }
    
    public Boat(double boatWeight, double boatPrice) {
        weight = boatWeight;
        price = boatPrice;
    }
    

The exact wording of the assignment is "Your array must include at least one object from each of the classes." From my understanding, this is what I described above as having one array that can hold objects from both the Plane class and the Boat class. The kicker is that I cannot import any of Java's built-in classes (i.e ArrayList, Hash Maps, etc.) I apologize in advance for any clarity issues as I'm only starting out with programming in general, I'd be happy to edit/add information if needed.

CodePudding user response:

In the code provided class is declared as

public class Plane

but constructors created:

public Airplane(){...}

and

public Airplane(String airplaneBrand, double airplanePrice, int airplanePower) {...}

constructor names should be same as class name.

Solution:

Create a Object[] array and use instanceof operator to find the class of the object :

import java.util.*;
class Airplane {


    private String brand;
    private double price; 
    private int horsepower;


    public Airplane() {
        brand = "example";
        price = 700000; 
        horsepower = 1750;
    }

    public Airplane(String airplaneBrand, double airplanePrice, int airplanePower) {
        this.brand = "AirIndia";
        this.price = airplanePrice;
        this.horsepower = airplanePower;
    }
    public String getBrand(){
        return brand;
    }

}
class Boat {

    private double weight;
    private double price;

    public Boat() {
        weight = 3475.6;
        price = 700000.99;
    }

    public Boat(double boatWeight, double boatPrice) {
        weight = boatWeight;
        price = boatPrice;
    }
    public double getWeight(){
        return weight;
    }
}


public class Test{
    public static void main(String ... $){
        var out = System.out;
        Object [] array = new Object[5];
        array[0] = new Boat();
        array[1] = new Airplane();
        array[2] = new Boat();
        array[3] = new String("Foo loves Bar");
        for(var o : array){
            if(o instanceof Boat b){
                out.println(b.getWeight());
            }
            if(o instanceof Airplane plane){
                out.println(plane.getBrand());
            }
            if(o instanceof String s){
                out.println(s);
            }

        }
    }
}

edit: thanks tgdavies for mentioning the efficient use of instanceof

output:

$ javac Test.java && java Test
3475.6
example
3475.6
Foo loves Bar
  • Related