Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completed lab 3 #44

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ 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 is more efficient for scenarios where you expect to add a relatively small number of elements compared to the initial capacity (10). Its resizing by 1.5x the current size avoids unnecessary memory allocations in cases of gradual growth.

IntVector is more efficient for scenarios where you expect to add a large number of elements relative to the initial capacity (20). Doubling the size
<br>

## FAQs
Expand Down
39 changes: 39 additions & 0 deletions src/BigDecimalOperations/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package BigDecimalOperations;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("4.2223455");
double data =acceptsBigDecimalReturnsDouble(bd);
BigDecimal data2 = acceptsBigDecimalReturnsBigDecimal(bd);
System.out.println(data2);
}
public static double acceptsBigDecimalReturnsDouble(BigDecimal bigDec) {
// Using the BigDecimal documentation, create a method that accepts a BigDecimal and returns a double of
// the BigDecimal number rounded to the nearest hundredth. For example, 4.2545 should return 4.25.
if (bigDec == null) {
System.out.println("it has to be a bigDecimal");

}
assert bigDec != null;
return bigDec.setScale(2, RoundingMode.HALF_UP).doubleValue();



}
// Using the BigDecimal documentation, create a method that accepts a BigDecimal, reverses the sign
// (if the parameter is positive, the result should be negative and vice versa),
// rounds the number to the nearest tenth and returns the result. For example,
//1.2345 should return -1.2 and -45.67 should return 45.7.

public static BigDecimal acceptsBigDecimalReturnsBigDecimal(BigDecimal bigDec) {
if (bigDec == null) {
System.out.println("it has to be a bigDecimal");
}
assert bigDec != null;
BigDecimal negative = bigDec.negate();
return negative.setScale(1, RoundingMode.HALF_UP);
}
}
65 changes: 65 additions & 0 deletions src/CarInventorySystem/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package CarInventorySystem;

public abstract class Car {
//Create an abstract class named Car and define the following properties and behaviors:
// vinNumber: a String representing the VIN number of the car
//make: a String representing the make of the car
//model: a String representing the model of the car
//mileage: an int representing the mileage of the car
//getInfo(): a method that returns a String containing all of the car's properties in a readable format
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 "Car{" +
"vinNumber='" + vinNumber + '\'' +
", make='" + make + '\'' +
", model='" + model + '\'' +
", 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;
}



}
7 changes: 7 additions & 0 deletions src/CarInventorySystem/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package CarInventorySystem;

public class Main {
public static void main(String[] args) {

}
}
8 changes: 8 additions & 0 deletions src/CarInventorySystem/Sedan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package CarInventorySystem;

public class Sedan extends Car {
public Sedan(String vinNumber, String make, String model, int mileage) {
super(vinNumber, make, model, mileage);
}

}
17 changes: 17 additions & 0 deletions src/CarInventorySystem/Truck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package CarInventorySystem;

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;
}
}
17 changes: 17 additions & 0 deletions src/CarInventorySystem/UtilityVehicle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package CarInventorySystem;

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

import java.util.ArrayList;

public class IntArrayList implements IntList{
private ArrayList<Integer> list;
private int size;
private static final int DefaultSize= 10;
private static final double IncreaseBy = 1.5;

public IntArrayList() {
this.list =new ArrayList<>(DefaultSize);
this.size = 10;
}

@Override
public void add(int value) {
System.out.println(list.size());
if(list.size()==size){
System.out.println("in hers");
size = (int) (size * IncreaseBy);
ArrayList<Integer> newList = new ArrayList<>(size);
newList.addAll(list); // Copy existing elements
list = newList; // Update reference to new list
System.out.println("in hers");
}
list.add(value);

}

@Override
public int get(int index) {
if (index >= list.size() || index < 0) {
System.out.println("ID is out of bounds");
}
return list.get(index);
}
}
6 changes: 6 additions & 0 deletions src/IntListInterface/IntList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package IntListInterface;

public interface IntList {
void add(int value);
int get(int index);
}
37 changes: 37 additions & 0 deletions src/IntListInterface/IntVector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package IntListInterface;

import java.util.ArrayList;

public class IntVector implements IntList{


private ArrayList<Integer> list;
private int size;
private static final int DefaultSize= 20;
private static final double IncreaseBy = 2;

public IntVector() {
this.list =new ArrayList<>(DefaultSize);
this.size = 10;
}

@Override
public void add(int value) {
if(list.size()==size){
size = (int) (size * IncreaseBy);
ArrayList<Integer> newList = new ArrayList<>(size);
newList.addAll(list); // Copy existing elements
list = newList; // Update reference to new list
}
list.add(value);

}

@Override
public int get(int index) {
if (index >= list.size() || index < 0) {
System.out.println("ID is out of bounds");
}
return list.get(index);
}
}
25 changes: 25 additions & 0 deletions src/IntListInterface/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package IntListInterface;

public class Main {
public static void main(String[] args) {
IntArrayList intArr = new IntArrayList();
intArr.add(1);
intArr.add(1);
intArr.add(1);
intArr.add(1);
intArr.add(1);
intArr.add(1);
intArr.add(1);
intArr.add(1);
intArr.add(1);
intArr.add(1);
intArr.add(1);
intArr.add(1);
intArr.add(1);

intArr.add(1);
System.out.println(intArr.get(2));


}
}
6 changes: 6 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {

System.out.println("Hello world!");
}
}
9 changes: 9 additions & 0 deletions src/VideoStreamingService/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package VideoStreamingService;

public class Main {
public static void main(String[] args) {
TvSeries data = new TvSeries("Shat",2,2);
String info = data.getInfo();
System.out.println(info);
}
}
18 changes: 18 additions & 0 deletions src/VideoStreamingService/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package VideoStreamingService;

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;
}
}
18 changes: 18 additions & 0 deletions src/VideoStreamingService/TvSeries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package VideoStreamingService;

public class TvSeries extends Video{
private int episodes;

public int getEpisodes() {
return episodes;
}

public void setEpisodes(int episodes) {
this.episodes = episodes;
}

public TvSeries(String title, int duration, int episodes) {
super(title, duration);
this.episodes = episodes;
}
}
34 changes: 34 additions & 0 deletions src/VideoStreamingService/Video.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package VideoStreamingService;

public abstract 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 +
'}';
}
}