SlideShare a Scribd company logo
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. Please also
see pages 478-480 of Liang's 10th Edition.
http://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html BE SURE AND
PUT YOUR NAME IN A COMMENT AT THE TOP OF YOUR CODE. The program that you
turn in will always be in a single file named unfid.java where unfid is YOUR unf id. So , I
would name my file n00009873.java . This implies that the name of the class containing main
must be n00009873. So , in general, my class containing main will be named n00009873 and
the file containing all my classes will be named n00009873.java 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.
Solution
Here is the code for the given scenario:
class Vehicle
{
private String ownerName;
private String address;
private String phone;
private String email;
public Vehicle(String ownerName, String address, String phone, String email)
{
this.ownerName = ownerName;
this.address= address;
this.phone = phone;
this.email = email;
}
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;
}
}
class Car extends Vehicle
{
private boolean convertible;
private String color;
public Car(String ownerName, String address, String phone, String email, boolean convertible,
String color)
{
super(ownerName, address, phone, email);
this.convertible = convertible;
this.color = color;
}
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;
}
public String toString()
{
return super.toString() + "  " +
"Convertible : " + convertible + "  " +
"Color : " + color;
}
}
class AmericanCar extends Car
{
private boolean madeDetroit;
private boolean unionShop;
public AmericanCar(String ownerName, String address, String phone, String email, boolean
convertible, String color, boolean madeDetroit, boolean unionShop)
{
super(ownerName, address, phone, email, convertible, color);
this.madeDetroit = madeDetroit;
this.unionShop = unionShop;
}
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;
}
public String toString()
{
return super.toString() + "  " +
"Made in Detroit : " + madeDetroit + "  " +
"Union shop : " + unionShop;
}
}
class ForeignCar extends Car
{
private String manufacturerCountry;
private float importDuty;
public String getManufacturerCountry()
{
return manufacturerCountry;
}
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;
}
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 country : " + manufacturerCountry + "  " +
"Import duty : " + importDuty;
}
}
class Bicycle extends Vehicle
{
private int numberSpeeds;
public Bicycle(String ownerName, String address, String phone, String email, int
numberSpeeds)
{
super(ownerName, address, phone, email);
this.numberSpeeds = numberSpeeds;
}
public int getNumberSpeeds()
{
return numberSpeeds;
}
public void setNumberSpeeds(int numberSpeeds)
{
this.numberSpeeds = numberSpeeds;
}
public String toString()
{
return super.toString() + "  " +
"Number of speeds : " + numberSpeeds;
}
}
class Truck extends Vehicle
{
private float numberTons;
private float truckCost;
private String datePurchased;
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;
}
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;
}
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
{
ArrayList vehicles = new ArrayList();
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();
}
}
}
}

More Related Content

DOCX
#include iostream#include string#include iomanip#inclu.docx
PDF
Goal The goal of this assignment is to help students understand the.pdf
PDF
ORM in Django
PPT
Chapter 08
PPTX
C_Progragramming_language_Tutorial_ppt_f.pptx
PDF
For the following questions, you will implement the data structure to.pdf
DOCX
#include iostream #include string #include fstream std.docx
PPT
Clean code _v2003
#include iostream#include string#include iomanip#inclu.docx
Goal The goal of this assignment is to help students understand the.pdf
ORM in Django
Chapter 08
C_Progragramming_language_Tutorial_ppt_f.pptx
For the following questions, you will implement the data structure to.pdf
#include iostream #include string #include fstream std.docx
Clean code _v2003

Similar to public static void main(String[], args) throws FileNotFoundExcepti.pdf (20)

DOC
Cosc 1436 java programming/tutorialoutlet
PPTX
Introduction to c
PDF
Assignment DIn Problem D1 we will use a file to contain the dat.pdf
PPT
Effective Java - Always override toString() method
PDF
Introduction To Fortran 95 And Numerical Computing A Jumpstart For Scientists...
PPTX
C programming language tutorial
PPTX
Software Construction Assignment Help
DOCX
#include iostream #include string#includeiomanip using.docx
PDF
Introduction To Scala
PPTX
Data Types & Objects .pptx
ODT
PPTX
Annotation processor and compiler plugin
PPT
Object Oriented Programming with Java
PDF
You are given a specification for some Java classes as follows.  A.pdf
PPS
PHP Built-in String Validation Functions
PPTX
Computer Network Assignment Help
DOCX
(1) Learn to create class structure in C++(2) Create an array of.docx
PDF
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
PDF
C#i need help creating the instance of stream reader to read from .pdf
PPT
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Cosc 1436 java programming/tutorialoutlet
Introduction to c
Assignment DIn Problem D1 we will use a file to contain the dat.pdf
Effective Java - Always override toString() method
Introduction To Fortran 95 And Numerical Computing A Jumpstart For Scientists...
C programming language tutorial
Software Construction Assignment Help
#include iostream #include string#includeiomanip using.docx
Introduction To Scala
Data Types & Objects .pptx
Annotation processor and compiler plugin
Object Oriented Programming with Java
You are given a specification for some Java classes as follows.  A.pdf
PHP Built-in String Validation Functions
Computer Network Assignment Help
(1) Learn to create class structure in C++(2) Create an array of.docx
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
C#i need help creating the instance of stream reader to read from .pdf
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ad

More from fms12345 (20)

PDF
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdf
PDF
Describe the procedure you would use in the laboratory to determine .pdf
PDF
CHEM 1011 Discussion question 2ObjectiveTo learn more about the .pdf
PDF
Could you implement this please. I was told to use pointers as the d.pdf
PDF
A piece of malware is running on a Windows 7 machine via process inj.pdf
PDF
Company names Aerial Drones Surveillance Inc. Your company descript.pdf
PDF
2. The Lorenz curve measures inequality in person income distribution.pdf
PDF
13 808 PM docs.google.com Covalent Bonding and lonic Bonding study.pdf
PDF
1.The shrimping industry needs female shrimp for production purposes.pdf
PDF
Who are the stakeholders in an income statement and whySolution.pdf
PDF
Which company maintains natural habitats while B allowing us to live .pdf
PDF
When multiple strains of the same bacterial species are sequenced, w.pdf
PDF
What specialized cells line the inner cavity and move fluids through.pdf
PDF
What does the metaphor meaning for the Iron curtainSolutionIr.pdf
PDF
What are the major developmental milestones between infancy and todd.pdf
PDF
Using the Web or another research tool, search for alternative means.pdf
PDF
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
PDF
TV Guide magazine ran a cover photo for a story emphasizing Oprah Wi.pdf
PDF
Tim Tassopoulos, the chief operating officer for Chick-Fll-A applies .pdf
PDF
The Hydrolysis of the Hydrated Pb ion PboH (aq) + H200SolutionP.pdf
Exercise 1 (10 Points) Define a FixdLenStringList class that encaps.pdf
Describe the procedure you would use in the laboratory to determine .pdf
CHEM 1011 Discussion question 2ObjectiveTo learn more about the .pdf
Could you implement this please. I was told to use pointers as the d.pdf
A piece of malware is running on a Windows 7 machine via process inj.pdf
Company names Aerial Drones Surveillance Inc. Your company descript.pdf
2. The Lorenz curve measures inequality in person income distribution.pdf
13 808 PM docs.google.com Covalent Bonding and lonic Bonding study.pdf
1.The shrimping industry needs female shrimp for production purposes.pdf
Who are the stakeholders in an income statement and whySolution.pdf
Which company maintains natural habitats while B allowing us to live .pdf
When multiple strains of the same bacterial species are sequenced, w.pdf
What specialized cells line the inner cavity and move fluids through.pdf
What does the metaphor meaning for the Iron curtainSolutionIr.pdf
What are the major developmental milestones between infancy and todd.pdf
Using the Web or another research tool, search for alternative means.pdf
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
TV Guide magazine ran a cover photo for a story emphasizing Oprah Wi.pdf
Tim Tassopoulos, the chief operating officer for Chick-Fll-A applies .pdf
The Hydrolysis of the Hydrated Pb ion PboH (aq) + H200SolutionP.pdf
Ad

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
RMMM.pdf make it easy to upload and study
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Paper A Mock Exam 9_ Attempt review.pdf.
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Structure & Organelles in detailed.
RMMM.pdf make it easy to upload and study
Weekly quiz Compilation Jan -July 25.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
01-Introduction-to-Information-Management.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Complications of Minimal Access Surgery at WLH
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Orientation - ARALprogram of Deped to the Parents.pptx
LDMMIA Reiki Yoga Finals Review Spring Summer
Final Presentation General Medicine 03-08-2024.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx

public static void main(String[], args) throws FileNotFoundExcepti.pdf

  • 1. 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. Please also see pages 478-480 of Liang's 10th Edition. http://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html BE SURE AND PUT YOUR NAME IN A COMMENT AT THE TOP OF YOUR CODE. The program that you turn in will always be in a single file named unfid.java where unfid is YOUR unf id. So , I would name my file n00009873.java . This implies that the name of the class containing main must be n00009873. So , in general, my class containing main will be named n00009873 and the file containing all my classes will be named n00009873.java 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-
  • 2. 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. Solution Here is the code for the given scenario: class Vehicle
  • 3. { private String ownerName; private String address; private String phone; private String email; public Vehicle(String ownerName, String address, String phone, String email) { this.ownerName = ownerName; this.address= address; this.phone = phone; this.email = email; } 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; }
  • 4. 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; } } class Car extends Vehicle { private boolean convertible; private String color; public Car(String ownerName, String address, String phone, String email, boolean convertible, String color) { super(ownerName, address, phone, email); this.convertible = convertible; this.color = color; } public boolean getConvertible() { return convertible; } public void setConvertible(boolean convertible) { this.convertible = convertible; } public String getColor()
  • 5. { return color; } public void setColor(String color) { this.color = color; } public String toString() { return super.toString() + " " + "Convertible : " + convertible + " " + "Color : " + color; } } class AmericanCar extends Car { private boolean madeDetroit; private boolean unionShop; public AmericanCar(String ownerName, String address, String phone, String email, boolean convertible, String color, boolean madeDetroit, boolean unionShop) { super(ownerName, address, phone, email, convertible, color); this.madeDetroit = madeDetroit; this.unionShop = unionShop; } public boolean getMadeDetroit() { return madeDetroit; } public void setMadeDetroit(boolean madeDetroit) { this.madeDetroit = madeDetroit; } public boolean getUnionShop() { return unionShop;
  • 6. } public void setUnionShop(boolean unionShop) { this.unionShop = unionShop; } public String toString() { return super.toString() + " " + "Made in Detroit : " + madeDetroit + " " + "Union shop : " + unionShop; } } class ForeignCar extends Car { private String manufacturerCountry; private float importDuty; public String getManufacturerCountry() { return manufacturerCountry; } 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; } public void setManufacturerCountry(String manufacturerCountry) { this.manufacturerCountry = manufacturerCountry; } public float getImportDuty() { return importDuty; } public void setImportDuty(float importDuty)
  • 7. { this.importDuty = importDuty; } public String toString() { return super.toString() + " " + "Manufacturer country : " + manufacturerCountry + " " + "Import duty : " + importDuty; } } class Bicycle extends Vehicle { private int numberSpeeds; public Bicycle(String ownerName, String address, String phone, String email, int numberSpeeds) { super(ownerName, address, phone, email); this.numberSpeeds = numberSpeeds; } public int getNumberSpeeds() { return numberSpeeds; } public void setNumberSpeeds(int numberSpeeds) { this.numberSpeeds = numberSpeeds; } public String toString() { return super.toString() + " " + "Number of speeds : " + numberSpeeds; } } class Truck extends Vehicle { private float numberTons;
  • 8. private float truckCost; private String datePurchased; 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; } 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; } public String toString() {
  • 9. 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 { ArrayList vehicles = new ArrayList(); 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")) {
  • 10. 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();
  • 11. } } 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);
  • 12. } 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(); }
  • 13. } 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(); } }
  • 14. } 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(); } } } }