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

Set12

The document contains two Java programs: one for performing basic arithmetic operations (addition, subtraction, multiplication, and division) and another for managing motor vehicle details, including a Car class that extends a MotorVehicle class. The arithmetic program takes user input for two numbers and an operation, returning the result, while the vehicle program collects details about a motor vehicle and a car, displaying their information and calculating the final price after applying a discount. Both programs utilize the Scanner class for user input and include error handling for invalid inputs.

Uploaded by

kareena.it222110
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)
2 views

Set12

The document contains two Java programs: one for performing basic arithmetic operations (addition, subtraction, multiplication, and division) and another for managing motor vehicle details, including a Car class that extends a MotorVehicle class. The arithmetic program takes user input for two numbers and an operation, returning the result, while the vehicle program collects details about a motor vehicle and a car, displaying their information and calculating the final price after applying a discount. Both programs utilize the Scanner class for user input and include error handling for invalid inputs.

Uploaded by

kareena.it222110
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

Set12

import java.util.Scanner;

public class ArithmeticOperations {

public static int operation(String num1, String num2, String operation) {

try {

int a = Integer.parseInt(num1);

int b = Integer.parseInt(num2);

switch (operation.toLowerCase()) {

case "add":

return a + b;

case "subtract":

return a - b;

case "multiply":

return a * b;

case "divide":

if (b == 0) {

return Integer.MIN_VALUE; // Division by zero

return a / b; // Integer division (results rounded down)

default:

throw new IllegalArgumentException("Invalid operation");

} catch (ArithmeticException e) {

return Integer.MIN_VALUE; // Handle any ArithmeticException

} catch (Exception e) {

System.out.println("Invalid input or operation");

return 0;

}
}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");

String num1 = scanner.nextLine();

System.out.print("Enter the second number: ");

String num2 = scanner.nextLine();

System.out.print("Enter the operation (add, subtract, multiply, divide): ");

String operation = scanner.nextLine();

int result = operation(num1, num2, operation);

System.out.println("Result: " + result);

scanner.close();

Output

Enter the first number: 1

Enter the second number: 2

Enter the operation (add, subtract, multiply, divide): add

Result: 3

Enter the first number: 4

Enter the second number: 5

Enter the operation (add, subtract, multiply, divide): subtract

Result: -1

Enter the first number: 6

Enter the second number: 3


Enter the operation (add, subtract, multiply, divide): divide

Result: 2

Q2

import java.util.Scanner;

class MotorVehicle {

String modelName;

int modelNumber;

double modelPrice;

// Constructor for MotorVehicle

public MotorVehicle(String modelName, int modelNumber, double modelPrice) {

this.modelName = modelName;

this.modelNumber = modelNumber;

this.modelPrice = modelPrice;

// Method to display motor vehicle details

public void display() {

System.out.println("Model Name: " + modelName);

System.out.println("Model Number: " + modelNumber);

System.out.println("Model Price: " + modelPrice);

class Car extends MotorVehicle {

double discountRate;

// Constructor for Car

public Car(String modelName, int modelNumber, double modelPrice, double discountRate) {


super(modelName, modelNumber, modelPrice);

this.discountRate = discountRate;

// Method to display car details along with discount rate

public void display() {

super.display(); // Call to the parent class display

System.out.println("Discount Rate: " + discountRate + "%");

// Method to compute discount

public double discount() {

return modelPrice - (modelPrice * (discountRate / 100));

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter details for the MotorVehicle:");

System.out.print("Model Name: ");

String modelName = scanner.nextLine();

System.out.print("Model Number: ");

int modelNumber = scanner.nextInt();

System.out.print("Model Price: ");

double modelPrice = scanner.nextDouble();

System.out.println("\nEnter details for the Car:");

System.out.print("Discount Rate (%): ");

double discountRate = scanner.nextDouble();


// Creating an instance of MotorVehicle

MotorVehicle mv = new MotorVehicle(modelName, modelNumber, modelPrice);

System.out.println("\nMotorVehicle Details:");

mv.display();

// Creating an instance of Car

Car car = new Car(modelName, modelNumber, modelPrice, discountRate);

System.out.println("\nCar Details:");

car.display();

double finalPrice = car.discount();

System.out.println("Final Price after discount: " + finalPrice);

scanner.close();

Output

Enter details for the MotorVehicle:

Model Name: Ford Mustang

Model Number: 1234

Model Price: 35000

Enter details for the Car:

Discount Rate (%): 10

MotorVehicle Details:

Model Name: Ford Mustang

Model Number: 1234

Model Price: 35000.0


Car Details:

Model Name: Ford Mustang

Model Number: 1234

Model Price: 35000.0

Discount Rate: 10.0%

Final Price after discount: 31500.0

You might also like