diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/lab-java-interfaces-and-abstract-classes.iml b/.idea/lab-java-interfaces-and-abstract-classes.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/.idea/lab-java-interfaces-and-abstract-classes.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/markdown.xml b/.idea/markdown.xml new file mode 100644 index 0000000..f6d2542 --- /dev/null +++ b/.idea/markdown.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4eb4f51 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ff55171 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index ce069b6..8344fdb 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,22 @@ 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. +# Choosing Between IntArrayList and IntVector + +This project has two different ways to handle an array that runs out of space. Here is a simple guide on when to use each one: + +### IntArrayList (Grows by 50%) +* **Best for:** Saving memory (RAM). +* **Why:** When the array gets full, it only adds half of its current capacity. This is great because it prevents having a lot of empty, unused spaces in memory. You should use this when your list grows slowly or if your computer doesn't have a lot of memory available. + +### IntVector (Doubles its size) +* **Best for:** Speed and performance. +* **Why:** Every time an array gets full, Java has to create a brand new one and copy all the old numbers into it. This process takes time! Because `IntVector` doubles its size, it doesn't have to do this heavy task as often. You should use this when you need to add thousands of numbers very fast, even if it means wasting some empty memory spaces at the end. + + + + +
## FAQs diff --git a/out/production/lab-java-interfaces-and-abstract-classes/Application.class b/out/production/lab-java-interfaces-and-abstract-classes/Application.class new file mode 100644 index 0000000..7adcf9e Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/Application.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/IntListInterface/Application.class b/out/production/lab-java-interfaces-and-abstract-classes/IntListInterface/Application.class new file mode 100644 index 0000000..4a1fca1 Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/IntListInterface/Application.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/IntListInterface/IntArrayList.class b/out/production/lab-java-interfaces-and-abstract-classes/IntListInterface/IntArrayList.class new file mode 100644 index 0000000..656d1b6 Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/IntListInterface/IntArrayList.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/IntListInterface/IntList.class b/out/production/lab-java-interfaces-and-abstract-classes/IntListInterface/IntList.class new file mode 100644 index 0000000..85a0482 Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/IntListInterface/IntList.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/IntListInterface/IntVector.class b/out/production/lab-java-interfaces-and-abstract-classes/IntListInterface/IntVector.class new file mode 100644 index 0000000..ffd0deb Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/IntListInterface/IntVector.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/OperationsBigDecimal/BigDecimalOperations.class b/out/production/lab-java-interfaces-and-abstract-classes/OperationsBigDecimal/BigDecimalOperations.class new file mode 100644 index 0000000..15ba8a4 Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/OperationsBigDecimal/BigDecimalOperations.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/ServiceVideoStreaming/Application.class b/out/production/lab-java-interfaces-and-abstract-classes/ServiceVideoStreaming/Application.class new file mode 100644 index 0000000..977d8b2 Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/ServiceVideoStreaming/Application.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/ServiceVideoStreaming/Movie.class b/out/production/lab-java-interfaces-and-abstract-classes/ServiceVideoStreaming/Movie.class new file mode 100644 index 0000000..32ae3f5 Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/ServiceVideoStreaming/Movie.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/ServiceVideoStreaming/TvSeries.class b/out/production/lab-java-interfaces-and-abstract-classes/ServiceVideoStreaming/TvSeries.class new file mode 100644 index 0000000..ef34175 Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/ServiceVideoStreaming/TvSeries.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/ServiceVideoStreaming/Video.class b/out/production/lab-java-interfaces-and-abstract-classes/ServiceVideoStreaming/Video.class new file mode 100644 index 0000000..85e0910 Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/ServiceVideoStreaming/Video.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/Application.class b/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/Application.class new file mode 100644 index 0000000..5cff2b6 Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/Application.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/Car.class b/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/Car.class new file mode 100644 index 0000000..7a02d71 Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/Car.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/Sedan.class b/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/Sedan.class new file mode 100644 index 0000000..31efbcd Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/Sedan.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/Truck.class b/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/Truck.class new file mode 100644 index 0000000..b7bed04 Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/Truck.class differ diff --git a/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/UtilityVehicle.class b/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/UtilityVehicle.class new file mode 100644 index 0000000..ea1eea4 Binary files /dev/null and b/out/production/lab-java-interfaces-and-abstract-classes/SystemInventoryCar/UtilityVehicle.class differ diff --git a/src/IntListInterface/Application.java b/src/IntListInterface/Application.java new file mode 100644 index 0000000..ea2195c --- /dev/null +++ b/src/IntListInterface/Application.java @@ -0,0 +1,26 @@ +package IntListInterface; + +public class Application { + public static void main(String[] args) { + + System.out.println("--- Probando IntArrayList (Crece un 50%) ---"); + IntList miArrayList = new IntArrayList(); + + + for (int i = 0; i < 12; i++) { + miArrayList.add(i * 10); // Añadimos 0, 10, 20, 30... + } + System.out.println("Elemento en ID 11: " + miArrayList.get(11)); + + + System.out.println("\n--- Probando IntVector (Crece un 100%) ---"); + IntList miVector = new IntVector(); + + + for (int i = 0; i < 25; i++) { + miVector.add(i * 100); // Añadimos 0, 100, 200, 300... + } + System.out.println("Elemento en ID 21: " + miVector.get(21)); + + } +} diff --git a/src/IntListInterface/IntArrayList.java b/src/IntListInterface/IntArrayList.java new file mode 100644 index 0000000..ac3fada --- /dev/null +++ b/src/IntListInterface/IntArrayList.java @@ -0,0 +1,38 @@ +package IntListInterface; + +import java.util.Arrays; + +public class IntArrayList implements IntList{ + private int[] array; + private int size; + + public IntArrayList() { + this.array = new int [10]; + this.size = 0; + } + + @Override + public void add(int number) { + + if (size == array.length) { + // Calcular el nuevo tamaño: el actual + 50% (la mitad) + int newSize = array.length + (array.length / 2); + + + array = Arrays.copyOf(array, newSize); + System.out.println("IntArrayList ha crecido a tamaño: " + newSize); // Para que lo veas en consola + } + + array[size] = number; + size++; + } + + @Override + public int get(int id) { + // Devuelve el elemento en la posición 'id' + if (id >= 0 && id < size) { + return array[id]; + } + throw new IndexOutOfBoundsException("ID fuera de rango"); + } +} diff --git a/src/IntListInterface/IntList.java b/src/IntListInterface/IntList.java new file mode 100644 index 0000000..4be9486 --- /dev/null +++ b/src/IntListInterface/IntList.java @@ -0,0 +1,6 @@ +package IntListInterface; + +public interface IntList { + void add(int number); + int get(int id); +} diff --git a/src/IntListInterface/IntVector.java b/src/IntListInterface/IntVector.java new file mode 100644 index 0000000..c9e3272 --- /dev/null +++ b/src/IntListInterface/IntVector.java @@ -0,0 +1,39 @@ +package IntListInterface; + +import java.util.Arrays; + +public class IntVector implements IntList { + private int[] array; + private int size; + + + public IntVector() { + this.array = new int[20]; + this.size = 0; + } + + @Override + public void add(int number) { + + if (size == array.length) { + // Calcular el nuevo tamaño: el doble del actual + int newSize = array.length * 2; + + + array = Arrays.copyOf(array, newSize); + System.out.println("IntVector ha crecido a tamaño: " + newSize); + } + + + array[size] = number; + size++; + } + + @Override + public int get(int id) { + if (id >= 0 && id < size) { + return array[id]; + } + throw new IndexOutOfBoundsException("ID fuera de rango"); + } +} \ No newline at end of file diff --git a/src/OperationsBigDecimal/Application.java b/src/OperationsBigDecimal/Application.java new file mode 100644 index 0000000..4e0745f --- /dev/null +++ b/src/OperationsBigDecimal/Application.java @@ -0,0 +1,18 @@ +import OperationsBigDecimal.BigDecimalOperations; + +import java.math.BigDecimal; + + + public static void main(String[] args) { + + System.out.println("--- TESTS BIGDECIMAL ---"); + + BigDecimal num1 = new BigDecimal("5.2565"); + System.out.println("Test1: " + BigDecimalOperations.redondearCentesima(num1)); + + BigDecimal num2 = new BigDecimal("2.3545"); + System.out.println("Test2_negative: " + BigDecimalOperations.invertirYRedondear(num2)); + + BigDecimal num3 = new BigDecimal("-54.7678"); + System.out.println("Test2_positive: " + BigDecimalOperations.invertirYRedondear(num3)); + } diff --git a/src/OperationsBigDecimal/BigDecimalOperations.java b/src/OperationsBigDecimal/BigDecimalOperations.java new file mode 100644 index 0000000..8b08f5f --- /dev/null +++ b/src/OperationsBigDecimal/BigDecimalOperations.java @@ -0,0 +1,17 @@ +package OperationsBigDecimal; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class BigDecimalOperations { + + + public static double redondearCentesima(BigDecimal numero) { + return numero.setScale(2, RoundingMode.HALF_UP).doubleValue(); + } + + + public static BigDecimal invertirYRedondear(BigDecimal numero) { + return numero.negate().setScale(1, RoundingMode.HALF_UP); + } +} \ No newline at end of file diff --git a/src/ServiceVideoStreaming/Application.java b/src/ServiceVideoStreaming/Application.java new file mode 100644 index 0000000..1a8c6be --- /dev/null +++ b/src/ServiceVideoStreaming/Application.java @@ -0,0 +1,16 @@ +package ServiceVideoStreaming; + +public class Application { + public static void main(String[] args) { + + Movie miPeli = new Movie("La vida es bella", 116, 8.6); + + TvSeries miSerie = new TvSeries("Pluribus", 50, 9); + + System.out.println("*--- INFO MOVIE ---*"); + System.out.println(miPeli.getInfo()); + + System.out.println("\n*--- INFO SERIE ---*"); + System.out.println(miSerie.getInfo()); + } +} \ No newline at end of file diff --git a/src/ServiceVideoStreaming/Movie.java b/src/ServiceVideoStreaming/Movie.java new file mode 100644 index 0000000..206cfc3 --- /dev/null +++ b/src/ServiceVideoStreaming/Movie.java @@ -0,0 +1,23 @@ +package ServiceVideoStreaming; + +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; + } + + @Override + public String getInfo() { + return "Title: " + getTitle() + "\nDuration: " + getDuration() + " minutes\nRating: " + getRating(); + } +} \ No newline at end of file diff --git a/src/ServiceVideoStreaming/TvSeries.java b/src/ServiceVideoStreaming/TvSeries.java new file mode 100644 index 0000000..901f0c1 --- /dev/null +++ b/src/ServiceVideoStreaming/TvSeries.java @@ -0,0 +1,23 @@ +package ServiceVideoStreaming; + +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; + } + + @Override + public String getInfo() { + return "Title: " + getTitle() + "\nDuration: " + getDuration() + " minutes\nEpisodes: " + getEpisodes(); + } +} \ No newline at end of file diff --git a/src/ServiceVideoStreaming/Video.java b/src/ServiceVideoStreaming/Video.java new file mode 100644 index 0000000..111eea2 --- /dev/null +++ b/src/ServiceVideoStreaming/Video.java @@ -0,0 +1,29 @@ +package ServiceVideoStreaming; + +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 abstract String getInfo(); +} diff --git a/src/SystemInventoryCar/Application.java b/src/SystemInventoryCar/Application.java new file mode 100644 index 0000000..0d1e312 --- /dev/null +++ b/src/SystemInventoryCar/Application.java @@ -0,0 +1,17 @@ +package SystemInventoryCar; + +public class Application { + static void main(String[] args) { + Car truck = new Truck("0007DWL", "Fiat", "Ducato", 180000, 3500.0); + Car sedan = new Sedan("1415NMB", "Opel", "Insignia", 21000); + Car utilityVehicle = new UtilityVehicle("6536MHN", "Toyota", "Hilux", 15000,true); + + + System.out.println(truck.getInfo()); + System.out.println("******"); + System.out.println(sedan.getInfo()); + System.out.println("******"); + System.out.println(utilityVehicle.getInfo()); + } +} + diff --git a/src/SystemInventoryCar/Car.java b/src/SystemInventoryCar/Car.java new file mode 100644 index 0000000..17ec630 --- /dev/null +++ b/src/SystemInventoryCar/Car.java @@ -0,0 +1,53 @@ +package SystemInventoryCar; + +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; + } + + public String getInfo(){ + return "Nº vin: " + vinNumber + "\nMake: " + make + "\nModel: " + model + "\nMileage: " + mileage + "KM."; + + } +} diff --git a/src/SystemInventoryCar/Sedan.java b/src/SystemInventoryCar/Sedan.java new file mode 100644 index 0000000..5c51072 --- /dev/null +++ b/src/SystemInventoryCar/Sedan.java @@ -0,0 +1,7 @@ +package SystemInventoryCar; + +public class Sedan extends Car { + public Sedan(String vinNumber, String make, String model, int mileage) { + super(vinNumber, make, model, mileage); + } +} diff --git a/src/SystemInventoryCar/Truck.java b/src/SystemInventoryCar/Truck.java new file mode 100644 index 0000000..b346878 --- /dev/null +++ b/src/SystemInventoryCar/Truck.java @@ -0,0 +1,24 @@ +package SystemInventoryCar; + +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 isTowingCapacity() { + return towingCapacity; + } + + public void setTowingCapacity(double towingCapacity){ + this.towingCapacity = towingCapacity; + } + + @Override + + public String getInfo (){ + return ("Nº vin: " + getVinNumber() + "\nMake: " + getMake() + "\nModel: " + getModel() + "\nMileage: " + getMileage() + "KM. " + "\nTowing capacity: " + towingCapacity + "Kg."); + } +} diff --git a/src/SystemInventoryCar/UtilityVehicle.java b/src/SystemInventoryCar/UtilityVehicle.java new file mode 100644 index 0000000..1e4d5af --- /dev/null +++ b/src/SystemInventoryCar/UtilityVehicle.java @@ -0,0 +1,24 @@ +package SystemInventoryCar; + +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; + } + + @Override + + public String getInfo (){ + return ("Nº vin: " + getVinNumber() + "\nMake: " + getMake() + "\nModel: " + getModel() + "\nMileage: " + getMileage() + "KM." + "\nTraction: " + fourWheelDrive); + } +}