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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ Once you finish the assignment, submit a URL link to your repository or your pul
3. `IntArrayList` should store numbers in an array with a length of 10 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 50% larger, move all elements over to the new array and add the new element. (For example, an array of length 10 would be increased to 15.)
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 would be more efficient if array grows slowly and memory usage is important.
IntVector would be more efficient if array grows fast and performance is more important than memory.

<br>

Expand Down
15 changes: 15 additions & 0 deletions src/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 vinNumber + " " + make + " " + model + " " + 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[] data;
private int size;

public IntArrayList() {
data = new int[10];
size = 0;
}
@Override
public void add(int number) {
if (size == data.length) {
int[] newData = new int[data.length + size / 2];
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
data = newData;
}

data[size] = number;
size++;
}
@Override
public int get(int id) {
if (id < 0 || id >= size) {
throw new ArrayIndexOutOfBoundsException("Invalid index");
}
return data[id];
}
}
4 changes: 4 additions & 0 deletions src/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);
}
27 changes: 27 additions & 0 deletions src/IntVector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class IntVector implements IntList{
private int[] data;
private int size;
public IntVector(){
data = new int[20];
size = 0;
}
@Override
public void add(int number) {
if (size == data.length) {
int[] newData = new int[data.length*2];
for(int i = 0; i < data.length; i++){
newData[i] = data[i];
}
data = newData;
}
data[size] = number;
size++;
}
@Override
public int get(int id) {
if (id < 0 || id >= size) {
throw new ArrayIndexOutOfBoundsException("Invalid index");
}
return data[id];
}
}
14 changes: 14 additions & 0 deletions src/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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;
}
@Override
public String getInfo() {
return "Type: Movie " + super.getInfo() + " Rating :" + rating;
}
}
10 changes: 10 additions & 0 deletions src/Sedan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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();
}
}
14 changes: 14 additions & 0 deletions src/Truck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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;
}
@Override
public String getInfo() {
return "Truck: " + super.getInfo() + " Towing capacity: " + towingCapacity;
}
}
14 changes: 14 additions & 0 deletions src/TvSeries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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;
}
@Override
public String getInfo() {
return "Type: Tv series " + super.getInfo() + " Episode count :" + episodes;
}
}
14 changes: 14 additions & 0 deletions src/UtilityVehicle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class UtilityVehicle extends Car{
public 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 this.fourWheelDrive;
}
@Override
public String getInfo(){
return "Utility Vehicle: " + super.getInfo() + " Four Wheel Drive: " + fourWheelDrive;
}
}
11 changes: 11 additions & 0 deletions src/Video.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public abstract class Video{
protected String title;
protected int duration;
public Video(String title, int duration) {
this.title = title;
this.duration = duration;
}
public String getInfo(){
return title + " " + duration;
}
}