Skip to content
Advertisement

Using parent class toString in object Array to print parent class variables for child class JAVA

I am trying to write a code that takes vehicle names and such and stores them in an array of objects. I have Vehicle as the parent class with a toString that prints the name, brand and gallons of tank, the two subclasses of parent are bus and car. Car and Bus ask how many doors,wheels or passengers. When printing the array out after randomizing the order i only get the to Strings from the parent class to print out when the index of the array is of the parent class. What i want to print out is: Vehicle 0: name: car brand: something : tanksize: 15 Doors: 4 : Wheels 4


My code is below:

import java.util.*;

public class Q1 {
    public static void main(String args[]){
        
        Scanner kb = new Scanner(System.in);

        Vehicle[] list = new Vehicle[3];

        for(int i =0;i < list.length;i++){
            int random = (int)(Math.random()*3);
            if(random == 0){
                System.out.println("What is the Name, brand and how big is the gas tank of the vehicle?");
                String name = kb.next();
                String brand = kb.next();
                int gasTank = kb.nextInt();
                
                Vehicle a = new Vehicle(name,brand,gasTank);
                
                list[i] = a;
    
            }
            else if(random == 1){
                System.out.println("How many doors and wheels does the car have");
                int doors = kb.nextInt();
                int wheels = kb.nextInt();
                
                System.out.println("What is the Name, brand and how big is the gas tank of the car?");
                String name = kb.next();
                String brand = kb.next();
                int gasTank = kb.nextInt();
                
                Car a = new Car(doors,wheels,name,brand,gasTank);
                list[i] = a;
            }
            else{
                System.out.println("How many doors and wheels does the bus have?");
                int doors = kb.nextInt();
                int wheels = kb.nextInt();
                
    
                System.out.println("What is the Name, brand and how big is the gas tank of the bus?");
                String name = kb.next();
                String brand = kb.next();
                int gasTank = kb.nextInt();
                
                Bus a = new Bus(doors,wheels,name,brand,gasTank);
    
                list[i] = a;
            }
            
        }
        printArray2(list);
        


    }
    public static void printArray(Vehicle[] list){
        for(int i = 0; i< list.length; i++){
            System.out.println("Vehicle "+ i);
            System.out.println(list[i]);
            System.out.println("___________");
        }
    }
    public static void printArray2(Vehicle[] list){
        for(int i = 0; i< list.length;i++){
            System.out.println("Vehicle "+ i);
            System.out.println(list[i].toString());
            System.out.println("___________");
        }
        
        
    }
}


class Vehicle {
    String name;
    String brand;
    int gasTank;

    public Vehicle(){
        this(" ", " ", 15);
    }
    public Vehicle(String name, String brand,int gasTank){
        this.name = name;
        this.brand = brand;
        this.gasTank = gasTank;

    }

    public String getname(){
        return name;
    }
    public String getbrand(){
        return brand;
    }  
    public int getgasTank(){
        return gasTank;
    } 
    public void setname(String name){
        this.name = name;
    }
    public void setbrand(String brand){
        this.brand = brand;
    }
    public void setgasTank(int gasTank){
        this.gasTank = gasTank;
    }
    
    public double milesPerGallon(double miles, double gallonsUsed){
        return  miles / gallonsUsed;
    }

    public String toString(){
        return " name: " + name + " Brand: " + brand + " Gallons in Tank: " + gasTank;
    }

    public double fillTank(double gallonsUsed){

        return (gasTank - gallonsUsed);
    }

}


class Car extends Vehicle{
    int numOfdoors;
    int numOfWheels;

    public Car(){
        this(2,4,"","",10);
    }
    public Car(int numOfDoors, int numOfWheels, String name, String brand, int gasTank){
        this.numOfdoors = numOfDoors;
        this.numOfWheels = numOfWheels;
        super.name = name;
        super.brand = brand;
        super.gasTank = gasTank;
    }
    public int getnumOfdoors(){
        return numOfdoors;
    }

    public int getnumOfWheels(){
        return numOfWheels;
    }
    public void setnumOfdoors(int numOfdoors){
        this.numOfdoors = numOfdoors;
    }
    public double milesPerGallon(double miles, double gallonsUsed){
        return miles / gallonsUsed;
    }

    public String toString(){
        return "The car has " + numOfWheels + " wheels and " + numOfdoors + " doors";
    }
}

class Bus extends Vehicle {
    int numOfWheels;
    int numOfPassengers;

    public Bus(){
        this(1,8,"","", 50);
    }
    public Bus(int numOfWheels, int numOfPassengers, String name, String brand, int gasTank){
        this.numOfWheels = numOfWheels;
        this.numOfPassengers = numOfPassengers;
        super.name = name;
        super.brand = brand;
        super.gasTank = gasTank;
    }

    public int getNumOfWheels(){
        return numOfWheels;
    }
    public int getNumOfPassengers(){
        return numOfPassengers;
    }

    public void setNumOfWheels(int numOfWheels){
        this.numOfWheels = numOfWheels;
    }
    public void setNumOfPassengers(int numOfPassengers){
        this.numOfPassengers = numOfPassengers;
    }
    public double milesPerGallon(double miles, double gallonsUsed){
        return miles / gallonsUsed;
    }
    public String toString(){
        return "The bus has "+ numOfWheels + " Wheels and " + numOfPassengers + " Passengers";
    }
}

Advertisement

Answer

In your code you are overriding the toString method in child classes. While what I understand is that you want to print values from parent also along with child class. So your toString in child class should be like below, where you first call parent toString also.

@Override
public String toString() {
 return super.toString() + "wheels " wheels + " doors " + numOfdoors ; // or something like this
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement