diff --git a/BigDecimalTasks/BigDecimalTask.class b/BigDecimalTasks/BigDecimalTask.class new file mode 100644 index 0000000..82409be Binary files /dev/null and b/BigDecimalTasks/BigDecimalTask.class differ diff --git a/BigDecimalTasks/BigDecimalTask.java b/BigDecimalTasks/BigDecimalTask.java new file mode 100644 index 0000000..f56aab7 --- /dev/null +++ b/BigDecimalTasks/BigDecimalTask.java @@ -0,0 +1,65 @@ +import java.math.BigDecimal; +import java.math.RoundingMode; + +/** + * Utility class providing precise rounding operations using BigDecimal. + * All methods handle null input safely by throwing IllegalArgumentException. + */ +public class BigDecimalTask { + + /** + * Rounds the given BigDecimal to 2 decimal places (nearest hundredth) + * using HALF_UP rounding mode and returns the result as a double. + * + * @param value the BigDecimal value to round + * @return the rounded value as a primitive double + * @throws IllegalArgumentException if value is null + */ + public static double roundToNearestHundredth(BigDecimal value) { + if (value == null) { + throw new IllegalArgumentException("Value cannot be null"); + } + return value.setScale(2, RoundingMode.HALF_UP).doubleValue(); + } + + /** + * Negates the given BigDecimal (changes its sign) and rounds the result + * to 1 decimal place (nearest tenth) using HALF_UP rounding mode. + * + * Examples: + * 1.2345 → -1.2 + * -45.67 → 45.7 + * + * @param value the BigDecimal value to negate and round + * @return a new BigDecimal with negated sign and rounded to 1 decimal place + * @throws IllegalArgumentException if value is null + */ + public static BigDecimal negateAndRoundToTenth(BigDecimal value) { + if (value == null) { + throw new IllegalArgumentException("Value cannot be null"); + } + return value.negate().setScale(1, RoundingMode.HALF_UP); + } + + /** + * Main method containing demonstration/test cases. + * Can be safely removed before final submission if not required. + */ + public static void main(String[] args) { + // Test case 1: rounding to hundredth + BigDecimal a = new BigDecimal("4.2545"); + System.out.println("4.2545 rounded to 2 decimals → " + roundToNearestHundredth(a)); // Expected: 4.25 + + // Test case 2: positive → negative, round to tenth + BigDecimal b = new BigDecimal("1.2345"); + System.out.println("1.2345 negated & rounded to 1 decimal → " + negateAndRoundToTenth(b)); // Expected: -1.2 + + // Test case 3: negative → positive, round to tenth + BigDecimal c = new BigDecimal("-45.67"); + System.out.println("-45.67 negated & rounded to 1 decimal → " + negateAndRoundToTenth(c)); // Expected: 45.7 + + // Additional edge case: exact value (no rounding needed) + BigDecimal d = new BigDecimal("10.0"); + System.out.println("10.0 negated & rounded → " + negateAndRoundToTenth(d)); // Expected: -10.0 + } +} \ No newline at end of file diff --git a/CarInventorySystem/Car.class b/CarInventorySystem/Car.class new file mode 100644 index 0000000..dfec04e Binary files /dev/null and b/CarInventorySystem/Car.class differ diff --git a/CarInventorySystem/CarHierarchy.class b/CarInventorySystem/CarHierarchy.class new file mode 100644 index 0000000..861062a Binary files /dev/null and b/CarInventorySystem/CarHierarchy.class differ diff --git a/CarInventorySystem/CarHierarchy.java b/CarInventorySystem/CarHierarchy.java new file mode 100644 index 0000000..0524deb --- /dev/null +++ b/CarInventorySystem/CarHierarchy.java @@ -0,0 +1,171 @@ +/** + * Abstract base class representing any type of car in the inventory system. + * All cars share common properties: VIN number, make, model, and mileage. + * Concrete subclasses (Sedan, UtilityVehicle, Truck) extend this class + * and may add type-specific properties. + */ +abstract class Car { + + private final String vinNumber; + private final String make; + private final String model; + private final int mileage; + + /** + * Constructs a new Car with the specified properties. + * All fields are final and immutable after construction. + * + * @param vinNumber unique Vehicle Identification Number (17 characters typically) + * @param make manufacturer of the vehicle (e.g., Toyota, Ford) + * @param model model name of the vehicle (e.g., Civic, F-150) + * @param mileage current mileage in kilometers + */ + public Car(String vinNumber, String make, String model, int mileage) { + this.vinNumber = vinNumber; + this.make = make; + this.model = model; + this.mileage = mileage; + } + + /** + * Returns a formatted string containing all common car properties. + * Subclasses should override this method to include type-specific information. + * + * @return a readable string representation of the car's common properties + */ + public String getInfo() { + return String.format("VIN: %s | Make: %s | Model: %s | Mileage: %d km", + vinNumber, make, model, mileage); + } + + // Getters – useful for future extensions (search, filtering, etc.) + public String getVinNumber() { return vinNumber; } + public String getMake() { return make; } + public String getModel() { return model; } + public int getMileage() { return mileage; } +} + +/** + * Represents a standard passenger Sedan. + * Inherits all properties from Car; no additional fields. + */ +class Sedan extends Car { + + public Sedan(String vinNumber, String make, String model, int mileage) { + super(vinNumber, make, model, mileage); + } + + /** + * Returns car information including the specific type "Sedan". + * + * @return formatted string with all properties + vehicle type + */ + @Override + public String getInfo() { + return super.getInfo() + " | Type: Sedan"; + } +} + +/** + * Represents a Utility Vehicle (SUV, crossover, etc.). + * Adds a four-wheel-drive indicator. + */ +class UtilityVehicle extends Car { + + private final boolean fourWheelDrive; + + /** + * Constructs a UtilityVehicle with four-wheel-drive specification. + * + * @param vinNumber unique VIN + * @param make manufacturer + * @param model model name + * @param mileage current mileage in km + * @param fourWheelDrive true if the vehicle has 4WD / AWD + */ + public UtilityVehicle(String vinNumber, String make, String model, + int mileage, boolean fourWheelDrive) { + super(vinNumber, make, model, mileage); + this.fourWheelDrive = fourWheelDrive; + } + + /** + * Returns car information including type and 4WD status. + * + * @return formatted string with all properties + type + 4WD info + */ + @Override + public String getInfo() { + return super.getInfo() + + " | Type: Utility Vehicle | 4WD: " + (fourWheelDrive ? "Yes" : "No"); + } + + public boolean hasFourWheelDrive() { + return fourWheelDrive; + } +} + +/** + * Represents a pickup Truck. + * Adds towing capacity in kilograms or pounds (depending on context). + */ +class Truck extends Car { + + private final double towingCapacity; + + /** + * Constructs a Truck with specified towing capacity. + * + * @param vinNumber unique VIN + * @param make manufacturer + * @param model model name + * @param mileage current mileage in km + * @param towingCapacity maximum towing capacity (suggested unit: kg or lbs) + */ + public Truck(String vinNumber, String make, String model, + int mileage, double towingCapacity) { + super(vinNumber, make, model, mileage); + this.towingCapacity = towingCapacity; + } + + /** + * Returns car information including type and towing capacity. + * Capacity is shown with one decimal place. + * + * @return formatted string with all properties + type + towing info + */ + @Override + public String getInfo() { + return super.getInfo() + + String.format(" | Type: Truck | Towing: %.1f kg", towingCapacity); + } + + public double getTowingCapacity() { + return towingCapacity; + } +} + +/** + * Demonstration of the car hierarchy and polymorphic behavior. + */ +public class CarHierarchy { + + public static void main(String[] args) { + Car sedan = new Sedan("1HGCM82633A004352", "Honda", "Civic", 85000); + Car suv = new UtilityVehicle("5XYZG6789K1234567", "Toyota", "RAV4", 45000, true); + Car truck = new Truck("1FTFW1ETXEKD98765", "Ford", "F-150", 120000, 12000.5); + + // Polymorphic calls – all use the overridden getInfo() + System.out.println(sedan.getInfo()); + System.out.println(suv.getInfo()); + System.out.println(truck.getInfo()); + + // Optional: showing access to subclass-specific properties + if (suv instanceof UtilityVehicle uv) { + System.out.println(" → Has 4WD: " + uv.hasFourWheelDrive()); + } + if (truck instanceof Truck t) { + System.out.println(" → Towing capacity: " + t.getTowingCapacity() + " kg"); + } + } +} \ No newline at end of file diff --git a/CarInventorySystem/Sedan.class b/CarInventorySystem/Sedan.class new file mode 100644 index 0000000..cc94f97 Binary files /dev/null and b/CarInventorySystem/Sedan.class differ diff --git a/CarInventorySystem/Truck.class b/CarInventorySystem/Truck.class new file mode 100644 index 0000000..fb9d1ce Binary files /dev/null and b/CarInventorySystem/Truck.class differ diff --git a/CarInventorySystem/UtilityVehicle.class b/CarInventorySystem/UtilityVehicle.class new file mode 100644 index 0000000..548100b Binary files /dev/null and b/CarInventorySystem/UtilityVehicle.class differ diff --git a/IntListTask/IntArrayList.class b/IntListTask/IntArrayList.class new file mode 100644 index 0000000..8a84981 Binary files /dev/null and b/IntListTask/IntArrayList.class differ diff --git a/IntListTask/IntList.class b/IntListTask/IntList.class new file mode 100644 index 0000000..d6284a1 Binary files /dev/null and b/IntListTask/IntList.class differ diff --git a/IntListTask/IntVector.class b/IntListTask/IntVector.class new file mode 100644 index 0000000..83685f3 Binary files /dev/null and b/IntListTask/IntVector.class differ diff --git a/IntListTask/Main.class b/IntListTask/Main.class new file mode 100644 index 0000000..4173470 Binary files /dev/null and b/IntListTask/Main.class differ diff --git a/IntListTask/Main.java b/IntListTask/Main.java new file mode 100644 index 0000000..f7de6aa --- /dev/null +++ b/IntListTask/Main.java @@ -0,0 +1,184 @@ +/** + * Interface defining the basic operations for a dynamic list of integers. + * Implementations must support adding elements and retrieving them by index. + */ +interface IntList { + + /** + * Appends the specified integer to the end of this list. + * + * @param number the integer to be added + */ + void add(int number); + + /** + * Returns the element at the specified position in this list. + * + * @param index index of the element to return + * @return the element at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + int get(int index); + + /** + * Returns the number of elements in this list. + * + * @return the number of elements currently stored + */ + int size(); +} + +/** + * Resizable array-based implementation of IntList. + * Grows by 50% when capacity is reached (with +1 to avoid staying at 0). + * Initial capacity: 10. + */ +class IntArrayList implements IntList { + + private static final int DEFAULT_CAPACITY = 10; + private static final double GROWTH_FACTOR = 1.5; + + private int[] elements; + private int size; + + /** + * Constructs an empty IntArrayList with an initial capacity of 10. + */ + public IntArrayList() { + this.elements = new int[DEFAULT_CAPACITY]; + this.size = 0; + } + + @Override + public void add(int number) { + ensureCapacity(); + elements[size++] = number; + } + + /** + * Increases the capacity if the array is full. + * New capacity = current capacity * 1.5 + 1 (rounded down). + */ + private void ensureCapacity() { + if (size == elements.length) { + int newCapacity = (int) (elements.length * GROWTH_FACTOR) + 1; + int[] newElements = new int[newCapacity]; + System.arraycopy(elements, 0, newElements, 0, size); + elements = newElements; + } + } + + @Override + public int get(int index) { + checkIndex(index); + return elements[index]; + } + + @Override + public int size() { + return size; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException( + "Index: " + index + ", Size: " + size); + } + } +} + +/** + * Resizable array-based implementation of IntList (Vector-style). + * Doubles capacity when full. + * Initial capacity: 20. + */ +class IntVector implements IntList { + + private static final int DEFAULT_CAPACITY = 20; + private static final int GROWTH_FACTOR = 2; + + private int[] elements; + private int size; + + /** + * Constructs an empty IntVector with an initial capacity of 20. + */ + public IntVector() { + this.elements = new int[DEFAULT_CAPACITY]; + this.size = 0; + } + + @Override + public void add(int number) { + ensureCapacity(); + elements[size++] = number; + } + + /** + * Increases the capacity if the array is full. + * New capacity = current capacity * 2. + */ + private void ensureCapacity() { + if (size == elements.length) { + int newCapacity = elements.length * GROWTH_FACTOR; + int[] newElements = new int[newCapacity]; + System.arraycopy(elements, 0, newElements, 0, size); + elements = newElements; + } + } + + @Override + public int get(int index) { + checkIndex(index); + return elements[index]; + } + + @Override + public int size() { + return size; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException( + "Index: " + index + ", Size: " + size); + } + } +} + +/** + * Demonstration of IntArrayList and IntVector usage. + */ +public class Main { + + public static void main(String[] args) { + System.out.println("=== IntArrayList Demo ==="); + IntList arrayList = new IntArrayList(); + addDemoElements(arrayList); + printList("IntArrayList", arrayList); + + System.out.println("\n=== IntVector Demo ==="); + IntList vector = new IntVector(); + addDemoElements(vector); + printList("IntVector", vector); + } + + private static void addDemoElements(IntList list) { + for (int i = 1; i <= 25; i++) { + list.add(i * 10); + } + } + + private static void printList(String name, IntList list) { + System.out.println(name + " size: " + list.size()); + System.out.print("Elements: ["); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i)); + if (i < list.size() - 1) { + System.out.print(", "); + } + } + System.out.println("]"); + } +} \ No newline at end of file diff --git a/IntListTask/README.md b/IntListTask/README.md new file mode 100644 index 0000000..cd46b68 --- /dev/null +++ b/IntListTask/README.md @@ -0,0 +1,30 @@ +# IntList Implementations + +## Overview +Two dynamic array-based implementations of the `IntList` interface: + +- **IntArrayList** — starts with capacity 10, grows by **50%** (+1) when full +- **IntVector** — starts with capacity 20, **doubles** capacity when full + +Both use `System.arraycopy` for efficient resizing and throw proper exceptions on invalid index access. + +## When to use IntArrayList (more efficient) +→ When you expect the list to grow slowly or stay relatively small most of the time. +→ 50% growth uses less memory in the long run compared to doubling, especially for many small-to-medium lists. +→ Fewer large memory allocations → better for memory-constrained environments or when many short-lived lists are created. + +Example scenario: storing recent user actions in a mobile app session (usually < 100 elements). + +## When to use IntVector (more efficient) +→ When you expect the list to grow very large or you want to minimize the number of resize operations. +→ Doubling reduces the total number of copies performed over many additions (amortized O(1) with better constant factors for very large sizes). +→ Fewer resizes → better performance when inserting thousands or millions of elements. + +Example scenario: reading a large log file into memory, building a dataset for machine learning, or any bulk data import where final size is potentially very big. + +## Summary – Choose based on expected size & growth pattern + +| Scenario | Recommended | Reason | +|-----------------------------------|------------------|-------------------------------------| +| Small to medium lists | IntArrayList | Lower memory overhead | +| Very large lists, bulk inserts | IntVector | Fewer resizes, better amortized cost| \ No newline at end of file diff --git a/VideoStreamingService/Movie.class b/VideoStreamingService/Movie.class new file mode 100644 index 0000000..ffbf635 Binary files /dev/null and b/VideoStreamingService/Movie.class differ diff --git a/VideoStreamingService/TvSeries.class b/VideoStreamingService/TvSeries.class new file mode 100644 index 0000000..19e1f5e Binary files /dev/null and b/VideoStreamingService/TvSeries.class differ diff --git a/VideoStreamingService/Video.class b/VideoStreamingService/Video.class new file mode 100644 index 0000000..0ba7647 Binary files /dev/null and b/VideoStreamingService/Video.class differ diff --git a/VideoStreamingService/VideoHierarchy.class b/VideoStreamingService/VideoHierarchy.class new file mode 100644 index 0000000..0e146cd Binary files /dev/null and b/VideoStreamingService/VideoHierarchy.class differ diff --git a/VideoStreamingService/VideoHierarchy.java b/VideoStreamingService/VideoHierarchy.java new file mode 100644 index 0000000..2e5d846 --- /dev/null +++ b/VideoStreamingService/VideoHierarchy.java @@ -0,0 +1,150 @@ +/** + * Abstract base class representing any video content in the streaming service. + * All videos share common properties: title and duration in minutes. + * Concrete subclasses (TvSeries, Movie) extend this class and add type-specific details. + */ +abstract class Video { + + private final String title; + private final int durationMinutes; + + /** + * Constructs a new Video with the specified title and duration. + * + * @param title the title of the video + * @param durationMinutes the total duration in minutes (must be positive) + */ + public Video(String title, int durationMinutes) { + if (title == null || title.trim().isEmpty()) { + throw new IllegalArgumentException("Title cannot be null or empty"); + } + if (durationMinutes <= 0) { + throw new IllegalArgumentException("Duration must be positive"); + } + this.title = title.trim(); + this.durationMinutes = durationMinutes; + } + + /** + * Returns a formatted string with the common video properties. + * Subclasses should override this method to append type-specific information. + * + * @return a readable string representation of the video's common properties + */ + public String getInfo() { + return String.format("Title: \"%s\" | Duration: %d min", title, durationMinutes); + } + + // Getters – useful for future extensions (search, filtering, recommendations, etc.) + public String getTitle() { + return title; + } + + public int getDurationMinutes() { + return durationMinutes; + } +} + +/** + * Represents a TV series consisting of multiple episodes. + */ +class TvSeries extends Video { + + private final int episodeCount; + + /** + * Constructs a TV Series with the specified title, episode duration, and total episode count. + * + * @param title series title + * @param durationMinutes typical duration of one episode in minutes + * @param episodeCount total number of episodes in the series + */ + public TvSeries(String title, int durationMinutes, int episodeCount) { + super(title, durationMinutes); + if (episodeCount <= 0) { + throw new IllegalArgumentException("Episode count must be positive"); + } + this.episodeCount = episodeCount; + } + + /** + * Returns video information including series-specific details (type and episode count). + * + * @return formatted string with all properties + type + episodes + */ + @Override + public String getInfo() { + return super.getInfo() + + String.format(" | Type: TV Series | Episodes: %d", episodeCount); + } + + public int getEpisodeCount() { + return episodeCount; + } +} + +/** + * Represents a single standalone movie. + */ +class Movie extends Video { + + private final double rating; + + /** + * Constructs a Movie with the specified title, duration, and rating. + * + * @param title movie title + * @param durationMinutes total runtime in minutes + * @param rating average/user rating (typically 0.0–10.0) + */ + public Movie(String title, int durationMinutes, double rating) { + super(title, durationMinutes); + if (rating < 0.0 || rating > 10.0) { + throw new IllegalArgumentException("Rating must be between 0.0 and 10.0"); + } + this.rating = rating; + } + + /** + * Returns video information including movie-specific details (type and rating). + * Rating is formatted to one decimal place. + * + * @return formatted string with all properties + type + rating + */ + @Override + public String getInfo() { + return super.getInfo() + + String.format(" | Type: Movie | Rating: %.1f", rating); + } + + public double getRating() { + return rating; + } +} + +/** + * Demonstration of the video hierarchy and polymorphic behavior. + */ +public class VideoHierarchy { + + public static void main(String[] args) { + Video series = new TvSeries("Breaking Bad", 47, 62); + Video movie = new Movie("Inception", 148, 8.8); + Video shortFilm = new Movie("Pi", 84, 7.4); + + // Polymorphic calls – each uses its own overridden getInfo() + System.out.println(series.getInfo()); + System.out.println(movie.getInfo()); + System.out.println(shortFilm.getInfo()); + + // Optional: showing access to subclass-specific properties + if (series instanceof TvSeries tv) { + System.out.printf(" → Total episodes: %d%n", tv.getEpisodeCount()); + } + if (movie instanceof Movie m) { + System.out.printf(" → IMDb-style rating: %.1f/10%n", m.getRating()); + } + } +} + +// Note: The main method serves as a demonstration of the class hierarchy and polymorphism. \ No newline at end of file