Skip to content

Commit 500b6a0

Browse files
committed
SOLVED : Lab Programs
1 parent dc46841 commit 500b6a0

8 files changed

+210
-7
lines changed

LAB4/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ super, abstract class and method.
1717
- [Q.N 7](https://github.com/pray3m/JavaPrograms/blob/main/LAB4/superMethod.java)
1818
- [Q.N 8](https://github.com/pray3m/JavaPrograms/blob/main/LAB4/SuperConstructor.java)
1919
- [Q.N 9](https://github.com/pray3m/JavaPrograms/blob/main/LAB4/OrderOfConstructor.java)
20-
- [Q.N 10*](https://github.com/pray3m/JavaPrograms/blob/main/LAB4/BoxDemo.java)
21-
- [Q.N 11*](https://github.com/pray3m/JavaPrograms/blob/main/LAB4/BoxDemo.java)
22-
- [Q.N 12*](https://github.com/pray3m/JavaPrograms/blob/main/LAB4/BoxDemo.java)
20+
- [Q.N 10](https://github.com/pray3m/JavaPrograms/blob/main/LAB4/RuntimePolymorphism.java)
21+
- [Q.N 11](https://github.com/pray3m/JavaPrograms/blob/main/LAB4/RuntimePolymorphismDemo.java)
22+
- [Q.N 12](https://github.com/pray3m/JavaPrograms/blob/main/LAB4/runtimePolymorphismSuper.java)
2323

LAB4/RuntimePolymorphism.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* 10. Run the following program in Java, that demonstrate the concepts of method overriding and
2+
run time polymorphism. NOTE: read the comment properly, it may confuse you. */
3+
4+
// Base Class
5+
class Parent {
6+
void show() {
7+
System.out.println("Parent's show()");
8+
}
9+
}
10+
11+
// Inherited class
12+
class Child extends Parent {
13+
// This method overrides show() of Parent
14+
//Override
15+
void show() {
16+
System.out.println("Child's show()");
17+
}
18+
}
19+
20+
public class RuntimePolymorphism {
21+
public static void main(String[] args) {
22+
// If a Parent type reference refers to a Parent object, then Parent's show is called
23+
Parent obj1 = new Parent();
24+
obj1.show();
25+
// If a Parent type reference refers to a Child object Child's show() is called RUN TIME POLYMORPHISM.
26+
Parent obj2 = new Child();
27+
obj2.show();
28+
}
29+
}

LAB4/RuntimePolymorphismDemo.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* 11. Complete the program in java that demonstrate the concepts of method overriding and run
2+
time polymorphism. */
3+
4+
class Bank{
5+
float getRateOfInterest(){
6+
return 0;
7+
}
8+
}
9+
10+
class NICASIA extends Bank{
11+
float getRateOfInterest() {
12+
return 8.4f;
13+
}
14+
}
15+
16+
class PRABHU extends Bank{
17+
float getRateOfInterest() {
18+
return 7.3f;
19+
}
20+
}
21+
22+
class MEGA extends Bank{
23+
float getRateOfInterest() {
24+
return 9.5f;
25+
}
26+
}
27+
28+
public class RuntimePolymorphismDemo{
29+
public static void main(String[] args){
30+
Bank a=new NICASIA();
31+
Bank b=new PRABHU();
32+
Bank c=new MEGA();
33+
System.out.println("NICASIA Rate of Interest: "+a.getRateOfInterest());
34+
System.out.println("PRABHU Rate of Interest: "+b.getRateOfInterest());
35+
System.out.println("MEGA Rate of Interest: "+c.getRateOfInterest());
36+
}
37+
}

LAB4/runtimePolymorphismSuper.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* 12. Complete the program to demonstrate the concept of run time polymorphism using super
2+
keyword. */
3+
4+
class Company {
5+
public void address() {
6+
System.out.println("This is Address of my Company...");
7+
}
8+
}
9+
10+
class eBay extends Company {
11+
public void address() {
12+
super.address(); // invokes the super class method
13+
System.out.println("This is eBay's Address...");
14+
}
15+
}
16+
17+
public class runtimePolymorphismSuper {
18+
public static void main(String[] args) {
19+
Company a = new Company(); // Company reference and object
20+
Company b = new eBay(); // Company reference but eBay object
21+
a.address();// runs the method in Company class
22+
b.address();// Runs the method in eBay class
23+
}
24+
}

LAB5/BikeMethods.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* 6. Complete the following program using the concept of interface */
2+
3+
/*
4+
* author: @pray3m
5+
*/
6+
7+
class Bicycle {
8+
int speed;
9+
int gear;
10+
11+
public void changeGear(int newGear){
12+
gear = newGear;
13+
}
14+
15+
public void speedUp(int increment){
16+
speed = speed + increment;
17+
}
18+
19+
public void applyBrakes(int decrement){
20+
speed = speed - decrement;
21+
}
22+
23+
public void printStates() {
24+
System.out.println("speed: " + speed + " gear: " + gear);
25+
}
26+
}
27+
28+
class Bike extends Bicycle{
29+
}
30+
31+
public class BikeMethods {
32+
public static void main (String[] args) {
33+
Bicycle bicycle = new Bicycle();
34+
bicycle.changeGear(3);
35+
bicycle.speedUp(34);
36+
bicycle.applyBrakes(12);
37+
System.out.println("Bicycle present state: ");
38+
bicycle.printStates();
39+
40+
Bike bike=new Bike();
41+
bike.speedUp(56);
42+
bike.changeGear(2);
43+
bike.applyBrakes(22);
44+
System.out.println("Bike present state:");
45+
bike.printStates();
46+
}
47+
}

LAB5/InterfaceCalculate.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*4. Create an interface called calculate which has methods int add(int x, int y) and int diff(int x,
2+
int y) to perform addition and subtraction of numbers passed as an arguments. Then define a
3+
class that implements interface calculate. */
4+
5+
/*
6+
* author: @pray3m
7+
*/
8+
9+
interface calculate{
10+
void add(int x,int y);
11+
void difference(int x,int y);
12+
}
13+
14+
class calculator implements calculate {
15+
public void add(int y, int x) {
16+
System.out.println("The sum is : " + (x + y));
17+
}
18+
19+
public void difference(int x, int y) {
20+
System.out.println("The difference is : " + (x - y));
21+
}
22+
}
23+
24+
public class InterfaceCalculate {
25+
public static void main(String[] args) {
26+
calculator c=new calculator();
27+
c.add(5,7);
28+
c.difference(7,4);
29+
}
30+
}

LAB5/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ super, abstract class and method.
1010
- [LAB 5 PDF](https://github.com/pray3m/JavaPrograms/blob/main/LAB5/lab%205(Abstract%20and%20interface).pdf)
1111
- [Q.N 1](https://github.com/pray3m/JavaPrograms/blob/main/LAB5/Abstraction.java)
1212
- [Q.N 2](https://github.com/pray3m/JavaPrograms/blob/main/LAB5/AbstractionDemo.java)
13-
- [Q.N 3*](https://github.com/pray3m/JavaPrograms/blob/main/LAB5/HierarchicalInheritance.java)
14-
- [Q.N 4*](https://github.com/pray3m/JavaPrograms/blob/main/LAB5/BoxDemo.java)
13+
- [Q.N 3](https://github.com/pray3m/JavaPrograms/blob/main/LAB5/abstractOverrides.java)
14+
- [Q.N 4](https://github.com/pray3m/JavaPrograms/blob/main/LAB5/InterfaceCalculate.java)
1515
- [Q.N 5](https://github.com/pray3m/JavaPrograms/blob/main/LAB5/InterfaceDemo.java)
16-
- [Q.N 6*](https://github.com/pray3m/JavaPrograms/blob/main/LAB5/SuperKey.java)
16+
- [Q.N 6](https://github.com/pray3m/JavaPrograms/blob/main/LAB5/BikeMethods.java)
1717
- [Q.N 7](https://github.com/pray3m/JavaPrograms/blob/main/LAB5/MultipleInheritance.java)
18-
- [Q.N 8*](https://github.com/pray3m/JavaPrograms/blob/main/LAB5/BoxDemo.java)
18+
- [Q.N 8](https://github.com/pray3m/JavaPrograms/blob/main/LAB5/InterfaceClass.java)
1919

LAB5/abstractOverrides.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* 3. Create an abstract class Fmachine, having methods getdata() and putdata(). Derive a class
2+
Airplane, having instance variables code, name, capacity and methods getdata() and putdata()
3+
(that overrides Fmachine's getdata() and putdata()) to ready and display the result. Create some
4+
instances of Airplane and call the required methods.*/
5+
6+
/*
7+
* author: @pray3m
8+
*/
9+
10+
abstract class Fmachine{
11+
abstract void getData(int code,String name,int capacity);
12+
abstract void putData();
13+
}
14+
15+
class Aeroplane extends Fmachine{
16+
int code;
17+
String name;
18+
int capacity;
19+
void getData(int code, String name,int capacity){
20+
this.code=code;
21+
this.name=name;
22+
this.capacity=capacity;
23+
}
24+
25+
void putData(){
26+
System.out.printf("\n Code:%d \n Name:%s \n Capacity:%d",code,name,capacity);
27+
}
28+
}
29+
30+
public class abstractOverrides {
31+
public static void main(String[] args) {
32+
Aeroplane a=new Aeroplane();
33+
a.getData(23221, "AirBus121",69);
34+
a.putData();
35+
}
36+
}

0 commit comments

Comments
 (0)