From de27ec7b10a4661fd4f4ac02f5f4b37451d8a04a Mon Sep 17 00:00:00 2001 From: Nick Villone Date: Thu, 14 Jul 2022 20:52:51 -0400 Subject: [PATCH 01/12] solved exercises --- src/exercises/AlicesWonderland.java | 33 ++++++++++++++++++++++++ src/exercises/GasUsed.java | 23 +++++++++++++++++ src/exercises/HelloWorld.java | 16 ++++++++++++ src/exercises/NumericTypes.java | 22 ++++++++++++++++ src/org/launchcode/java/demos/Hello.java | 3 +-- 5 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 src/exercises/AlicesWonderland.java create mode 100644 src/exercises/GasUsed.java create mode 100644 src/exercises/HelloWorld.java create mode 100644 src/exercises/NumericTypes.java diff --git a/src/exercises/AlicesWonderland.java b/src/exercises/AlicesWonderland.java new file mode 100644 index 000000000..536f142b8 --- /dev/null +++ b/src/exercises/AlicesWonderland.java @@ -0,0 +1,33 @@ +package exercises; + +import java.util.Scanner; + +public class AlicesWonderland { + public static void main(String[] args) { + String alice = "Alice was beginning to get very tired of sitting by her sister on the bank, " + + "and of having nothing to do: once or twice she had peeped into the book her sister was reading, " + + "but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice " + + "'without pictures or conversation?'"; + + Scanner input = new Scanner(System.in); + System.out.println("Pick a word to find: "); + String searchTerm = input.nextLine(); + + if(alice.toLowerCase().contains(searchTerm)) { + System.out.println("The string includes " + searchTerm + "."); + int index = alice.indexOf(searchTerm); + int length = searchTerm.length(); + System.out.println("Your search term is at index " + index + ". Your search term is " + length + " characters." ); + String newSentence = alice.replace(searchTerm, ""); + System.out.println(newSentence); + } else { + System.out.println(searchTerm + " is not included in the string."); + } + + + + + + + } +} diff --git a/src/exercises/GasUsed.java b/src/exercises/GasUsed.java new file mode 100644 index 000000000..95b7a8674 --- /dev/null +++ b/src/exercises/GasUsed.java @@ -0,0 +1,23 @@ +package exercises; + +import java.util.Scanner; + +public class GasUsed { + public static void main(String[] args){ + Scanner input = new Scanner(System.in); + System.out.println("How many miles did you drive? "); + Double miles = input.nextDouble(); + + + Scanner input2 = new Scanner(System.in); + System.out.println("How many gallons of gas did you use? "); + Double gallons = input2.nextDouble(); + input.close(); + + double gallonsPerMile = miles / gallons; + System.out.println("You are running on " + gallonsPerMile + " gpm."); + + + } + +} diff --git a/src/exercises/HelloWorld.java b/src/exercises/HelloWorld.java new file mode 100644 index 000000000..40541f885 --- /dev/null +++ b/src/exercises/HelloWorld.java @@ -0,0 +1,16 @@ +package exercises; + +import java.util.Scanner; + + +public class HelloWorld { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.println("Hello, what is your name? "); + + String name = input.nextLine(); + System.out.println("Hello " + name); + + } + +} diff --git a/src/exercises/NumericTypes.java b/src/exercises/NumericTypes.java new file mode 100644 index 000000000..36f559e32 --- /dev/null +++ b/src/exercises/NumericTypes.java @@ -0,0 +1,22 @@ +package exercises; + +import java.util.Scanner; + +public class NumericTypes { + public static void main(String[] args) { + + Scanner input = new Scanner(System.in); + System.out.println("Enter the length of the rectangle: "); + Integer length = input.nextInt(); + + Scanner input2 = new Scanner(System.in); + System.out.println("Enter the width of the rectangle: "); + Integer width = input2.nextInt(); + + int rectangleArea = length * width; + System.out.println("The area of the rectangle is " + rectangleArea); + + + } + +} diff --git a/src/org/launchcode/java/demos/Hello.java b/src/org/launchcode/java/demos/Hello.java index e2bd31390..c62fa522c 100644 --- a/src/org/launchcode/java/demos/Hello.java +++ b/src/org/launchcode/java/demos/Hello.java @@ -4,7 +4,6 @@ * From "Java Web Development" */ public class Hello { - public static void main(String[] args) { - System.out.println("Hello, World"); + public static void main(String[] args) { System.out.println("Hello, World"); } } From 5faef47f74088d33a8da094e0068931fd105a715 Mon Sep 17 00:00:00 2001 From: Nick Villone Date: Mon, 18 Jul 2022 21:17:47 -0400 Subject: [PATCH 02/12] solved studio and bonus missions --- .../java/studios/areaofacircle/Area.java | 27 +++++++++++++++++++ .../java/studios/areaofacircle/Circle.java | 9 +++++++ 2 files changed, 36 insertions(+) create mode 100644 src/org/launchcode/java/studios/areaofacircle/Area.java create mode 100644 src/org/launchcode/java/studios/areaofacircle/Circle.java diff --git a/src/org/launchcode/java/studios/areaofacircle/Area.java b/src/org/launchcode/java/studios/areaofacircle/Area.java new file mode 100644 index 000000000..f072235c7 --- /dev/null +++ b/src/org/launchcode/java/studios/areaofacircle/Area.java @@ -0,0 +1,27 @@ +package org.launchcode.java.studios.areaofacircle; + +import java.util.Scanner; + +public class Area { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.println("Enter the radius of the circle: "); + double radius; + if (input.hasNextDouble()) { + radius = input.nextDouble(); + + if (radius > 0) { + Double circleArea = Circle.getArea(radius); + System.out.println("The area of a circle of radius " + radius + " is: " + circleArea); + + } else { + System.out.println("radius must be a positive number"); + } + } else { + System.out.println("radius must be a number"); + } + + input.close(); + + } +} diff --git a/src/org/launchcode/java/studios/areaofacircle/Circle.java b/src/org/launchcode/java/studios/areaofacircle/Circle.java new file mode 100644 index 000000000..c206d9f1f --- /dev/null +++ b/src/org/launchcode/java/studios/areaofacircle/Circle.java @@ -0,0 +1,9 @@ +package org.launchcode.java.studios.areaofacircle; + +public class Circle { + public static Double getArea(Double radius){ + return 3.14 * radius * radius; + } + + +} From c7e1bf9882bfea9cc756f507794fa7039ee374df Mon Sep 17 00:00:00 2001 From: Nick Villone Date: Wed, 20 Jul 2022 20:30:10 -0400 Subject: [PATCH 03/12] array exercise complete --- src/exercises/Array.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/exercises/Array.java diff --git a/src/exercises/Array.java b/src/exercises/Array.java new file mode 100644 index 000000000..26a559538 --- /dev/null +++ b/src/exercises/Array.java @@ -0,0 +1,25 @@ +package exercises; + +import java.util.Arrays; + +public class Array { + public static void main(String[] args) { + int[] integerArray = {1, 1, 2, 3, 5, 8}; + + for (int i : integerArray) { + if ( i%2 == 1 ) System.out.println(i); + } + + + String phrase = "I would not, could not, in a box. I would not, could not with a fox. I will not eat them in a house. I will not eat them with a mouse."; + String[] words = phrase.split(" "); + System.out.println(Arrays.toString(words)); + + String[] sentences = phrase.split("\\."); + System.out.println(Arrays.toString(sentences)); + + } + +} + + From fd9e2d7b6429135ffcfc8695329300807edf719e Mon Sep 17 00:00:00 2001 From: Nick Villone Date: Wed, 20 Jul 2022 21:54:42 -0400 Subject: [PATCH 04/12] Hashmap exercise complete --- src/exercises/HashMap.java | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/exercises/HashMap.java diff --git a/src/exercises/HashMap.java b/src/exercises/HashMap.java new file mode 100644 index 000000000..79c27c94c --- /dev/null +++ b/src/exercises/HashMap.java @@ -0,0 +1,42 @@ +package exercises; + +import java.util.Map; +import java.util.Scanner; + +public class HashMap { + + public static void main(String[] args) { + + java.util.HashMap classRoster = new java.util.HashMap<>(); + Scanner input = new Scanner(System.in); + String newStudent; + + System.out.println("Enter your students (or ENTER to finish):"); + + // Get student names and grades + do { + + System.out.print("Student: "); + newStudent = input.nextLine(); + + if (!newStudent.equals("")) { + System.out.print("ID: "); + Integer newID = input.nextInt(); + classRoster.put(newStudent, newID); + + // Read in the newline before looping back + input.nextLine(); + } + + } while(!newStudent.equals("")); + + // Print class roster + System.out.println("\nClass roster:"); + + for (Map.Entry student : classRoster.entrySet()) { + System.out.println(student.getKey() + " (" + student.getValue() + ")"); + } + + System.out.println("Number of students in roster: " + classRoster.size()); + } +} From f3f3188d3bd6094eafd71080b1a4e269b440d288 Mon Sep 17 00:00:00 2001 From: Nick Villone Date: Thu, 21 Jul 2022 18:03:04 -0400 Subject: [PATCH 05/12] exercises complete --- src/exercises/AlicesWonderland.java | 1 + src/exercises/Array.java | 38 ++++++++++++++++++++++++---- src/exercises/ArrayListPractice.java | 29 +++++++++++++++++++++ src/exercises/GasUsed.java | 1 + src/exercises/HelloWorld.java | 1 + src/exercises/NumericTypes.java | 2 ++ 6 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 src/exercises/ArrayListPractice.java diff --git a/src/exercises/AlicesWonderland.java b/src/exercises/AlicesWonderland.java index 536f142b8..946032b17 100644 --- a/src/exercises/AlicesWonderland.java +++ b/src/exercises/AlicesWonderland.java @@ -12,6 +12,7 @@ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Pick a word to find: "); String searchTerm = input.nextLine(); + input.close(); if(alice.toLowerCase().contains(searchTerm)) { System.out.println("The string includes " + searchTerm + "."); diff --git a/src/exercises/Array.java b/src/exercises/Array.java index 26a559538..6339169b6 100644 --- a/src/exercises/Array.java +++ b/src/exercises/Array.java @@ -1,17 +1,18 @@ package exercises; -import java.util.Arrays; +import java.util.*; public class Array { public static void main(String[] args) { - int[] integerArray = {1, 1, 2, 3, 5, 8}; + int[] intArray = {1, 1, 2, 3, 5, 8}; - for (int i : integerArray) { + for (int i : intArray) { if ( i%2 == 1 ) System.out.println(i); } - String phrase = "I would not, could not, in a box. I would not, could not with a fox. I will not eat them in a house. I will not eat them with a mouse."; + String phrase = "I would not, could not, in a box. I would not, could not with a fox. " + + "I will not eat them in a house. I will not eat them with a mouse."; String[] words = phrase.split(" "); System.out.println(Arrays.toString(words)); @@ -20,6 +21,33 @@ public static void main(String[] args) { } -} + + public static void main(ArrayList arr) { + + Scanner input = new Scanner(System.in); + + String str = "Java convert String to ArrayList"; + + // split string by no space + String[] strSplit = str.split(""); + + // Now convert string into ArrayList + ArrayList strList = new ArrayList<>( + Arrays.asList(strSplit)); + + // Now print the ArrayList + for (String s : strList) { + System.out.println(s); + System.out.println("Enter a word length: "); + int numChars = input.nextInt(); + if (numChars == s.length()) { + System.out.println(s); + } + } + + } + + } + diff --git a/src/exercises/ArrayListPractice.java b/src/exercises/ArrayListPractice.java new file mode 100644 index 000000000..15f500378 --- /dev/null +++ b/src/exercises/ArrayListPractice.java @@ -0,0 +1,29 @@ +package exercises; + +import java.util.ArrayList; + + +public class ArrayListPractice { + public static void main(String[] args) { + + ArrayList arr = new ArrayList<>(); + arr.add(2); + arr.add(11); + arr.add(12); + arr.add(15); + arr.add(17); + arr.add(22); + arr.add(26); + arr.add(29); + arr.add(31); + arr.add(36); + int total = 0; + for (int integer : arr) { + if (integer % 2 == 0) { + total += integer; + } + } + System.out.println(total); + } + +} diff --git a/src/exercises/GasUsed.java b/src/exercises/GasUsed.java index 95b7a8674..b5e082f46 100644 --- a/src/exercises/GasUsed.java +++ b/src/exercises/GasUsed.java @@ -13,6 +13,7 @@ public static void main(String[] args){ System.out.println("How many gallons of gas did you use? "); Double gallons = input2.nextDouble(); input.close(); + input2.close(); double gallonsPerMile = miles / gallons; System.out.println("You are running on " + gallonsPerMile + " gpm."); diff --git a/src/exercises/HelloWorld.java b/src/exercises/HelloWorld.java index 40541f885..cdb4bd105 100644 --- a/src/exercises/HelloWorld.java +++ b/src/exercises/HelloWorld.java @@ -10,6 +10,7 @@ public static void main(String[] args) { String name = input.nextLine(); System.out.println("Hello " + name); + input.close(); } diff --git a/src/exercises/NumericTypes.java b/src/exercises/NumericTypes.java index 36f559e32..5701ed05f 100644 --- a/src/exercises/NumericTypes.java +++ b/src/exercises/NumericTypes.java @@ -16,6 +16,8 @@ public static void main(String[] args) { int rectangleArea = length * width; System.out.println("The area of the rectangle is " + rectangleArea); + input.close(); + input2.close(); } From dc57916f74c1acdd00d0f434fb6a5ad27bc27a03 Mon Sep 17 00:00:00 2001 From: Nick Villone Date: Thu, 21 Jul 2022 21:15:05 -0400 Subject: [PATCH 06/12] character counter studio complete --- .../java/studios/countingCharacters/Main.java | 27 +++++++++++++ .../countingCharacters/stringConversion.java | 38 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/org/launchcode/java/studios/countingCharacters/Main.java create mode 100644 src/org/launchcode/java/studios/countingCharacters/stringConversion.java diff --git a/src/org/launchcode/java/studios/countingCharacters/Main.java b/src/org/launchcode/java/studios/countingCharacters/Main.java new file mode 100644 index 000000000..01cdd505a --- /dev/null +++ b/src/org/launchcode/java/studios/countingCharacters/Main.java @@ -0,0 +1,27 @@ +package org.launchcode.java.studios.countingCharacters; + +import java.util.Scanner; + +public class Main { + + + + public static void main(String[] args) { + + Scanner input; + + input = new Scanner (System.in); + + System.out.println("Enter a quote: "); + String phrase = input.nextLine(); + input.close(); + +// String quote = "If the product of two terms is zero then common sense says at" + +// " least one of the two terms has to be zero to start with. So if you move all the " + +// "terms over to one side, you can put the quadratics into a form that can be factored " + +// "allowing that side of the equation to equal zero. Once you’ve done that, it’s pretty " + +// "straightforward from there."; + + stringConversion.CountChars(phrase); + } +} diff --git a/src/org/launchcode/java/studios/countingCharacters/stringConversion.java b/src/org/launchcode/java/studios/countingCharacters/stringConversion.java new file mode 100644 index 000000000..eedad7208 --- /dev/null +++ b/src/org/launchcode/java/studios/countingCharacters/stringConversion.java @@ -0,0 +1,38 @@ +package org.launchcode.java.studios.countingCharacters; + + +import java.util.HashMap; +import java.util.Map; + +public class stringConversion { + public static void CountChars(String input) { + +// turn string to array of characters + char[] characters = input.toUpperCase().toCharArray(); + + HashMap scores = new HashMap<>(); + + for (char letter : characters){ + if(Character.isLetter(letter)) { +// if scores doesn't have letter then add it to scores HashMap + if(!scores.containsKey(letter)){ + scores.put(letter, 1); + }else { + scores.put(letter, (scores.get(letter) + 1)); + } + + + } + + + } + for (Map.Entry score : scores.entrySet()) { + System.out.printf("%S : %S%n", score.getKey(), score.getValue()); + } + + + } + + + } + From d2d54f4c09390f1b104f294d254ff1f39e2aa538 Mon Sep 17 00:00:00 2001 From: Nick Villone Date: Mon, 25 Jul 2022 18:50:13 -0400 Subject: [PATCH 07/12] chapter 3 exercises completed --- .../java/demos/lsn3classes1/Course.java | 11 ++++++ .../demos/lsn3classes1/SchoolPractice.java | 9 +++++ .../java/demos/lsn3classes1/Student.java | 23 ++++++++++++ .../java/demos/lsn3classes1/Teacher.java | 36 +++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 src/org/launchcode/java/demos/lsn3classes1/Course.java create mode 100644 src/org/launchcode/java/demos/lsn3classes1/Teacher.java diff --git a/src/org/launchcode/java/demos/lsn3classes1/Course.java b/src/org/launchcode/java/demos/lsn3classes1/Course.java new file mode 100644 index 000000000..d3030ac8d --- /dev/null +++ b/src/org/launchcode/java/demos/lsn3classes1/Course.java @@ -0,0 +1,11 @@ +package org.launchcode.java.demos.lsn3classes1; + +import java.util.ArrayList; + +public class Course { + + private String subject; + private Teacher instructor; + private ArrayList students; + +} diff --git a/src/org/launchcode/java/demos/lsn3classes1/SchoolPractice.java b/src/org/launchcode/java/demos/lsn3classes1/SchoolPractice.java index 7ea3d53ee..104bb2e5a 100644 --- a/src/org/launchcode/java/demos/lsn3classes1/SchoolPractice.java +++ b/src/org/launchcode/java/demos/lsn3classes1/SchoolPractice.java @@ -1,7 +1,16 @@ package org.launchcode.java.demos.lsn3classes1; +import org.w3c.dom.ls.LSOutput; + public class SchoolPractice { public static void main(String[] args) { // Instantiate your Student class for part 2 here! + Student nick = new Student("Nick", 12342, 1, 4.0); + + System.out.println(nick.getName()); + System.out.println(nick); + } + + } diff --git a/src/org/launchcode/java/demos/lsn3classes1/Student.java b/src/org/launchcode/java/demos/lsn3classes1/Student.java index 73f21c5fc..20fe59d23 100644 --- a/src/org/launchcode/java/demos/lsn3classes1/Student.java +++ b/src/org/launchcode/java/demos/lsn3classes1/Student.java @@ -10,4 +10,27 @@ public class Student { private int numberOfCredits = 0; private double gpa = 0.0; + public Student(String name, int studentId, int numberOfCredits, double gpa) { + this.name = name; + this.studentId = studentId; + this.numberOfCredits = numberOfCredits; + this.gpa = gpa; + } + public void setName(String name) {this.name = name;} + + public void setStudentId(int studentId) {this.studentId = studentId;} + +// public void setGpa(double gpa) {this.gpa = gpa;} + +// public void setNumberOfCredits(int numberOfCredits) {this.numberOfCredits = numberOfCredits;} + + public String getName() {return this.name;} + + private int getStudentId() {return this.studentId;} + + public int getNumberOfCredits() {return this.numberOfCredits;} + + public double getGpa() {return this.gpa;} + + } \ No newline at end of file diff --git a/src/org/launchcode/java/demos/lsn3classes1/Teacher.java b/src/org/launchcode/java/demos/lsn3classes1/Teacher.java new file mode 100644 index 000000000..2fc8ac8c9 --- /dev/null +++ b/src/org/launchcode/java/demos/lsn3classes1/Teacher.java @@ -0,0 +1,36 @@ +package org.launchcode.java.demos.lsn3classes1; + +public class Teacher { + + private String firstName; + private String lastName; + private String subject; + private int yearsTeaching; + + public Teacher(String firstName, String lastName, String subject, int yearsTeaching) { + this.firstName = firstName; + this.lastName = lastName; + this.subject = subject; + this.yearsTeaching = yearsTeaching; + } + + + public void setFirstName(String firstName) {this.firstName = firstName;} + + public void setLastName(String lastName) {this.lastName = lastName;} + + public void setSubject(String subject) {this.subject = subject;} + + public void setYearsTeaching(int yearsTeaching) {this.yearsTeaching = yearsTeaching;} + + public String getFirstName() {return firstName;} + + public String getLastName() { return lastName;} + + public String getSubject() { return subject;} + + public int getYearsTeaching() {return yearsTeaching;} + + + +} From a7da7fb67f465fb07ae04c2728d3333763dbb68f Mon Sep 17 00:00:00 2001 From: Nick Villone Date: Mon, 25 Jul 2022 20:58:41 -0400 Subject: [PATCH 08/12] studio Menu and MenuItem --- .../java/studios/RestaurantMenu/Menu.java | 23 ++++++++ .../java/studios/RestaurantMenu/MenuItem.java | 53 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/org/launchcode/java/studios/RestaurantMenu/Menu.java create mode 100644 src/org/launchcode/java/studios/RestaurantMenu/MenuItem.java diff --git a/src/org/launchcode/java/studios/RestaurantMenu/Menu.java b/src/org/launchcode/java/studios/RestaurantMenu/Menu.java new file mode 100644 index 000000000..837fc8444 --- /dev/null +++ b/src/org/launchcode/java/studios/RestaurantMenu/Menu.java @@ -0,0 +1,23 @@ +package org.launchcode.java.studios.RestaurantMenu; + +import java.time.Month; +import java.time.MonthDay; +import java.util.Date; + + +public class Menu { + + + public static void main(String[] args) { + + + + MenuItem fries = new MenuItem("fries", "appetizers", 4, false); + MenuItem burger = new MenuItem("burger", "main course", 5, true); + MenuItem cake = new MenuItem("cake", "dessert", 4, false); + + System.out.println("Name: " + fries.getName() + " Price: " + fries.getPrice() + " New Item: " + fries.getNewItem()); + + System.out.println("This menu was updated: " + new Date()); + } +} diff --git a/src/org/launchcode/java/studios/RestaurantMenu/MenuItem.java b/src/org/launchcode/java/studios/RestaurantMenu/MenuItem.java new file mode 100644 index 000000000..b6b729b5d --- /dev/null +++ b/src/org/launchcode/java/studios/RestaurantMenu/MenuItem.java @@ -0,0 +1,53 @@ +package org.launchcode.java.studios.RestaurantMenu; + +public class MenuItem { + + private String name; + + private String category; + + private Integer price; + + private Boolean newItem; + + + public MenuItem (String name, String category, int price, Boolean newItem){ + this.name = name; + this.category = category; + this.price = price; + this.newItem = newItem; + } + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public Boolean getNewItem() { + return newItem; + } + + public void setNewItem(Boolean newItem) { + this.newItem = newItem; + } + + public Integer getPrice() { + return price; + } + + public void setPrice(Integer price) { + this.price = price; + } +} From 7fe2cf913a485c24a822af5f974850b8a2ca806b Mon Sep 17 00:00:00 2001 From: Nick Villone Date: Sun, 31 Jul 2022 19:30:13 -0400 Subject: [PATCH 09/12] completed first two exercises --- .../java/demos/lsn4classes2/Course.java | 2 ++ .../java/demos/lsn4classes2/Student.java | 20 ++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/org/launchcode/java/demos/lsn4classes2/Course.java b/src/org/launchcode/java/demos/lsn4classes2/Course.java index cb04bc96a..1da62f376 100644 --- a/src/org/launchcode/java/demos/lsn4classes2/Course.java +++ b/src/org/launchcode/java/demos/lsn4classes2/Course.java @@ -1,6 +1,7 @@ package org.launchcode.java.demos.lsn4classes2; import java.util.ArrayList; +import java.util.Objects; public class Course { private String topic; @@ -15,3 +16,4 @@ public class Course { // Course objects equal. } + diff --git a/src/org/launchcode/java/demos/lsn4classes2/Student.java b/src/org/launchcode/java/demos/lsn4classes2/Student.java index b51eb666a..e550ccbb7 100644 --- a/src/org/launchcode/java/demos/lsn4classes2/Student.java +++ b/src/org/launchcode/java/demos/lsn4classes2/Student.java @@ -30,13 +30,27 @@ public String studentInfo() { //TODO: Uncomment and complete the getGradeLevel method here: -// public String getGradeLevel() { -// // Determine the grade level of the student based on numberOfCredits -// } + public static String getGradeLevel(int credits) { + // Determine the grade level of the student based on numberOfCredits + + if(credits <= 29) { + return "Freshman"; + } else if(credits <= 59) { + return "Sophomore"; + }else if(credits <= 89){ + return "Junior"; + } else { + return "Senior"; + } + } // TODO: Complete the addGrade method. public void addGrade(int courseCredits, double grade) { // Update the appropriate fields: numberOfCredits, gpa + double totalQualityScore = this.gpa * this.numberOfCredits; + totalQualityScore += courseCredits * grade; + this.numberOfCredits += courseCredits; + this.gpa = totalQualityScore / this.numberOfCredits; } // TODO: Add your custom 'toString' method here. Make sure it returns a well-formatted String rather From 72c44ada3e6f40b921c34e7ea244c853dd8ba57e Mon Sep 17 00:00:00 2001 From: Nick Villone Date: Sun, 31 Jul 2022 21:19:10 -0400 Subject: [PATCH 10/12] lesson 4 exercise 3 & 4 complete --- .../java/demos/lsn4classes2/Course.java | 23 +++++++++++++++++++ .../java/demos/lsn4classes2/Student.java | 22 ++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/org/launchcode/java/demos/lsn4classes2/Course.java b/src/org/launchcode/java/demos/lsn4classes2/Course.java index 1da62f376..bb36bfa68 100644 --- a/src/org/launchcode/java/demos/lsn4classes2/Course.java +++ b/src/org/launchcode/java/demos/lsn4classes2/Course.java @@ -11,9 +11,32 @@ public class Course { // TODO: Add your custom 'toString' method here. Make sure it returns a well-formatted String rather than // just the class fields. + public String toString() { + String courseReport = String.format("%s is taught by %T", this.topic, this.instructor); + return courseReport; + } // TODO: Add your custom 'equals' method here. Consider which fields should match in order to call two // Course objects equal. + public boolean equals(Object toBeCompared) { + if(toBeCompared == this) { + return true; + } + if(toBeCompared == null) { + return false; + } + + if(toBeCompared.getClass() != getClass()){ + return false; + } + + Course theCourse = (Course) toBeCompared; + return theCourse.getCoursetopic() == getCoursetopic(); + } + + public String getCoursetopic() { + return topic; + } } diff --git a/src/org/launchcode/java/demos/lsn4classes2/Student.java b/src/org/launchcode/java/demos/lsn4classes2/Student.java index e550ccbb7..b65b61ed0 100644 --- a/src/org/launchcode/java/demos/lsn4classes2/Student.java +++ b/src/org/launchcode/java/demos/lsn4classes2/Student.java @@ -55,10 +55,32 @@ public void addGrade(int courseCredits, double grade) { // TODO: Add your custom 'toString' method here. Make sure it returns a well-formatted String rather // than just the class fields. + public String toString() { + String studentReport = String.format("%s is %s with %d credits and a GPA of %.2f", this.name, this.getGradeLevel(this.numberOfCredits), this.getNumberOfCredits(), this.getGpa()); + return studentReport; + } + // TODO: Add your custom 'equals' method here. Consider which fields should match in order to call two // Student objects equal. + public boolean equals(Object toBeCompared) { + if(toBeCompared == this) { + return true; + } + + if(toBeCompared == null) { + return false; + } + + if(toBeCompared.getClass() != getClass()) { + return false; + } + + Student theStudent = (Student) toBeCompared; + return theStudent.getStudentId() == getStudentId(); + } + public String getName() { return name; } From 9d397f354556ac231e141e683a80e9e0f7026285 Mon Sep 17 00:00:00 2001 From: Nick Villone Date: Tue, 2 Aug 2022 17:00:35 -0400 Subject: [PATCH 11/12] Restaurant studio 2 --- .../java/studios/RestaurantMenu/Menu.java | 32 ++++++++++---- .../java/studios/RestaurantMenu/MenuItem.java | 42 ++++++++++++++++++- .../studios/RestaurantMenu/Restaurant.java | 23 ++++++++++ 3 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 src/org/launchcode/java/studios/RestaurantMenu/Restaurant.java diff --git a/src/org/launchcode/java/studios/RestaurantMenu/Menu.java b/src/org/launchcode/java/studios/RestaurantMenu/Menu.java index 837fc8444..1d793d4bc 100644 --- a/src/org/launchcode/java/studios/RestaurantMenu/Menu.java +++ b/src/org/launchcode/java/studios/RestaurantMenu/Menu.java @@ -1,23 +1,39 @@ package org.launchcode.java.studios.RestaurantMenu; -import java.time.Month; -import java.time.MonthDay; +import java.util.ArrayList; import java.util.Date; public class Menu { + public ArrayList getItems() { + return items; + } + + public void setItems(ArrayList items) { + this.items = items; + } + + private ArrayList items; + + private Date lastUpdated; - public static void main(String[] args) { - MenuItem fries = new MenuItem("fries", "appetizers", 4, false); - MenuItem burger = new MenuItem("burger", "main course", 5, true); - MenuItem cake = new MenuItem("cake", "dessert", 4, false); - System.out.println("Name: " + fries.getName() + " Price: " + fries.getPrice() + " New Item: " + fries.getNewItem()); + public Menu(ArrayList items) { + this.items = items; - System.out.println("This menu was updated: " + new Date()); } + + public ArrayList menuAddItem(MenuItem item) { + this.items.add(item); + return items; + } + + static Date getDate() { + return new Date(); + } + } diff --git a/src/org/launchcode/java/studios/RestaurantMenu/MenuItem.java b/src/org/launchcode/java/studios/RestaurantMenu/MenuItem.java index b6b729b5d..171096f1d 100644 --- a/src/org/launchcode/java/studios/RestaurantMenu/MenuItem.java +++ b/src/org/launchcode/java/studios/RestaurantMenu/MenuItem.java @@ -1,9 +1,17 @@ package org.launchcode.java.studios.RestaurantMenu; +import java.util.Objects; + public class MenuItem { + private int nextId = 1; + private String name; + private final int id; + + private String description; + private String category; private Integer price; @@ -11,14 +19,40 @@ public class MenuItem { private Boolean newItem; - public MenuItem (String name, String category, int price, Boolean newItem){ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof MenuItem)) return false; + MenuItem menuItem = (MenuItem) o; + return getId() == menuItem.getId(); + } + + @Override + public int hashCode() { + return Objects.hash(getId()); + } + + public MenuItem (String name, String description, String category, int price, Boolean newItem){ + this.id = nextId; this.name = name; + this.description = description; this.category = category; this.price = price; this.newItem = newItem; + nextId++; + } + + public MenuItem (String name, String description, String category, int price) { + this.id = nextId; + this.name = name; + this.description = description; + this.category = category; + this.price = price; + nextId++; } + public int getId() { return nextId;} public String getName() { return name; } @@ -27,6 +61,10 @@ public void setName(String name) { this.name = name; } + public String getDescription() {return description;} + + public void setDescription(String description) {this.description = description;} + public String getCategory() { return category; } @@ -51,3 +89,5 @@ public void setPrice(Integer price) { this.price = price; } } + + diff --git a/src/org/launchcode/java/studios/RestaurantMenu/Restaurant.java b/src/org/launchcode/java/studios/RestaurantMenu/Restaurant.java new file mode 100644 index 000000000..929646180 --- /dev/null +++ b/src/org/launchcode/java/studios/RestaurantMenu/Restaurant.java @@ -0,0 +1,23 @@ +package org.launchcode.java.studios.RestaurantMenu; +import java.util.ArrayList; +import java.util.Date; + +import static org.launchcode.java.studios.RestaurantMenu.Menu.getDate; + + +public class Restaurant { + + public static void main(String[] args) { + + ArrayList myBucket = new ArrayList<>(); + + MenuItem baconBrusselsSprouts = new MenuItem("Bacon-wrapped Brussels Sprouts", "They're wrapped in bacon", "Appetizer", 15); + MenuItem BLT = new MenuItem("BLT", "bacon, lettuce and tomato", "MainCourse", 10); + + myBucket.add(baconBrusselsSprouts); + myBucket.add(BLT); +// System.out.println("Name: " + fries.getName() + " Price: " + fries.getPrice() + " New Item: " + fries.getNewItem()); + + System.out.println("This menu was updated: " + getDate()); + } + } From 2e598f0f7e040bec4c2942391a16de1bce226dd9 Mon Sep 17 00:00:00 2001 From: Nick Villone Date: Tue, 2 Aug 2022 21:06:47 -0400 Subject: [PATCH 12/12] completed lesson 5 exercises --- .../java/demos/lsn5unittesting/main/Car.java | 9 +++ .../demos/lsn5unittesting/test/CarTest.java | 55 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/org/launchcode/java/demos/lsn5unittesting/main/Car.java b/src/org/launchcode/java/demos/lsn5unittesting/main/Car.java index 1fc03a6d6..4ddbb8e11 100644 --- a/src/org/launchcode/java/demos/lsn5unittesting/main/Car.java +++ b/src/org/launchcode/java/demos/lsn5unittesting/main/Car.java @@ -47,6 +47,9 @@ public double getGasTankLevel() { } public void setGasTankLevel(double gasTankLevel) { + if(gasTankLevel > this.getGasTankLevel()) { + throw new IllegalArgumentException("Can't exceed tank size"); + } this.gasTankLevel = gasTankLevel; } @@ -85,4 +88,10 @@ public void drive(double miles) this.odometer += milesAbleToTravel; } + public void addGas(double gas) { + this.setGasTankLevel(gas + this.getGasTankLevel()); + } + + + } diff --git a/src/org/launchcode/java/demos/lsn5unittesting/test/CarTest.java b/src/org/launchcode/java/demos/lsn5unittesting/test/CarTest.java index 7cde639dc..6fd3d4754 100644 --- a/src/org/launchcode/java/demos/lsn5unittesting/test/CarTest.java +++ b/src/org/launchcode/java/demos/lsn5unittesting/test/CarTest.java @@ -1,11 +1,66 @@ package org.launchcode.java.demos.lsn5unittesting.test; +import org.junit.Before; +import org.junit.Test; +import org.launchcode.java.demos.lsn5unittesting.main.Car; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + public class CarTest { + Car test_car; + + @Before + public void createCarObject() { + test_car = new Car("Toyota", "Prius", 10, 50); + } + //TODO: add emptyTest so we can configure our runtime environment (remove this test before pushing to your personal GitLab account) + @Test + public void emptyTest() { + assertEquals(10,10,.001); + } + //TODO: constructor sets gasTankLevel properly + @Test + public void testInitialGasTank() { + + assertEquals(10, test_car.getGasTankLevel(), .001); + } + + //use assertFalse to test getGasTankSize() + @Test + public void testInitialGasTank2() { + assertFalse(test_car.getGasTankSize() == 0); + } + + //use assertTrue to test getGasTankSize() + @Test + public void testInitialGasTank3() { + assertTrue(test_car.getGasTankSize() > 0); + } + + //TODO: gasTankLevel is accurate after driving within tank range + @Test + public void testGasTankAfterDriving() { + test_car.drive(50); + assertEquals(9, test_car.getGasTankLevel(),.001); + } //TODO: gasTankLevel is accurate after attempting to drive past tank range + @Test + public void testGasTankAfterExceedingTankRange(){ + test_car.drive(600); + assertEquals(test_car.getGasTankLevel(), 0, .001); + + } //TODO: can't have more gas than tank size, expect an exception + @Test(expected = IllegalArgumentException.class) + public void testGasOverfillException() { + test_car.addGas(5); + fail("Shouldn't get here, car cannot have more gas in tank than the size of the tank"); + } }