Skip to content

Commit 4f61709

Browse files
committed
Calculator program
1 parent c141974 commit 4f61709

File tree

4 files changed

+181
-0
lines changed

4 files changed

+181
-0
lines changed

src/com/lab/dec_24/Car.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.lab.dec_24;
2+
3+
public class Car implements Runnable
4+
{
5+
6+
private String name;
7+
private PetrolPump petrolPump;
8+
9+
public Car(String name, PetrolPump petrolPump)
10+
{
11+
super();
12+
this.name = name;
13+
this.petrolPump = petrolPump;
14+
}
15+
16+
@Override
17+
public void run()
18+
{
19+
petrolPump.refillCar(name);
20+
}
21+
22+
}
23+
/*
24+
*
25+
Create a Car class that implements the Runnable interface. Include the following members:
26+
name (String): The name of the car.
27+
petrolPump (PetrolPump): A reference to the petrol pump.
28+
run(): Implement the run method from the Runnable interface.
29+
Inside this method,
30+
call the refillCar method of the petrol pump to simulate the refilling process.
31+
*/

src/com/lab/dec_24/PetrolPump.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.lab.dec_24;
2+
3+
public class PetrolPump
4+
{
5+
6+
public synchronized void refillCar(String carName)
7+
{
8+
System.out.println(carName+" started refilling...");
9+
try
10+
{
11+
Thread.sleep(3000);
12+
} catch (InterruptedException e)
13+
{
14+
e.printStackTrace();
15+
}
16+
System.out.println(carName+" completed refilling!");
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.lab.dec_24;
2+
3+
public class PetrolPumpSimulation {
4+
5+
public static void main(String[] args)
6+
{
7+
//object creation for PetrolPump
8+
PetrolPump pmp = new PetrolPump();
9+
10+
// Creating runnable type of objects to start threads
11+
Car bmw = new Car("BMW", pmp);
12+
Car rr = new Car("RR", pmp);
13+
Car tata = new Car("TaTa", pmp);
14+
15+
16+
// New threads created for every car
17+
Thread bmwThread = new Thread(bmw);
18+
Thread rrThread = new Thread(rr);
19+
Thread tataThread = new Thread(tata);
20+
21+
// start method to get threads from NEW to RUNNABLE state
22+
bmwThread.start();
23+
rrThread.start();
24+
tataThread.start();
25+
26+
}
27+
28+
}
29+
/*
30+
*
31+
In the main method, create an instance of PetrolPump.
32+
Create an array of Car objects, each initialized with a unique name and the reference to the petrol pump.
33+
Create an array of Thread objects to represent each car's thread.
34+
Start each car thread and wait for all car threads to complete using the join method.
35+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.lab.sep_18;
2+
3+
import java.util.Scanner;
4+
5+
public class CalculationWithThirdVeriavle
6+
{
7+
8+
final static Scanner sc = new Scanner(System.in);
9+
10+
public static void main(String[] args)
11+
{
12+
int a, b, choice;
13+
14+
System.out.println("Enter numbers to calculate ");
15+
System.out.print("\t\t 1st Number :");
16+
a = sc.nextInt();
17+
System.out.print("\t\t 2nd Number :");
18+
b = sc.nextInt();
19+
20+
System.out.print("\nWhich operation you want to perform\n");
21+
System.out.println("1. ADD");
22+
System.out.println("2. SUB");
23+
System.out.println("3. MUL");
24+
System.out.println("4. MOD");
25+
System.out.println("5. DIV");
26+
System.out.println("YOUR CHOICE :");
27+
choice = sc.nextInt();
28+
29+
switch (choice)
30+
{
31+
case 1:
32+
{
33+
add(a, b);
34+
break;
35+
}
36+
case 2:
37+
{
38+
sub(a, b);
39+
break;
40+
}
41+
case 3:
42+
{
43+
mul(a, b);
44+
break;
45+
}
46+
case 4:
47+
{
48+
mod(a, b);
49+
break;
50+
}
51+
case 5:
52+
{
53+
div(a, b);
54+
break;
55+
}
56+
default:
57+
throw new IllegalArgumentException("Unexpected value: " + choice);
58+
}
59+
60+
61+
}
62+
63+
public static void add(int a, int b)
64+
{
65+
System.out.println(a+b);
66+
}
67+
68+
public static void sub(int a, int b)
69+
{
70+
System.out.println(a-b);
71+
}
72+
73+
public static void mul(int a, int b)
74+
{
75+
System.out.println(a*b);
76+
}
77+
78+
public static void mod(int a, int b)
79+
{
80+
System.out.println(a%b);
81+
}
82+
83+
public static void div(int a, int b)
84+
{
85+
try
86+
{
87+
if(b == 0)
88+
throw new ArithmeticException("Can not divide by 0");
89+
System.out.println(a/b);
90+
} catch (ArithmeticException e)
91+
{
92+
System.out.println(e.getMessage());
93+
}
94+
95+
}
96+
97+
}

0 commit comments

Comments
 (0)