0% found this document useful (0 votes)
23 views4 pages

Lab 2 B

Uploaded by

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

Lab 2 B

Uploaded by

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

Started on Wednesday, 19 June 2024, 6:50 PM

State Finished
Completed on Wednesday, 19 June 2024, 7:38 PM
Time taken 48 mins 8 secs
Marks 20.00/20.00
Grade 100.00 out of 100.00
Name 730422243022 CHANDRU S
Question 1 Write a java program which performs the following.
Correct

Mark 10.00 out


of 10.00 Create a super class called Car. The Car class has the following fields and methods.
int speed;
double regularPrice;
String color;
double getSalePrice();
Create a sub class of Car class and name it as Truck. The Truck class has the following fields and
methods.
int weight;
double getSalePrice(); //If weight>2000,10%discount. Otherwise, 20%discount.
Create a subclass of Car class and name it as Hatchback. The Ford class has the following fields
and methods.
int year;
int manufacturerDiscount;
double getSalePrice();//From the saleprice computed from Car class, subtract the
manufacturer Discount.
Create a subclass of Car class and name it as Sedan. The Sedan class has the following fields and
methods.
int length;
double getSalePrice(); //If length>20 feet, 5% discount, Otherwise,10%discount.
Create MyOwnAutoShop class which contains the main() method. Perform the following within
the main() method.
Create an instance of Sedan class and initialize all the fields with appropriate values.
Create an instance of the Hatchback class and initialize all the fields with appropriate
values.
Create an instance of the Truck class and initialize all the fields with appropriate values.
Create an instance of Car class and initialize all the fields with appropriate values and
Display the sale prices of all instance.

For example:

Input Result

155 85000 Green 2200 Enter Speed,regularPrice, color, weight for Truck
180 120000 White 7 Truck Price 76500.00
175 75000 Grey 1998 Enter Speed,regularPrice, color, length for Sedan
5000 Sedan Price 108000.00
155 50000 Yellow Enter Speed,regularPrice, color, year, manufacturerDiscount for
Hatchback
Hatchback Price 70000.00
Enter Speed,regularPrice, color for Car
Basic Car Price 50000.00

Answer: (penalty regime: 0 %)


1 ▼ import java.util.Scanner;
2 ▼ class Car {
3 int speed;
4 double regularPrice;
5 String color;
6
7 ▼ Car(int speed, double regularPrice, String color) {
8 this.speed = speed;
9 this.regularPrice = regularPrice;
10 this.color = color;
11 }
12
13 ▼ double getSalePrice() {
14 return regularPrice;
16 }
17
18 ▼ class Truck extends Car {
19 int weight;
20 ▼ Truck(int speed, double regularPrice, String color, int weight) {
21 super(speed, regularPrice, color);
22 this.weight = weight;
23 }
24
25 @Override
26 ▼ double getSalePrice() {
27 ▼ if (weight > 2000) {
28 return super.getSalePrice() * 0.9;
29 ▼ } else {
30 return super.getSalePrice() * 0.8;
31 }
32 }
33 }
34
35 ▼ class Hatchback extends Car {
36 int year;
37 int manufacturerDiscount;
38
39 ▼ Hatchback(int speed, double regularPrice, String color, int year, int ma
40 super(speed, regularPrice, color);
41 this.year = year;
42 this.manufacturerDiscount = manufacturerDiscount;
43 }
44
45 @Override
46 ▼ double getSalePrice() {
47 return super.getSalePrice() - manufacturerDiscount;
48 }
49 }
50
51 ▼ class Sedan extends Car {
52 int length;
53
54 ▼ Sedan(int speed, double regularPrice, String color, int length) {
55 super(speed, regularPrice, color);
56 this.length = length;
57 }
58
59 @Override
60 ▼ double getSalePrice() {
61 ▼ if (length > 20) {
62 return super.getSalePrice() * 0.95;
63 ▼ } else {
64 return super.getSalePrice() * 0.9;
65 }
66 }
67 }
68
69 ▼ public class MyOwnAutoShop {
70 ▼ public static void main(String[] args) {
71 Scanner scanner = new Scanner(System.in);
72
73 System.out.print("Enter Speed,regularPrice, color, weight for Truck\
74 int truckSpeed = scanner.nextInt();
75 double truckPrice = scanner.nextDouble();
76 String truckColor = scanner.next();
77 int truckWeight = scanner.nextInt();
78
79 Truck truck = new Truck(truckSpeed, truckPrice, truckColor, truckWei
80 System.out.printf("Truck Price %.2f\n", truck.getSalePrice());
81
82 System.out.print("Enter Speed,regularPrice, color, length for Sedan\
83 int sedanSpeed = scanner.nextInt();
84 double sedanPrice = scanner.nextDouble();
85 String sedanColor = scanner.next();
86 int sedanLength = scanner.nextInt();
87
88 Sedan sedan = new Sedan(sedanSpeed, sedanPrice, sedanColor, sedanLen
89 System.out.printf("Sedan Price %.2f\n", sedan.getSalePrice());
90
91 System.out.print("Enter Speed,regularPrice, color, year, manufacture
92 int hatchbackSpeed = scanner.nextInt();
93 double hatchbackPrice = scanner.nextDouble();
94 String hatchbackColor = scanner.next();
95 int hatchbackYear = scanner.nextInt();
96 int hatchbackDiscount = scanner.nextInt();
97
98 Hatchback hatchback = new Hatchback(hatchbackSpeed, hatchbackPrice,
99 System.out.printf("Hatchback Price %.2f\n", hatchback.getSalePrice()
100
101 System.out.print("Enter Speed,regularPrice, color for Car\n");
102 int carSpeed = scanner.nextInt();
103 double carPrice = scanner.nextDouble();
104 String carColor = scanner.next();
105
106 Car car = new Car(carSpeed, carPrice, carColor);
107 System.out.printf("Basic Car Price %.2f\n", car.getSalePrice());
108 }
109 }

Input Expected Got

 155 85000 Enter Speed,regularPrice, color, Enter Speed,regularPrice, color, 


Green 2200 weight for Truck weight for Truck
180 120000 Truck Price 76500.00 Truck Price 76500.00
White 7 Enter Speed,regularPrice, color, Enter Speed,regularPrice, color,
175 75000 length for Sedan length for Sedan
Grey 1998 Sedan Price 108000.00 Sedan Price 108000.00
5000 Enter Speed,regularPrice, color, Enter Speed,regularPrice, color,
155 50000 year, manufacturerDiscount for year, manufacturerDiscount for
Yellow Hatchback Hatchback
Hatchback Price 70000.00 Hatchback Price 70000.00
Enter Speed,regularPrice, color Enter Speed,regularPrice, color
for Car for Car
Basic Car Price 50000.00 Basic Car Price 50000.00

Passed all tests! 

▸ Show/hide question author's solution (Java)


Correct
Marks for this submission: 10.00/10.00.

You might also like