From 3f0ed7a96bfb4614a39b54f4f9afc12b977d9ee7 Mon Sep 17 00:00:00 2001 From: Julio Gil Date: Sat, 14 Sep 2024 13:06:24 +0200 Subject: [PATCH] finished lab --- .idea/.gitignore | 3 + .idea/vcs.xml | 4 + Lab-1.08/.gitignore | 29 +++++ Lab-1.08/.idea/.gitignore | 3 + Lab-1.08/.idea/misc.xml | 6 ++ Lab-1.08/.idea/modules.xml | 8 ++ Lab-1.08/.idea/vcs.xml | 4 + Lab-1.08/Lab-1.08.iml | 11 ++ Lab-1.08/src/Main.java | 209 +++++++++++++++++++++++++++++++++++++ 9 files changed, 277 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/vcs.xml create mode 100644 Lab-1.08/.gitignore create mode 100644 Lab-1.08/.idea/.gitignore create mode 100644 Lab-1.08/.idea/misc.xml create mode 100644 Lab-1.08/.idea/modules.xml create mode 100644 Lab-1.08/.idea/vcs.xml create mode 100644 Lab-1.08/Lab-1.08.iml create mode 100644 Lab-1.08/src/Main.java 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/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..d843f34 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Lab-1.08/.gitignore b/Lab-1.08/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/Lab-1.08/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/Lab-1.08/.idea/.gitignore b/Lab-1.08/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Lab-1.08/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Lab-1.08/.idea/misc.xml b/Lab-1.08/.idea/misc.xml new file mode 100644 index 0000000..47478b9 --- /dev/null +++ b/Lab-1.08/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Lab-1.08/.idea/modules.xml b/Lab-1.08/.idea/modules.xml new file mode 100644 index 0000000..8b71e48 --- /dev/null +++ b/Lab-1.08/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Lab-1.08/.idea/vcs.xml b/Lab-1.08/.idea/vcs.xml new file mode 100644 index 0000000..d843f34 --- /dev/null +++ b/Lab-1.08/.idea/vcs.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Lab-1.08/Lab-1.08.iml b/Lab-1.08/Lab-1.08.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Lab-1.08/Lab-1.08.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Lab-1.08/src/Main.java b/Lab-1.08/src/Main.java new file mode 100644 index 0000000..bf828aa --- /dev/null +++ b/Lab-1.08/src/Main.java @@ -0,0 +1,209 @@ +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.List; + +class BigDecimalOperations { + + public static double roundToHundredth(BigDecimal number) { + return number.setScale(2, RoundingMode.HALF_UP).doubleValue(); + } + + public static double invertSignAndRound(BigDecimal number) { + BigDecimal inverted = number.negate(); + return inverted.setScale(1, RoundingMode.HALF_UP).doubleValue(); + } +} + +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(); +} + +class Sedan extends Car { + public Sedan(String vinNumber, String make, String model, int mileage) { + super(vinNumber, make, model, mileage); + } + + @Override + public String getInfo() { + return String.format("Sedan [VIN: %s, Make: %s, Model: %s, Mileage: %d]", vinNumber, make, model, mileage); + } +} + +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 String.format("UtilityVehicle [VIN: %s, Make: %s, Model: %s, Mileage: %d, FourWheelDrive: %b]", vinNumber, make, model, mileage, fourWheelDrive); + } +} + +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 String.format("Truck [VIN: %s, Make: %s, Model: %s, Mileage: %d, TowingCapacity: %.2f]", vinNumber, make, model, mileage, towingCapacity); + } +} + +abstract class Video { + protected String title; + protected int duration; + + public Video(String title, int duration) { + this.title = title; + this.duration = duration; + } + + public abstract String getInfo(); +} + +class TvSeries extends Video { + private int episodes; + + public TvSeries(String title, int duration, int episodes) { + super(title, duration); + this.episodes = episodes; + } + + @Override + public String getInfo() { + return String.format("TvSeries [Title: %s, Duration: %d minutes, Episodes: %d]", title, duration, episodes); + } +} + +class Movie extends Video { + private double rating; + + public Movie(String title, int duration, double rating) { + super(title, duration); + this.rating = rating; + } + + @Override + public String getInfo() { + return String.format("Movie [Title: %s, Duration: %d minutes, Rating: %.1f]", title, duration, rating); + } +} + +interface IntList { + void add(int number); + int get(int id); +} + +class IntArrayList implements IntList { + private int[] array; + private int size; + + public IntArrayList() { + array = new int[10]; + size = 0; + } + + @Override + public void add(int number) { + if (size == array.length) { + int newSize = (int) (array.length * 1.5); + int[] newArray = new int[newSize]; + System.arraycopy(array, 0, newArray, 0, array.length); + array = newArray; + } + 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 List list; + + public IntVector() { + list = new ArrayList<>(20); + } + + @Override + public void add(int number) { + list.add(number); + if (list.size() == list.size()) { + int newSize = list.size() * 2; + List newList = new ArrayList<>(newSize); + newList.addAll(list); + list = newList; + } + } + + @Override + public int get(int id) { + if (id < 0 || id >= list.size()) { + throw new IndexOutOfBoundsException(); + } + return list.get(id); + } +} + +public class Main { + public static void main(String[] args) { + BigDecimal num = new BigDecimal("4.2545"); + System.out.println("Round to Hundredth: " + BigDecimalOperations.roundToHundredth(num)); + + num = new BigDecimal("1.2345"); + System.out.println("Invert Sign and Round: " + BigDecimalOperations.invertSignAndRound(num)); + + Car sedan = new Sedan("1HGBH41JXMN109186", "Honda", "Civic", 12000); + Car utilityVehicle = new UtilityVehicle("1FTRW08L96KC85929", "Ford", "Explorer", 45000, true); + Car truck = new Truck("1GT12UEY1EZ351938", "Chevrolet", "Silverado", 80000, 9500); + + System.out.println(sedan.getInfo()); + System.out.println(utilityVehicle.getInfo()); + System.out.println(truck.getInfo()); + + Video tvSeries = new TvSeries("Breaking Bad", 45, 62); + Video movie = new Movie("Inception", 148, 8.8); + + System.out.println(tvSeries.getInfo()); + System.out.println(movie.getInfo()); + + IntList arrayList = new IntArrayList(); + IntList vectorList = new IntVector(); + + for (int i = 0; i < 12; i++) { + arrayList.add(i); + vectorList.add(i); + } + + for (int i = 0; i < 12; i++) { + System.out.println("ArrayList get(" + i + "): " + arrayList.get(i)); + System.out.println("VectorList get(" + i + "): " + vectorList.get(i)); + } + } +} \ No newline at end of file