0% found this document useful (0 votes)
3 views5 pages

JAVA Assignment 12

Uploaded by

gtoworld00
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
3 views5 pages

JAVA Assignment 12

Uploaded by

gtoworld00
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Assignment – 12

13-03-2024
(Inheritance)
NAME – DIPANJAN SAHOO
ROLL NO – 2370155

1. Create a class Vehicle as follows :


Data members (All are private) –[Brand, Country_of_Origin, Base_price]
Methods - input (to input details of vehicle) and display (to show vehicle details).
Create a sub class Car as follows:
Data members – [Model, speed, Market_price]
[NB: Market price of a car can be calculated from the Base price and speed. If speed
is above 80km/hr, market price will be 15% more than the base price otherwise
market price will be 5% less than the base price.]
Methods – read (to input car details) and show (to show car details).
In addition to above methods add more appropriate methods to set the required
data members.
Now create objects. Input required data and show the details (Brand,
Country_of_Origin, Base_price, Model, speed, Market_price) of any car.

Ans –

import java.util.*;
class Vehicle {
private String brand;
private String countryOfOrigin;
private double basePrice;

public Vehicle(String brand, String countryOfOrigin, double basePrice) {


this.brand = brand;
this.countryOfOrigin = countryOfOrigin;
this.basePrice = basePrice;
}
public void input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Brand: ");
brand = scanner.nextLine();
System.out.print("Enter Country of Origin: ");
countryOfOrigin = scanner.nextLine();
System.out.print("Enter Base Price: ");
basePrice = scanner.nextDouble();
}
public void display() {
System.out.println("Brand: " + brand);
System.out.println("Country of Origin: " + countryOfOrigin);
System.out.println("Base Price: $" + basePrice);
}
double bp(){
return basePrice;
}
}

class Car extends Vehicle {


private String model;
private int speed;
private double marketPrice;

Car(String brand, String countryOfOrigin, double basePrice, String model, int speed) {
super(brand, countryOfOrigin, basePrice);
this.model = model;
this.speed = speed;
calculateMarketPrice();
}
private void calculateMarketPrice() {
if (speed > 80) {
marketPrice = bp() * 1.15;
} else {
marketPrice = bp() * 0.95;
}
}

public void read() {


super.input(); // Call parent class input method
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Model: ");
model = scanner.nextLine();
System.out.print("Enter Speed (km/hr): ");
speed = scanner.nextInt();
}

@Override
public void display() {
super.display(); // Call parent class display method
System.out.println("Model: " + model);
System.out.println("Speed: " + speed + " km/hr");
System.out.println("Market Price: $" + marketPrice);
}

public void setModel(String model) {


this.model = model;
}

public void setSpeed(int speed) {


this.speed = speed;
calculateMarketPrice();
}
}

public class Main {


public static void main(String[] args) {
Car car = new Car("", "", 0.0, "", 0);
car.read();
System.out.println("\nCar Details:");
car.display();
}
}

2. Create a class Number :


Data member: An array of type integer.
Constructor: Constructor with one parameter n, that is the size of the array. Allocate
n memory for the array and input n numbers into the array.
Method-1: To display all the values in the array.
Derive a class OddNum from the class Number:
Data member: An array of type integer.
Constructor: To count the odd numbers present in the array of its base class Number
and accordingly allocate memory for its own array.
Method-1: To copy all odd numbers from its base class array to its own array.
Method-2: To display all odd numbers.
Derive a class PrimeNum from the class OddNum:
Data member: An array of type integer.
Constructor: To count the prime numbers present in the array of its base class
OddNum and accordingly allocate memory for its own array.
Method-1: To copy all prime numbers from its base class array to its own array.
Method-2: To display all prime numbers.
Ans –

import java.util.*;

class Number {
int[] arr;

Number(int n) {
arr = new int[n];
Scanner scanner = new Scanner(System.in);
System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
}
void display() {
System.out.println("Numbers:");
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}

class OddNum extends Number {


int[] oddArr;

OddNum(Number num) {
super(num.arr.length);
int oddCount = 0;
for (int n : arr) {
if (n % 2 != 0) {
oddCount++;
}
}
oddArr = new int[oddCount];
copyOddNumbers();
}

void copyOddNumbers() {
int j = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
oddArr[j++] = arr[i];
}
}
}
void displayOddNumbers() {
System.out.println("Odd Numbers:");
for (int num : oddArr) {
System.out.print(num + " ");
}
System.out.println();
}
}

class PrimeNum extends OddNum {


int[] primeArr;

PrimeNum(OddNum oddNum) {
super(oddNum);
int primeCount = 0;
for (int num : oddArr) {
if (isPrime(num)) {
primeCount++;
}
}
primeArr = new int[primeCount];
copyPrimeNumbers();
}
boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

void copyPrimeNumbers() {
int j = 0;
for (int num : oddArr) {
if (isPrime(num)) {
primeArr[j++] = num;
}
}
}
void displayPrimeNumbers() {
System.out.println("Prime Numbers:");
for (int num : primeArr) {
System.out.print(num + " ");
}
System.out.println();
}
}

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = scanner.nextInt();

Number numbers = new Number(n);


numbers.display();

OddNum oddNums = new OddNum(numbers);


oddNums.displayOddNumbers();

PrimeNum primeNums = new PrimeNum(oddNums);


primeNums.displayPrimeNumbers();
}
}

You might also like