Question

create an array of Vehicle references. Within the application, you assign Sailboat objects and Bicycle objects...

create an array of Vehicle references. Within the application, you assign Sailboat objects and Bicycle objects to the same array. Then, because the different object types are stored in the same array, you can easily manipulate them by using a for loop.

1. Open a new file, and then enter the following first few lines of the VehicleDatabase program:

import javax.swing.*;

public class VehicleDatabase {

public static void main(String[] args) {

2. Create the following array of five Vehicle references and an integer subscript to use with the array:

Vehicle[] vehicles = new Vehicle[5];

int x;

3. Enter the following for loop that prompts you to select whether to enter a sailboat or a bicycle in the array. Based on user input, instantiate the appropriate object type.

for(x = 0; x < vehicles.length; ++x) {

String userEntry;

int vehicleType;

userEntry = JOptionPane.showInputDialog(null,

"Please select the type of\n " + "vehicle you want to enter: \n1 - Sailboat\n" + " 2 - Bicycle");

vehicleType = Integer.parseInt(userEntry);

if(vehicleType == 1)

vehicles[x] = new Sailboat();

else

vehicles[x] = new Bicycle();

}

4. After entering the information for each vehicle, display the array contents by typing the following code. First, create a StringBuffer to hold the list of vehicles. Then, in a for loop, build an output String by repeatedly adding a newline character, a counter, and a vehicle from the array to the StringBuffer object. Display the constructed StringBuffer in a dialog box. Then type the closing curly braces for the main() method and the class:

StringBuffer outString = new StringBuffer();

for(x = 0; x < vehicles.length; ++x) {

outString.append("\n#" + (x + 1) + " ");

outString.append(vehicles[x].toString());

}

JOptionPane.showMessageDialog(null, "Our available Vehicles include:\n" + outString);

}

}

5. Save the file as VehicleDatabase.java, and then compile it. Run the application, entering five objects of your choice.

when you run it it should show a message like

#1 The bicycle is powered by a person; it has 2 wheels and costs $2400

#2 The 22 foot sailboat is powered by wind; it has 0 wheels and costs $35000

#3 The 26 foot sailboat is powered by wind; it has 0 wheels and costs $48500

#4 The bicycle is powered by a person; it has 2 wheels and costs $1525

#5 The bicycle is powered by a person; it has 2 wheels and costs $750

Homework Answers

Answer #1

I have created three classes: Vehicle(Parent class), Sailboat and Bicycle as child classes so that Sailboat and Bicycle objects can be storeed in Vehicle array.

You can add additional attributes to these classes. These classes will help your existing code to work.

Note: You may have to add the package declaration n these classes as per you directory structure. Main class is just for testing purpose.

Vehicle.java

public class Vehicle {

    protected String brand;
    protected int seatingCapacity;
    protected String vehicleName;
}

Sailboat.java

public class Sailboat extends Vehicle{

    public Sailboat(){
        vehicleName = "Tailwinds";
        brand = "Abc";
        seatingCapacity = 5;
    }

    @Override
    public String toString() {
        return "Sailboat: name - " + vehicleName + ", brand - " + brand + ", seating capacity - " + seatingCapacity;
    }
}

Bicycle.java

public class Bicycle extends Vehicle{

    public Bicycle() {
        vehicleName = "The Beast";
        brand = "Xyz";
        seatingCapacity = 2;
    }

    @Override
    public String toString() {
        return "Bicycle: name - " + vehicleName + ", brand - " + brand + ", seating capacity - " + seatingCapacity;
    }
}

Main.java:

public class Main {

    public static void main(String[] args) throws Exception {

        Vehicle[] vehicles = new Vehicle[2];
        Sailboat s = new Sailboat();
        Bicycle b = new Bicycle();
        vehicles[0] = s;
        vehicles[1] = b;
        for(Vehicle v : vehicles){
            System.out.println(v.toString());
        }
    }
}

Output:

i hope it helps..

If you have any doubts please comment and please don't dislike.

PLEASE GIVE ME A LIKE. ITS VERY IMPORTANT FOR ME

Know the answer?
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
Similar Questions
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January - December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs to adjust the...
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs to adjust the...
Create an application to accept data for an array of five CertOfDeposit objects, and then display...
Create an application to accept data for an array of five CertOfDeposit objects, and then display the data. Use these classes and this code template to solve the problem: import java.time.*; public class CertOfDeposit {     private String certNum;     private String lastName;     private double balance;     private LocalDate issueDate;     private LocalDate maturityDate;     public CertOfDeposit(String num, String name, double bal, LocalDate issue) {     }     public void setCertNum(String n) {     }     public void setName(String name) {     }     public void setBalance(double bal) {     }     public...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
USING C++ Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Create your objects in the stack...
USING C++ Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Create your objects in the stack (not on the heap). Add a friend function, kilotopound, which will convert kilograms to pounds. Change your weight mutator to ask whether weight is input in kilograms or pounds. If it is kilograms, call the friend function kilotopound to convert it to pounds and return pounds. There are 2.2 pounds in one kilogram. Create an object on the stack with the following information:     ...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
The Power Set of a set of (distinct) objects S is the set of all subsets...
The Power Set of a set of (distinct) objects S is the set of all subsets of S, including S itself and an empty set. For example, let S be a set of words: S={Hello, World, Students}. Then possible subsets are: {Hello} {World} {Students} {Hello, World} {Hello, Students} {World, Students} {Hello, World, Students} From a course in Discrete Mathematics, you should know that if S contains n elements, then its power set consists of 2^n elements. Please write a recursive...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes a contestant’s name, talent code, and talent description. The competition has become so popular that separate contests with differing entry fees have been established for children, teenagers, and adults. Modify the Contestant class to contain a field that holds the entry fee for each category, and add get and set accessors. Extend the Contestant class to create three subclasses: ChildContestant, TeenContestant, and AdultContestant. Child...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT