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
10 changes: 10 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/lab-java-interfaces-and-abstract-classes.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
18 changes: 18 additions & 0 deletions Interfaces_And_Abstract_Classes/src/BigDecimalOps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.math.BigDecimal;
import java.math.RoundingMode;

public final class BigDecimalOps {
private BigDecimalOps() {
}

public static double roundToNearestHundredth(BigDecimal value) {
if (value == null) throw new IllegalArgumentException("value cannot be null");
return value.setScale(2, RoundingMode.HALF_UP).doubleValue();
}

public static BigDecimal reverseSignAndRoundToNearestTenth(BigDecimal value) {
if (value == null) throw new IllegalArgumentException("value cannot be null");
return value.negate().setScale(1, RoundingMode.HALF_UP);
}
}

34 changes: 34 additions & 0 deletions Interfaces_And_Abstract_Classes/src/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public abstract class Car {
private final String vinNumber;
private final String make;
private final String model;
private int mileage;

protected Car(String vinNumber, String make, String model, int mileage) {
if (vinNumber == null || vinNumber.isBlank()) throw new IllegalArgumentException("vinNumber is required");
if (make == null || make.isBlank()) throw new IllegalArgumentException("make is required");
if (model == null || model.isBlank()) throw new IllegalArgumentException("model is required");
if (mileage < 0) throw new IllegalArgumentException("mileage cannot be negative");

this.vinNumber = vinNumber;
this.make = make;
this.model = model;
this.mileage = mileage;
}

public String getVinNumber() { return vinNumber; }
public String getMake() { return make; }
public String getModel() { return model; }
public int getMileage() { return mileage; }

public void setMileage(int mileage) {
if (mileage < 0) throw new IllegalArgumentException("mileage cannot be negative");
this.mileage = mileage;
}

public String getInfo() {
return String.format("VIN: %s | Make: %s | Model: %s | Mileage: %d",
vinNumber, make, model, mileage);
}
}

4 changes: 4 additions & 0 deletions Interfaces_And_Abstract_Classes/src/Inlist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public interface Inlist {
void add(int number);
int get(int id);
}
30 changes: 30 additions & 0 deletions Interfaces_And_Abstract_Classes/src/IntArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
public final class IntArrayList implements IntList {
private int[] data = new int[10];
private int size = 0;

@Override
public void add(int number) {
if (size == data.length) {
int newCap = data.length + (data.length / 2);
if (newCap == data.length) newCap = data.length + 1;
int[] newArr = new int[newCap];
System.arraycopy(data, 0, newArr, 0, data.length);
data = newArr;
}
data[size++] = number;
}

@Override
public int get(int id) {
if (id < 0 || id >= size) throw new IndexOutOfBoundsException("id: " + id + ", size: " + size);
return data[id];
}

public int size() {
return size;
}

public int capacity() {
return data.length;
}
}
4 changes: 4 additions & 0 deletions Interfaces_And_Abstract_Classes/src/IntList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public interface IntList {
void add(int number);
int get(int id);
}
29 changes: 29 additions & 0 deletions Interfaces_And_Abstract_Classes/src/IntVector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public final class IntVector implements IntList {
private int[] data = new int[20];
private int size = 0;

@Override
public void add(int number) {
if (size == data.length) {
int newCap = data.length * 2;
int[] newArr = new int[newCap];
System.arraycopy(data, 0, newArr, 0, data.length);
data = newArr;
}
data[size++] = number;
}

@Override
public int get(int id) {
if (id < 0 || id >= size) throw new IndexOutOfBoundsException("id: " + id + ", size: " + size);
return data[id];
}

public int size() {
return size;
}

public int capacity() {
return data.length;
}
}
70 changes: 70 additions & 0 deletions Interfaces_And_Abstract_Classes/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.math.BigDecimal;

public class Main {
public static void main(String[] args) {

// ---- BigDecimal Operations ----
BigDecimal num1 = new BigDecimal("4.2545");
double res1 = BigDecimalOps.roundToNearestHundredth(num1);
System.out.println("4.2545 -> " + res1);

BigDecimal num2 = new BigDecimal("1.2345");
BigDecimal res2 = BigDecimalOps.reverseSignAndRoundToNearestTenth(num2);
System.out.println("1.2345 -> " + res2);

BigDecimal num3 = new BigDecimal("-45.67");
BigDecimal res3 = BigDecimalOps.reverseSignAndRoundToNearestTenth(num3);
System.out.println("-45.67 -> " + res3);

System.out.println("---------------");

// ---- Car Inventory System ----
Car c1 = new Sedan("VIN001", "Toyota", "Camry", 120000);
Car c2 = new UtilityVehicle("VIN002", "Jeep", "Wrangler", 80000, true);
Car c3 = new Truck("VIN003", "Ford", "F-150", 65000, 13500.5);

System.out.println("Cars:");
System.out.println(c1.getInfo());
System.out.println(c2.getInfo());
System.out.println(c3.getInfo());

System.out.println("---------------");

// ---- Video Streaming Service ----
Video v1 = new TvSeries("Breaking Bad", 49, 62);
Video v2 = new Movie("Interstellar", 169, 8.6);

System.out.println("Videos:");
System.out.println(v1.getInfo());
System.out.println(v2.getInfo());

System.out.println("---------------");

// ---- IntList ----
IntArrayList listA = new IntArrayList();
IntVector listB = new IntVector();

int i = 1;
while (i <= 25) {
listA.add(i);
listB.add(i);
i++;
}

System.out.println("IntArrayList values:");
System.out.println("first: " + listA.get(0));
System.out.println("middle: " + listA.get(12));
System.out.println("last: " + listA.get(24));
System.out.println("size: " + listA.size());
System.out.println("capacity: " + listA.capacity());

System.out.println();

System.out.println("IntVector values:");
System.out.println("first: " + listB.get(0));
System.out.println("middle: " + listB.get(12));
System.out.println("last: " + listB.get(24));
System.out.println("size: " + listB.size());
System.out.println("capacity: " + listB.capacity());
}
}
18 changes: 18 additions & 0 deletions Interfaces_And_Abstract_Classes/src/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public final class Movie extends Video {
private final double rating;

public Movie(String title, int duration, double rating) {
super(title, duration);
if (rating < 0.0 || rating > 10.0) throw new IllegalArgumentException("rating must be between 0 and 10");
this.rating = rating;
}

public double getRating() {
return rating;
}

@Override
public String getInfo() {
return super.getInfo() + String.format(" | Rating: %.1f", rating);
}
}
5 changes: 5 additions & 0 deletions Interfaces_And_Abstract_Classes/src/Sedan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public final class Sedan extends Car {
public Sedan(String vinNumber, String make, String model, int mileage) {
super(vinNumber, make, model, mileage);
}
}
19 changes: 19 additions & 0 deletions Interfaces_And_Abstract_Classes/src/Truck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public final class Truck extends Car {
private final double towingCapacity;

public Truck(String vinNumber, String make, String model, int mileage, double towingCapacity) {
super(vinNumber, make, model, mileage);
if (towingCapacity < 0) throw new IllegalArgumentException("towingCapacity cannot be negative");
this.towingCapacity = towingCapacity;
}

public double getTowingCapacity() {
return towingCapacity;
}

@Override
public String getInfo() {
return super.getInfo() + String.format(" | Towing Capacity: %.1f", towingCapacity);
}
}

18 changes: 18 additions & 0 deletions Interfaces_And_Abstract_Classes/src/TvSeries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public final class TvSeries extends Video {
private final int episodes;

public TvSeries(String title, int duration, int episodes) {
super(title, duration);
if (episodes <= 0) throw new IllegalArgumentException("episodes must be positive");
this.episodes = episodes;
}

public int getEpisodes() {
return episodes;
}

@Override
public String getInfo() {
return super.getInfo() + String.format(" | Episodes: %d", episodes);
}
}
17 changes: 17 additions & 0 deletions Interfaces_And_Abstract_Classes/src/UtilityVehicle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public final class UtilityVehicle extends Car {
private final boolean fourWheelDrive;

public UtilityVehicle(String vinNumber, String make, String model, int mileage, boolean fourWheelDrive) {
super(vinNumber, make, model, mileage);
this.fourWheelDrive = fourWheelDrive;
}

public boolean isFourWheelDrive() {
return fourWheelDrive;
}

@Override
public String getInfo() {
return super.getInfo() + String.format(" | 4WD: %s", fourWheelDrive);
}
}
18 changes: 18 additions & 0 deletions Interfaces_And_Abstract_Classes/src/Video.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public abstract class Video {
private final String title;
private final int duration;

protected Video(String title, int duration) {
if (title == null || title.isBlank()) throw new IllegalArgumentException("title is required");
if (duration <= 0) throw new IllegalArgumentException("duration must be positive");
this.title = title;
this.duration = duration;
}

public String getTitle() { return title; }
public int getDuration() { return duration; }

public String getInfo() {
return String.format("Title: %s | Duration: %d min", title, duration);
}
}