Search code examples
javastringinheritanceoverridingsuperclass

How do I string all the variables together in order to articulate them in one sentence?


The current code prints the statements individually whereas I want to string all the variables regarding the car together (colour, number of cylinders, and number of seats) and string all the variables regarding the truck together (colour, number of cylinders, and towing capacity). The variable seats is in the subclass Car and prints its own independent statement and the variable towing capacity is in the subclass Truck which also prints its own statement. I want all the variables for the car in one sentence and all the variables for the truck in one sentence.

import java.util.Scanner;

class Vehicle {

    Scanner s = new Scanner(System.in);
    private String color;
    private int noOfCylinders;
    public int noOfSeats;
    public int towingCapacity;

    public Vehicle() {
        color = "Black";
        noOfCylinders = 0;
        noOfSeats = 1;
        towingCapacity =0;
    }

    public Vehicle(String color, int noOfCylinders, int noOfSeats, int towingCapacity) {
        this.color = color;
        this.noOfCylinders = noOfCylinders;
        this.noOfSeats = noOfSeats;
        this.towingCapacity = towingCapacity;
    }

    public void setColor() {
        System.out.print("Enter color of vehicle: ");
        color = s.nextLine();
    }

    public String getColor() {
        return color;
    }

    public void setNoOfCylinders() {
        System.out.print("Enter number of cylinders: ");
        noOfCylinders = s.nextInt();
    }

    public int getNoOfCylinders() {
        return noOfCylinders;
    }

    public void setNoOfSeats() {
        System.out.print("Enter numer of seats: ");
        noOfSeats = s.nextInt();
    }

    public void setTowingCapacity() {
        System.out.print("Enter towing Capacity: ");
        towingCapacity = s.nextInt();
    }

    public String toString() {
        String information;
        information = "is " + color + " and it has " + noOfCylinders + " cylinders.";
        return information;
    }
}

public class CreateVehicle {
    public static void main(String[] args) {

        Car CarObject = new Car();
        Truck TruckObject = new Truck();

        CarObject.getColor();
        CarObject.setColor();
        CarObject.getNoOfCylinders();
        CarObject.setNoOfCylinders();
        CarObject.toString();
        CarObject.setNoOfSeats();

        TruckObject.getColor();
        TruckObject.setColor();
        TruckObject.getNoOfCylinders();
        TruckObject.setNoOfCylinders();
        TruckObject.toString();
        TruckObject.setTowingCapacity();

        CarObject.toStringSeats();
        TruckObject.toStringCapacity();
        System.out.print(("\nThe car ")+CarObject.toString());
        System.out.print(("\nThe truck ")+TruckObject.toString());      
    }
}


class Car extends Vehicle{
    public void toStringSeats(){
        System.out.print("\nThe car has " + noOfSeats + " seats.");
    }
}

    class Truck extends Vehicle {
        public void toStringCapacity() {
            System.out.print("\nThe truck has a towing capacity of " + towingCapacity + ".");
        }
    }

Solution

  • you don't need some code as I have commented the same below. Use the toString methods as given below.

    public class CreateVehicle {
        public static void main(String[] args) {
    
            Car CarObject = new Car();
            Truck TruckObject = new Truck();
    
            //CarObject.getColor();
            CarObject.setColor();
            //  CarObject.getNoOfCylinders();
            CarObject.setNoOfCylinders();
            //CarObject.toString();
            CarObject.setNoOfSeats();
    
            //TruckObject.getColor();
            TruckObject.setColor();
            //TruckObject.getNoOfCylinders();
            TruckObject.setNoOfCylinders();
            //TruckObject.toString();
            TruckObject.setTowingCapacity();
    
            System.out.print(("\nThe car ") + CarObject.toString());
            System.out.print(("\nThe truck ") + TruckObject.toString());
        }
    }
    
        class Car extends Vehicle {
            public String toString() {
                String information;
                information = super.toString() + " and " + noOfSeats + " seats";
                return information;
            }
        }
    
        class Truck extends Vehicle {
            public String toString() {
                String information;
                information = super.toString() + " and has a towing capacity of " + 
                towingCapacity;
                return information;
            }
        }