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
62 changes: 62 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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

28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,34 @@ Once you finish the assignment, submit a URL link to your repository or your pul

<br>

## 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)

<br>

## FAQs

<br>
Expand Down
15 changes: 15 additions & 0 deletions src/BigDecimalOperations.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

51 changes: 51 additions & 0 deletions src/Car.java
Original file line number Diff line number Diff line change
@@ -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";
}
}

39 changes: 39 additions & 0 deletions src/IntArrayList.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

6 changes: 6 additions & 0 deletions src/IntList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

public interface IntList {
void add(int number);
int get(int id);
}

39 changes: 39 additions & 0 deletions src/IntVector.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

77 changes: 77 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
}

23 changes: 23 additions & 0 deletions src/Movie.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

13 changes: 13 additions & 0 deletions src/Sedan.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

23 changes: 23 additions & 0 deletions src/Truck.java
Original file line number Diff line number Diff line change
@@ -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";
}
}

Loading