Car Driving Simulator Program Final 2
Car Driving Simulator Program Final 2
Computer Programming 3
MAWD 1-2AP
Members:
Auguis, John Leodrick
Benedicto, Jhunard June
Biadan, Wesel Nicolai
Lingcon, Reavin
Lumanta, Jovien
Ronquillo, Junero Charls
Description
Upon execution, the program checks if the file "vehicles.txt" exists. If the file
is found, it reads the data and creates Car objects for each line in the file. For each
Car object, the program simulates driving by displaying a message indicating the
brand and model of the car being driven. If the file is not found, the program
notifies the user with a "File not found" message. This program serves as an
educational tool for learning Java fundamentals like classes, object
Inheritance: The Car class inherits from the Vehicle class, which means it
inherits its properties and methods.
Start
Check if
"vehicles.txt" exists
If file exists:
End
Program Flow Narrative
1. Start:
The program begins execution.
2. Check File Existence:
It checks if the file "vehicles.txt" exists.
3. File Existence Decision:
If the file exists:
- It reads data from the file.
- For each line in the file:
- It splits the data into brand and model.
- Creates a Car object with the extracted brand
- Drives the Car.
If the file doesn't exist:
- It displays "File not found" message.
4. End:
The program ends.
Source Code
class Car extends Vehicle {
private String model;
public Car(String brand, String model) {
super(brand);
this.model = model;
}
public String getModel() {return model;
}
@Override
public void drive() {
System.out.println("Car " + getBrand() + " " + model + " is being driven.");
}
}
class Vehicle {
private String brand;
public Vehicle(String brand) {
this.brand = brand;
}
public String getBrand() {
return brand;
}
public void drive() {
System.out.println("Vehicle is being driven.");
}
}
mport java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
try {
File file = new File("vehicles.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String[] data = scanner.nextLine().split(",");
if (data.length == 2) {
Car car = new Car(data[0], data[1]);
car.drive();
}}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
Sample Outputs