0% found this document useful (0 votes)
45 views

Car Driving Simulator Program Final 2

Uploaded by

hbg55psg9g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Car Driving Simulator Program Final 2

Uploaded by

hbg55psg9g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Car Driving Simulator Program

Computer Programming 3
MAWD 1-2AP

Members:
Auguis, John Leodrick
Benedicto, Jhunard June
Biadan, Wesel Nicolai
Lingcon, Reavin
Lumanta, Jovien
Ronquillo, Junero Charls
Description

The "Car Driving Simulator Program" is a Java application designed to


simulate driving various cars based on data stored in a text file named
"vehicles.txt". The program utilizes object-oriented programming concepts such as
classes, objects, and methods to model cars and their driving behavior. It also
incorporates file input/output operations for reading data from the text file.

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

ds, inheritance, exception handling, and file operations.


Classes and Objects: Classes are templates for creating objects. In this
program, we have a Vehicle class as the parent and a Car class as a child inheriting
from Vehicle.

Inheritance: The Car class inherits from the Vehicle class, which means it
inherits its properties and methods.

Methods: Methods are functions defined within classes to perform certain


actions. In this program, drive() is a method defined in both the Vehicle and Car
classes to simulate driving.

Exception Handling: The program handles the FileNotFoundException that


might occur if the file "vehicles.txt" is not found.

File Input/Output: The program reads data from a file ("vehicles.txt") to


create Car objects, demonstrating file input/output operations.
Flowchart

Start

Check if
"vehicles.txt" exists

If file exists:

Read data from file

For each line in file:

Split data into brand and model

Create Car object

Drive the Car

If file doesn't exist:

Display "File not found"

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy