From 6bf6e41b82c38ba559c5695f4faca224511c95f7 Mon Sep 17 00:00:00 2001 From: Amber Yeh <266454930+amberyeh41@users.noreply.github.com> Date: Sat, 30 May 2026 18:03:33 +0200 Subject: [PATCH] LabWeek3 --- LabWeek3/.gitignore | 39 ++++++++ LabWeek3/.idea/.gitignore | 10 ++ LabWeek3/.idea/encodings.xml | 7 ++ LabWeek3/.idea/misc.xml | 14 +++ LabWeek3/.idea/vcs.xml | 6 ++ LabWeek3/pom.xml | 17 ++++ .../src/main/java/BigDecimalExercise.java | 26 +++++ LabWeek3/src/main/java/CarSystem.java | 49 +++++++++ LabWeek3/src/main/java/IntListInterface.java | 99 +++++++++++++++++++ .../src/main/java/VideoStreamingService.java | 39 ++++++++ 10 files changed, 306 insertions(+) create mode 100644 LabWeek3/.gitignore create mode 100644 LabWeek3/.idea/.gitignore create mode 100644 LabWeek3/.idea/encodings.xml create mode 100644 LabWeek3/.idea/misc.xml create mode 100644 LabWeek3/.idea/vcs.xml create mode 100644 LabWeek3/pom.xml create mode 100644 LabWeek3/src/main/java/BigDecimalExercise.java create mode 100644 LabWeek3/src/main/java/CarSystem.java create mode 100644 LabWeek3/src/main/java/IntListInterface.java create mode 100644 LabWeek3/src/main/java/VideoStreamingService.java diff --git a/LabWeek3/.gitignore b/LabWeek3/.gitignore new file mode 100644 index 0000000..480bdf5 --- /dev/null +++ b/LabWeek3/.gitignore @@ -0,0 +1,39 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ +.kotlin + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/LabWeek3/.idea/.gitignore b/LabWeek3/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/LabWeek3/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/LabWeek3/.idea/encodings.xml b/LabWeek3/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/LabWeek3/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/LabWeek3/.idea/misc.xml b/LabWeek3/.idea/misc.xml new file mode 100644 index 0000000..56a2e7b --- /dev/null +++ b/LabWeek3/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/LabWeek3/.idea/vcs.xml b/LabWeek3/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/LabWeek3/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LabWeek3/pom.xml b/LabWeek3/pom.xml new file mode 100644 index 0000000..624ccd6 --- /dev/null +++ b/LabWeek3/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + com.workshop + LabWeek3 + 1.0-SNAPSHOT + + + 25 + 25 + UTF-8 + + + \ No newline at end of file diff --git a/LabWeek3/src/main/java/BigDecimalExercise.java b/LabWeek3/src/main/java/BigDecimalExercise.java new file mode 100644 index 0000000..8df8fbe --- /dev/null +++ b/LabWeek3/src/main/java/BigDecimalExercise.java @@ -0,0 +1,26 @@ + +double round(BigDecimal num){ + MathContext m = new MathContext(3); + BigDecimal rounded = num.round(m); + return rounded.doubleValue(); +} +double p2(BigDecimal num){ + MathContext m = new MathContext(2); + BigDecimal rounded = num.round(m); + rounded = rounded.multiply(new BigDecimal("-1")); + return rounded.doubleValue(); +} + + + + +void main() { + BigDecimal b1 = new BigDecimal("4.2585"); + double result = round(b1); + System.out.println("Rounded: " + result); + + result = p2(b1); + System.out.println("Rounded and flipped: " + result); +} + + diff --git a/LabWeek3/src/main/java/CarSystem.java b/LabWeek3/src/main/java/CarSystem.java new file mode 100644 index 0000000..26450c1 --- /dev/null +++ b/LabWeek3/src/main/java/CarSystem.java @@ -0,0 +1,49 @@ +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 "vinNumber: "+ vinNumber + + " make: " + make + " model: " + +model+" mileage: "+mileage; + } + +} + +public class Sedan extends Car { + public Sedan(String vinNumber, String make, String model, int mileage) { + super(vinNumber, make, model, mileage); + } +} +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 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; + } +} + + + + +void main() { + +} \ No newline at end of file diff --git a/LabWeek3/src/main/java/IntListInterface.java b/LabWeek3/src/main/java/IntListInterface.java new file mode 100644 index 0000000..3a6b957 --- /dev/null +++ b/LabWeek3/src/main/java/IntListInterface.java @@ -0,0 +1,99 @@ +public interface IntList{ + void add(int number); + int get(int id) throws Exception; +} +public class IntArrayList implements IntList{ + private int[] nums; + private int capacity = 10; + private int elements = 0; + + public IntArrayList() { + this.nums = new int[capacity]; + } + + @Override + public void add(int number) { + if (elements == capacity) { + int new_capacity = (int) Math.floor(capacity * 1.5); + int[] new_nums = new int[new_capacity]; + for(int i = 0; i < elements; ++i) { + new_nums[i] = nums[i]; + } + this.nums = new_nums; + this.capacity = new_capacity; + } + this.nums[elements] = number; + this.elements++; + } + + @Override + public int get(int id) throws Exception { + if (id < elements) { + return nums[id]; + } + throw new Exception("not enough elements"); + } +} + + +public class IntVector implements IntList{ + private int[] nums; + private int capacity = 20; + private int elements = 0; + + public IntVector() { + this.nums = new int[capacity]; + } + + @Override + public void add(int number) { + if (elements == capacity) { + int new_capacity = capacity * 2; + int[] new_nums = new int[new_capacity]; + for(int i = 0; i < elements; ++i) { + new_nums[i] = nums[i]; + } + this.nums = new_nums; + this.capacity = new_capacity; + } + this.nums[elements] = number; + this.elements++; + } + + @Override + public int get(int id) throws Exception { + if (id < elements) { + return nums[id]; + } + throw new Exception("not enough elements"); + } + +} + + + + + +void main() { + IntList my_list = new IntArrayList(); + for (int i = 0; i < 14; ++i) { + my_list.add(i); + } + try { + int elem = my_list.get(13); + System.out.println(elem); + } catch (Exception e) { + throw new RuntimeException(e); + } + + my_list = new IntVector(); + for (int i = 0; i < 14; ++i) { + my_list.add(i); + } + try { + int elem = my_list.get(13); + System.out.println(elem); + } catch (Exception e) { + throw new RuntimeException(e); + } +} \ No newline at end of file diff --git a/LabWeek3/src/main/java/VideoStreamingService.java b/LabWeek3/src/main/java/VideoStreamingService.java new file mode 100644 index 0000000..bb104e6 --- /dev/null +++ b/LabWeek3/src/main/java/VideoStreamingService.java @@ -0,0 +1,39 @@ +public abstract class Video{ + private String title; + private int duration; + + public Video(String title, int duration) { + this.title = title; + this.duration = duration; + } + + public String getInfo(){ + return "title: "+ title + " duration: " + duration; + } + +} + +public class TvSeries extends Video{ + private int episodes; + + public TvSeries(String title, int duration, int episodes) { + super(title, duration); + this.episodes = episodes; + } +} +public class Movie extends Video{ + private double rating; + + public Movie(String title, int duration,double rating) { + super(title, duration); + this.rating = rating; + } +} + + + + + +void main() { + +} \ No newline at end of file