From be2703851408455da088d1c8cbef675e6545d010 Mon Sep 17 00:00:00 2001 From: URLeeo Date: Mon, 23 Feb 2026 13:42:23 +0400 Subject: [PATCH] Completed Task About Interfaces And Abstract Classes --- .idea/.gitignore | 10 +++ ...b-java-interfaces-and-abstract-classes.iml | 9 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ .../Interfaces_And_Abstract_Classes.iml | 11 +++ .../src/BigDecimalOps.java | 18 +++++ Interfaces_And_Abstract_Classes/src/Car.java | 34 +++++++++ .../src/Inlist.java | 4 ++ .../src/IntArrayList.java | 30 ++++++++ .../src/IntList.java | 4 ++ .../src/IntVector.java | 29 ++++++++ Interfaces_And_Abstract_Classes/src/Main.java | 70 +++++++++++++++++++ .../src/Movie.java | 18 +++++ .../src/Sedan.java | 5 ++ .../src/Truck.java | 19 +++++ .../src/TvSeries.java | 18 +++++ .../src/UtilityVehicle.java | 17 +++++ .../src/Video.java | 18 +++++ 19 files changed, 334 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/lab-java-interfaces-and-abstract-classes.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Interfaces_And_Abstract_Classes/Interfaces_And_Abstract_Classes.iml create mode 100644 Interfaces_And_Abstract_Classes/src/BigDecimalOps.java create mode 100644 Interfaces_And_Abstract_Classes/src/Car.java create mode 100644 Interfaces_And_Abstract_Classes/src/Inlist.java create mode 100644 Interfaces_And_Abstract_Classes/src/IntArrayList.java create mode 100644 Interfaces_And_Abstract_Classes/src/IntList.java create mode 100644 Interfaces_And_Abstract_Classes/src/IntVector.java create mode 100644 Interfaces_And_Abstract_Classes/src/Main.java create mode 100644 Interfaces_And_Abstract_Classes/src/Movie.java create mode 100644 Interfaces_And_Abstract_Classes/src/Sedan.java create mode 100644 Interfaces_And_Abstract_Classes/src/Truck.java create mode 100644 Interfaces_And_Abstract_Classes/src/TvSeries.java create mode 100644 Interfaces_And_Abstract_Classes/src/UtilityVehicle.java create mode 100644 Interfaces_And_Abstract_Classes/src/Video.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/lab-java-interfaces-and-abstract-classes.iml b/.idea/lab-java-interfaces-and-abstract-classes.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/lab-java-interfaces-and-abstract-classes.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f5bd2df --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ff55171 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Interfaces_And_Abstract_Classes/Interfaces_And_Abstract_Classes.iml b/Interfaces_And_Abstract_Classes/Interfaces_And_Abstract_Classes.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Interfaces_And_Abstract_Classes/Interfaces_And_Abstract_Classes.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Interfaces_And_Abstract_Classes/src/BigDecimalOps.java b/Interfaces_And_Abstract_Classes/src/BigDecimalOps.java new file mode 100644 index 0000000..deb1816 --- /dev/null +++ b/Interfaces_And_Abstract_Classes/src/BigDecimalOps.java @@ -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); + } +} + diff --git a/Interfaces_And_Abstract_Classes/src/Car.java b/Interfaces_And_Abstract_Classes/src/Car.java new file mode 100644 index 0000000..d2a2c54 --- /dev/null +++ b/Interfaces_And_Abstract_Classes/src/Car.java @@ -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); + } +} + diff --git a/Interfaces_And_Abstract_Classes/src/Inlist.java b/Interfaces_And_Abstract_Classes/src/Inlist.java new file mode 100644 index 0000000..64b2857 --- /dev/null +++ b/Interfaces_And_Abstract_Classes/src/Inlist.java @@ -0,0 +1,4 @@ +public interface Inlist { + void add(int number); + int get(int id); +} diff --git a/Interfaces_And_Abstract_Classes/src/IntArrayList.java b/Interfaces_And_Abstract_Classes/src/IntArrayList.java new file mode 100644 index 0000000..d137680 --- /dev/null +++ b/Interfaces_And_Abstract_Classes/src/IntArrayList.java @@ -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; + } +} \ No newline at end of file diff --git a/Interfaces_And_Abstract_Classes/src/IntList.java b/Interfaces_And_Abstract_Classes/src/IntList.java new file mode 100644 index 0000000..9ab6613 --- /dev/null +++ b/Interfaces_And_Abstract_Classes/src/IntList.java @@ -0,0 +1,4 @@ +public interface IntList { + void add(int number); + int get(int id); +} \ No newline at end of file diff --git a/Interfaces_And_Abstract_Classes/src/IntVector.java b/Interfaces_And_Abstract_Classes/src/IntVector.java new file mode 100644 index 0000000..9704ccd --- /dev/null +++ b/Interfaces_And_Abstract_Classes/src/IntVector.java @@ -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; + } +} \ No newline at end of file diff --git a/Interfaces_And_Abstract_Classes/src/Main.java b/Interfaces_And_Abstract_Classes/src/Main.java new file mode 100644 index 0000000..de0a969 --- /dev/null +++ b/Interfaces_And_Abstract_Classes/src/Main.java @@ -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()); + } +} \ No newline at end of file diff --git a/Interfaces_And_Abstract_Classes/src/Movie.java b/Interfaces_And_Abstract_Classes/src/Movie.java new file mode 100644 index 0000000..cc96492 --- /dev/null +++ b/Interfaces_And_Abstract_Classes/src/Movie.java @@ -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); + } +} \ No newline at end of file diff --git a/Interfaces_And_Abstract_Classes/src/Sedan.java b/Interfaces_And_Abstract_Classes/src/Sedan.java new file mode 100644 index 0000000..9219e05 --- /dev/null +++ b/Interfaces_And_Abstract_Classes/src/Sedan.java @@ -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); + } +} \ No newline at end of file diff --git a/Interfaces_And_Abstract_Classes/src/Truck.java b/Interfaces_And_Abstract_Classes/src/Truck.java new file mode 100644 index 0000000..b8508a4 --- /dev/null +++ b/Interfaces_And_Abstract_Classes/src/Truck.java @@ -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); + } +} + diff --git a/Interfaces_And_Abstract_Classes/src/TvSeries.java b/Interfaces_And_Abstract_Classes/src/TvSeries.java new file mode 100644 index 0000000..d4e1116 --- /dev/null +++ b/Interfaces_And_Abstract_Classes/src/TvSeries.java @@ -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); + } +} \ No newline at end of file diff --git a/Interfaces_And_Abstract_Classes/src/UtilityVehicle.java b/Interfaces_And_Abstract_Classes/src/UtilityVehicle.java new file mode 100644 index 0000000..faf263b --- /dev/null +++ b/Interfaces_And_Abstract_Classes/src/UtilityVehicle.java @@ -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); + } +} \ No newline at end of file diff --git a/Interfaces_And_Abstract_Classes/src/Video.java b/Interfaces_And_Abstract_Classes/src/Video.java new file mode 100644 index 0000000..66b0ba6 --- /dev/null +++ b/Interfaces_And_Abstract_Classes/src/Video.java @@ -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); + } +} \ No newline at end of file