diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eaf3c7d --- /dev/null +++ b/.gitignore @@ -0,0 +1,62 @@ +# Compiled class files +*.class + +# Log files +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# Virtual machine crash logs +hs_err_pid* + +# Maven +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar + +# IntelliJ IDEA +.idea/ +*.iws +*.iml +*.ipr +out/ + +# Eclipse +.classpath +.project +.settings/ +bin/ + +# NetBeans +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +# VS Code +.vscode/ + +# macOS +.DS_Store + diff --git a/README.md b/README.md index ce069b6..f71e30b 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,34 @@ Once you finish the assignment, submit a URL link to your repository or your pul
+## IntArrayList vs IntVector - Efficiency Analysis + +### When IntArrayList is More Efficient + +**IntArrayList** is more efficient when you have a **predictable growth pattern** with moderate additions to the list. Since it grows by 50% each time, it uses memory more conservatively and has less wasted space. + +**Example scenario:** +- You're building a shopping cart system where users typically add 10-20 items +- Starting with 10 slots and growing by 50% (10 → 15 → 22 → 33) is efficient +- Less memory overhead compared to doubling +- Fewer reallocations needed for moderate-sized collections + +### When IntVector is More Efficient + +**IntVector** is more efficient when you expect **rapid or unpredictable growth** in the number of elements. Doubling the capacity reduces the number of resize operations needed. + +**Example scenario:** +- Processing a large data stream where you might receive hundreds or thousands of elements +- Starting with 20 slots and doubling (20 → 40 → 80 → 160 → 320) reaches large capacities quickly +- Fewer copy operations overall when dealing with large datasets +- Better performance for bulk operations despite using more memory + +### Summary +- **IntArrayList**: Better for small to medium collections with predictable growth (more memory efficient) +- **IntVector**: Better for large or rapidly growing collections (fewer resize operations) + +
+ ## FAQs
diff --git a/src/BigDecimalOperations.java b/src/BigDecimalOperations.java new file mode 100644 index 0000000..1124bbf --- /dev/null +++ b/src/BigDecimalOperations.java @@ -0,0 +1,15 @@ + +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class BigDecimalOperations { + + public static double roundToNearestHundredth(BigDecimal number) { + return number.setScale(2, RoundingMode.HALF_UP).doubleValue(); + } + + public static double reverseSignAndRoundToNearestTenth(BigDecimal number) { + return number.negate().setScale(1, RoundingMode.HALF_UP).doubleValue(); + } +} + diff --git a/src/Car.java b/src/Car.java new file mode 100644 index 0000000..5cadd75 --- /dev/null +++ b/src/Car.java @@ -0,0 +1,51 @@ + +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 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; + } + + public String getInfo() { + return "VIN: " + vinNumber + ", Make: " + make + ", Model: " + model + ", Mileage: " + mileage + " miles"; + } +} + diff --git a/src/IntArrayList.java b/src/IntArrayList.java new file mode 100644 index 0000000..ecc8e33 --- /dev/null +++ b/src/IntArrayList.java @@ -0,0 +1,39 @@ + +public class IntArrayList implements IntList { + private int[] array; + private int size; + + public IntArrayList() { + this.array = new int[10]; + this.size = 0; + } + + @Override + public void add(int number) { + if (size == array.length) { + int newLength = (int) (array.length * 1.5); + int[] newArray = new int[newLength]; + System.arraycopy(array, 0, newArray, 0, array.length); + array = newArray; + } + array[size] = number; + size++; + } + + @Override + public int get(int id) { + if (id < 0 || id >= size) { + throw new IndexOutOfBoundsException("Index " + id + " is out of bounds"); + } + return array[id]; + } + + public int getSize() { + return size; + } + + public int getCapacity() { + return array.length; + } +} + diff --git a/src/IntList.java b/src/IntList.java new file mode 100644 index 0000000..fda674a --- /dev/null +++ b/src/IntList.java @@ -0,0 +1,6 @@ + +public interface IntList { + void add(int number); + int get(int id); +} + diff --git a/src/IntVector.java b/src/IntVector.java new file mode 100644 index 0000000..74b8407 --- /dev/null +++ b/src/IntVector.java @@ -0,0 +1,39 @@ + +public class IntVector implements IntList { + private int[] array; + private int size; + + public IntVector() { + this.array = new int[20]; + this.size = 0; + } + + @Override + public void add(int number) { + if (size == array.length) { + int newLength = array.length * 2; + int[] newArray = new int[newLength]; + System.arraycopy(array, 0, newArray, 0, array.length); + array = newArray; + } + array[size] = number; + size++; + } + + @Override + public int get(int id) { + if (id < 0 || id >= size) { + throw new IndexOutOfBoundsException("Index " + id + " is out of bounds"); + } + return array[id]; + } + + public int getSize() { + return size; + } + + public int getCapacity() { + return array.length; + } +} + diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..ffa00ca --- /dev/null +++ b/src/Main.java @@ -0,0 +1,77 @@ + +import java.math.BigDecimal; + +public class Main { + public static void main(String[] args) { + System.out.println("=== BigDecimal Operations ==="); + testBigDecimalOperations(); + + System.out.println("\n=== Car Inventory System ==="); + testCarInventory(); + + System.out.println("\n=== Video Streaming Service ==="); + testVideoStreaming(); + + System.out.println("\n=== IntList Interface ==="); + testIntList(); + } + + private static void testBigDecimalOperations() { + BigDecimal num1 = new BigDecimal("4.2545"); + System.out.println("Original: " + num1); + System.out.println("Rounded to nearest hundredth: " + + BigDecimalOperations.roundToNearestHundredth(num1)); + + BigDecimal num2 = new BigDecimal("1.2345"); + System.out.println("\nOriginal: " + num2); + System.out.println("Reversed sign and rounded to nearest tenth: " + + BigDecimalOperations.reverseSignAndRoundToNearestTenth(num2)); + + BigDecimal num3 = new BigDecimal("-45.67"); + System.out.println("\nOriginal: " + num3); + System.out.println("Reversed sign and rounded to nearest tenth: " + + BigDecimalOperations.reverseSignAndRoundToNearestTenth(num3)); + } + + private static void testCarInventory() { + Sedan sedan = new Sedan("1HGBH41JXMN109186", "Honda", "Accord", 25000); + UtilityVehicle suv = new UtilityVehicle("5FNRL6H74HB123456", "Honda", "CR-V", 15000, true); + Truck truck = new Truck("1FTFW1ET5EFC10312", "Ford", "F-150", 30000, 13200.0); + + System.out.println(sedan.getInfo()); + System.out.println(suv.getInfo()); + System.out.println(truck.getInfo()); + } + + private static void testVideoStreaming() { + TvSeries series = new TvSeries("Breaking Bad", 3000, 62); + Movie movie = new Movie("The Shawshank Redemption", 142, 9.3); + + System.out.println(series.getInfo()); + System.out.println(movie.getInfo()); + } + + private static void testIntList() { + System.out.println("Testing IntArrayList:"); + IntArrayList arrayList = new IntArrayList(); + for (int i = 0; i < 15; i++) { + arrayList.add(i * 10); + System.out.println("Added " + (i * 10) + " - Size: " + arrayList.getSize() + + ", Capacity: " + arrayList.getCapacity()); + } + + System.out.println("\nTesting IntVector:"); + IntVector vector = new IntVector(); + for (int i = 0; i < 25; i++) { + vector.add(i * 5); + System.out.println("Added " + (i * 5) + " - Size: " + vector.getSize() + + ", Capacity: " + vector.getCapacity()); + } + + System.out.println("\nRetrieving values from IntArrayList:"); + for (int i = 0; i < 5; i++) { + System.out.println("Index " + i + ": " + arrayList.get(i)); + } + } +} + diff --git a/src/Movie.java b/src/Movie.java new file mode 100644 index 0000000..7451d6e --- /dev/null +++ b/src/Movie.java @@ -0,0 +1,23 @@ + +public class Movie extends Video { + private double rating; + + public Movie(String title, int duration, double rating) { + super(title, duration); + this.rating = rating; + } + + public double getRating() { + return rating; + } + + public void setRating(double rating) { + this.rating = rating; + } + + @Override + public String getInfo() { + return "Movie - " + super.getInfo() + ", Rating: " + rating; + } +} + diff --git a/src/Sedan.java b/src/Sedan.java new file mode 100644 index 0000000..e0c5bb8 --- /dev/null +++ b/src/Sedan.java @@ -0,0 +1,13 @@ + +public class Sedan extends Car { + + public Sedan(String vinNumber, String make, String model, int mileage) { + super(vinNumber, make, model, mileage); + } + + @Override + public String getInfo() { + return "Sedan - " + super.getInfo(); + } +} + diff --git a/src/Truck.java b/src/Truck.java new file mode 100644 index 0000000..367c270 --- /dev/null +++ b/src/Truck.java @@ -0,0 +1,23 @@ + +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; + } + + public double getTowingCapacity() { + return towingCapacity; + } + + public void setTowingCapacity(double towingCapacity) { + this.towingCapacity = towingCapacity; + } + + @Override + public String getInfo() { + return "Truck - " + super.getInfo() + ", Towing Capacity: " + towingCapacity + " lbs"; + } +} + diff --git a/src/TvSeries.java b/src/TvSeries.java new file mode 100644 index 0000000..968b7ac --- /dev/null +++ b/src/TvSeries.java @@ -0,0 +1,23 @@ + +public class TvSeries extends Video { + private int episodes; + + public TvSeries(String title, int duration, int episodes) { + super(title, duration); + this.episodes = episodes; + } + + public int getEpisodes() { + return episodes; + } + + public void setEpisodes(int episodes) { + this.episodes = episodes; + } + + @Override + public String getInfo() { + return "TV Series - " + super.getInfo() + ", Episodes: " + episodes; + } +} + diff --git a/src/UtilityVehicle.java b/src/UtilityVehicle.java new file mode 100644 index 0000000..76ab877 --- /dev/null +++ b/src/UtilityVehicle.java @@ -0,0 +1,23 @@ + +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; + } + + public boolean isFourWheelDrive() { + return fourWheelDrive; + } + + public void setFourWheelDrive(boolean fourWheelDrive) { + this.fourWheelDrive = fourWheelDrive; + } + + @Override + public String getInfo() { + return "Utility Vehicle - " + super.getInfo() + ", Four Wheel Drive: " + fourWheelDrive; + } +} + diff --git a/src/Video.java b/src/Video.java new file mode 100644 index 0000000..733c68e --- /dev/null +++ b/src/Video.java @@ -0,0 +1,31 @@ + +public abstract class Video { + private String title; + private int duration; + + public Video(String title, int duration) { + this.title = title; + this.duration = duration; + } + + 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; + } + + public String getInfo() { + return "Title: " + title + ", Duration: " + duration + " minutes"; + } +} +