Question

How can i print the array in VehicleTest.java with out the hashcode behind the array element?...

How can i print the array in VehicleTest.java with out the hashcode behind the array element?

Vehicle.java

public abstract class Vehicle {
    // class that can describe many vehicle types
    abstract public double GetCarbonFootPrint();
}

Car.java

public class Car extends Vehicle{

    private double gallon;

    public Car()
    {
        this.gallon = 0;// set gallon to 0
    }

    public Car(double gallon)
    {
        this.gallon = gallon;
    }

    @Override
    public double GetCarbonFootPrint() {
        return (this.gallon * 20);// carbon footprint is #gallons*20
    }
}

Bicycle.java

public class Bicycle extends Vehicle{

    @Override
    public double GetCarbonFootPrint() {
        return 0; // carbon footprint is always 0
    }
}

VehicleTest.java

import java.util.Arrays;

public class VehicleTest {

    public static void main(String[]args)
    {
        // array to hold each vehicle
        Vehicle vehicles[] = new Vehicle[2];

        vehicles[0] = new Car(40);
        vehicles[1] = new Bicycle();
        // loop to display results of each vehicles footprint
        for(int i = 0; i < vehicles.length; i++)
        {
            System.out.println("Carbon Footprint for " + vehicles[i] + ": "
                    + String.format("%.2f", vehicles[i].GetCarbonFootPrint()));
        }
    }
}

OUTPUT

Carbon Footprint for Car@1e67b872: 800.00
Carbon Footprint for Bicycle@60addb54: 0.00

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Vehicle.java
public abstract class Vehicle {
    // class that can describe many vehicle types
    abstract public double GetCarbonFootPrint();
}

/////////////////////////////////////////////////////////

//Car.java
public class Car extends Vehicle{

    private double gallon;

    public Car()
    {
        this.gallon = 0;// set gallon to 0
    }

    public Car(double gallon)
    {
        this.gallon = gallon;
    }

    @Override
    public double GetCarbonFootPrint() {
        return (this.gallon * 20);// carbon footprint is #gallons*20
    }

    @Override
    public String toString() {
        return "Car{" +
                "gallon=" + gallon +
                '}';
    }
}

/////////////////////////////////////////////////////////

//Bicycle.java
public class Bicycle extends Vehicle{

    @Override
    public double GetCarbonFootPrint() {
        return 0; // carbon footprint is always 0
    }

    @Override
    public String toString() {
        return "Bicycle{}";
    }
}

/////////////////////////////////////////////////////////

//VehicleTest.java
public class VehicleTest {

    public static void main(String[]args)
    {
        // array to hold each vehicle
        Vehicle vehicles[] = new Vehicle[2];

        vehicles[0] = new Car(40);
        vehicles[1] = new Bicycle();
        // loop to display results of each vehicles footprint
        for(int i = 0; i < vehicles.length; i++)
        {
            System.out.println("Carbon Footprint for " + vehicles[i] + ": "
                    + String.format("%.2f", vehicles[i].GetCarbonFootPrint()));
        }
    }
}

/////////////////////////////////////////////////////////

Add a comment
Know the answer?
Add Answer to:
How can i print the array in VehicleTest.java with out the hashcode behind the array element?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is output? public abstract class People { protected string name; protected int age; public abstract...

    What is output? public abstract class People { protected string name; protected int age; public abstract void PrintInfo(); public void PrintInformation() { System.out.println("In Base Class People"); public class Teacher extends People { private int experience; public void PrintInfo() { System.out.println("In Child Class Teacher"); public class Principal extends Teacher { public void PrintInformation() { System.out.println("In Child Class Principal"); public static void main(String args[]) { Principal tim; tim = new Principal(); tim.PrintInfo(); In Base Class People Error: Compiler error In Child Class...

  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for...

    Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for their inventory management system. The program needs the following classes: A main class (SpaceCarDealer.java) that includes the main method and GUI. Two instantiable classes: CarDealer.java o Variables to store the dealer name, and the carsOnLot array in Car type. o A constructor to initialize the name variable with the given dealer name. o An accessor method to return the name of the dealer. o...

  • I need help in getting the second output to show both the commission rate and the...

    I need help in getting the second output to show both the commission rate and the earnings for the String method output package CompemsationTypes; public class BasePlusCommissionEmployee extends CommissionEmployee { public void setGrossSales(double grossSales) { super.setGrossSales(grossSales); } public double getGrossSales() { return super.getGrossSales(); } public void setCommissionRate(double commissionRate) { super.setCommissionRate(commissionRate); } public double getCommissionRate() { return super.getCommissionRate(); } public String getFirstName() { return super.getFirstName(); } public String getLastName() { return super.getLastName(); } public String getSocialSecurityNumber() { return super.getSocialSecurityNumber(); } private...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • Hello, How can I make the program print out "Invalid data!!" and nothing else if the...

    Hello, How can I make the program print out "Invalid data!!" and nothing else if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero? I need this done by tomorrow if possible please. The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • COVERT TO PSEUDOCODE /****************************Vacation.java*****************************/ public abstract class Vacation {    /*    * private data field...

    COVERT TO PSEUDOCODE /****************************Vacation.java*****************************/ public abstract class Vacation {    /*    * private data field    */    private double cost;    private double budget;    private String destination;    /**    *    * @param cost    * @param budget    * @param destination    */    public Vacation(double cost, double budget, String destination) {        super();        this.cost = cost;        this.budget = budget;        this.destination = destination;    }    //getter and...

  • PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is to...

    PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is too difficult you do not have to do part 3 but it would be greatly appreciated if you do ! Obtain example code files Circle.java, Shape.java, CircleShape2.java, Sphere.java, and CircleShapeApp.java from the downloaded files in Ch8. Compile and execute the example and understand the polymorphism it performs. (I have copied and pasted all of these files below) Modify...

  • This is my code that i need to finish. In BoxRegion.java I have no idea how...

    This is my code that i need to finish. In BoxRegion.java I have no idea how to create the constructor. I tried to use super(x,y) but It is hard to apply. And Also In BoxRegionHashTable, I don't know how to create displayAnnotation BoxRegion.java ------------------------------------------------ public final class BoxRegion { final Point2D p1; final Point2D p2; /** * Create a new 3D point with given x, y and z values * * @param x1, y1 are the x,y coordinates for point...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT