diff --git a/README.md b/README.md index ce069b6..294ec53 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,18 @@ Once you finish the assignment, submit a URL link to your repository or your pul 4. `IntVector` should store numbers in an array with a length of 20 by default. When the `add` method is called, you must first determine if the array is full. If it is, create a new array that is double the size of the current array, move all elements over to the new array and add the new element. (For example, an array of length 10 would be increased to 20.) 5. In your `README.md`, include an example of when `IntArrayList` would be more efficient and when `IntVector` would be more efficient. +#### IntArrayList vs IntVector Efficiency + +**When IntArrayList is more efficient:** +- **Memory-constrained environments**: IntArrayList uses less initial memory (default capacity of 10 vs 20) and grows incrementally by 50%, making it ideal when memory is limited. +- **Predictable, modest growth**: When you know the data will grow slowly and predictably over time, the 50% growth factor provides better memory utilization without excessive padding. +- **Example scenario**: A cache system that stores recent user queries where you expect to store 20-30 items over an extended period. + +**When IntVector is more efficient:** +- **Frequent array resizing**: When you add elements very frequently and unpredictably, IntVector's doubling strategy reduces the number of resize operations (fewer copies of existing elements). +- **High-performance requirements**: The fewer resize operations mean fewer array copy operations, reducing CPU overhead during intensive add operations. +- **Example scenario**: A real-time data collection system receiving streaming sensor data where rapid, continuous additions are expected. +
## FAQs diff --git a/src/main/java/com/lab/BigDecimalOperations.java b/src/main/java/com/lab/BigDecimalOperations.java new file mode 100644 index 0000000..297adb9 --- /dev/null +++ b/src/main/java/com/lab/BigDecimalOperations.java @@ -0,0 +1,30 @@ +package com.lab; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class BigDecimalOperations { + + /** + * Accepts a BigDecimal and returns a double rounded to the nearest hundredth. + * + * @param number the BigDecimal number to round + * @return a double rounded to the nearest hundredth + */ + public static double roundToNearestHundredth(BigDecimal number) { + BigDecimal rounded = number.setScale(2, RoundingMode.HALF_UP); + return rounded.doubleValue(); + } + + /** + * Accepts a BigDecimal, reverses the sign, rounds to the nearest tenth, and + * returns the result. + * + * @param number the BigDecimal number to process + * @return a BigDecimal with reversed sign and rounded to the nearest tenth + */ + public static BigDecimal reverseSignAndRoundToTenth(BigDecimal number) { + BigDecimal negated = number.negate(); + return negated.setScale(1, RoundingMode.HALF_UP); + } +} diff --git a/src/main/java/com/lab/Car.java b/src/main/java/com/lab/Car.java new file mode 100644 index 0000000..56eef0b --- /dev/null +++ b/src/main/java/com/lab/Car.java @@ -0,0 +1,52 @@ +package com.lab; + +public abstract class Car { + private String vinNumber; + private String make; + private String model; + private int mileage; + + public Car(String vinNumber, String make, String model, int mileage) { + this.vinNumber = vinNumber; + this.make = make; + this.model = model; + this.mileage = mileage; + } + + public String getInfo() { + return "VIN: " + vinNumber + ", Make: " + make + ", Model: " + model + ", Mileage: " + mileage; + } + + // Getters and setters + public String getVinNumber() { + return vinNumber; + } + + public void setVinNumber(String vinNumber) { + this.vinNumber = vinNumber; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public int getMileage() { + return mileage; + } + + public void setMileage(int mileage) { + this.mileage = mileage; + } +} diff --git a/src/main/java/com/lab/IntArrayList.java b/src/main/java/com/lab/IntArrayList.java new file mode 100644 index 0000000..305d40e --- /dev/null +++ b/src/main/java/com/lab/IntArrayList.java @@ -0,0 +1,40 @@ +package com.lab; + +public class IntArrayList implements IntList { + private int[] array; + private int size; + private static final int DEFAULT_CAPACITY = 10; + + public IntArrayList() { + this.array = new int[DEFAULT_CAPACITY]; + this.size = 0; + } + + @Override + public void add(int number) { + if (size >= array.length) { + int newCapacity = (int) (array.length * 1.5); + int[] newArray = new int[newCapacity]; + System.arraycopy(array, 0, newArray, 0, size); + array = newArray; + } + array[size] = number; + size++; + } + + @Override + public int get(int id) { + if (id < 0 || id >= size) { + throw new IndexOutOfBoundsException("Index: " + id + ", Size: " + size); + } + return array[id]; + } + + public int size() { + return size; + } + + public int capacity() { + return array.length; + } +} diff --git a/src/main/java/com/lab/IntList.java b/src/main/java/com/lab/IntList.java new file mode 100644 index 0000000..01a6f9f --- /dev/null +++ b/src/main/java/com/lab/IntList.java @@ -0,0 +1,18 @@ +package com.lab; + +public interface IntList { + /** + * Adds a new number to the list + * + * @param number the number to add + */ + void add(int number); + + /** + * Retrieves an element by its ID (index) + * + * @param id the index of the element + * @return the element at the specified index + */ + int get(int id); +} diff --git a/src/main/java/com/lab/IntVector.java b/src/main/java/com/lab/IntVector.java new file mode 100644 index 0000000..aa3d5c7 --- /dev/null +++ b/src/main/java/com/lab/IntVector.java @@ -0,0 +1,40 @@ +package com.lab; + +public class IntVector implements IntList { + private int[] array; + private int size; + private static final int DEFAULT_CAPACITY = 20; + + public IntVector() { + this.array = new int[DEFAULT_CAPACITY]; + this.size = 0; + } + + @Override + public void add(int number) { + if (size >= array.length) { + int newCapacity = array.length * 2; + int[] newArray = new int[newCapacity]; + System.arraycopy(array, 0, newArray, 0, size); + array = newArray; + } + array[size] = number; + size++; + } + + @Override + public int get(int id) { + if (id < 0 || id >= size) { + throw new IndexOutOfBoundsException("Index: " + id + ", Size: " + size); + } + return array[id]; + } + + public int size() { + return size; + } + + public int capacity() { + return array.length; + } +} diff --git a/src/main/java/com/lab/Main.java b/src/main/java/com/lab/Main.java new file mode 100644 index 0000000..06cb145 --- /dev/null +++ b/src/main/java/com/lab/Main.java @@ -0,0 +1,68 @@ +package com.lab; + +import java.math.BigDecimal; + +public class Main { + public static void main(String[] args) { + System.out.println("=== BigDecimal Operations ==="); + + // Test roundToNearestHundredth + BigDecimal num1 = new BigDecimal("4.2545"); + double rounded = BigDecimalOperations.roundToNearestHundredth(num1); + System.out.println("4.2545 rounded to nearest hundredth: " + rounded); + + // Test reverseSignAndRoundToTenth + BigDecimal num2 = new BigDecimal("1.2345"); + BigDecimal result1 = BigDecimalOperations.reverseSignAndRoundToTenth(num2); + System.out.println("1.2345 with reversed sign and rounded to tenth: " + result1); + + BigDecimal num3 = new BigDecimal("-45.67"); + BigDecimal result2 = BigDecimalOperations.reverseSignAndRoundToTenth(num3); + System.out.println("-45.67 with reversed sign and rounded to tenth: " + result2); + + System.out.println("\n=== Car Inventory System ==="); + + Sedan sedan = new Sedan("VIN123456", "Toyota", "Camry", 45000); + System.out.println("Sedan: " + sedan.getInfo()); + + UtilityVehicle uv = new UtilityVehicle("VIN234567", "Ford", "Explorer", 35000, true); + System.out.println("Utility Vehicle: " + uv.getInfo()); + + Truck truck = new Truck("VIN345678", "Chevrolet", "Silverado", 60000, 12000.5); + System.out.println("Truck: " + truck.getInfo()); + + System.out.println("\n=== Video Streaming Service ==="); + + Movie movie = new Movie("The Matrix", 136, 8.7); + System.out.println("Movie: " + movie.getInfo()); + + TvSeries tvSeries = new TvSeries("Breaking Bad", 47, 62); + System.out.println("TV Series: " + tvSeries.getInfo()); + + System.out.println("\n=== IntList Interface ==="); + + // Test IntArrayList + System.out.println("IntArrayList (capacity: 10, grows by 50%):"); + IntArrayList arrayList = new IntArrayList(); + for (int i = 0; i < 25; i++) { + arrayList.add(i); + if (i + 1 == 10 || i + 1 == 15 || i + 1 == 25) { + System.out.println(" After adding " + (i + 1) + " elements: capacity = " + arrayList.capacity()); + } + } + System.out.println(" Get element at index 5: " + arrayList.get(5)); + System.out.println(" Get element at index 24: " + arrayList.get(24)); + + // Test IntVector + System.out.println("\nIntVector (capacity: 20, grows by 100%):"); + IntVector vector = new IntVector(); + for (int i = 0; i < 25; i++) { + vector.add(i); + if (i + 1 == 20 || i + 1 == 40) { + System.out.println(" After adding " + (i + 1) + " elements: capacity = " + vector.capacity()); + } + } + System.out.println(" Get element at index 10: " + vector.get(10)); + System.out.println(" Get element at index 24: " + vector.get(24)); + } +} diff --git a/src/main/java/com/lab/Movie.java b/src/main/java/com/lab/Movie.java new file mode 100644 index 0000000..e38d9dc --- /dev/null +++ b/src/main/java/com/lab/Movie.java @@ -0,0 +1,23 @@ +package com.lab; + +public class Movie extends Video { + private double rating; + + public Movie(String title, int duration, double rating) { + super(title, duration); + this.rating = rating; + } + + @Override + public String getInfo() { + return super.getInfo() + ", Rating: " + rating; + } + + public double getRating() { + return rating; + } + + public void setRating(double rating) { + this.rating = rating; + } +} diff --git a/src/main/java/com/lab/Sedan.java b/src/main/java/com/lab/Sedan.java new file mode 100644 index 0000000..1922fd0 --- /dev/null +++ b/src/main/java/com/lab/Sedan.java @@ -0,0 +1,8 @@ +package com.lab; + +public class Sedan extends Car { + + public Sedan(String vinNumber, String make, String model, int mileage) { + super(vinNumber, make, model, mileage); + } +} diff --git a/src/main/java/com/lab/Truck.java b/src/main/java/com/lab/Truck.java new file mode 100644 index 0000000..ebf9332 --- /dev/null +++ b/src/main/java/com/lab/Truck.java @@ -0,0 +1,23 @@ +package com.lab; + +public class Truck extends Car { + private double towingCapacity; + + public Truck(String vinNumber, String make, String model, int mileage, double towingCapacity) { + super(vinNumber, make, model, mileage); + this.towingCapacity = towingCapacity; + } + + @Override + public String getInfo() { + return super.getInfo() + ", Towing Capacity: " + towingCapacity; + } + + public double getTowingCapacity() { + return towingCapacity; + } + + public void setTowingCapacity(double towingCapacity) { + this.towingCapacity = towingCapacity; + } +} diff --git a/src/main/java/com/lab/TvSeries.java b/src/main/java/com/lab/TvSeries.java new file mode 100644 index 0000000..fd448af --- /dev/null +++ b/src/main/java/com/lab/TvSeries.java @@ -0,0 +1,23 @@ +package com.lab; + +public class TvSeries extends Video { + private int episodes; + + public TvSeries(String title, int duration, int episodes) { + super(title, duration); + this.episodes = episodes; + } + + @Override + public String getInfo() { + return super.getInfo() + ", Episodes: " + episodes; + } + + public int getEpisodes() { + return episodes; + } + + public void setEpisodes(int episodes) { + this.episodes = episodes; + } +} diff --git a/src/main/java/com/lab/UtilityVehicle.java b/src/main/java/com/lab/UtilityVehicle.java new file mode 100644 index 0000000..99758d2 --- /dev/null +++ b/src/main/java/com/lab/UtilityVehicle.java @@ -0,0 +1,23 @@ +package com.lab; + +public class UtilityVehicle extends Car { + private boolean fourWheelDrive; + + public UtilityVehicle(String vinNumber, String make, String model, int mileage, boolean fourWheelDrive) { + super(vinNumber, make, model, mileage); + this.fourWheelDrive = fourWheelDrive; + } + + @Override + public String getInfo() { + return super.getInfo() + ", Four Wheel Drive: " + fourWheelDrive; + } + + public boolean isFourWheelDrive() { + return fourWheelDrive; + } + + public void setFourWheelDrive(boolean fourWheelDrive) { + this.fourWheelDrive = fourWheelDrive; + } +} diff --git a/src/main/java/com/lab/Video.java b/src/main/java/com/lab/Video.java new file mode 100644 index 0000000..3213d56 --- /dev/null +++ b/src/main/java/com/lab/Video.java @@ -0,0 +1,32 @@ +package com.lab; + +public abstract class Video { + private String title; + private int duration; + + public Video(String title, int duration) { + this.title = title; + this.duration = duration; + } + + public String getInfo() { + return "Title: " + title + ", Duration: " + duration + " minutes"; + } + + // Getters and setters + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getDuration() { + return duration; + } + + public void setDuration(int duration) { + this.duration = duration; + } +} diff --git a/target/classes/com/lab/BigDecimalOperations.class b/target/classes/com/lab/BigDecimalOperations.class new file mode 100644 index 0000000..39ce3ea Binary files /dev/null and b/target/classes/com/lab/BigDecimalOperations.class differ diff --git a/target/classes/com/lab/Car.class b/target/classes/com/lab/Car.class new file mode 100644 index 0000000..d32d406 Binary files /dev/null and b/target/classes/com/lab/Car.class differ diff --git a/target/classes/com/lab/IntArrayList.class b/target/classes/com/lab/IntArrayList.class new file mode 100644 index 0000000..2d7a8d9 Binary files /dev/null and b/target/classes/com/lab/IntArrayList.class differ diff --git a/target/classes/com/lab/IntList.class b/target/classes/com/lab/IntList.class new file mode 100644 index 0000000..823ea8a Binary files /dev/null and b/target/classes/com/lab/IntList.class differ diff --git a/target/classes/com/lab/IntVector.class b/target/classes/com/lab/IntVector.class new file mode 100644 index 0000000..bd9e9f2 Binary files /dev/null and b/target/classes/com/lab/IntVector.class differ diff --git a/target/classes/com/lab/Main.class b/target/classes/com/lab/Main.class new file mode 100644 index 0000000..c810467 Binary files /dev/null and b/target/classes/com/lab/Main.class differ diff --git a/target/classes/com/lab/Movie.class b/target/classes/com/lab/Movie.class new file mode 100644 index 0000000..5d2a828 Binary files /dev/null and b/target/classes/com/lab/Movie.class differ diff --git a/target/classes/com/lab/Sedan.class b/target/classes/com/lab/Sedan.class new file mode 100644 index 0000000..77eb8e0 Binary files /dev/null and b/target/classes/com/lab/Sedan.class differ diff --git a/target/classes/com/lab/Truck.class b/target/classes/com/lab/Truck.class new file mode 100644 index 0000000..0546d1f Binary files /dev/null and b/target/classes/com/lab/Truck.class differ diff --git a/target/classes/com/lab/TvSeries.class b/target/classes/com/lab/TvSeries.class new file mode 100644 index 0000000..ddddc65 Binary files /dev/null and b/target/classes/com/lab/TvSeries.class differ diff --git a/target/classes/com/lab/UtilityVehicle.class b/target/classes/com/lab/UtilityVehicle.class new file mode 100644 index 0000000..b81eb26 Binary files /dev/null and b/target/classes/com/lab/UtilityVehicle.class differ diff --git a/target/classes/com/lab/Video.class b/target/classes/com/lab/Video.class new file mode 100644 index 0000000..c0c8210 Binary files /dev/null and b/target/classes/com/lab/Video.class differ