The goal is to create a parent class and use split() with file reading. --------
ID: 3707253 • Letter: T
Question
The goal is to create a parent class and use split() with file reading.
-----------------------------------
Inheritance Stuff!
Vehicle:
This should be a new class with:attributes for:
speed
number of passengers
get methods for both attributes
set methods for both attributes (make sure to verify that attributes won't be negative!)
a toString() method
a default constructor:
sets speed to 0
sets number of passengers to 0
a full parameter constructor:
Remember to verify that attributes aren't negative!
If an input parameter is negative, set to zero instead!
Auto:
Adjust Auto to be a child/derived class of Vehicle:
Adjust the class header
Change the Constructors to correctly use the super() call.
Change the toString() method to correctly generate a String with both Vehicle and Auto attribute information
Bicycle:
Adjust Bicycle to be a child/derived class of Vehicle:
Adjust the class header
Change the Constructors to correctly use the super() call.
Change the toString() method to correctly generate a String with both Vehicle and Bicycle attribute information
----------------------------------------------
File Stuff!
Use you driver to:
Create an ArrayList of Vehicle type
Create Scanner to read from the Vehicles.txt file
In your reading loop:
read a line from the file
break the line into parts using split()
Determine if the line has Auto data or Bicycle data (data element 0 on each line is either "Auto" or "Bicycle")
Build the Auto or Bicycle described by the data:
Data element 1 is the speed of the vehicle
Data element 2 is the number of passengers
Data element 3 is the serialnumber OR licenseplate data
Data element 4 is the permitStatus OR locked status
Generate Color or Brand yourself using the appropriate method call from the appropriate enum
Store the newly create Auto or Bicycle into your ArrayList of Vehicles
After all Vehicles have been created and stored in your ArrayList of Vehicles (Done reading the file), have another loop to print out the data from each Vehicle in your ArrayList (this should test/verify that your toString() changes worked!)
--------------------------------------------
Grading Breakdown:
Vehicle: 20 points
Auto Changes: 20 points
Bicycle Changes: 20 points
Reading from the File: 30 points
ArrayList stuff (10 points)
File I/O stuff (10 points)
split() stuff (10 points)
Printing out the Vehicles in separate loop: 10 points
Make a Java for each java file seen below if necessary and include driver file
Auto.java
Bicycle.java
Brand.java
Color.java
Driver.java
Parkable.java
ParkingLot.Java
ParkingSpace.java
ValidParkingOject.java
Vehicle.java
Vehicles.txt
Explanation / Answer
package January;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
// Class Vehicles definition
class Vehicles
{
// Instance variable to store data
int speed;
int numberOfPassengers;
// Default constructor to initialize instance variables
Vehicles()
{
speed = 0;
numberOfPassengers = 0;
}// End of constructor
// Method to set speed
void setSpeed(int speed)
{
// Checks if the speed is negative set it to zero
if(speed < 0)
speed = 0;
// Otherwise set it the parameter value
else
this.speed = speed;
}// End of method
// Method to set number of passengers
void setNumberOfPassengers(int numberOfPassengers)
{
// Checks if the number of passengers is negative set it to zero
if(numberOfPassengers < 0)
numberOfPassengers = 0;
// Otherwise set it the parameter value
else
this.numberOfPassengers = numberOfPassengers;
}// End of method
// Method to return speed
int getSpeed()
{
return speed;
}// End of method
// Method to return number of passengers
int getNumberOfPassengers()
{
return numberOfPassengers;
}// End of method
// Overrides toString() method to return object information
public String toString()
{
// this.getClass().getSimpleName() returns class name
return " " + this.getClass().getSimpleName() + " Speed: " + this.getSpeed() +
" Number of passengers: " + this.getNumberOfPassengers();
}// End of method
}// End of class
// Class Auto derived from Vehicles class
class Auto extends Vehicles
{
// Instance variable to store data
int licenseplate;
boolean permitStatus;
// Default constructor to initialize instance variables
Auto()
{
// Calls base class constructor
super();
licenseplate = 0;
permitStatus = false;
}// End of constructor
// Method to set license plate number
public void setLicenseplate(int licenseplate)
{
// Checks if the license plate is negative set it to zero
if(licenseplate < 0)
licenseplate = 0;
// Otherwise set it the parameter value
else
this.licenseplate = licenseplate;
}// End of method
// Method to set permit status
public void setPermitStatus(boolean permitStatus)
{
this.permitStatus = permitStatus;
}// End of method
// Method to return license plate number
public int getLicenseplate()
{
return licenseplate;
}// End of method
// Method to return permit status
public boolean getPermitStatus()
{
return permitStatus;
}// End of method
// Overrides toString() method to return object information
public String toString()
{
// Calls base class toString() method and concatenates it with current class information
return super.toString() + " License Number: " + this.getLicenseplate() +
" Permit Status: " + this.getPermitStatus();
}// End of method
}// End of class
//Class Bicycle derived from Vehicles class
class Bicycle extends Vehicles
{
// Instance variable to store data
int serialnumber;
boolean lockedStatus;
// Default constructor to initialize instance variables
Bicycle()
{
// Calls base class constructor
super();
serialnumber = 0;
lockedStatus = false;
}// End of constructor
// Method to set serial number
public void setSerialnumber(int serialnumber)
{
// Checks if the serial number is negative set it to zero
if(serialnumber < 0)
serialnumber = 0;
// Otherwise set it the parameter value
else
this.serialnumber = serialnumber;
}// End of method
// Method to set locked status
public void setLockedStatus(boolean lockedStatus)
{
this.lockedStatus = lockedStatus;
}// End of method
// Method to return serial number
public int getSerialnumber()
{
return serialnumber;
}// End of method
// Method to return locked status
public boolean getLockedStatus()
{
return lockedStatus;
}// End of method
// Overrides toString() method to return object information
public String toString()
{
// Calls base class toString() method and concatenates it with current class information
return super.toString() + " License Number: " + this.getSerialnumber() +
" Permit Status: " + this.getLockedStatus();
}// End of method
}// End of class
// Driver class VehiclesDriver definition
public class VehiclesDriver
{
// main method definition
public static void main(String[] args)
{
// Creates an array list to store Vehicles objects
ArrayList<Vehicles> vehicleType = new ArrayList<Vehicles>();
// Creates an object for both Auto and Bicycle class
Auto auto = new Auto();
Bicycle bicycle = new Bicycle();
// try block begins
try
{
// Scanner class to read file
Scanner fileRead = new Scanner(new File("Vehicles.txt"));
// Loops till not end of the file
while(fileRead.hasNextLine())
{
// Reads one line data from file
String data = fileRead.nextLine();
// Split the data with space delimiter and stores it in array
String splitData[] = data.split(" ");
// Checks the first splitted data is "Auto"
if(splitData[0].equalsIgnoreCase("Auto"))
{
// Adds the split data to auto object using setter method
auto.setSpeed(Integer.parseInt(splitData[1]));
auto.setNumberOfPassengers(Integer.parseInt(splitData[2]));
auto.setLicenseplate(Integer.parseInt(splitData[3]));
auto.setPermitStatus(Boolean.parseBoolean(splitData[4]));
// Adds the auto object to vechcleType array list
vehicleType.add(auto);
}// End of if condition
// Otherwise checks if the first splitted data is "Bicycle"
else if(splitData[0].equalsIgnoreCase("Bicycle"))
{
// Adds the split data to auto object using setter method
bicycle.setSpeed(Integer.parseInt(splitData[1]));
bicycle.setNumberOfPassengers(Integer.parseInt(splitData[2]));
bicycle.setSerialnumber(Integer.parseInt(splitData[3]));
bicycle.setLockedStatus(Boolean.parseBoolean(splitData[4]));
vehicleType.add(bicycle);
// Adds the bicycle object to vechcleType array list
}// End of if condition
// Otherwise invalid vehicle type
else
System.out.println("Invalid Vehicle Type!");
}// End of while loop
// Close the file
fileRead.close();
}// End of try block
// Catch bloc to handle file not found exception
catch(FileNotFoundException fe)
{
fe.printStackTrace();
}// End of catch block
// Loops till the size of the array list
for(int x = 0; x < vehicleType.size(); x++)
// Displays each object information
System.out.println(vehicleType.get(x));
}// End of main method
}// End of class
Sample Output:
Auto
Speed: 17
Number of passengers: 3
License Number: 221245
Permit Status: false
Bicycle
Speed: 11
Number of passengers: 1
License Number: 133311
Permit Status: true
Auto
Speed: 17
Number of passengers: 3
License Number: 221245
Permit Status: false
Auto
Speed: 17
Number of passengers: 3
License Number: 221245
Permit Status: false
Bicycle
Speed: 11
Number of passengers: 1
License Number: 133311
Permit Status: true
Auto
Speed: 17
Number of passengers: 3
License Number: 221245
Permit Status: false
Bicycle
Speed: 11
Number of passengers: 1
License Number: 133311
Permit Status: true
Vehicles.txt file contents
Auto 20 3 111222 true
Bicycle 10 2 123321 false
Auto 60 6 131244 false
Auto 12 1 331789 true
Bicycle 14 3 521341 true
Auto 17 3 221245 false
Bicycle 11 1 133311 true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.