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

JavaAs1

The document contains Java programs that implement a vehicle class hierarchy with subclasses for Truck, Car, and Motorcycle, each having properties like make, model, year, and fuel type, along with methods for calculating distance and displaying information. It also includes an interface Flyable with implementations for Rocket, Plane, and Helicopter, and two file reading programs that handle exceptions for file not found and empty files. Additionally, a custom exception class FileIsEmptyException is defined for handling empty file scenarios.

Uploaded by

sainathg1002
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)
9 views

JavaAs1

The document contains Java programs that implement a vehicle class hierarchy with subclasses for Truck, Car, and Motorcycle, each having properties like make, model, year, and fuel type, along with methods for calculating distance and displaying information. It also includes an interface Flyable with implementations for Rocket, Plane, and Helicopter, and two file reading programs that handle exceptions for file not found and empty files. Additionally, a custom exception class FileIsEmptyException is defined for handling empty file scenarios.

Uploaded by

sainathg1002
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/ 7

1.Write a Java program to make a vehicle class progressive system.

The base class ought to be


Vehicle, with subclasses Truck, Vehicle and Cruiser. Every subclass ought to have properties like
make, model, year, and fuel type. Carry out strategies for computing eco-friendliness, distance
voyaged, and most extreme speed.

Source code

class Vehicle {

String make;

String model;

int year;

String fuelType;

twofold fuelEfficiency;

twofold maxSpeed;

public Vehicle(String make, String model, int year, String fuelType, twofold fuelEfficiency, twofold
maxSpeed) {

this.make = make;

this.model = model;

this.year = year;

this.fuelType = fuelType;

this.fuelEfficiency = fuelEfficiency;

this.maxSpeed = maxSpeed;

public twofold calculateDistance(double fuelUsed) {

return fuelUsed * fuelEfficiency;

public void displayInfo() {

System.out.println("Vehicle Data: " + year + " " + make + " " + model);
System.out.println("Fuel Type: " + fuelType);

System.out.println("Fuel Effectiveness: " + fuelEfficiency + " miles per gallon");

System.out.println("Max Speed: " + maxSpeed + " mph");

class Truck expands Vehicle {

twofold cargoCapacity;

public Truck(String make, String model, int year, String fuelType, twofold fuelEfficiency, twofold
maxSpeed, twofold cargoCapacity) {

super(make, model, year, fuelType, fuelEfficiency, maxSpeed);

this.cargoCapacity = cargoCapacity;

public void displayInfo() {

super.displayInfo();

System.out.println("Cargo Limit: " + cargoCapacity + " tons");

class Vehicle expands Vehicle {

boolean isElectric;

public Car(String make, String model, int year, String fuelType, twofold fuelEfficiency, twofold
maxSpeed, boolean isElectric) {

super(make, model, year, fuelType, fuelEfficiency, maxSpeed);

this.isElectric = isElectric;

public void displayInfo() {

super.displayInfo();
System.out.println("Is Electric: " + (isElectric ? "Indeed" : "No"));

class Cruiser broadens Vehicle {

boolean hasSidecar;

public Motorcycle(String make, String model, int year, String fuelType, twofold fuelEfficiency,
twofold maxSpeed, boolean hasSidecar) {

super(make, model, year, fuelType, fuelEfficiency, maxSpeed);

this.hasSidecar = hasSidecar;

public void displayInfo() {

super.displayInfo();

System.out.println("Has Sidecar: " + (hasSidecar ? "Indeed" : "No"));

public class VehicleTest {

public static void main(String[] args) {

Truck = new Truck("Ford", "F-150", 2022, "Gas", 20, 120, 3.5);

Vehicle = new Car("Tesla", "Model S", 2023, "Electric", 120, 155, valid);

Bike cruiser = new Motorcycle("Harley-Davidson", "Road 750", 2021, "Gas", 50, 110,
misleading);

System.out.println("Truck Information:");

truck.displayInfo();

System.out.println("Distance went with 10 gallons of fuel: " + truck.calculateDistance(10) + "


miles\n");

System.out.println("Car Information:");
car.displayInfo();

System.out.println("Distance went with 10 gallons of fuel: " + car.calculateDistance(10) + "


miles\n");

System.out.println("Motorcycle Information:");

motorcycle.displayInfo();

System.out.println("Distance went with 10 gallons of fuel: " + motorcycle.calculateDistance(10)


+ " miles\n");

2. Compose a Java program to make a connection point Flyable with a strategy called fly_obj().
Make three classes Rocket, Plane, and Helicopter that carry out the Flyable point of interaction.
Carry out the fly_obj() strategy for every one of the three classes.

interface Flyable {

void fly_obj();//Technique signature for flying items

class Space apparatus carries out Flyable {

public void fly_obj() {

System.out.println("Spacecraft is flying in space!");

class Plane executes Flyable {

public void fly_obj() {

System.out.println("Airplane is flying through the sky!");

3. Compose a Java program to make a technique that peruses a record and tosses a special case in
the event that the document isn't found.
import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class FileReadingApp {

public static void main(String[] args) {

String filePath = "data.txt";//Change this to your document way

attempt {

readFile(filePath);

} get (FileNotFoundException e) {

System.out.println("Error: The document " + filePath + " doesn't exist.");

public static void readFile(String filePath) tosses FileNotFoundException {

Document record = new File(filePath);

if (!file.exists()) {

toss new FileNotFoundException("File not found: " + filePath);

Scanner = new Scanner(file);

while (scanner.hasNextLine()) {

String line = scanner.nextLine();

System.out.println(line);

scanner.close();
}

4.Write a Java program that peruses a document and tosses a special case assuming that the
record is vacant.

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.io.FileNotFoundException;

public class FileProcessor {

public static void main(String[] args) {

String fileLocation = "sample.txt";

attempt {

checkFileForContent(fileLocation);

} get (FileIsEmptyException ex) {

System.out.println(ex.getMessage());

} get (IOException ex) {

ex.printStackTrace();

public static void checkFileForContent(String fileLocation) tosses IOException, FileIsEmptyException


{

bufferedReader = invalid;
attempt {

bufferedReader = new BufferedReader(new FileReader(fileLocation));

String currentLine = bufferedReader.readLine();

if (currentLine == invalid) {

toss new FileIsEmptyException("The document is vacant: " + fileLocation);

} System.out.println("File content:");

while (currentLine != invalid) {

System.out.println(currentLine);

currentLine = bufferedReader.readLine();

} get (FileNotFoundException ex) {

System.out.println("The document couldn't be found: " + fileLocation);

} at last {

on the off chance that (bufferedReader != invalid) {

bufferedReader.close();

class FileIsEmptyException expands Exemption {

public FileIsEmptyException(String message) {

super(message);

You might also like