Design classes vehicle, car, american_car, foreign_car, truck, and bicycle with
ID: 3841226 • Letter: D
Question
Design classes vehicle, car, american_car, foreign_car, truck, and bicycle with ALL appropriate inheritances. vehicle is the parent because everything is a vehicle or a member of a descendant of vehicle. The design of the classes is VERY important. Each of these classes will be in the file with your nnumber class that has the main() method. DESIGN, DESIGN, DESIGN. Your code must override the toString method in each class to display ALL the relevant information from the record. Also design an application class (your nnumber class with a main) that tests your classes and processes a file of records. The file of data to be used will be constructed by me with the following format used (examples are here): I WILL USE NOTEPAD TO CONSTRUCT THE FILE. TEST WITH A FILE FROM NOTEPAD. vehicle owner's name (string) address (string) phone (string) email (string) car owner's name (string) address (string) phone (string) email (string) true or false for convertible (boolean) color (string) american car owner's name (string) address (string) phone (string) email (string) true or false for convertible (boolean) color (string) true or false for made in Detroit (boolean) true or false for union shop (boolean) foreign car owner's name (string) address (string) phone (string) email (string) true or false for convertible (boolean) color (string) country of manufacturer (string) import duty (float) bicycle owner's name (string) address (string) phone (string) email (string) # of speeds (int) truck owner's name (string) address (string) phone (string) email (string) # of tons (float) cost of truck (float) date purchased (format below in exmample) etc.....these records can appear in any order and there can be up to 200 of them. Records will have a blank line between them. You will need to use an array of vehicle to store the data. Here are some examples of data: foreign car aMarioy Mario's house (777) 777-7777 gmario@mario.com false black Italy 4415.91 truck aDougy Doug's house (123) 456-7890 hdoug@doug.com 30 61234.56 8/10/2003 vehicle aRobby Rob's house (987) 654-3210 irob@rob.com bicycle bTommy Tom's house (246) 810-1214 jtom@tom.com 7 truck bGeorge George's house (666) 666-6666 kgeorge@george.com 25 51234.56 12/4/2004 vehicle bTim Tim's house (111) 111-1111 tim@tim.com bicycle bJim Jim's house (555) 555-5555 Ajim@jim.com 5 american car bJohn John's house (888) 888-8888 Bjohn@john.com true green false true car cKen Ken's house (999) 999-9999 Cken@ken.com false orange foreign car cMario Mario's house (777) 777-7777 Dmario@mario.com false black Italy 4415.91 truck zDoug Doug's house (123) 456-7890 Edoug@doug.com 30 61234.56 8/10/2003 vehicle eRob Rob's house (987) 654-3210 Frob@rob.com bicycle fTom Tom's house (246) 810-1214 Gtom@tom.com 7 american car gSam Sam's house (333) 333-3333 Hsam@sam.com false blue true false Write an application class (your nnumber with a main) that reads a file (from the command line) and fills an array of type vehicle[] with new vehicle (params), new car (params), new american car (params) new foreign car(params) , new truck (params), new bicycle (params), etc.: the params depend on the first line that identifies each record. params is just a shorthand name for parameter list (the arguments to a method.) To get the file , in jGrasp you must click on the tab file/check run args, and then type the name of the file in the box at the top. I will test your program with my own file! You must not type in the name of the file in your code because it is only specified at run time. The name of the file in your code will be args[0] when you use public static void main(String[], args) throws FileNotFoundException . Because the input comes from the file instead of the keyboard you should be able to modify Scanner to deal with wrapping! Scanner x = new Scanner(new File(args[0])). Google "java scanner" to learn about Scanner and/or "java command line" to learn about args[0]. This information is located on pages 478-480 of Liang's 10th Edition. Print the output from each of the following calls: 1. Call a printAll method that can be passed an array of type vehicle[] and which prints each element of the array using the appropriate toString() methods. ArrayList is fine if you wish to use it. 2. Call a sort method that can be passed an array of type vehicle[] and which sorts the array by email addresses and prints the new sorted array using appropriate toString() methods. Any sort method is fine, but it should sort according to unicode (case sensitive, that is to say that all upper case is before any lower case)! 3. Call a method that prints the number of records. 4. Call a method that prints just the bicycles and trucks (from the sorted array using the appropriate toString() methods). 5. Call a method that prints the vehicles in area code 987. THERE ARE NO PROMPTS. JUST RUN THE PROGRAM. Be sure to declare variables as private, to extend all the classes appropriately, and to have the right constructors (using super where appropriate), and the getters and setters for ALL the variables. MUST SEND ALL THE OUTPUT FROM PRINTING TO THE CONSOLE, NOT TO A WINDOW.
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class VehicleApplication {
public static void main(String[] args) {
Vehicle[] vehicles = new Vehicle[200];
int numOfVehicles = 0;
if(args.length==0){
System.out.println("Please provide input file as command line argument");
return;
}
String fileName = args[0];
System.out.println(fileName);
try{
Scanner sc = new Scanner(new java.io.File(fileName));
while(sc.hasNext()){
String line = sc.nextLine();
System.out.println("line:"+line);
if(line.contains("vehicle")){
vehicles[numOfVehicles] = new Vehicle(sc.nextLine(), sc.nextLine(), sc.nextLine(), sc.nextLine());
System.out.println(vehicles[numOfVehicles]);
numOfVehicles++;
}
else if(line.contains("truck")){
vehicles[numOfVehicles] = new Truck(sc.nextLine(), sc.nextLine(), sc.nextLine(), sc.nextLine(), sc.nextFloat(), sc.nextFloat(), sc.next());
System.out.println(vehicles[numOfVehicles]);
numOfVehicles++;
}
else if(line.contains("car")){
vehicles[numOfVehicles] = new Car(sc.nextLine(), sc.nextLine(), sc.nextLine(), sc.nextLine(), Boolean.parseBoolean(sc.next()),sc.next());
System.out.println(vehicles[numOfVehicles]);
numOfVehicles++;
}
else if(line.contains("american")){
vehicles[numOfVehicles] = new AmericanCar(sc.nextLine(), sc.nextLine(), sc.nextLine(), sc.nextLine(), Boolean.parseBoolean(sc.next()),sc.next(),Boolean.parseBoolean(sc.next()),Boolean.parseBoolean(sc.next()));
System.out.println(vehicles[numOfVehicles]);
numOfVehicles++;
}
else if(line.contains("foreign")){
vehicles[numOfVehicles] = new ForeignCar(sc.nextLine(), sc.nextLine(), sc.nextLine(), sc.nextLine(), Boolean.parseBoolean(sc.next()),sc.next(),sc.next(),sc.nextFloat());
System.out.println(vehicles[numOfVehicles]);
numOfVehicles++;
}
else if(line.contains("bycycle")){
vehicles[numOfVehicles] = new Bicycle(sc.nextLine(), sc.nextLine(), sc.nextLine(),sc.next(),sc.nextInt());
System.out.println(vehicles[numOfVehicles]);
numOfVehicles++;
}
}
}
catch(FileNotFoundException fnotf){
fnotf.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
}
public class Truck extends Vehicle {
private float numOfTons;
private float costOfTruck;
private String purchaseDate;
public Truck(String name,String add,String ph,String email,float tons,float cost,String date) {
super(name, add, ph, email);
numOfTons = tons;
costOfTruck = cost;
purchaseDate = date;
}
public Truck() {
}
public float getNumOfTons() {
return numOfTons;
}
public void setNumOfTons(float numOfTons) {
this.numOfTons = numOfTons;
}
public float getCostOfTruck() {
return costOfTruck;
}
public void setCostOfTruck(float costOfTruck) {
this.costOfTruck = costOfTruck;
}
public String getPurchaseDate() {
return purchaseDate;
}
public void setPurchaseDate(String purchaseDate) {
this.purchaseDate = purchaseDate;
}
public String toString(){
return super.toString()+"# of Tons : "+numOfTons+" Cost of Truck : "+costOfTruck+" Purchase Date"+purchaseDate+" ";
}
}
public class Bicycle extends Vehicle {
private int numOfSpeed;
public Bicycle(String name,String add,String ph,String email,int speed) {
super(name, add, ph, email);
numOfSpeed = speed;
}
public Bicycle() {
}
public int getNumOfSpeed() {
return numOfSpeed;
}
public void setNumOfSpeed(int numOfSpeed) {
this.numOfSpeed = numOfSpeed;
}
public String toString(){
return super.toString()+"# of Speed : "+numOfSpeed+" ";
}
}
public class ForeignCar extends Car {
private String manufacturerCountry;
private float importDuty;
public ForeignCar(String name,String add,String ph,String email ,boolean con,String col,String contry,float duty) {
super(name, add, ph, email, con, col);
manufacturerCountry = contry;
importDuty = duty;
}
public ForeignCar() {
}
public String getManufacturerCountry() {
return manufacturerCountry;
}
public void setManufacturerCountry(String manufacturerCountry) {
this.manufacturerCountry = manufacturerCountry;
}
public float getImportDuty() {
return importDuty;
}
public void setImportDuty(float importDuty) {
this.importDuty = importDuty;
}
public String toString(){
return super.toString()+"Manufacturer Contry : "+manufacturerCountry+" Import Duty : "+importDuty+" ";
}
}
public class AmericanCar extends Car {
private boolean madeInDetroit;
private boolean unionShop;
public AmericanCar(String name,String add,String ph,String email ,boolean con,String col,boolean madein,boolean unshp) {
super(name, add, ph, email, con, col);
madeInDetroit = madein;
unionShop = unshp;
}
public AmericanCar() {
}
public boolean isMadeInDetroit() {
return madeInDetroit;
}
public void setMadeInDetroit(boolean madeInDetroit) {
this.madeInDetroit = madeInDetroit;
}
public boolean isUnionShop() {
return unionShop;
}
public void setUnionShop(boolean unionShop) {
this.unionShop = unionShop;
}
public String toString(){
return super.toString()+"Made in Detroit : "+madeInDetroit+" Union Shop : "+unionShop+" ";
}
}
public class Car extends Vehicle {
private boolean convertible;
private String color;
Car(String name,String add,String ph,String email ,boolean con,String col){
super(name, add, ph, email);
convertible = con;
color = col;
}
public Car() {
}
public boolean isConvertible() {
return convertible;
}
public void setConvertible(boolean convertible) {
this.convertible = convertible;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String toString(){
return super.toString()+"Convertible : "+convertible+" Color:"+color+" ";
}
}
import javax.jws.Oneway;
public class Vehicle {
private String ownerName;
private String address;
private String phone;
private String email;
public Vehicle(String name,String add,String ph,String email) {
ownerName = name;
address = add;
phone = ph;
this.email = email;
}
public Vehicle() {
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString(){
return "Owner's Name : "+ownerName+" Address : "+address+" Phone : "+phone+" Email : "+email+" ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.