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

public class BigDecimalOperations {

public static double roundToNearestHundredth(BigDecimal number) {
return number.setScale(2, RoundingMode.HALF_UP).doubleValue();
}

public static double reverseSignAndRoundToNearestTenth(BigDecimal number) {
BigDecimal reversed = number.negate();
return reversed.setScale(1, RoundingMode.HALF_UP).doubleValue();
}
}
36 changes: 36 additions & 0 deletions Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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 String getMake() {
return make;
}

public String getModel() {
return model;
}

public int getMileage() {
return mileage;
}

public String getInfo() {
return "VIN: " + vinNumber +
", Make: " + make +
", Model: " + model +
", Mileage: " + mileage;
}
}
42 changes: 42 additions & 0 deletions IntArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
public class IntArrayList implements IntList {

private int[] numbers;
private int size;

public IntArrayList() {
numbers = new int[10]; // tamaño inicial 10
size = 0;
}

@Override
public void add(int number) {

// Si el array está lleno, lo ampliamos un 50%
if (size == numbers.length) {

int newLength = numbers.length + (numbers.length / 2);
int[] newArray = new int[newLength];

// Copiar elementos al nuevo array
for (int i = 0; i < numbers.length; i++) {
newArray[i] = numbers[i];
}

numbers = newArray;
}

// Añadir el nuevo número
numbers[size] = number;
size++;
}

@Override
public int get(int id) {

if (id < 0 || id >= size) {
throw new IndexOutOfBoundsException("Índice fuera de rango");
}

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

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

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

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

numbers = newArray;
}

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

@Override
public int get(int id) {
if (id < 0 || id >= size) {
throw new IndexOutOfBoundsException("Índice fuera de rango");
}
return numbers[id];
}
}
57 changes: 57 additions & 0 deletions Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.math.BigDecimal;

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

System.out.println("=== BIGDECIMAL OPERATIONS ===");
BigDecimal num1 = new BigDecimal("4.2545");
BigDecimal num2 = new BigDecimal("1.2345");
BigDecimal num3 = new BigDecimal("-45.67");

System.out.println("Rounded to nearest hundredth: " +
BigDecimalOperations.roundToNearestHundredth(num1));

System.out.println("Reverse sign and round to nearest tenth (1.2345): " +
BigDecimalOperations.reverseSignAndRoundToNearestTenth(num2));

System.out.println("Reverse sign and round to nearest tenth (-45.67): " +
BigDecimalOperations.reverseSignAndRoundToNearestTenth(num3));

System.out.println();
System.out.println("=== CAR INVENTORY SYSTEM ===");

Car sedan = new Sedan("VIN001", "Toyota", "Corolla", 50000);
Car suv = new UtilityVehicle("VIN002", "Jeep", "Cherokee", 30000, true);
Car truck = new Truck("VIN003", "Ford", "F-150", 45000, 3500.5);

System.out.println(sedan.getInfo());
System.out.println(suv.getInfo());
System.out.println(truck.getInfo());

System.out.println();
System.out.println("=== VIDEO STREAMING SERVICE ===");

Video series = new TvSeries("Breaking Bad", 50, 62);
Video movie = new Movie("Inception", 148, 8.8);

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

System.out.println();
System.out.println("=== INTLIST INTERFACE ===");

IntList arrayList = new IntArrayList();
IntList vector = new IntVector();

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

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

System.out.println("IntArrayList element at index 1: " + arrayList.get(1));
System.out.println("IntVector element at index 2: " + vector.get(2));
}
}
17 changes: 17 additions & 0 deletions Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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 super.getInfo() + ", Rating: " + rating;
}
}
6 changes: 6 additions & 0 deletions 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);
}
}
17 changes: 17 additions & 0 deletions Truck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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 super.getInfo() + ", Towing Capacity: " + towingCapacity;
}
}
17 changes: 17 additions & 0 deletions TvSeries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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 super.getInfo() + ", Episodes: " + episodes;
}
}
17 changes: 17 additions & 0 deletions UtilityVehicle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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;
}

@Override
public String getInfo() {
return super.getInfo() + ", Four Wheel Drive: " + fourWheelDrive;
}
}
21 changes: 21 additions & 0 deletions Video.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 int getDuration() {
return duration;
}

public String getInfo() {
return "Title: " + title + ", Duration: " + duration + " minutes";
}
}