Merge pull request 'AnotherVersionIGuess' (#1) from AnotherVersionIGuess into main
Reviewed-on: http://192.168.2.55:3000/Kilokem/JavaIntermediate/pulls/1
This commit is contained in:
7
MoreOnClasses/src/main/java/com/oop/ElVehicle.java
Normal file
7
MoreOnClasses/src/main/java/com/oop/ElVehicle.java
Normal file
@ -0,0 +1,7 @@
|
||||
package com.oop;
|
||||
|
||||
public class ElVehicle extends Vehicle {
|
||||
public void resource(){
|
||||
System.out.println("I use electricity");
|
||||
}
|
||||
}
|
7
MoreOnClasses/src/main/java/com/oop/HybridVehicle.java
Normal file
7
MoreOnClasses/src/main/java/com/oop/HybridVehicle.java
Normal file
@ -0,0 +1,7 @@
|
||||
package com.oop;
|
||||
|
||||
public class HybridVehicle extends Vehicle {
|
||||
public void resource(){
|
||||
System.out.println("I use both petrol and electricity");
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
20
MoreOnClasses/src/main/java/com/oop/Program.java
Normal file
20
MoreOnClasses/src/main/java/com/oop/Program.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
BIN
MoreOnClasses/target/classes/com/oop/ElVehicle.class
Normal file
BIN
MoreOnClasses/target/classes/com/oop/ElVehicle.class
Normal file
Binary file not shown.
BIN
MoreOnClasses/target/classes/com/oop/HybridVehicle.class
Normal file
BIN
MoreOnClasses/target/classes/com/oop/HybridVehicle.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
MoreOnClasses/target/classes/com/oop/Program.class
Normal file
BIN
MoreOnClasses/target/classes/com/oop/Program.class
Normal file
Binary file not shown.
Binary file not shown.
16
castingandothers/pom.xml
Normal file
16
castingandothers/pom.xml
Normal file
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>castingandothers</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
8
castingandothers/src/main/java/com/example/Animal.java
Normal file
8
castingandothers/src/main/java/com/example/Animal.java
Normal file
@ -0,0 +1,8 @@
|
||||
package com.example;
|
||||
|
||||
class Animal{
|
||||
public String name;
|
||||
public void Animal(){
|
||||
this.name = "Something!!!";
|
||||
}
|
||||
}
|
12
castingandothers/src/main/java/com/example/Cat.java
Normal file
12
castingandothers/src/main/java/com/example/Cat.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
41
castingandothers/src/main/java/com/example/Main.java
Normal file
41
castingandothers/src/main/java/com/example/Main.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
BIN
castingandothers/target/classes/com/example/Animal.class
Normal file
BIN
castingandothers/target/classes/com/example/Animal.class
Normal file
Binary file not shown.
BIN
castingandothers/target/classes/com/example/Cat.class
Normal file
BIN
castingandothers/target/classes/com/example/Cat.class
Normal file
Binary file not shown.
BIN
castingandothers/target/classes/com/example/Main$1.class
Normal file
BIN
castingandothers/target/classes/com/example/Main$1.class
Normal file
Binary file not shown.
BIN
castingandothers/target/classes/com/example/Main.class
Normal file
BIN
castingandothers/target/classes/com/example/Main.class
Normal file
Binary file not shown.
Reference in New Issue
Block a user