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
18 changes: 18 additions & 0 deletions src/main/java/BigDecimalUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class BigDecimalUtils {
/**
* Rounds the given BigDecimal to the nearest hundredth and returns a double.
*/
public static double roundToHundredths(java.math.BigDecimal value) {
if (value == null) return 0.0;
return value.setScale(2, java.math.RoundingMode.HALF_UP).doubleValue();
}

/**
* Reverses the sign of the given BigDecimal, rounds to nearest tenth, and returns a double.
*/
public static double reverseSignAndRoundToTenth(java.math.BigDecimal value) {
if (value == null) return 0.0;
java.math.BigDecimal reversed = value.negate();
return reversed.setScale(1, java.math.RoundingMode.HALF_UP).doubleValue();
}
}
17 changes: 17 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public abstract class Car {
protected String vinNumber;
protected String make;
protected String model;
protected 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 String.format("VIN: %s, Make: %s, Model: %s, Mileage: %d", vinNumber, make, model, mileage);
}
}
32 changes: 32 additions & 0 deletions src/main/java/IntArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.Arrays;

public class IntArrayList implements IntList {
private int[] data;
private int size;

public IntArrayList() {
this.data = new int[10];
this.size = 0;
}

@Override
public void add(int number) {
ensureCapacity();
data[size++] = number;
}

private void ensureCapacity() {
if (size >= data.length) {
int newLength = data.length + data.length / 2; // 50% larger
data = Arrays.copyOf(data, newLength);
}
}

@Override
public int get(int id) {
if (id < 0 || id >= size) {
throw new IndexOutOfBoundsException();
}
return data[id];
}
}
4 changes: 4 additions & 0 deletions src/main/java/IntList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public interface IntList {
void add(int number);
int get(int id);
}
32 changes: 32 additions & 0 deletions src/main/java/IntVector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.Arrays;

public class IntVector implements IntList {
private int[] data;
private int size;

public IntVector() {
this.data = new int[20];
this.size = 0;
}

@Override
public void add(int number) {
ensureCapacity();
data[size++] = number;
}

private void ensureCapacity() {
if (size >= data.length) {
int newLength = data.length * 2;
data = Arrays.copyOf(data, newLength);
}
}

@Override
public int get(int id) {
if (id < 0 || id >= size) {
throw new IndexOutOfBoundsException();
}
return data[id];
}
}
37 changes: 37 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.math.BigDecimal;

public class Main {
public static void main(String[] args) {
// BigDecimal utilities demo
BigDecimal bd1 = new BigDecimal("4.2545");
System.out.println("Rounded to hundredths: " +
BigDecimalUtils.roundToHundredths(bd1)); // 4.25

BigDecimal bd2 = new BigDecimal("1.2345");
System.out.println("Reverse sign & round to tenth: " +
BigDecimalUtils.reverseSignAndRoundToTenth(bd2)); // -1.2

// Car hierarchy demo
Car sedan = new Sedan("1HGBH41JXMN109186", "Toyota", "Camry", 30000);
System.out.println(sedan.getInfo());
Car uv = new UtilityVehicle("2FTRX18W1XCA12345", "Ford", "Explorer", 45000, true);
System.out.println(uv.getInfo());
Car truck = new Truck("3C6UR5FL8GE123456", "RAM", "2500", 120000, 7500.0);
System.out.println(truck.getInfo());

// Video hierarchy demo
Video series = new TvSeries("Stranger Things", 50, 25);
System.out.println(series.getInfo());
Video movie = new Movie("Inception", 148, 8.8);
System.out.println(movie.getInfo());

// IntList demos
IntList arrayList = new IntArrayList();
for (int i = 0; i < 12; i++) arrayList.add(i);
System.out.println("IntArrayList[5] = " + arrayList.get(5));

IntList vector = new IntVector();
for (int i = 0; i < 25; i++) vector.add(i * 2);
System.out.println("IntVector[20] = " + vector.get(20));
}
}
13 changes: 13 additions & 0 deletions src/main/java/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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;
}
}
5 changes: 5 additions & 0 deletions src/main/java/Sedan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Sedan extends Car {
public Sedan(String vinNumber, String make, String model, int mileage) {
super(vinNumber, make, model, mileage);
}
}
13 changes: 13 additions & 0 deletions src/main/java/Truck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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() + ", TowingCapacity: " + towingCapacity;
}
}
13 changes: 13 additions & 0 deletions src/main/java/TvSeries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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;
}
}
13 changes: 13 additions & 0 deletions src/main/java/UtilityVehicle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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() + ", FourWheelDrive: " + fourWheelDrive;
}
}
13 changes: 13 additions & 0 deletions src/main/java/Video.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public abstract class Video {
protected String title;
protected int duration; // in minutes

public Video(String title, int duration) {
this.title = title;
this.duration = duration;
}

public String getInfo() {
return String.format("Title: %s, Duration: %d minutes", title, duration);
}
}