diff --git a/MoreOnClasses/src/main/java/com/oop/ElVehicle.java b/MoreOnClasses/src/main/java/com/oop/ElVehicle.java new file mode 100644 index 0000000..fa040f3 --- /dev/null +++ b/MoreOnClasses/src/main/java/com/oop/ElVehicle.java @@ -0,0 +1,7 @@ +package com.oop; + +public class ElVehicle extends Vehicle { + public void resource(){ + System.out.println("I use electricity"); + } +} diff --git a/MoreOnClasses/src/main/java/com/oop/HybridVehicle.java b/MoreOnClasses/src/main/java/com/oop/HybridVehicle.java new file mode 100644 index 0000000..e3c0b8a --- /dev/null +++ b/MoreOnClasses/src/main/java/com/oop/HybridVehicle.java @@ -0,0 +1,7 @@ +package com.oop; + +public class HybridVehicle extends Vehicle { + public void resource(){ + System.out.println("I use both petrol and electricity"); + } +} diff --git a/MoreOnClasses/src/main/java/com/oop/Main.java b/MoreOnClasses/src/main/java/com/oop/Main.java index 390240b..5b974fc 100644 --- a/MoreOnClasses/src/main/java/com/oop/Main.java +++ b/MoreOnClasses/src/main/java/com/oop/Main.java @@ -21,5 +21,45 @@ public class Main { pro1.write(); pro1.useEffects(); pro1.changeResolution(); + + + //Polymorphism + // Vehicles can be classified according to what energy source powers them. The program you are given has 3 vehicle classes: Vehicle, ElectricVehicle and HybridVehicle. + + // ElectricVehicle and HybridVehicle classes are inherited from Vehicle class. + + // Complete the program by reimplementing method resource() in inherited classes, so that the given calls work correctly. + + Vehicle vehicle = new Vehicle(); + Vehicle electric = new ElVehicle(); + Vehicle hybrid = new HybridVehicle(); + + //calls + vehicle.start(); + vehicle.resource(); + electric.start(); + electric.resource(); + hybrid.start(); + hybrid.resource(); + + //overriding funcions! For more visit Program.js + System.out.println(Program.max(8, 17)); + System.out.println(Program.max(3.14, 7.68)); + + //Overloading or overriding + a = 5; + double b = 10.2; + + System.out.println(doubleTheValue(a)); + System.out.println(doubleTheValue(b)); } + + //complete the method for integer value type + public static int doubleTheValue(int a) { + return a*2; + } + //overload the method for double value type + public static double doubleTheValue(double a) { + return a*2; + } } \ No newline at end of file diff --git a/MoreOnClasses/src/main/java/com/oop/Program.java b/MoreOnClasses/src/main/java/com/oop/Program.java new file mode 100644 index 0000000..33e134d --- /dev/null +++ b/MoreOnClasses/src/main/java/com/oop/Program.java @@ -0,0 +1,20 @@ +package com.oop; + +public class Program { + static double max(double a, double b) { + if(a > b) { + return a; + } + else { + return b; + } + } + static int max(int a, int b) { + if(a > b) { + return a; + } + else { + return b; + } + } +} diff --git a/MoreOnClasses/src/main/java/com/oop/Vehicle.java b/MoreOnClasses/src/main/java/com/oop/Vehicle.java index 5aa01d2..b09fd7e 100644 --- a/MoreOnClasses/src/main/java/com/oop/Vehicle.java +++ b/MoreOnClasses/src/main/java/com/oop/Vehicle.java @@ -27,5 +27,11 @@ public class Vehicle { public void setColor(String c) { this.color = c; } - + public void start(){ + System.out.println("Starting"); + } + + public void resource(){ + System.out.println("I use petrol"); + } } diff --git a/MoreOnClasses/target/classes/com/oop/ElVehicle.class b/MoreOnClasses/target/classes/com/oop/ElVehicle.class new file mode 100644 index 0000000..f355d85 Binary files /dev/null and b/MoreOnClasses/target/classes/com/oop/ElVehicle.class differ diff --git a/MoreOnClasses/target/classes/com/oop/HybridVehicle.class b/MoreOnClasses/target/classes/com/oop/HybridVehicle.class new file mode 100644 index 0000000..0657db0 Binary files /dev/null and b/MoreOnClasses/target/classes/com/oop/HybridVehicle.class differ diff --git a/MoreOnClasses/target/classes/com/oop/Main.class b/MoreOnClasses/target/classes/com/oop/Main.class index 41d020a..60141f0 100644 Binary files a/MoreOnClasses/target/classes/com/oop/Main.class and b/MoreOnClasses/target/classes/com/oop/Main.class differ diff --git a/MoreOnClasses/target/classes/com/oop/Program.class b/MoreOnClasses/target/classes/com/oop/Program.class new file mode 100644 index 0000000..8b7f12d Binary files /dev/null and b/MoreOnClasses/target/classes/com/oop/Program.class differ diff --git a/MoreOnClasses/target/classes/com/oop/Vehicle.class b/MoreOnClasses/target/classes/com/oop/Vehicle.class index bb05307..35552fd 100644 Binary files a/MoreOnClasses/target/classes/com/oop/Vehicle.class and b/MoreOnClasses/target/classes/com/oop/Vehicle.class differ diff --git a/castingandothers/pom.xml b/castingandothers/pom.xml new file mode 100644 index 0000000..074fc54 --- /dev/null +++ b/castingandothers/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + com.example + castingandothers + 1.0-SNAPSHOT + + + 17 + 17 + + + \ No newline at end of file diff --git a/castingandothers/src/main/java/com/example/Animal.java b/castingandothers/src/main/java/com/example/Animal.java new file mode 100644 index 0000000..3b75755 --- /dev/null +++ b/castingandothers/src/main/java/com/example/Animal.java @@ -0,0 +1,8 @@ +package com.example; + +class Animal{ + public String name; + public void Animal(){ + this.name = "Something!!!"; + } +} \ No newline at end of file diff --git a/castingandothers/src/main/java/com/example/Cat.java b/castingandothers/src/main/java/com/example/Cat.java new file mode 100644 index 0000000..1e0fc07 --- /dev/null +++ b/castingandothers/src/main/java/com/example/Cat.java @@ -0,0 +1,12 @@ +package com.example; + +public class Cat extends Animal { + public String sound; + public void Cat(){ + this.sound = "MEEEEOW"; + } + public void makesound(){ + this.sound = "Something like MEEEEOW"; + System.err.println(sound); + } +} diff --git a/castingandothers/src/main/java/com/example/Main.java b/castingandothers/src/main/java/com/example/Main.java new file mode 100644 index 0000000..6c5fbe2 --- /dev/null +++ b/castingandothers/src/main/java/com/example/Main.java @@ -0,0 +1,41 @@ +package com.example; + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + + double asd = 3.5423; + int castedasd = (int) asd; + System.out.println(castedasd); + System.out.print("Gimme a letter and Ill tel you the ASCII code! "); + Scanner read = new Scanner(System.in); + char a = read.next().charAt(0); + + //your code goes here + System.out.println((int)a); + + + // Upcasting + // You can cast an instance of a subclass to its superclass. + + // Consider the following example, assuming that Cat is a subclass of Animal. + Animal C = new Cat(); + + Animal B = new Cat(); + System.out.println(((Cat)B).sound); + + // Anonymous Classes + // Anonymous classes are a way to extend the existing classes on the fly. + + // For example, consider having a class Machine: + Cat c = new Cat(){ + @Override public void makesound(){ + System.err.println("MOOOOOOOOOOOOOO"); + } + }; + + c.makesound(); + } +} \ No newline at end of file diff --git a/castingandothers/target/classes/com/example/Animal.class b/castingandothers/target/classes/com/example/Animal.class new file mode 100644 index 0000000..830297c Binary files /dev/null and b/castingandothers/target/classes/com/example/Animal.class differ diff --git a/castingandothers/target/classes/com/example/Cat.class b/castingandothers/target/classes/com/example/Cat.class new file mode 100644 index 0000000..ce4c4cb Binary files /dev/null and b/castingandothers/target/classes/com/example/Cat.class differ diff --git a/castingandothers/target/classes/com/example/Main$1.class b/castingandothers/target/classes/com/example/Main$1.class new file mode 100644 index 0000000..927e7e7 Binary files /dev/null and b/castingandothers/target/classes/com/example/Main$1.class differ diff --git a/castingandothers/target/classes/com/example/Main.class b/castingandothers/target/classes/com/example/Main.class new file mode 100644 index 0000000..304f426 Binary files /dev/null and b/castingandothers/target/classes/com/example/Main.class differ