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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<br>

## FAQs
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/lab/BigDecimalOperations.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/lab/Car.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/lab/IntArrayList.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/lab/IntList.java
Original file line number Diff line number Diff line change
@@ -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);
}
40 changes: 40 additions & 0 deletions src/main/java/com/lab/IntVector.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
68 changes: 68 additions & 0 deletions src/main/java/com/lab/Main.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/lab/Movie.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/lab/Sedan.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/lab/Truck.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/lab/TvSeries.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/lab/UtilityVehicle.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading