Skip to content
Open

Lab 3 #152

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
10 changes: 10 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/lab-java-interfaces-and-abstract-classes.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Once you finish the assignment, submit a URL link to your repository or your pul

### BigDecimal Operations

1. Using the [BigDecimal documentation](https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html), 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`.
2. Using the [BigDecimal documentation](https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html), 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`.
1. Using the [BigDecimal documentation](https://docs.oracle.com/javase/7/docs/api/java/com.ironhack.math/BigDecimal.html), 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`.
2. Using the [BigDecimal documentation](https://docs.oracle.com/javase/7/docs/api/java/com.ironhack.math/BigDecimal.html), 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`.

<br>

Expand Down Expand Up @@ -117,7 +117,7 @@ Once you finish the assignment, submit a URL link to your repository or your pul
Here's how you can use `BigDecimal` in a Java program:

```java
import java.math.BigDecimal;
import java.com.ironhack.math.BigDecimal;

public class BigDecimalExample {
public static void main(String[] args) {
Expand Down Expand Up @@ -149,7 +149,7 @@ Once you finish the assignment, submit a URL link to your repository or your pul
`setScale()` is used to set the scale of a `BigDecimal` object, which determines the number of decimal places to keep. For example:

```java
import java.math.BigDecimal;
import java.com.ironhack.math.BigDecimal;

public class BigDecimalExample {
public static void main(String[] args) {
Expand All @@ -166,8 +166,8 @@ Once you finish the assignment, submit a URL link to your repository or your pul
`RoundingMode` is an enumeration in Java that defines the different rounding modes that can be used with `BigDecimal`. For example:

```java
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.com.ironhack.math.BigDecimal;
import java.com.ironhack.math.RoundingMode;

public class BigDecimalExample {
public static void main(String[] args) {
Expand All @@ -184,7 +184,7 @@ Once you finish the assignment, submit a URL link to your repository or your pul
`negate()` is used to negate the value of a `BigDecimal` object, converting a positive value to a negative and vice versa. For example:

```java
import java.math.BigDecimal;
import java.com.ironhack.math.BigDecimal;

public class BigDecimalExample {
public static void main(String[] args) {
Expand Down Expand Up @@ -337,3 +337,7 @@ Once you finish the assignment, submit a URL link to your repository or your pul

</details>

Criterio IntArrayList (50%) IntVector (100%)
Uso de Memoria Más eficiente (ahorra espacio) Menos eficiente (desperdicia espacio)
Velocidad Más lenta en inserciones constantes Más rápida en inserciones constantes
Casos de uso Apps móviles o dispositivos embebidos Procesamiento de grandes volúmenes de datos
9 changes: 9 additions & 0 deletions lab-solution3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>lab-solution3</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Archetype - lab-solution3</name>
<url>http://maven.apache.org</url>
</project>
59 changes: 59 additions & 0 deletions lab-solution3/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.ironhack;

import java.math.BigDecimal;

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

// 1. Prueba de BigDecimal Operations
System.out.println("--- 1. BigDecimal Operations ---");
BigDecimal testNum1 = new BigDecimal("4.2545");
System.out.println("Original: 4.2545 | Redondeado (centésimas): " +
BigDecimalOperations.roundToHundredth(testNum1));

BigDecimal testNum2 = new BigDecimal("1.2345");
BigDecimal testNum3 = new BigDecimal("-45.67");
System.out.println("Original: 1.2345 | Invertido y Redondeado (décimas): " +
BigDecimalOperations.inverseAndRoundToTenth(testNum2));
System.out.println("Original: -45.67 | Invertido y Redondeado (décimas): " +
BigDecimalOperations.inverseAndRoundToTenth(testNum3));
System.out.println();

// 2. Prueba de Car Inventory System
System.out.println("--- 2. Car Inventory System ---");
Car mySedan = new Sedan("ABC12345", "Toyota", "Corolla", 15000);
Car mySUV = new UtilityVehicle("XYZ67890", "Jeep", "Wrangler", 5000, true);
Car myTruck = new Truck("TRK999", "Ford", "F-150", 25000, 5000.5);

System.out.println(mySedan.getInfo());
System.out.println(mySUV.getInfo());
System.out.println(myTruck.getInfo());
System.out.println();

// 3. Prueba de Video Streaming Service
System.out.println("--- 3. Video Streaming Service ---");
Video myMovie = new Movie("Inception", 148, 8.8);
Video mySeries = new TvSeries("The Bear", 30, 18);

System.out.println(myMovie.getInfo());
System.out.println(mySeries.getInfo());
System.out.println();

// 4. Prueba de IntList Interface
System.out.println("--- 4. IntList System (Growth Test) ---");

IntList arrayList = new IntArrayList();
System.out.println("Añadiendo 12 elementos a IntArrayList...");
for (int i = 1; i <= 12; i++) {
arrayList.add(i * 10);
}
System.out.println("Elemento en índice 11: " + arrayList.get(11));

IntList vector = new IntVector();
System.out.println("Añadiendo 22 elementos a IntVector...");
for (int i = 1; i <= 22; i++) {
vector.add(i * 5);
}
System.out.println("Elemento en índice 21: " + vector.get(21));
}
}
26 changes: 26 additions & 0 deletions lab-solution3/src/main/java/com/ironhack/BigDecimalOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ironhack;

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

/**
* Operaciones de precisión con BigDecimal.
*/
public class BigDecimalOperations {

/**
* Redondea a la centésima más cercana (2 decimales).
*/
public static double roundToHundredth(BigDecimal value) {
if (value == null) return 0.0;
return value.setScale(2, RoundingMode.HALF_UP).doubleValue();
}

/**
* Invierte el signo y redondea a la décima más cercana (1 decimal).
*/
public static BigDecimal inverseAndRoundToTenth(BigDecimal value) {
if (value == null) return BigDecimal.ZERO;
return value.negate().setScale(1, RoundingMode.HALF_UP);
}
}
66 changes: 66 additions & 0 deletions lab-solution3/src/main/java/com/ironhack/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.ironhack;

/**
* Clase abstracta base para el inventario de coches.
*/
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 getInfo() {
return String.format("VIN: %s | Marca: %s | Modelo: %s | KM: %d",
vinNumber, make, model, mileage);
}
}

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

/**
* Clase UtilityVehicle con tracción 4x4.
*/
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() + " | 4x4: " + (fourWheelDrive ? "Sí" : "No");
}
}

/**
* Clase Truck con capacidad de remolque.
*/
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() + " | Capacidad Remolque: " + towingCapacity + " kg";
}
}
52 changes: 52 additions & 0 deletions lab-solution3/src/main/java/com/ironhack/IntList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.ironhack;

import java.util.Arrays;

/**
* Interfaz y sus implementaciones con estrategias de crecimiento.
*/
public interface IntList {
void add(int number);
int get(int id);
}

class IntArrayList implements IntList {
private int[] array = new int[10];
private int size = 0;

@Override
public void add(int number) {
if (size == array.length) {
// Crecimiento del 50%
int newCapacity = array.length + (array.length / 2);
array = Arrays.copyOf(array, newCapacity);
}
array[size++] = number;
}

@Override
public int get(int id) {
if (id < 0 || id >= size) throw new IndexOutOfBoundsException();
return array[id];
}
}

class IntVector implements IntList {
private int[] array = new int[20];
private int size = 0;

@Override
public void add(int number) {
if (size == array.length) {
// Crecimiento del 100% (doble)
array = Arrays.copyOf(array, array.length * 2);
}
array[size++] = number;
}

@Override
public int get(int id) {
if (id < 0 || id >= size) throw new IndexOutOfBoundsException();
return array[id];
}
}
Loading