Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions abstractclasses/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package abstractclasses;

public class Circle extends Shape{
public void draw() {
System.out.println("Drawing Circle");
}

public static void main(String args[]) {
Shape circ = new Circle();
circ.draw();
}
}
13 changes: 13 additions & 0 deletions abstractclasses/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package abstractclasses;

public class Rectangle extends Shape {
@Override
public void draw() {
System.out.println("Drawing rectangle");
}

public static void main(String args[]) {
Shape rect = new Rectangle();
rect.draw();
}
}
5 changes: 5 additions & 0 deletions abstractclasses/Shape.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package abstractclasses;

abstract class Shape {
abstract void draw();
}
43 changes: 43 additions & 0 deletions anonymousclasses/AnimalShowcase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package anonymousclasses;

public class AnimalShowcase extends BirdClass implements BirdInterface, Pig {
public AnimalShowcase(String name) {
super(name); // Call the constructor of BirdClass
}

// Implement the methods from the Pig interface
@Override
public void oink() {
System.out.println(getName() + " the bird-pig says: Oink!");
}

@Override
public void walk() {
System.out.println(getName() + " the bird-pig walks around happily.");
}

// Override the chirp method from BirdClass to customize the behavior
@Override
public void chirp() {
System.out.println("Chirp chirp! I'm " + getName() + ", the unique bird-pig!");
}

// Override the fly method from BirdClass to customize the behavior
@Override
public void fly() {
System.out.println(getName() + " spreads its wings and flies!");
}

// Additional method to showcase the combined behaviors
public void showcaseAbilities() {
chirp();
fly();
oink();
walk();
}

public static void main(String[] args) {
AnimalShowcase showcase = new AnimalShowcase("Mike");
showcase.showcaseAbilities();
}
}
43 changes: 43 additions & 0 deletions designConsiderations/goodDesign/mainClasses/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package designConsiderations.goodDesign.mainClasses;

import designConsiderations.goodDesign.mainInterfaces.Measurable;

// Good design because it utilizes interface to have the same method call to have different functionality depending on the object type.

public class Circle implements Measurable {
private double radius;

// Constructor to initialize the circle with a specific radius
public Circle(double radius) {
this.radius = radius;
}

// Getter method for the radius
public double getRadius() {
return radius;
}

// Setter method for the radius
public void setRadius(double radius) {
this.radius = radius;
}

// Method to calculate the area of the circle
@Override
public double measureArea() {
return Math.PI * radius * radius;
}

// Method to calculate the circumference of the circle
public double calculateCircumference() {
return 2 * Math.PI * radius;
}

// Method to display the circle's details
public void displayDetails() {
System.out.println("Circle: ");
System.out.println("Radius: " + radius);
System.out.println("Area: " + measureArea());
System.out.println("Circumference: " + calculateCircumference());
}
}
37 changes: 37 additions & 0 deletions designConsiderations/goodDesign/mainClasses/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package designConsiderations.goodDesign.mainClasses;

import designConsiderations.goodDesign.mainInterfaces.Measurable;

public class Rectangle implements Measurable {
private double length;
private double width;

// Constructor to initialize the rectangle
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}

// Implementing the measure method to calculate area
@Override
public double measureArea() {
return length * width;
}

// Getter and Setter methods
public double getLength() {
return length;
}

public void setLength(double length) {
this.length = length;
}

public double getWidth() {
return width;
}

public void setWidth(double width) {
this.width = width;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package designConsiderations.goodDesign.mainInterfaces;

public interface Measurable {
double measureArea();
}
50 changes: 50 additions & 0 deletions designpatterns/FacadeDesign.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package designpatterns;

class SubsystemOne {
void operationOne() {
System.out.println("SubsystemOne: Operation One");
}

void operationTwo() {
System.out.println("SubsystemOne: Operation Two");
}
}

class SubsystemTwo {
void operationOne() {
System.out.println("SubsystemTwo: Operation One");
}

void operationTwo() {
System.out.println("SubsystemTwo: Operation Two");
}
}

class Facade {
private SubsystemOne subsystemOne;
private SubsystemTwo subsystemTwo;

public Facade() {
subsystemOne = new SubsystemOne();
subsystemTwo = new SubsystemTwo();
}

public void operationGroupOne() {
subsystemOne.operationOne();
subsystemTwo.operationOne();
}

public void operationGroupTwo() {
subsystemOne.operationTwo();
subsystemTwo.operationTwo();
}
}

public class FacadeDesign {
public static void main(String[] args) {
Facade facade = new Facade();

facade.operationGroupOne();
facade.operationGroupTwo();
}
}
35 changes: 35 additions & 0 deletions exceptionhandling/dogWithExceptions/DogTrainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package exceptionhandling.dogWithExceptions;

public class DogTrainer {
// Method to train a dog and handle the exception
public void trainAndHandleException(Dog dog) {
try {
Dog trainedDog = trainDog(dog);
System.out.println("Successfully trained: " + trainedDog);
} catch (DogNotInitializedException error) {
System.err.println("Error while training the dog: " + error.getMessage());
}
}

// Method to train a dog, potentially throwing an exception
public Dog trainDog(Dog traineeDog) throws DogNotInitializedException {
if (traineeDog != null) {
traineeDog.setCanShakeHands(true);
return traineeDog;
} else {
throw new DogNotInitializedException("Trainee Dog was not initialized!");
}
}

public static void main(String[] args) {
DogTrainer trainer = new DogTrainer();

// Example with a properly initialized dog
Dog buddy = new Dog("Mike", 5);
trainer.trainAndHandleException(buddy);

// Example with a null dog
Dog unknownDog = null;
trainer.trainAndHandleException(unknownDog);
}
}
33 changes: 33 additions & 0 deletions generics/GenericContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package generics;

public class GenericContainer<T> {
private T object;

public GenericContainer(T object) {
this.object = object;
}

public void setObject(T object) {
this.object = object;
}

public T getObject() {
return object;
}

public static void main(String[] args) {
// Example usage of GenericContainer with different types

// Integer container
GenericContainer<Integer> intContainer = new GenericContainer<>(123);
System.out.println("Integer Value: " + intContainer.getObject());

// String container
GenericContainer<String> stringContainer = new GenericContainer<>("a");
System.out.println("String Value: " + stringContainer.getObject());

// Double container
GenericContainer<Double> doubleContainer = new GenericContainer<>(9.99);
System.out.println("Double Value: " + doubleContainer.getObject());
}
}
39 changes: 39 additions & 0 deletions inheritance/InheritanceDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package inheritance;

class Vehicle {
private String make;
private String model;

public Vehicle(String make, String model) {
this.make = make;
this.model = model;
}

public void displayInfo() {
System.out.println("Vehicle Make: " + make + ", Model: " + model);
}
}

// Subclass
class Car extends Vehicle {
private int numberOfDoors;

public Car(String make, String model, int numberOfDoors) {
super(make, model); // Calling the constructor of the superclass
this.numberOfDoors = numberOfDoors;
}

public void displayCarInfo() {
// This method extends the functionality of the superclass
super.displayInfo(); // Calling the method from the superclass
System.out.println("Number of Doors: " + numberOfDoors);
}
}

// Main class to demonstrate inheritance
public class InheritanceDemo {
public static void main(String[] args) {
Car car = new Car("Toyota", "Camry", 4);
car.displayCarInfo(); // Displays information about the car
}
}
28 changes: 28 additions & 0 deletions interfaces/Music.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package interfaces;

// First class implementing the interface
class Guitar implements Playable {
@Override
public void play() {
System.out.println("Playing the guitar.");
}
}

// Second class implementing the interface
class Piano implements Playable {
@Override
public void play() {
System.out.println("Playing the piano.");
}
}

// Main class to demonstrate the use of the interface
public class Music {
public static void main(String[] args) {
Playable guitar = new Guitar();
Playable piano = new Piano();

guitar.play(); // Outputs: Playing the guitar.
piano.play(); // Outputs: Playing the piano.
}
}
5 changes: 5 additions & 0 deletions interfaces/Playable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package interfaces;

public interface Playable {
void play();
}
5 changes: 5 additions & 0 deletions lambdaexpressions/Action.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lambdaexpressions;

public interface Action {
void perform();
}
19 changes: 19 additions & 0 deletions lambdaexpressions/LambdaExamples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package lambdaexpressions;

public class LambdaExamples {
public static void main(String[] args) {
// Lambda expression for a greeting action
Action greet = () -> System.out.println("Hello, world!");

// Lambda expression for a task completion action
Action completeTask = () -> System.out.println("Task completed successfully!");

// Performing the actions
performAction(greet);
performAction(completeTask);
}

private static void performAction(Action action) {
action.perform();
}
}
Loading