diff --git a/.vscode/targets.log b/.vscode/targets.log index e4380962..797d33e0 100644 --- a/.vscode/targets.log +++ b/.vscode/targets.log @@ -7,7 +7,7 @@ make all --print-data-base --no-builtin-variables --no-builtin-rules --question # This program built for i386-apple-darwin11.3.0 -# Make data base, printed on Wed Apr 26 10:59:51 2023 +# Make data base, printed on Wed Apr 26 15:17:54 2023 # Variables @@ -170,7 +170,7 @@ MAKELEVEL := 0 # environment CONDA_PREFIX = /Users/yash/anaconda3 # environment -VSCODE_PID = 30534 +VSCODE_PID = 46681 # environment LANG = C # variable set hash-table stats: @@ -342,6 +342,6 @@ convert: .FORCE # strcache size: total = 4096 / max = 4096 / min = 4096 / avg = 4096 # strcache free: total = 4087 / max = 4087 / min = 4087 / avg = 4087 -# Finished Make data base on Wed Apr 26 10:59:52 2023 +# Finished Make data base on Wed Apr 26 15:17:54 2023 diff --git a/_notebooks/2023-04-27-CarLot.ipynb b/_notebooks/2023-04-27-CarLot.ipynb index e69de29b..222d21a7 100644 --- a/_notebooks/2023-04-27-CarLot.ipynb +++ b/_notebooks/2023-04-27-CarLot.ipynb @@ -0,0 +1,556 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Redo of the \"Random Test\"\n", + "> Redo of the \"Random Test\" for 1 seed points\n", + "\n", + "- title: Car Lot\n", + "- toc: true\n", + "- comments: false\n", + "- categories: [Week-31]" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Part 1\n", + "**class Book (Part 1) Close Book**\n", + "1. Define 1 argument constructor for car lot, \n", + "2. Define toString method for id and car lot.\n", + "3. Generate unique id for each vehicle\n", + "4. Create a public getter that has vehicleCount\n", + "5. Define tester method that initializes at least 2 vehicles, outputs id and year, and provides a count of vehicles in carLot." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Vehicle count: 2\n", + "Car 1 id: 8cb1f5e3-e7fa-4036-8671-1078b1aa0934\n", + "Car 1 year: 2020\n", + "Car 2 id: be41845c-7b19-4742-a6eb-526b4f2be355\n", + "Car 2 year: 2019\n", + "\n", + "Cars in CarLot:\n", + "Car ID: 8cb1f5e3-e7fa-4036-8671-1078b1aa0934, Year: 2020\n", + "Car ID: be41845c-7b19-4742-a6eb-526b4f2be355, Year: 2019\n", + "\n" + ] + } + ], + "source": [ + "import java.util.ArrayList;\n", + "import java.util.List;\n", + "import java.util.UUID;\n", + "\n", + "public class Car {\n", + " private String id;\n", + " private int year;\n", + "\n", + " public Car(int year) {\n", + " this.year = year;\n", + " this.id = UUID.randomUUID().toString();\n", + " }\n", + "\n", + " public String getId() {\n", + " return id;\n", + " }\n", + "\n", + " public int getYear() {\n", + " return year;\n", + " }\n", + "\n", + " public String toString() {\n", + " return \"Car ID: \" + id + \", Year: \" + year;\n", + " }\n", + "}\n", + "\n", + "public class CarLot {\n", + " private List cars;\n", + " private static int vehicleCount = 0;\n", + "\n", + " public CarLot(List cars) {\n", + " this.cars = cars;\n", + " vehicleCount += cars.size();\n", + " }\n", + "\n", + " public int getVehicleCount() {\n", + " return vehicleCount;\n", + " }\n", + "\n", + " public String toString() {\n", + " StringBuilder sb = new StringBuilder();\n", + " sb.append(\"Cars in CarLot:\\n\");\n", + " for (Car car : cars) {\n", + " sb.append(car.toString() + \"\\n\");\n", + " }\n", + " return sb.toString();\n", + " }\n", + " \n", + " public static void main(String[] args) {\n", + " Car car1 = new Car(2020);\n", + " Car car2 = new Car(2019);\n", + " List carList = new ArrayList<>();\n", + " carList.add(car1);\n", + " carList.add(car2);\n", + " CarLot carLot = new CarLot(carList);\n", + " System.out.println(\"Vehicle count: \" + carLot.getVehicleCount());\n", + " System.out.println(\"Car 1 id: \" + car1.getId() + \"\\nCar 1 year: \" + car1.getYear());\n", + " System.out.println(\"Car 2 id: \" + car2.getId() + \"\\nCar 2 year: \" + car2.getYear());\n", + " System.out.println(\"\\n\" + carLot.toString());\n", + " }\n", + "}\n", + "\n", + "CarLot.main(null);" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Part 2\n", + "**extended Classes (Part 2) Try to use alternate forms of loops and techniques for construction.**\n", + "1. Ensure Motorcycle and Coupe run the Constructor from Car.\n", + "2. Create instance variables unique to Motorcycle has 2 wheels, Coupe has 4 wheels. New items are not required by Constructor. \n", + "3. Make Getters and Setters for all new items. You can add your own.\n", + "4. Add a time when vehicle entered the lot. This should be same for Parent and Subclasses. \n", + "5. Make sure there are getters and setters for items as needed. For instance, be able to set items not required by constructor.\n", + "6. Define tester method to test all items." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Vehicle count: 2\n", + "a863c72a-c509-4cd7-a876-a3f5dcab08bc 2021 2\n", + "3197190c-127e-412c-b3cc-555ac3283024 2022 4 4\n", + "Cars in CarLot:\n", + "Car ID: a863c72a-c509-4cd7-a876-a3f5dcab08bc, Year: 2021, Entry Time: 2023-04-26T21:11:06.089458, Number of Wheels: 2\n", + "Car ID: 3197190c-127e-412c-b3cc-555ac3283024, Year: 2022, Entry Time: 2023-04-26T21:11:06.089504, Number of Wheels: 4, Number of Doors: 4\n", + "\n" + ] + } + ], + "source": [ + "import java.time.LocalDateTime;\n", + "import java.time.format.DateTimeFormatter;\n", + "import java.util.ArrayList;\n", + "import java.util.List;\n", + "import java.util.UUID;\n", + "\n", + "public class Car {\n", + " private String id;\n", + " private int year;\n", + " private LocalDateTime entryTime;\n", + "\n", + " public Car(int year) {\n", + " this.year = year;\n", + " this.id = UUID.randomUUID().toString();\n", + " this.entryTime = LocalDateTime.now();\n", + " }\n", + "\n", + " public String getId() {\n", + " return id;\n", + " }\n", + "\n", + " public int getYear() {\n", + " return year;\n", + " }\n", + "\n", + " public LocalDateTime getEntryTime() {\n", + " return entryTime;\n", + " }\n", + "\n", + " public String toString() {\n", + " return \"Car ID: \" + id + \", Year: \" + year + \", Entry Time: \" + entryTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);\n", + " }\n", + "}\n", + "\n", + "public class Motorcycle extends Car {\n", + " private int numWheels;\n", + "\n", + " public Motorcycle(int year) {\n", + " super(year);\n", + " this.numWheels = 2;\n", + " }\n", + "\n", + " public int getNumWheels() {\n", + " return numWheels;\n", + " }\n", + "\n", + " public void setNumWheels(int numWheels) {\n", + " this.numWheels = numWheels;\n", + " }\n", + "\n", + " public String toString() {\n", + " return super.toString() + \", Number of Wheels: \" + numWheels;\n", + " }\n", + "}\n", + "\n", + "public class Coupe extends Car {\n", + " private int numWheels;\n", + " private int numDoors;\n", + "\n", + " public Coupe(int year) {\n", + " super(year);\n", + " this.numWheels = 4;\n", + " this.numDoors = 2;\n", + " }\n", + "\n", + " public int getNumWheels() {\n", + " return numWheels;\n", + " }\n", + "\n", + " public void setNumWheels(int numWheels) {\n", + " this.numWheels = numWheels;\n", + " }\n", + "\n", + " public int getNumDoors() {\n", + " return numDoors;\n", + " }\n", + "\n", + " public void setNumDoors(int numDoors) {\n", + " this.numDoors = numDoors;\n", + " }\n", + "\n", + " public String toString() {\n", + " return super.toString() + \", Number of Wheels: \" + numWheels + \", Number of Doors: \" + numDoors;\n", + " }\n", + "}\n", + "\n", + "public class CarLot {\n", + " private List cars;\n", + " private static int vehicleCount = 0;\n", + "\n", + " public CarLot(List cars) {\n", + " this.cars = cars;\n", + " vehicleCount += cars.size();\n", + " }\n", + "\n", + " public int getVehicleCount() {\n", + " return vehicleCount;\n", + " }\n", + "\n", + " public String toString() {\n", + " StringBuilder sb = new StringBuilder();\n", + " sb.append(\"Cars in CarLot:\\n\");\n", + " for (Car car : cars) {\n", + " sb.append(car.toString() + \"\\n\");\n", + " }\n", + " return sb.toString();\n", + " }\n", + "\n", + " public static void main(String[] args) {\n", + " Motorcycle moto1 = new Motorcycle(2021);\n", + " Coupe coupe1 = new Coupe(2022);\n", + "\n", + " moto1.setNumWheels(2);\n", + " coupe1.setNumDoors(4);\n", + "\n", + " List carList = new ArrayList<>();\n", + " carList.add(moto1);\n", + " carList.add(coupe1);\n", + " CarLot carLot = new CarLot(carList);\n", + "\n", + " System.out.println(\"Vehicle count: \" + carLot.getVehicleCount());\n", + " System.out.println(moto1.getId() + \" \" + moto1.getYear() + \" \" + moto1.getNumWheels());\n", + " System.out.println(coupe1.getId() + \" \" + coupe1.getYear() + \" \" + coupe1.getNumWheels() + \" \" + coupe1.getNumDoors());\n", + " System.out.println(carLot.toString());\n", + " }\n", + "}\n", + "\n", + "CarLot.main(null);" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Part 3\n", + "**Simulation (Part 3)**\n", + "1. Build a Tester Method that does a Simulation.\n", + "2. Define a default method in Vehicle that returns \"current shelfLife\" from date/time of construction. (Hint, think about capturing time in sorts)\n", + "3. Define shelfLife expiration methods as needed in Motorcycle and Coupe. \n", + " - A Coupe has a fixed shelf life based on the date/time of constructor. (Hint, simulation 3 yrs as 3000 nanoseconds)\n", + " - A Motorcycle has a computed shelf life of expiration, the simulation should extend shelf life if a certain number of return stamps where placed in the book. (Hint, 3 return stamps renews for an extra year)\n", + "4. Use a sleep in Java to assist with simulation\n", + "5. Make a method that looks at vehicle in lot and determines if they need to get of the shelf, try to have year and on/off status in output.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Adding vehicles to the lot...\n", + "Car Lot:\n", + "Vehicle ID: 5d4270c0-445c-4f68-bc31-3d23c6d3510f, Year: 2023, Entry Time: 2023-04-26T21:24:00.893494\n", + "Vehicle ID: 523be08c-3cbe-4e7e-8ac3-a5a1cfdb6b13, Year: 2015, Entry Time: 2023-04-26T21:24:00.893551\n", + "\n", + "Vehicle count: 2\n", + "Simulating passage of time...\n", + "Motorcycle with ID 5d4270c0-445c-4f68-bc31-3d23c6d3510f is expired.\n", + "Coupe with ID 523be08c-3cbe-4e7e-8ac3-a5a1cfdb6b13 is not yet expired.\n", + "Motorcycle with ID 5d4270c0-445c-4f68-bc31-3d23c6d3510f is expired.\n", + "Coupe with ID 523be08c-3cbe-4e7e-8ac3-a5a1cfdb6b13 is not yet expired.\n", + "Motorcycle with ID 5d4270c0-445c-4f68-bc31-3d23c6d3510f is expired.\n", + "Coupe with ID 523be08c-3cbe-4e7e-8ac3-a5a1cfdb6b13 is not yet expired.\n", + "Removing vehicles from the lot...\n", + "Car Lot:\n", + "Vehicle ID: 5d4270c0-445c-4f68-bc31-3d23c6d3510f, Year: 2023, Entry Time: 2023-04-26T21:24:00.893494\n", + "\n", + "Vehicle count: 1\n" + ] + } + ], + "source": [ + "import java.time.Duration;\n", + "import java.time.LocalDateTime;\n", + "import java.time.format.DateTimeFormatter;\n", + "import java.util.ArrayList;\n", + "import java.util.List;\n", + "import java.util.UUID;\n", + "\n", + "public abstract class Vehicle {\n", + " private String id;\n", + " private int year;\n", + " private LocalDateTime entryTime;\n", + " private LocalDateTime shelfLifeExpiration;\n", + "\n", + " public Vehicle(int year) {\n", + " this.year = year;\n", + " this.id = UUID.randomUUID().toString();\n", + " this.entryTime = LocalDateTime.now();\n", + " this.shelfLifeExpiration = entryTime.plusNanos(1000);\n", + " }\n", + "\n", + " public String getId() {\n", + " return id;\n", + " }\n", + "\n", + " public int getYear() {\n", + " return year;\n", + " }\n", + "\n", + " public LocalDateTime getEntryTime() {\n", + " return entryTime;\n", + " }\n", + "\n", + " public LocalDateTime getShelfLifeExpiration() {\n", + " return shelfLifeExpiration;\n", + " }\n", + "\n", + " public void setShelfLifeExpiration(LocalDateTime shelfLifeExpiration) {\n", + " this.shelfLifeExpiration = shelfLifeExpiration;\n", + " }\n", + "\n", + " public Duration getCurrentShelfLife() {\n", + " LocalDateTime now = LocalDateTime.now();\n", + " return Duration.between(entryTime, now);\n", + " }\n", + "\n", + " public abstract void checkShelfLifeExpiration();\n", + "\n", + " public String toString() {\n", + " return \"Vehicle ID: \" + id + \", Year: \" + year + \", Entry Time: \" + entryTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);\n", + " }\n", + "}\n", + "\n", + "public class Motorcycle extends Vehicle {\n", + " private int numWheels;\n", + " private int numReturnStamps;\n", + "\n", + " public Motorcycle(int year) {\n", + " super(year);\n", + " this.numWheels = 2;\n", + " this.numReturnStamps = 0;\n", + " }\n", + "\n", + " public int getNumWheels() {\n", + " return numWheels;\n", + " }\n", + "\n", + " public void setNumWheels(int numWheels) {\n", + " this.numWheels = numWheels;\n", + " }\n", + "\n", + " public int getNumReturnStamps() {\n", + " return numReturnStamps;\n", + " }\n", + "\n", + " public void setNumReturnStamps(int numReturnStamps) {\n", + " this.numReturnStamps = numReturnStamps;\n", + " }\n", + "\n", + " public void checkShelfLifeExpiration() {\n", + " LocalDateTime now = LocalDateTime.now();\n", + " if (now.isAfter(getShelfLifeExpiration())) {\n", + " System.out.println(\"Motorcycle with ID \" + getId() + \" is expired.\");\n", + " } else {\n", + " System.out.println(\"Motorcycle with ID \" + getId() + \" is not yet expired.\");\n", + " }\n", + " }\n", + "}\n", + "\n", + "public class Coupe extends Vehicle {\n", + " private int numWheels;\n", + " private int numDoors;\n", + "\n", + " public Coupe(int year) {\n", + " super(year);\n", + " this.numWheels = 4;\n", + " this.numDoors = 2;\n", + " this.setShelfLifeExpiration(getEntryTime().plusNanos(Duration.ofDays(1095).toNanos())); // 1095 days = 3 years\n", + " }\n", + "\n", + " public int getNumWheels() {\n", + " return numWheels;\n", + " }\n", + "\n", + " public void setNumWheels(int numWheels) {\n", + " this.numWheels = numWheels;\n", + " }\n", + "\n", + " public int getNumDoors() {\n", + " return numDoors;\n", + " }\n", + "\n", + " public void setNumDoors(int numDoors) {\n", + " this.numDoors = numDoors;\n", + " }\n", + "\n", + " public void checkShelfLifeExpiration() {\n", + " LocalDateTime now = LocalDateTime.now();\n", + " if (now.isAfter(getShelfLifeExpiration())) {\n", + " System.out.println(\"Coupe with ID \" + getId() + \" is expired.\");\n", + " } else {\n", + " System.out.println(\"Coupe with ID \" + getId() + \" is not yet expired.\");\n", + " }\n", + " }\n", + "}\n", + "\n", + "public class CarLot {\n", + " private List lot;\n", + " private int vehicleCount;\n", + "\n", + " public CarLot() {\n", + " this.vehicleCount = 0;\n", + " this.lot = new ArrayList<>();\n", + " }\n", + "\n", + " public int getVehicleCount() {\n", + " return vehicleCount;\n", + " }\n", + "\n", + " public void addVehicle(Vehicle vehicle) {\n", + " vehicleCount++;\n", + " this.lot.add(vehicle);\n", + " }\n", + "\n", + " public void removeVehicle(Vehicle vehicle) {\n", + " vehicleCount--;\n", + " this.lot.remove(vehicle);\n", + " }\n", + "\n", + " public void checkShelfLifeExpiration() {\n", + " for (Vehicle vehicle : this.lot) {\n", + " vehicle.checkShelfLifeExpiration();\n", + " }\n", + " }\n", + "\n", + " public String toString() {\n", + " StringBuilder sb = new StringBuilder();\n", + " sb.append(\"Car Lot:\\n\");\n", + " for (Vehicle vehicle : this.lot) {\n", + " sb.append(vehicle.toString() + \"\\n\");\n", + " }\n", + " return sb.toString();\n", + " }\n", + "}\n", + "\n", + "public class Tester {\n", + " public static void main(String[] args) throws InterruptedException {\n", + " CarLot lot = new CarLot();\n", + " Motorcycle motorcycle = new Motorcycle(2023);\n", + " Coupe coupe = new Coupe(2015);\n", + "\n", + " System.out.println(\"Adding vehicles to the lot...\");\n", + " lot.addVehicle(motorcycle);\n", + " lot.addVehicle(coupe);\n", + " System.out.println(lot.toString());\n", + " System.out.println(\"Vehicle count: \" + lot.getVehicleCount());\n", + "\n", + " System.out.println(\"Simulating passage of time...\");\n", + " Thread.sleep(2000); // 2 seconds\n", + " lot.checkShelfLifeExpiration();\n", + " Thread.sleep(2000); // 2 seconds\n", + " lot.checkShelfLifeExpiration();\n", + " Thread.sleep(2000); // 2 seconds\n", + " lot.checkShelfLifeExpiration();\n", + "\n", + " System.out.println(\"Removing vehicles from the lot...\");\n", + " lot.removeVehicle(coupe);\n", + " System.out.println(lot.toString());\n", + " System.out.println(\"Vehicle count: \" + lot.getVehicleCount());\n", + " }\n", + "}\n", + "\n", + "Tester.main(null);" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Java", + "language": "java", + "name": "java" + }, + "language_info": { + "codemirror_mode": "java", + "file_extension": ".jshell", + "mimetype": "text/x-java-source", + "name": "Java", + "pygments_lexer": "java", + "version": "11.0.12+8-LTS-237" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/_notebooks/2023-04-27-FRQs.ipynb b/_notebooks/2023-04-27-FRQs.ipynb index 07516b88..191f4b65 100644 --- a/_notebooks/2023-04-27-FRQs.ipynb +++ b/_notebooks/2023-04-27-FRQs.ipynb @@ -26,11 +26,7 @@ { "cell_type": "code", "execution_count": 25, - "metadata": { - "vscode": { - "languageId": "java" - } - }, + "metadata": {}, "outputs": [], "source": [ "//////////////////// GIVEN CODE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", @@ -130,11 +126,7 @@ { "cell_type": "code", "execution_count": 26, - "metadata": { - "vscode": { - "languageId": "java" - } - }, + "metadata": {}, "outputs": [], "source": [ "public class StepTracker {\n", @@ -204,11 +196,7 @@ { "cell_type": "code", "execution_count": 27, - "metadata": { - "vscode": { - "languageId": "java" - } - }, + "metadata": {}, "outputs": [], "source": [ "import java.util.ArrayList;\n", @@ -284,11 +272,7 @@ { "cell_type": "code", "execution_count": 28, - "metadata": { - "vscode": { - "languageId": "java" - } - }, + "metadata": {}, "outputs": [], "source": [ "public class LightBoard {\n", @@ -346,7 +330,7 @@ "codemirror_mode": "java", "file_extension": ".jshell", "mimetype": "text/x-java-source", - "name": "Java", + "name": "java", "pygments_lexer": "java", "version": "11.0.12+8-LTS-237" },