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
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
Get Answers For Free
Most questions answered within 1 hours.