From 0c4f68d97c73586a486320cbc99bcc0c9a4cf3eb Mon Sep 17 00:00:00 2001 From: Linxi Xu Date: Mon, 20 Sep 2021 20:37:04 -0400 Subject: [PATCH 01/10] Finished studio and bonus --- .../java/studios/areaofacircle/Area.java | 58 +++++++++++++++++++ .../java/studios/areaofacircle/Circle.java | 7 +++ 2 files changed, 65 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..81903aa7f --- /dev/null +++ b/src/org/launchcode/java/studios/areaofacircle/Area.java @@ -0,0 +1,58 @@ +package org.launchcode.java.studios.areaofacircle; + +import java.util.InputMismatchException; +import java.util.Scanner; +import java.lang.Boolean; +import java.lang.Math; + +public class Area { + public static void main(String[] args) throws InputMismatchException { +// Notes: +// Scanner scanny = new Scanner(System.in); +// System.out.print("What's your favorite number? "); +// int favoriteNumber = scanny.nextInt(); +// if(favoriteNumber % 2 == 1) { +// System.out.println("Odd number!"); +// } else { +// System.out.println("Even number!"); +// } +// System.out.println("Hello World!"); + Scanner scanny = new Scanner(System.in); + System.out.print("What's your radius: "); + double radius = scanny.nextDouble(); + + while (radius < 0) { + System.out.print("What's your radius: "); + radius = scanny.nextDouble(); + } + +// boolean cont = true; +//// +// do { +// try { +// cont = false; +// } catch (InputMismatchException e) { +// cont = true; +// System.out.print("What's your radius: "); +// radius = scanny.nextDouble(); +// } +// }while (cont); + + +// try { +// while (radius < 0) { +// System.out.print("What's your radius: "); +// radius = scanny.nextDouble(); +// } +// } catch (InputMismatchException e) { +// System.out.print("What's your radius: "); +// radius = scanny.nextDouble(); +// } + + +// double area = Math.PI * Math.pow(radius, radius); +// System.out.println(area); + double area = Circle.getArea(radius); + System.out.println(area); + } +} 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..36bc90035 --- /dev/null +++ b/src/org/launchcode/java/studios/areaofacircle/Circle.java @@ -0,0 +1,7 @@ +package org.launchcode.java.studios.areaofacircle; + +public class Circle { + public static Double getArea(Double radius) { + return 3.14 * radius * radius; + } +} From a380f2e8ef782b5db9f33ff530ecb609a4ec5f32 Mon Sep 17 00:00:00 2001 From: Linxi Xu Date: Tue, 21 Sep 2021 00:25:20 -0400 Subject: [PATCH 02/10] Class 1 Exercises --- src/exercises/Alice.java | 48 +++++++++++++++++++ src/exercises/HelloWorld.java | 13 +++++ .../demos/lsn1datatypes/HelloMethods.java | 2 + 3 files changed, 63 insertions(+) create mode 100644 src/exercises/Alice.java create mode 100644 src/exercises/HelloWorld.java diff --git a/src/exercises/Alice.java b/src/exercises/Alice.java new file mode 100644 index 000000000..3335ee0d9 --- /dev/null +++ b/src/exercises/Alice.java @@ -0,0 +1,48 @@ +package exercises; + +import java.util.Locale; +import java.util.Scanner; + +public class Alice { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.println("Enter the length of the rectange: "); + double length = input.nextDouble(); + + System.out.println("Enter the length of the rectange: "); + double width = input.nextDouble(); + + System.out.println("The area is: " + length * width); + + System.out.println("How many miles have you driven?"); + Double numMiles = input.nextDouble(); + + System.out.println("How much gas did you use? In gallons."); + double numGallons = input.nextDouble(); + + double mpg = numMiles / numGallons; + System.out.println("You are running on " + mpg + " mpg."); + + String aliceText = "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?'"; + + System.out.println(aliceText.toLowerCase(Locale.ROOT)); + System.out.println(aliceText); + + System.out.println(aliceText.toLowerCase(Locale.ROOT).contains("alice".toLowerCase(Locale.ROOT))); + + System.out.println("Enter a word that contained in the aliceText: "); + String aliceWord = input.nextLine(); + + int wordIndex = aliceText.indexOf(aliceWord); + int wordLength = aliceWord.length(); + System.out.println(wordIndex); + System.out.println(wordLength); + + String aliceTextmodified = aliceText.replace(aliceWord, ""); + System.out.println(aliceTextmodified); + + + + + } +} diff --git a/src/exercises/HelloWorld.java b/src/exercises/HelloWorld.java new file mode 100644 index 000000000..0cad1bd44 --- /dev/null +++ b/src/exercises/HelloWorld.java @@ -0,0 +1,13 @@ +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/org/launchcode/java/demos/lsn1datatypes/HelloMethods.java b/src/org/launchcode/java/demos/lsn1datatypes/HelloMethods.java index a375d208a..9cd64b260 100644 --- a/src/org/launchcode/java/demos/lsn1datatypes/HelloMethods.java +++ b/src/org/launchcode/java/demos/lsn1datatypes/HelloMethods.java @@ -5,6 +5,8 @@ public class HelloMethods { public static void main(String[] args) { String message = Message.getMessage("fr"); System.out.println(message); + String message_Eng = Message.getMessage("en"); + System.out.println(message_Eng); } } From ebbbcff6615519e37cd4710adf6a06d0dcc2ac9a Mon Sep 17 00:00:00 2001 From: Linxi Xu Date: Wed, 22 Sep 2021 15:56:42 -0400 Subject: [PATCH 03/10] Class 1 Exercises --- src/exercises/{ => 2-4}/Alice.java | 9 +++------ src/exercises/{ => 2-4}/HelloWorld.java | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) rename src/exercises/{ => 2-4}/Alice.java (92%) rename src/exercises/{ => 2-4}/HelloWorld.java (92%) diff --git a/src/exercises/Alice.java b/src/exercises/2-4/Alice.java similarity index 92% rename from src/exercises/Alice.java rename to src/exercises/2-4/Alice.java index 3335ee0d9..32631021c 100644 --- a/src/exercises/Alice.java +++ b/src/exercises/2-4/Alice.java @@ -1,4 +1,4 @@ -package exercises; +package exercises.2-4; import java.util.Locale; import java.util.Scanner; @@ -14,8 +14,8 @@ public static void main(String[] args) { System.out.println("The area is: " + length * width); - System.out.println("How many miles have you driven?"); - Double numMiles = input.nextDouble(); + Systesm.out.println("How many miles have you driven?"); + double numMiles = input.nextDouble(); System.out.println("How much gas did you use? In gallons."); double numGallons = input.nextDouble(); @@ -41,8 +41,5 @@ public static void main(String[] args) { String aliceTextmodified = aliceText.replace(aliceWord, ""); System.out.println(aliceTextmodified); - - - } } diff --git a/src/exercises/HelloWorld.java b/src/exercises/2-4/HelloWorld.java similarity index 92% rename from src/exercises/HelloWorld.java rename to src/exercises/2-4/HelloWorld.java index 0cad1bd44..78be84a98 100644 --- a/src/exercises/HelloWorld.java +++ b/src/exercises/2-4/HelloWorld.java @@ -1,4 +1,4 @@ -package exercises; +package exercises.2-4; import java.util.Scanner; public class HelloWorld { From e2c8f78e12233015cdd2f414dbdb207330d9167e Mon Sep 17 00:00:00 2001 From: Linxi Xu Date: Wed, 22 Sep 2021 16:00:11 -0400 Subject: [PATCH 04/10] Class 1 Exercises --- src/exercises/{2-4 => class2}/Alice.java | 5 +++-- src/exercises/{2-4 => class2}/HelloWorld.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) rename src/exercises/{2-4 => class2}/Alice.java (94%) rename src/exercises/{2-4 => class2}/HelloWorld.java (91%) diff --git a/src/exercises/2-4/Alice.java b/src/exercises/class2/Alice.java similarity index 94% rename from src/exercises/2-4/Alice.java rename to src/exercises/class2/Alice.java index 32631021c..7fbee7ab0 100644 --- a/src/exercises/2-4/Alice.java +++ b/src/exercises/class2/Alice.java @@ -1,4 +1,5 @@ -package exercises.2-4; +package exercises.class2; + import java.util.Locale; import java.util.Scanner; @@ -14,7 +15,7 @@ public static void main(String[] args) { System.out.println("The area is: " + length * width); - Systesm.out.println("How many miles have you driven?"); + System.out.println("How many miles have you driven?"); double numMiles = input.nextDouble(); System.out.println("How much gas did you use? In gallons."); diff --git a/src/exercises/2-4/HelloWorld.java b/src/exercises/class2/HelloWorld.java similarity index 91% rename from src/exercises/2-4/HelloWorld.java rename to src/exercises/class2/HelloWorld.java index 78be84a98..6042ed1fd 100644 --- a/src/exercises/2-4/HelloWorld.java +++ b/src/exercises/class2/HelloWorld.java @@ -1,4 +1,4 @@ -package exercises.2-4; +package exercises.class2; import java.util.Scanner; public class HelloWorld { From 25ce1e8c51feee843167c6bcc70f46144948afa4 Mon Sep 17 00:00:00 2001 From: Linxi Xu Date: Thu, 23 Sep 2021 20:28:35 -0400 Subject: [PATCH 05/10] Studio 3.8 --- .../class3/ControlFlowAndCollections.java | 75 ++++++++ .../java/studios/areaofacircle/Area.java | 39 +++-- .../countingcharacters/CharacterCount.java | 162 ++++++++++++++++++ .../studios/countingcharacters/phrase.txt | 1 + 4 files changed, 259 insertions(+), 18 deletions(-) create mode 100644 src/exercises/class3/ControlFlowAndCollections.java create mode 100644 src/org/launchcode/java/studios/countingcharacters/CharacterCount.java create mode 100644 src/org/launchcode/java/studios/countingcharacters/phrase.txt diff --git a/src/exercises/class3/ControlFlowAndCollections.java b/src/exercises/class3/ControlFlowAndCollections.java new file mode 100644 index 000000000..0b7fee7d3 --- /dev/null +++ b/src/exercises/class3/ControlFlowAndCollections.java @@ -0,0 +1,75 @@ +package exercises.class3; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ControlFlowAndCollections { + public static void main(String[] args) { + + // 3.7.1. Array Practice + + int [] intArr = {1, 1, 2, 3, 5, 8}; + + for (int n : intArr) { + if (n % 2 == 1) { + System.out.println(n); + } + } + + for (int i = 0; i < intArr.length; i++) { + if (intArr[i] % 2 == 1) { + System.out.println(intArr[i]); + } + } + + String s1 = "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[] s2 = s1.split(" "); + System.out.println(s2); + for (String s: s2) { + System.out.println(s); + } + System.out.println(Arrays.toString(s2)); + + String[] s4 = s1.split("//."); + System.out.println(s4); + for(String sentence : s4) { + System.out.println(sentence); + } + System.out.println(Arrays.toString(s4)); + +// 3.7.2. ArrayList Practice + ArrayList arrlist = new ArrayList<>(Arrays.asList(1, 2)); + arrlist.add(3); + arrlist.add(4); + arrlist.add(5); + arrlist.add(6); + arrlist.add(7); + arrlist.add(8); + arrlist.add(9); + arrlist.add(10); + + System.out.println(calculateEvenSum((arrlist))); +// System.out.println(printFiveLetterWord(s1.split(" "))); + } + + public static int calculateEvenSum( ArrayList arr) { + int sum = 0; + + for (int num : arr) { + if ( num % 2 == 1) { + sum += num; + } + } + return sum; + } + + public static void printFiveLetterWord( List strList) { + for (String word: strList) { + if (word.length() == 5 ) { + System.out.println(word); + } + } + } +} diff --git a/src/org/launchcode/java/studios/areaofacircle/Area.java b/src/org/launchcode/java/studios/areaofacircle/Area.java index 81903aa7f..164cc0a2a 100644 --- a/src/org/launchcode/java/studios/areaofacircle/Area.java +++ b/src/org/launchcode/java/studios/areaofacircle/Area.java @@ -19,24 +19,27 @@ public static void main(String[] args) throws InputMismatchException { // System.out.println("Hello World!"); Scanner scanny = new Scanner(System.in); System.out.print("What's your radius: "); - double radius = scanny.nextDouble(); - - while (radius < 0) { - System.out.print("What's your radius: "); - radius = scanny.nextDouble(); - } - -// boolean cont = true; -//// -// do { -// try { -// cont = false; -// } catch (InputMismatchException e) { -// cont = true; -// System.out.print("What's your radius: "); -// radius = scanny.nextDouble(); -// } -// }while (cont); + double radius; + +// while (radius < 0) { +// System.out.print("What's your radius: "); +// radius = scanny.nextDouble(); +// } + + +// + do { + try { + radius = scanny.nextDouble(); + } catch (InputMismatchException e) { + radius = -1; + System.out.print("What's your radius: "); + scanny.next(); + } + if (radius <= 0) { + System.out.println("Invalid input. Please enter a positive number."); + } + }while (radius <= 0); // try { diff --git a/src/org/launchcode/java/studios/countingcharacters/CharacterCount.java b/src/org/launchcode/java/studios/countingcharacters/CharacterCount.java new file mode 100644 index 000000000..234188154 --- /dev/null +++ b/src/org/launchcode/java/studios/countingcharacters/CharacterCount.java @@ -0,0 +1,162 @@ +package org.launchcode.java.studios.countingcharacters; + +import java.io.File; +import java.io.FileNotFoundException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + +public class CharacterCount { + public static void main(String[] args) throws FileNotFoundException { + CharacterCount(); + CharacterCountFile(); + } + + public static void CharacterCount() { +// String phrase = "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."; + Scanner input = new Scanner(System.in); + System.out.println("Put in your desire phrase: "); + String phrase = input.nextLine(); + + + char[] charactersInString = phrase.toCharArray(); + + HashMap characterCountCaseSensitive = new HashMap<>(); + HashMap characterCountCaseInSensitive = new HashMap<>(); + HashMap characterCountCaseInSensitiveExclusive = new HashMap<>(); + + // case sensitive + for (char c: charactersInString) { + if (characterCountCaseSensitive.containsKey(c)) { + characterCountCaseSensitive.put(c,characterCountCaseSensitive.get(c) + 1); + } else { + characterCountCaseSensitive.put(c, 1); + } + } + + System.out.println("Case sensitive result:"); + for (Map.Entry c : characterCountCaseSensitive.entrySet()) { + System.out.println(c.getKey() + ": " + c.getValue()); + } + +// char f = Character.toLowerCase('L'); + + // case in-sensitive + for (char c : charactersInString) { + c = Character.toLowerCase(c); + if(characterCountCaseInSensitive.containsKey(c)) { + characterCountCaseInSensitive.put(c, characterCountCaseInSensitive.get(c) + 1); + } else { + characterCountCaseInSensitive.put(c, 1); + } + } + + System.out.println("Case in-sensitive result:"); + for (Map.Entry ci : characterCountCaseInSensitive.entrySet()) { + System.out.println(ci.getKey() + ": " + ci.getValue()); + } + +// Exclude non-alphabetic characters. + + for (char c : charactersInString) { + c = Character.toLowerCase(c); + if (Character.isLetter(c)) { + if (characterCountCaseInSensitiveExclusive.containsKey(c)) { + characterCountCaseInSensitiveExclusive.put(c, characterCountCaseInSensitiveExclusive.get(c) + 1); + } else { + characterCountCaseInSensitiveExclusive.put(c, 1); + } + } + } + + System.out.println("Case in-sensitive result (Exclude non-alphabetic characters.):"); + for (Map.Entry ci : characterCountCaseInSensitiveExclusive.entrySet()) { + System.out.println(ci.getKey() + ": " + ci.getValue()); + } + +// System.out.println(' ' + ": " + Character.isLetter((' '))); + } + + + public static void CharacterCountFile() throws FileNotFoundException { +// String phrase = "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."; + + + // without FileNotFoundException, we could also use try/catch block + + Scanner inputFilePath = new Scanner(System.in); + System.out.println("Put in your file path: "); + String fileName = inputFilePath.nextLine(); + + Scanner inputFileName = new Scanner(new File(fileName)); + String phrase = inputFileName.nextLine(); + + +// String fileName = fileHandler.nextLine(); +//// Path filePath = Path.of(fileName); +// Path filePath = Paths.get(fileName); +// String phrase = Files.readString(filePath); + + + char[] charactersInString = phrase.toCharArray(); + + HashMap characterCountCaseSensitive = new HashMap<>(); + HashMap characterCountCaseInSensitive = new HashMap<>(); + HashMap characterCountCaseInSensitiveExclusive = new HashMap<>(); + + // case sensitive + for (char c: charactersInString) { + if (characterCountCaseSensitive.containsKey(c)) { + characterCountCaseSensitive.put(c,characterCountCaseSensitive.get(c) + 1); + } else { + characterCountCaseSensitive.put(c, 1); + } + } + + System.out.println("Case sensitive result:"); + for (Map.Entry c : characterCountCaseSensitive.entrySet()) { + System.out.println(c.getKey() + ": " + c.getValue()); + } + +// char f = Character.toLowerCase('L'); + + // case in-sensitive + for (char c : charactersInString) { + c = Character.toLowerCase(c); + if(characterCountCaseInSensitive.containsKey(c)) { + characterCountCaseInSensitive.put(c, characterCountCaseInSensitive.get(c) + 1); + } else { + characterCountCaseInSensitive.put(c, 1); + } + } + + System.out.println("Case in-sensitive result:"); + for (Map.Entry ci : characterCountCaseInSensitive.entrySet()) { + System.out.println(ci.getKey() + ": " + ci.getValue()); + } + +// Exclude non-alphabetic characters. + + for (char c : charactersInString) { + c = Character.toLowerCase(c); + if (Character.isLetter(c)) { + if (characterCountCaseInSensitiveExclusive.containsKey(c)) { + characterCountCaseInSensitiveExclusive.put(c, characterCountCaseInSensitiveExclusive.get(c) + 1); + } else { + characterCountCaseInSensitiveExclusive.put(c, 1); + } + } + } + + System.out.println("Case in-sensitive result (Exclude non-alphabetic characters.):"); + for (Map.Entry ci : characterCountCaseInSensitiveExclusive.entrySet()) { + System.out.println(ci.getKey() + ": " + ci.getValue()); + } + +// System.out.println(' ' + ": " + Character.isLetter((' '))); + } + +} diff --git a/src/org/launchcode/java/studios/countingcharacters/phrase.txt b/src/org/launchcode/java/studios/countingcharacters/phrase.txt new file mode 100644 index 000000000..7b6fe6993 --- /dev/null +++ b/src/org/launchcode/java/studios/countingcharacters/phrase.txt @@ -0,0 +1 @@ +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. \ No newline at end of file From f58418988eba6569adff5fb90ba663ea2d125af1 Mon Sep 17 00:00:00 2001 From: Linxi Xu Date: Thu, 23 Sep 2021 20:45:13 -0400 Subject: [PATCH 06/10] Class 2 exercise --- .../class3/ControlFlowAndCollections.java | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/exercises/class3/ControlFlowAndCollections.java b/src/exercises/class3/ControlFlowAndCollections.java index 0b7fee7d3..8c03236fd 100644 --- a/src/exercises/class3/ControlFlowAndCollections.java +++ b/src/exercises/class3/ControlFlowAndCollections.java @@ -1,8 +1,6 @@ package exercises.class3; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; public class ControlFlowAndCollections { public static void main(String[] args) { @@ -52,6 +50,7 @@ public static void main(String[] args) { System.out.println(calculateEvenSum((arrlist))); // System.out.println(printFiveLetterWord(s1.split(" "))); + studentLogHashMap(); } public static int calculateEvenSum( ArrayList arr) { @@ -72,4 +71,39 @@ public static void printFiveLetterWord( List strList) { } } } + + // 3.7.3 HashMap + public static void studentLogHashMap() { + HashMap studentLog = new HashMap<>(); + Scanner input = new Scanner(System.in); + String newStudent; + Integer studentID; + + System.out.println("Enter your students (or ENTER to finish):"); + + // Get student names and ID numbers + do { + + System.out.println("Student: "); + newStudent = input.nextLine(); + + if (!newStudent.equals((""))) { + System.out.println("ID: "); + studentID = input.nextInt(); + studentLog.put(studentID, newStudent); + + input.nextLine(); + } + } while(!newStudent.equals("")); + + input.close(); + System.out.println("\nClass roster:"); + + for (Map.Entry student: studentLog.entrySet()) { + System.out.println(student.getValue() + "'s ID: " + student.getKey()); + } + + System.out.println("Number of students in the roster: " + studentLog.size()); + + } } From 188652659ff2a3cf1d409503b72e9de7d26b52ce Mon Sep 17 00:00:00 2001 From: Linxi Xu Date: Mon, 4 Oct 2021 23:26:27 -0400 Subject: [PATCH 07/10] finished class 3 exercises --- .../java/demos/lsn3classes1/Course.java | 34 ++++++++++++++++ .../java/demos/lsn3classes1/Student.java | 39 +++++++++++++++++-- .../java/demos/lsn3classes1/Teacher.java | 11 ++++++ 3 files changed, 81 insertions(+), 3 deletions(-) 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..add2e41f1 --- /dev/null +++ b/src/org/launchcode/java/demos/lsn3classes1/Course.java @@ -0,0 +1,34 @@ +package org.launchcode.java.demos.lsn3classes1; + +import java.util.ArrayList; +import java.util.HashMap; + +public class Course { + private String courseName; + private ArrayList studentNames; + private HashMap studentIDs; + + public String getCourseName() { + return courseName; + } + + public ArrayList getStudentNames(){ + return studentNames; + } + + public HashMap getStudentIDs() { + return studentIDs; + } + + public void setCourseName(String aCourseName) { + this.courseName = aCourseName; + } + + public void setStudentNames(ArrayList aStudentNames) { + this.studentNames = aStudentNames; + } + + public void setStudentIDs(HashMap aStudentIDs) { + this.studentIDs = aStudentIDs; + } +} diff --git a/src/org/launchcode/java/demos/lsn3classes1/Student.java b/src/org/launchcode/java/demos/lsn3classes1/Student.java index 73f21c5fc..a8c14afd9 100644 --- a/src/org/launchcode/java/demos/lsn3classes1/Student.java +++ b/src/org/launchcode/java/demos/lsn3classes1/Student.java @@ -5,9 +5,42 @@ public class Student { - private String name; + private String name = "Lindsey"; private int studentId; - private int numberOfCredits = 0; - private double gpa = 0.0; + private int numberOfCredits = 1; + private double gpa = 4.0; + + public String getName() { + return name; + } + + public void setName(String aName) { + this.name = aName; + } + + + public int getStudentId() { + return studentId; + } + + public void setStudentId(int aStudentID) { + this.studentId = aStudentID; + } + + public int getNumberOfCredits() { + return numberOfCredits; + } + + public void setNumberOfCredits(int aNumberCredits) { + this.numberOfCredits = aNumberCredits; + } + + public double getGpa() { + return gpa; + } + + public void setGpa(double aGpa) { + this.gpa = aGpa; + } } \ 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..d33ae4aa3 --- /dev/null +++ b/src/org/launchcode/java/demos/lsn3classes1/Teacher.java @@ -0,0 +1,11 @@ +package org.launchcode.java.demos.lsn3classes1; + +public class Teacher { + private String firstName; + private String lastName; + private String subject; + private Integer yearsTeaching; + + + +} From f6edb3b42be4b6fa48de67d46fb6ef72caa697e8 Mon Sep 17 00:00:00 2001 From: Linxi Xu Date: Mon, 11 Oct 2021 14:19:30 -0400 Subject: [PATCH 08/10] Class 3 exercise --- .../java/demos/lsn4classes2/Student.java | 46 +++++++++++++++---- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/org/launchcode/java/demos/lsn4classes2/Student.java b/src/org/launchcode/java/demos/lsn4classes2/Student.java index b51eb666a..52e1ae3c3 100644 --- a/src/org/launchcode/java/demos/lsn4classes2/Student.java +++ b/src/org/launchcode/java/demos/lsn4classes2/Student.java @@ -29,21 +29,47 @@ 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 String getGradeLevel(int credits) { + String gradeLevel; + if (credits <= 29) { + gradeLevel = "Freshman"; + } else if (credits <= 59) { + gradeLevel = "Sophomore"; + } else if (credits <= 89) { + gradeLevel = "Junior"; + } else{ + gradeLevel = "Senior"; + } + return gradeLevel; + } - // 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 - // than just the class fields. + public String toString() { + String studentReport = String.format("%s is a %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 c8d6fc02b6c7bd1071d28ae545008d497844f244 Mon Sep 17 00:00:00 2001 From: Linxi Xu Date: Mon, 11 Oct 2021 15:23:32 -0400 Subject: [PATCH 09/10] Class 3 exercise --- src/org/launchcode/java/demos/lsn4classes2/Student.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/org/launchcode/java/demos/lsn4classes2/Student.java b/src/org/launchcode/java/demos/lsn4classes2/Student.java index 52e1ae3c3..ee5ef4cc7 100644 --- a/src/org/launchcode/java/demos/lsn4classes2/Student.java +++ b/src/org/launchcode/java/demos/lsn4classes2/Student.java @@ -48,7 +48,6 @@ public void addGrade(int courseCredits, double grade) { totalQualityScore += courseCredits * grade; this.numberOfCredits += courseCredits; this.gpa = totalQualityScore/this.numberOfCredits; - } public String toString() { From f74df14daa5887e23c79bd7c802d611433c56301 Mon Sep 17 00:00:00 2001 From: Linxi Xu Date: Mon, 11 Oct 2021 15:57:06 -0400 Subject: [PATCH 10/10] Class 4 exercise --- .../java/demos/lsn5unittesting/main/Car.java | 7 +++ .../demos/lsn5unittesting/test/CarTest.java | 44 ++++++++++++++++--- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/org/launchcode/java/demos/lsn5unittesting/main/Car.java b/src/org/launchcode/java/demos/lsn5unittesting/main/Car.java index 1fc03a6d6..52028a41d 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.getGasTankSize()) { + throw new IllegalArgumentException("Can't exceed tank size"); + } this.gasTankLevel = gasTankLevel; } @@ -85,4 +88,8 @@ 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..eef29be5a 100644 --- a/src/org/launchcode/java/demos/lsn5unittesting/test/CarTest.java +++ b/src/org/launchcode/java/demos/lsn5unittesting/test/CarTest.java @@ -1,11 +1,45 @@ package org.launchcode.java.demos.lsn5unittesting.test; +import org.junit.Test; +import org.launchcode.java.demos.lsn5unittesting.main.Car; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; +import org.junit.Test; + public class CarTest { + Car test_car = new Car("Toyota", "Prius", 10, 50); + // 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, 0.001); + } + // constructor sets gasTankLevel properly + @Test public void testInitialGasTank() { + + assertEquals(10, test_car.getGasTankLevel(), .001); + } + + // gasTankLevel is accurate after driving within tank range + @Test + public void testGasTankAfterDriving() { + test_car.drive(50); + assertEquals(9, test_car.getGasTankLevel(), .001); + + } + + // gasTankLevel is accurate after attempting to drive past tank range + @Test + public void testGasTankAfterExceedingTankRange() { + test_car.drive(501); + assertEquals(test_car.getGasTankLevel(), 0, .001); + } - //TODO: add emptyTest so we can configure our runtime environment (remove this test before pushing to your personal GitLab account) - //TODO: constructor sets gasTankLevel properly - //TODO: gasTankLevel is accurate after driving within tank range - //TODO: gasTankLevel is accurate after attempting to drive past tank range - //TODO: can't have more gas than tank size, expect an exception + // 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 there, car cannot have more gas in tank than the size of the tank"); + } }