diff --git a/src/exercises/class2/Alice.java b/src/exercises/class2/Alice.java new file mode 100644 index 000000000..7fbee7ab0 --- /dev/null +++ b/src/exercises/class2/Alice.java @@ -0,0 +1,46 @@ +package exercises.class2; + + +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/class2/HelloWorld.java b/src/exercises/class2/HelloWorld.java new file mode 100644 index 000000000..6042ed1fd --- /dev/null +++ b/src/exercises/class2/HelloWorld.java @@ -0,0 +1,13 @@ +package exercises.class2; +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/class3/ControlFlowAndCollections.java b/src/exercises/class3/ControlFlowAndCollections.java new file mode 100644 index 000000000..8c03236fd --- /dev/null +++ b/src/exercises/class3/ControlFlowAndCollections.java @@ -0,0 +1,109 @@ +package exercises.class3; + +import java.util.*; + +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(" "))); + studentLogHashMap(); + } + + 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); + } + } + } + + // 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()); + + } +} 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); } } 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; + + + +} diff --git a/src/org/launchcode/java/demos/lsn4classes2/Student.java b/src/org/launchcode/java/demos/lsn4classes2/Student.java index b51eb666a..ee5ef4cc7 100644 --- a/src/org/launchcode/java/demos/lsn4classes2/Student.java +++ b/src/org/launchcode/java/demos/lsn4classes2/Student.java @@ -29,21 +29,46 @@ 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; 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"); + } } 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..164cc0a2a --- /dev/null +++ b/src/org/launchcode/java/studios/areaofacircle/Area.java @@ -0,0 +1,61 @@ +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; + +// 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 { +// 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; + } +} 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