This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CommunicationDriver cd = new Bluetooth(); | |
cd.connectDevice("OnePlus"); | |
cd.sendFile("Downloads/image.jpg"); | |
cd.receiveFile(); | |
cd.disconnectDevice(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Bluetooth implements CommunicationDriver{ | |
String connectedDevice; | |
@Override | |
public void connectDevice(String deviceName) { | |
this.connectedDevice = deviceName; | |
System.out.println("Connected to " + this.connectedDevice); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface CommunicationDriver { | |
void connectDevice(String deviceName); | |
default void disconnectDevice() { | |
System.out.println("Device disconnected"); | |
} | |
void sendFile(String filePath); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We can't create an object of Device class | |
// Device device = new Device(); is not allowed | |
Device mp = new MediaPlayer(); | |
mp.switchOn(); | |
mp.switchOff(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A Media Player IS A Device | |
public class MediaPlayer extends Device{ | |
boolean turnedOn; | |
// In absence of super() statement, java automatically adds one during compilation | |
public MediaPlayer() { | |
this.turnedOn = false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Device { | |
abstract void switchOn(); | |
abstract void switchOff(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tiger tiger = new Tiger(10, "Bengal Tiger"); | |
tiger.eatFood("Deer"); | |
tiger.printDetails(); | |
tiger.roar(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Tiger extends Animal { | |
// All data members are inherited from Animal class | |
// We specify additional data member specific to Tiger | |
String tigerSpecies; | |
String lastPrey; | |
public Tiger(int age, String tigerSpecies) { | |
// the constructor for the superclass must be defined | |
// super() must be the first line in the constructor of base class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Extends Object Class by default | |
public class Animal { | |
public static enum FoodHabits{ | |
CARNIVORE(0), HERBIVORE(1), OMNIVORE(2); | |
private int index; | |
public int getIndex() { | |
return index; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Program to return average of numbers | |
public static double doAverage(ArrayList<Integer> marks) { | |
// double for more precision than Integer during division | |
double avg = 0.0; | |
for(int mark: marks) { | |
avg += mark; | |
} | |
// size() method returns the number of elements in array | |
avg = avg/marks.size(); |
NewerOlder