public static void main(String[], args) throws FileNotFoundException should be y
ID: 3676405 • Letter: P
Question
public static void main(String[], args) throws FileNotFoundException
should be your main method to deal with the possibility that the file
is not found. It can have any name.
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 4ARE 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. can someone please help me with this. In java CONSOLE, NOT TO A WINDOW
Explanation / Answer
// Vehicle.java as parent class
class Vehicle
{
//data members declared as private
private String ownerName;
private String address;
private String phone;
private String email;
//parameterized constructor of vechicle class
public Vehicle(String ownerName, String address, String phone, String email)
{
this.ownerName = ownerName;
this.address= address;
this.phone = phone;
this.email = email;
}
//getter and setters for data members
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;
}
//toString(0 returns String type contains data member values of a vehicle class
public String toString()
{
return "Owner's name : " + ownerName + " " +
"Address : " + address + " " +
"Phone : " + phone + " " +
"Email : " + email;
}
}
//Car is child class inherits from vechile
class Car extends Vehicle
{
//datamembers intilation as private
private boolean convertible;
private String color;
//parameterized constructor of Car class
public Car(String ownerName, String address, String phone, String email, boolean convertible, String color)
{
//calling Vechicle class constructor using super
super(ownerName, address, phone, email);
this.convertible = convertible;
this.color = color;
}
//getters and setters for convertible and getcolor data members of car
public boolean getConvertible()
{
return convertible;
}
public void setConvertible(boolean convertible)
{
this.convertible = convertible;
}
public String getColor()
{
return color;
}
public void setColor(String color)
{
this.color = color;
}
//toString() method returns String type And over rides parent class is Vehicle
public String toString()
{
return super.toString() + " " +
"Convertible : " + convertible + " " +
"Color : " + color;
}
}//end car class
/*AmericanCar inherits Car and Car inherits vechile .i.e multilevel inhertience between vechile and AmericanCar*/
class AmericanCar extends Car
{
//Adding two data members
private boolean madeDetroit;
private boolean unionShop;
//parameterized constructor of American car
public AmericanCar(String ownerName, String address, String phone, String email, boolean convertible, String color, boolean madeDetroit, boolean unionShop)
{
//calling car class parameterized constructor
super(ownerName, address, phone, email, convertible, color);
this.madeDetroit = madeDetroit;
this.unionShop = unionShop;
}
//getter and setters for madeDetroit, unionShop data members
public boolean getMadeDetroit()
{
return madeDetroit;
}
public void setMadeDetroit(boolean madeDetroit)
{
this.madeDetroit = madeDetroit;
}
public boolean getUnionShop()
{
return unionShop;
}
public void setUnionShop(boolean unionShop)
{
this.unionShop = unionShop;
}
//overriding super class toString() and returns adding with newly added variables
public String toString()
{
return super.toString() + " " +
"Made in Detroit : " + madeDetroit + " " +
"Union shop : " + unionShop;
}
}
//child class ForeignCar inherits from Car parent class
class ForeignCar extends Car
{
// ForeignCar data members intilization
private String manufacturerCountry;
private float importDuty;
//parameterized constructor for ForeignCar
public ForeignCar(String ownerName, String address, String phone, String email, boolean convertible, String color, String manufacturerCountry, float importDuty)
{
super(ownerName, address, phone, email, convertible, color);
this.manufacturerCountry = manufacturerCountry;
this.importDuty = importDuty;
}
//getters and setters for manufacturerCountry, importDuty
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;
}
//toString() overrides parent class car
public String toString()
{
return super.toString() + " " +
"Manufacturer country : " + manufacturerCountry + " " +
"Import duty : " + importDuty;
}
}
//child class Bicycle inherits from vehicle parent class
class Bicycle extends Vehicle
{
//initialize data member as private
private int numberSpeeds;
//parameter constructor of Bicycle
public Bicycle(String ownerName, String address, String phone, String email, int numberSpeeds)
{
super(ownerName, address, phone, email);
this.numberSpeeds = numberSpeeds;
}
//getter and setter of numberspeeds data member
public int getNumberSpeeds()
{
return numberSpeeds;
}
public void setNumberSpeeds(int numberSpeeds)
{
this.numberSpeeds = numberSpeeds;
}
//overriding toString method() return String type
public String toString()
{
return super.toString() + " " +
"Number of speeds : " + numberSpeeds;
}
}
//child class Truck inherits parent class Vechicle
class Truck extends Vehicle
{
private float numberTons;
private float truckCost;
private String datePurchased;
//parameter constructor of Truck
public Truck(String ownerName, String address, String phone, String email, float numberTons, float truckCost, String datePurchased)
{
super(ownerName, address, phone, email);
this.numberTons = numberTons;
this.truckCost = truckCost;
this.datePurchased = datePurchased;
}
//getters and setter for Truck data members
public float getNumberTons()
{
return numberTons;
}
public void setNumberTons(float numberTons)
{
this.numberTons = numberTons;
}
public float getTruckCost()
{
return truckCost;
}
public void setTruckCost(float truckCost)
{
this.truckCost = truckCost;
}
public String getDatePurchased()
{
return datePurchased;
}
public void setDatePurchased(String datePurchased)
{
this.datePurchased = datePurchased;
}
//toString() override superclass method
public String toString()
{
return super.toString() + " " +
"Number of tons : " + numberTons + " " +
"Truck cost : " + truckCost + " " +
"Date purchased : " + datePurchased;
}
}
// VehicleTest.java
import java.io.*;
import java.util.*;
public class VehicleTest
{
public static void main(String[] args) throws IOException
{
//creating arraylist for vechicle class
ArrayList<Vehicle> vehicles = new ArrayList<Vehicle>();
FileInputStream in = new FileInputStream("vehicles.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
String ownerName, address, phone, email, color, manufacturerCountry, datePurchased;
boolean convertible, madeDetroit, unionShop;
float importDuty, numberTons, truckCost;
int numberSpeeds;
try
{
while((line = br.readLine()) != null)
{
ownerName = br.readLine();
address = br.readLine();
phone = br.readLine();
email = br.readLine();
if (line.equals("vehicle"))
{
vehicles.add(new Vehicle(ownerName, address, phone, email));
}
else if (line.equals("car"))
{
convertible = Boolean.parseBoolean(br.readLine());
color = br.readLine();
vehicles.add(new Car(ownerName, address, phone, email, convertible, color));
}
else if (line.equals("american car"))
{
convertible = Boolean.parseBoolean(br.readLine());
color = br.readLine();
madeDetroit = Boolean.parseBoolean(br.readLine());
unionShop = Boolean.parseBoolean(br.readLine());
vehicles.add(new AmericanCar(ownerName, address, phone, email, convertible, color, madeDetroit, unionShop));
}
else if (line.equals("foreign car"))
{
convertible = Boolean.parseBoolean(br.readLine());
color = br.readLine();
manufacturerCountry = br.readLine();
importDuty = Float.parseFloat(br.readLine());
vehicles.add(new ForeignCar(ownerName, address, phone, email, convertible, color, manufacturerCountry, importDuty));
}
else if (line.equals("bicycle"))
{
numberSpeeds = Integer.parseInt(br.readLine());
vehicles.add(new Bicycle(ownerName, address, phone, email, numberSpeeds));
}
else if (line.equals("truck"))
{
numberTons = Float.parseFloat(br.readLine());
truckCost = Float.parseFloat(br.readLine());
datePurchased = br.readLine();
vehicles.add(new Truck(ownerName, address, phone, email, numberTons, truckCost, datePurchased));
}
br.readLine();
}
}
catch(Exception e)
{
System.out.println(e);
return;
}
finally
{
br.close();
in.close();
}
// convert to an array
Vehicle[] va = new Vehicle[vehicles.size()];
va = vehicles.toArray(va);
br = new BufferedReader(new InputStreamReader(System.in));
while (true)
{
System.out.println(" Menu");
System.out.println(" ---- ");
System.out.println("1. Print all records. ");
System.out.println("2. Sort records by email address. ");
System.out.println("3. Print number of records. ");
System.out.println("4. Print bicycle and truck records. ");
System.out.println("5. Print vehicles in area code 987. ");
int choice = 0;
try
{
do
{
System.out.print("Your choice 1-5 or stop to close: ");
String input = br.readLine();
if (input.toLowerCase().equals("stop")) return;
choice = Integer.parseInt(input);
}
while(choice < 1 || choice > 5);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println();
switch(choice)
{
case 1:
printAll(va);
break;
case 2:
sort(va, true);
break;
case 3:
printNumberRecords(va);
break;
case 4:
printBicyclesAndTrucks(va);
break;
case 5:
printAreaCode987(va);
break;
}
}
}
private static void printAll(Vehicle[] va)
{
for(int i = 0; i < va.length; i++)
{
System.out.println("Vehicle type : " + va[i].getClass().getName());
System.out.println(va[i].toString());
System.out.println();
}
}
private static void sort(Vehicle[] va, boolean print)
{
Vehicle temp;
for(int i = 0;i < va.length - 1;i++)
{
for(int j = i + 1; j < va.length;j++)
{
if(va[j].getEmail().compareTo(va[i].getEmail()) < 0)
{
temp = va[j];
va[j] = va[i];
va[i] = temp;
}
}
}
if (print) printAll(va);
}
private static void printNumberRecords(Vehicle[] va)
{
System.out.println("The number of records is " + va.length);
System.out.println();
}
private static void printBicyclesAndTrucks(Vehicle[] va)
{
sort(va, false); // make sure sorted first
for(int i = 0; i < va.length; i++)
{
String typeName = va[i].getClass().getName();
if (typeName.equals("Bicycle") || typeName.equals("Truck"))
{
System.out.println("Vehicle type : " + typeName);
System.out.println(va[i].toString());
System.out.println();
}
}
}
private static void printAreaCode987(Vehicle[] va)
{
for(int i = 0; i < va.length; i++)
{
if (va[i].getPhone().startsWith("(987)", 0))
{
System.out.println("Vehicle type : " + va[i].getClass().getName());
System.out.println(va[i].toString());
System.out.println();
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.