From f7b842fc8b9289d2866f83fd05aee58a98179771 Mon Sep 17 00:00:00 2001 From: cldcr Date: Sat, 2 May 2026 10:43:28 +0200 Subject: [PATCH 1/2] LAb wk 3 --- .idea/.gitignore | 10 +++++ ...b-java-interfaces-and-abstract-classes.iml | 9 +++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 9 +++++ .idea/vcs.xml | 6 +++ README.md | 12 +++--- bigDecimal/bigDecimal.iml | 11 ++++++ bigDecimal/src/ArrotondamentoBigDecimal.java | 26 +++++++++++++ bigDecimal/src/Car/Car.java | 23 +++++++++++ bigDecimal/src/Car/Main.java | 31 +++++++++++++++ bigDecimal/src/Car/Sedan.java | 8 ++++ bigDecimal/src/Car/Truck.java | 15 ++++++++ bigDecimal/src/Car/UtilityVehicle.java | 15 ++++++++ bigDecimal/src/Int/IntArrayList.java | 24 ++++++++++++ bigDecimal/src/Int/IntList.java | 6 +++ bigDecimal/src/Int/IntVector.java | 24 ++++++++++++ bigDecimal/src/Int/Main.java | 38 +++++++++++++++++++ .../src/VideoStreamingService/Main.java | 31 +++++++++++++++ .../src/VideoStreamingService/Movie.java | 15 ++++++++ .../src/VideoStreamingService/TvSeries.java | 15 ++++++++ .../src/VideoStreamingService/Video.java | 15 ++++++++ bigDecimal/src/eje1.java | 19 ++++++++++ 22 files changed, 362 insertions(+), 6 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/lab-java-interfaces-and-abstract-classes.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 bigDecimal/bigDecimal.iml create mode 100644 bigDecimal/src/ArrotondamentoBigDecimal.java create mode 100644 bigDecimal/src/Car/Car.java create mode 100644 bigDecimal/src/Car/Main.java create mode 100644 bigDecimal/src/Car/Sedan.java create mode 100644 bigDecimal/src/Car/Truck.java create mode 100644 bigDecimal/src/Car/UtilityVehicle.java create mode 100644 bigDecimal/src/Int/IntArrayList.java create mode 100644 bigDecimal/src/Int/IntList.java create mode 100644 bigDecimal/src/Int/IntVector.java create mode 100644 bigDecimal/src/Int/Main.java create mode 100644 bigDecimal/src/VideoStreamingService/Main.java create mode 100644 bigDecimal/src/VideoStreamingService/Movie.java create mode 100644 bigDecimal/src/VideoStreamingService/TvSeries.java create mode 100644 bigDecimal/src/VideoStreamingService/Video.java create mode 100644 bigDecimal/src/eje1.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..30cf57e --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.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..d6ebd48 --- /dev/null +++ b/.idea/lab-java-interfaces-and-abstract-classes.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ 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..8d7581f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ 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..eab1446 100644 --- a/README.md +++ b/README.md @@ -30,18 +30,18 @@ Once you finish the assignment, submit a URL link to your repository or your pul
-### Car Inventory System +### Car.Car Inventory System -1. Suppose you are building a car inventory system. All cars have a `vinNumber`, `make`, `model` and `mileage`. But no car is just a car. Each car is either a `Sedan`, a `UtilityVehicle` or a `Truck`. -2. Create an abstract class named `Car` and define the following properties and behaviors: +1. Suppose you are building a car inventory system. All cars have a `vinNumber`, `make`, `model` and `mileage`. But no car is just a car. Each car is either a `Car.Sedan`, a `Car.UtilityVehicle` or a `Car.Truck`. +2. Create an abstract class named `Car.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 -3. Create three classes that extend `Car`: `Sedan`, `UtilityVehicle` and `Truck`. -4. `UtilityVehicle` objects should have an additional `fourWheelDrive` property, a `boolean` that represents whether the vehicle has four-wheel drive. -5. `Truck` objects should have an additional `towingCapacity` property, a `double` that represents the towing capacity of the truck. +3. Create three classes that extend `Car.Car`: `Car.Sedan`, `Car.UtilityVehicle` and `Car.Truck`. +4. `Car.UtilityVehicle` objects should have an additional `fourWheelDrive` property, a `boolean` that represents whether the vehicle has four-wheel drive. +5. `Car.Truck` objects should have an additional `towingCapacity` property, a `double` that represents the towing capacity of the truck.
diff --git a/bigDecimal/bigDecimal.iml b/bigDecimal/bigDecimal.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/bigDecimal/bigDecimal.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/bigDecimal/src/ArrotondamentoBigDecimal.java b/bigDecimal/src/ArrotondamentoBigDecimal.java new file mode 100644 index 0000000..c9eebf2 --- /dev/null +++ b/bigDecimal/src/ArrotondamentoBigDecimal.java @@ -0,0 +1,26 @@ +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class ArrotondamentoBigDecimal { + + public static void main(String[] args) { + // Esempi di test + System.out.println("Input 1.2345 -> Risultato: " + reverseAndRound(new BigDecimal("1.2345"))); + System.out.println("Input -45.67 -> Risultato: " + reverseAndRound(new BigDecimal("-45.67"))); + System.out.println("Input null -> Risultato: " + reverseAndRound(null)); + } + + /** + * Inverte il segno e arrotonda al decimo più vicino. + */ + public static BigDecimal reverseAndRound(BigDecimal value) { + // Gestione del null: restituisce ZERO per evitare errori + if (value == null) { + return BigDecimal.ZERO; + } + + // Inverte il segno e arrotonda a 1 cifra decimale + return value.negate() + .setScale(1, RoundingMode.HALF_UP); + } +} diff --git a/bigDecimal/src/Car/Car.java b/bigDecimal/src/Car/Car.java new file mode 100644 index 0000000..5d5e2bf --- /dev/null +++ b/bigDecimal/src/Car/Car.java @@ -0,0 +1,23 @@ +package Car; + +public 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 getInfo() { + return String.format("VIN: %s, Marca: %s, Modello: %s, Chilometraggio: %d km", + vinNumber, make, model, mileage); + } + + // Getter e Setter possono essere aggiunti qui se necessario +} + diff --git a/bigDecimal/src/Car/Main.java b/bigDecimal/src/Car/Main.java new file mode 100644 index 0000000..607682b --- /dev/null +++ b/bigDecimal/src/Car/Main.java @@ -0,0 +1,31 @@ +package Car; + +import java.util.ArrayList; +import java.util.List; + +public class Main { + public static void main(String[] args) { + // Creazione di una lista di Car per sfruttare il polimorfismo + List concessionario = new ArrayList<>(); + + // 1. Creazione di una Sedan + Sedan miaSedan = new Sedan("ABC12345", "Toyota", "Corolla", 15000); + + // 2. Creazione di un UtilityVehicle (con parametro fourWheelDrive) + UtilityVehicle mioSUV = new UtilityVehicle("XYZ67890", "Jeep", "Wrangler", 5000, true); + + // 3. Creazione di un Truck (con parametro towingCapacity) + Truck mioTruck = new Truck("TRK44556", "Ford", "F-150", 1200, 3500.5); + + // Aggiungiamo i veicoli alla lista + concessionario.add(miaSedan); + concessionario.add(mioSUV); + concessionario.add(mioTruck); + + // Ciclo attraverso la lista e stampa delle informazioni + System.out.println("--- Inventario Veicoli ---"); + for (Car veicolo : concessionario) { + System.out.println(veicolo.getInfo()); + } + } +} \ No newline at end of file diff --git a/bigDecimal/src/Car/Sedan.java b/bigDecimal/src/Car/Sedan.java new file mode 100644 index 0000000..7dfc081 --- /dev/null +++ b/bigDecimal/src/Car/Sedan.java @@ -0,0 +1,8 @@ +package Car; + +// Classe Car.Sedan: estensione semplice di Car.Car +public class Sedan extends Car { + public Sedan(String vinNumber, String make, String model, int mileage) { + super(vinNumber, make, model, mileage); + } +} diff --git a/bigDecimal/src/Car/Truck.java b/bigDecimal/src/Car/Truck.java new file mode 100644 index 0000000..0f15ea9 --- /dev/null +++ b/bigDecimal/src/Car/Truck.java @@ -0,0 +1,15 @@ +package Car; + +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 super.getInfo() + ", Capacità di traino: " + towingCapacity; + } +} diff --git a/bigDecimal/src/Car/UtilityVehicle.java b/bigDecimal/src/Car/UtilityVehicle.java new file mode 100644 index 0000000..f7cc6f7 --- /dev/null +++ b/bigDecimal/src/Car/UtilityVehicle.java @@ -0,0 +1,15 @@ +package Car; + +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 super.getInfo() + ", 4WD: " + fourWheelDrive; + } +} diff --git a/bigDecimal/src/Int/IntArrayList.java b/bigDecimal/src/Int/IntArrayList.java new file mode 100644 index 0000000..e9f398b --- /dev/null +++ b/bigDecimal/src/Int/IntArrayList.java @@ -0,0 +1,24 @@ +package Int; + +import java.util.Arrays; + +public class IntArrayList implements IntList { + private int[] array = new int[10]; + private int size = 0; + + @Override + public void add(int number) { + if (size == array.length) { + // Aumenta del 50% (es. da 10 a 15) + int newSize = array.length + (array.length / 2); + array = Arrays.copyOf(array, newSize); + } + array[size++] = number; + } + + @Override + public int get(int id) { + if (id < 0 || id >= size) throw new IndexOutOfBoundsException(); + return array[id]; + } +} \ No newline at end of file diff --git a/bigDecimal/src/Int/IntList.java b/bigDecimal/src/Int/IntList.java new file mode 100644 index 0000000..a26e5f1 --- /dev/null +++ b/bigDecimal/src/Int/IntList.java @@ -0,0 +1,6 @@ +package Int; + +public interface IntList { + void add(int number); + int get(int id); +} \ No newline at end of file diff --git a/bigDecimal/src/Int/IntVector.java b/bigDecimal/src/Int/IntVector.java new file mode 100644 index 0000000..f5be54b --- /dev/null +++ b/bigDecimal/src/Int/IntVector.java @@ -0,0 +1,24 @@ +package Int; + +import java.util.Arrays; + +public class IntVector implements IntList { + private int[] array = new int[20]; + private int size = 0; + + @Override + public void add(int number) { + if (size == array.length) { + // Raddoppia la dimensione (es. da 20 a 40) + int newSize = array.length * 2; + array = Arrays.copyOf(array, newSize); + } + array[size++] = number; + } + + @Override + public int get(int id) { + if (id < 0 || id >= size) throw new IndexOutOfBoundsException(); + return array[id]; + } +} \ No newline at end of file diff --git a/bigDecimal/src/Int/Main.java b/bigDecimal/src/Int/Main.java new file mode 100644 index 0000000..1a19afe --- /dev/null +++ b/bigDecimal/src/Int/Main.java @@ -0,0 +1,38 @@ +package Int; + +public class Main { + public static void main(String[] args) { + // --- Test IntArrayList (Capacità iniziale 10, cresce del 50%) --- + IntArrayList myArrayList = new IntArrayList(); + System.out.println("Test IntArrayList:"); + + // Aggiungiamo 12 elementi per forzare il primo ridimensionamento (da 10 a 15) + for (int i = 1; i <= 12; i++) { + myArrayList.add(i * 10); + } + + System.out.println("Elemento a id 0: " + myArrayList.get(0)); // 10 + System.out.println("Elemento a id 11: " + myArrayList.get(11)); // 120 + System.out.println(); + + + // --- Test IntVector (Capacità iniziale 20, raddoppia) --- + IntList myVector = new IntVector(); + System.out.println("Test IntVector:"); + + // Aggiungiamo 25 elementi per forzare il primo ridimensionamento (da 20 a 40) + for (int i = 1; i <= 25; i++) { + myVector.add(i * 100); + } + + System.out.println("Elemento a id 0: " + myVector.get(0)); // 100 + System.out.println("Elemento a id 24: " + myVector.get(24)); // 2500 + + // Test errore (ID fuori dai limiti) + try { + System.out.println(myVector.get(100)); + } catch (IndexOutOfBoundsException e) { + System.out.println("\nErrore gestito: ID 100 non esiste ancora."); + } + } +} diff --git a/bigDecimal/src/VideoStreamingService/Main.java b/bigDecimal/src/VideoStreamingService/Main.java new file mode 100644 index 0000000..ca41c85 --- /dev/null +++ b/bigDecimal/src/VideoStreamingService/Main.java @@ -0,0 +1,31 @@ +package VideoStreamingService; + +import java.util.ArrayList; +import java.util.List; + +public class Main { + public static void main(String[] args) { + // Creazione di una lista che può contenere qualsiasi tipo di Video + List