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
32 changes: 32 additions & 0 deletions BigDecimalOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalOperations {

// Method to round a BigDecimal to the nearest hundredth
public static double roundToNearestHundredth(BigDecimal number) {
// Set the scale to 2 and round to the nearest centesimal digit
return number.setScale(2, RoundingMode.HALF_UP).doubleValue();
}

// Method to invert the sign of a BigDecimal, round it to the nearest tenth, and return the result
public static BigDecimal invertSignAndRoundToNearestTenth(BigDecimal number) {
// Invert the sign of the number
BigDecimal invertedNumber = number.negate();
// Set the scale to 1 and round to the nearest tenth
return invertedNumber.setScale(1, RoundingMode.HALF_UP);
}

public static void main(String[] args) {
BigDecimal number1 = new BigDecimal("4.2545");
BigDecimal number2 = new BigDecimal("-45.67");

// Example of rounding to the nearest hundredth
double roundedNumber1 = roundToNearestHundredth(number1);
System.out.println("Rounding to the nearest hundredth of 4.2545: " + roundedNumber1);

// Example of inverting the sign and rounding to the nearest tenth
BigDecimal result = invertSignAndRoundToNearestTenth(number2);
System.out.println("Inverting the sign and rounding to the nearest tenth of -45.67: " + result);
}
}
16 changes: 16 additions & 0 deletions Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Car.java
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 abstract String getInfo();
}
48 changes: 48 additions & 0 deletions IntArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// IntArrayList.java
// This class implements IntList using an array with a default length of 10.

package com.example.intlist;

public class IntArrayList implements IntList {
// The array that stores the integers.
private int[] array;

// The number of integers in the array.
private int count;

// The constructor initializes the array with a default length of 10.
public IntArrayList() {
array = new int[10];
count = 0;
}

// Adds a new integer to the array.
@Override
public void add(int number) {
if (count == array.length) {
// Increase the size of the array by 50%.
int newLength = (int) (array.length * 1.5);
int[] newArray = new int[newLength];

// Copy the elements from the old array to the new array.
System.arraycopy(array, 0, newArray, 0, count);

// Update the array reference.
array = newArray;
}

// Add the new integer to the array.
array[count] = number;
count++;
}

// Gets an integer from the array by its ID.
@Override
public int get(int id) {
if (id < 0 || id >= count) {
throw new IndexOutOfBoundsException("ID out of bounds");
}

return array[id];
}
}
9 changes: 9 additions & 0 deletions IntList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// IntList.java
// This interface defines the behavior of an IntList.

package com.example.intlist;

public interface IntList {
void add(int number);
int get(int id);
}
48 changes: 48 additions & 0 deletions IntVector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// IntVector.java
// This class implements IntList using an array with a default length of 20.

package com.example.intlist;

public class IntVector implements IntList {
// The array that stores the integers.
private int[] array;

// The number of integers in the array.
private int count;

// The constructor initializes the array with a default length of 20.
public IntVector() {
array = new int[20];
count = 0;
}

// Adds a new integer to the array.
@Override
public void add(int number) {
if (count == array.length) {
// Double the size of the array.
int newLength = array.length * 2;
int[] newArray = new int[newLength];

// Copy the elements from the old array to the new array.
System.arraycopy(array, 0, newArray, 0, count);

// Update the array reference.
array = newArray;
}

// Add the new integer to the array.
array[count] = number;
count++;
}

// Gets an integer from the array by its ID.
@Override
public int get(int id) {
if (id < 0 || id >= count) {
throw new IndexOutOfBoundsException("ID out of bounds");
}

return array[id];
}
}
Binary file added Movie.class
Binary file not shown.
15 changes: 15 additions & 0 deletions Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

// Class Movie that extends Video
class Movie extends Video {
double rating;

public Movie(String title, int duration, double rating) {
super(title, duration); // Calling parent constructor
this.rating = rating;
}

@Override
public String getInfo() { // Overriding the abstract method
return "Title: " + title + "\nDuration: " + duration + " minutes\nRating: " + rating;
}
}
11 changes: 11 additions & 0 deletions ReadMe.md.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# IntList

This project contains an interface `IntList` and two implementations: `IntArrayList` and `IntVector`.

`IntArrayList` stores integers in an array with a default length of 10. When the array is full, it creates a new array that is 50% larger, copies the elements from the old array to the new array, and adds the new element.

`IntVector` stores integers in an array with a default length of 20. When the array is full, it creates a new array that is twice the size of the old array, copies the elements from the old array to the new array, and adds the new element.

`IntArrayList` would be more efficient when the number of elements is small and the array needs to be resized frequently, because it resizes the array by 50% each time.

`IntVector` would be more efficient when the number of elements is large and the array needs to be resized infrequently, because it doubles the size of the array each time.
11 changes: 11 additions & 0 deletions Sedan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Sedan.java
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 Info: VIN - " + vinNumber + ", Make - " + make + ", Model - " + model + ", Mileage - " + mileage;
}
}
14 changes: 14 additions & 0 deletions Truck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Truck.java
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 "Truck Info: VIN - " + vinNumber + ", Make - " + make + ", Model - " + model + ", Mileage - " + mileage + ", Towing Capacity - " + towingCapacity;
}
}
Binary file added TvSeries.class
Binary file not shown.
15 changes: 15 additions & 0 deletions TvSeries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

// Class TvSeries that extends Video
class TvSeries extends Video {
int episodes;

public TvSeries(String title, int duration, int episodes) {
super(title, duration); // Calling parent constructor
this.episodes = episodes;
}

@Override
public String getInfo() { // Overriding the abstract method
return "Title: " + title + "\nDuration: " + duration + " minutes\nEpisodes: " + episodes;
}
}
14 changes: 14 additions & 0 deletions UtilityVehicle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// UtilityVehicle.java
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 "Utility Vehicle Info: VIN - " + vinNumber + ", Make - " + make + ", Model - " + model + ", Mileage - " + mileage + ", 4WD - " + fourWheelDrive;
}
}
Binary file added Video.class
Binary file not shown.
12 changes: 12 additions & 0 deletions Video.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Abstract class Video
abstract class Video {
String title;
int duration;

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

public abstract String getInfo(); // Abstract method to retrieve video information
}