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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,8 @@ Once you finish the assignment, submit a URL link to your repository or your pul

</details>

## IntArrayList vs IntVector

IntArrayList can be more efficient when I do not need the list to grow very fast. It starts with 10 spaces and only grows by 50%, so it can avoid wasting too much unused space.

IntVector can be more efficient when I need to add many numbers quickly. It starts with 20 spaces and doubles its size when it is full, so it resizes less often than IntArrayList.
16 changes: 16 additions & 0 deletions src/BigDecimalOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalOperations {

public double roundingToHundredth(BigDecimal number) {
BigDecimal result = number.setScale(2, RoundingMode.HALF_UP);
return result.doubleValue();
}

public BigDecimal reverseAndRound(BigDecimal number) {
BigDecimal result = number.negate();
result = result.setScale(1, RoundingMode.HALF_UP);
return result;
}
}
56 changes: 56 additions & 0 deletions src/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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;
}

@Override
public String toString() {
return "Car{" +
"vinNumber='" + vinNumber + '\'' +
", make='" + make + '\'' +
", model='" + model + '\'' +
", mileage=" + mileage +
'}';
}
}

29 changes: 29 additions & 0 deletions src/IntArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class IntArrayList implements IntList {

private int[] numbers;
private int size;

public IntArrayList() {
numbers = new int[10];
size = 0;
}

public void add(int number) {
if (size == numbers.length) {
int[] newNumbers = new int[numbers.length + numbers.length / 2];

for (int i = 0; i < numbers.length; i++) {
newNumbers[i] = numbers[i];
}

numbers = newNumbers;
}

numbers[size] = number;
size++;
}

public int get(int id) {
return numbers[id];
}
}
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);
}
29 changes: 29 additions & 0 deletions src/IntVector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class IntVector implements IntList {

private int[] numbers;
private int size;

public IntVector() {
numbers = new int[20];
size = 0;
}

public void add(int number) {
if (size == numbers.length) {
int[] newNumbers = new int[numbers.length * 2];

for (int i = 0; i < numbers.length; i++) {
newNumbers[i] = numbers[i];
}

numbers = newNumbers;
}

numbers[size] = number;
size++;
}

public int get(int id) {
return numbers[id];
}
}
54 changes: 54 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.math.BigDecimal;

public class Main {

public static void main(String[] args) {
BigDecimalOperations operations = new BigDecimalOperations();

BigDecimal number1 = new BigDecimal("4.2545");
double result1 = operations.roundingToHundredth(number1);
System.out.println(result1);

BigDecimal number2 = new BigDecimal("1.2345");
BigDecimal result2 = operations.reverseAndRound(number2);
System.out.println(result2);

BigDecimal number3 = new BigDecimal("-45.67");
BigDecimal result3 = operations.reverseAndRound(number3);
System.out.println(result3);

Sedan sedan = new Sedan("VIN123", "Toyota", "Corolla", 50000);
UtilityVehicle utilityVehicle = new UtilityVehicle("VIN456", "Jeep", "Wrangler", 30000, true);
Truck truck = new Truck("VIN789", "Ford", "F150", 70000, 3500);

System.out.println(sedan.toString());
System.out.println(utilityVehicle.getInfo());
System.out.println(truck.getInfo());

TvSeries tvSeries = new TvSeries("Breaking Bad", 45, 62);
Movie movie = new Movie("Inception", 148, 8.8);

System.out.println(tvSeries.getInfo());
System.out.println(movie.getInfo());

IntList list1 = new IntArrayList();

list1.add(10);
list1.add(20);
list1.add(30);

System.out.println(list1.get(0));
System.out.println(list1.get(1));
System.out.println(list1.get(2));

IntList list2 = new IntVector();

list2.add(100);
list2.add(200);
list2.add(300);

System.out.println(list2.get(0));
System.out.println(list2.get(1));
System.out.println(list2.get(2));
}
}
21 changes: 21 additions & 0 deletions src/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;
}

public String getInfo() {
return super.getInfo() + ", Rating: " + rating;
}
}
6 changes: 6 additions & 0 deletions src/Sedan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Sedan extends Car {

public Sedan(String vinNumber, String make, String model, int mileage) {
super(vinNumber, make, model, mileage);
}
}
21 changes: 21 additions & 0 deletions src/Truck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;
}

public String getInfo() {
return super.toString() + ", Towing capacity: " + towingCapacity;
}
}
21 changes: 21 additions & 0 deletions src/TvSeries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;
}

public String getInfo() {
return super.getInfo() + ", Episodes: " + episodes;
}
}
21 changes: 21 additions & 0 deletions src/UtilityVehicle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;
}

public String getInfo() {
return super.toString() + ", Four wheel drive: " + fourWheelDrive;
}
}
32 changes: 32 additions & 0 deletions src/Video.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public 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 "Video{" +
"title='" + title + '\'' +
", duration=" + duration +
'}';
}
}