Skip to content

Instantly share code, notes, and snippets.

CommunicationDriver cd = new Bluetooth();
cd.connectDevice("OnePlus");
cd.sendFile("Downloads/image.jpg");
cd.receiveFile();
cd.disconnectDevice();
public class Bluetooth implements CommunicationDriver{
String connectedDevice;
@Override
public void connectDevice(String deviceName) {
this.connectedDevice = deviceName;
System.out.println("Connected to " + this.connectedDevice);
}
public interface CommunicationDriver {
void connectDevice(String deviceName);
default void disconnectDevice() {
System.out.println("Device disconnected");
}
void sendFile(String filePath);
// We can't create an object of Device class
// Device device = new Device(); is not allowed
Device mp = new MediaPlayer();
mp.switchOn();
mp.switchOff();
// 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;
}
public abstract class Device {
abstract void switchOn();
abstract void switchOff();
}
Tiger tiger = new Tiger(10, "Bengal Tiger");
tiger.eatFood("Deer");
tiger.printDetails();
tiger.roar();
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
// 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;
// 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();