diff --git a/src/exercises/AlicesWonderland.java b/src/exercises/AlicesWonderland.java new file mode 100644 index 000000000..946032b17 --- /dev/null +++ b/src/exercises/AlicesWonderland.java @@ -0,0 +1,34 @@ +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(); + input.close(); + + 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/Array.java b/src/exercises/Array.java new file mode 100644 index 000000000..6339169b6 --- /dev/null +++ b/src/exercises/Array.java @@ -0,0 +1,53 @@ +package exercises; + +import java.util.*; + +public class Array { + public static void main(String[] args) { + int[] intArray = {1, 1, 2, 3, 5, 8}; + + 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[] words = phrase.split(" "); + System.out.println(Arrays.toString(words)); + + String[] sentences = phrase.split("\\."); + System.out.println(Arrays.toString(sentences)); + + } + + + 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 new file mode 100644 index 000000000..b5e082f46 --- /dev/null +++ b/src/exercises/GasUsed.java @@ -0,0 +1,24 @@ +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(); + input2.close(); + + double gallonsPerMile = miles / gallons; + System.out.println("You are running on " + gallonsPerMile + " gpm."); + + + } + +} 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()); + } +} diff --git a/src/exercises/HelloWorld.java b/src/exercises/HelloWorld.java new file mode 100644 index 000000000..cdb4bd105 --- /dev/null +++ b/src/exercises/HelloWorld.java @@ -0,0 +1,17 @@ +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); + input.close(); + + } + +} diff --git a/src/exercises/NumericTypes.java b/src/exercises/NumericTypes.java new file mode 100644 index 000000000..5701ed05f --- /dev/null +++ b/src/exercises/NumericTypes.java @@ -0,0 +1,24 @@ +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); + + input.close(); + input2.close(); + + } + +} 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"); } } 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..8b728f361 100644 --- a/src/org/launchcode/java/demos/lsn3classes1/Student.java +++ b/src/org/launchcode/java/demos/lsn3classes1/Student.java @@ -8,6 +8,29 @@ public class Student { private String name; private int studentId; private int numberOfCredits = 0; - private double gpa = 0.0; + private double gpa = 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;} + + + +} diff --git a/src/org/launchcode/java/demos/lsn4classes2/Course.java b/src/org/launchcode/java/demos/lsn4classes2/Course.java index cb04bc96a..bb36bfa68 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; @@ -10,8 +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 b51eb666a..af3ebf3b1 100644 --- a/src/org/launchcode/java/demos/lsn4classes2/Student.java +++ b/src/org/launchcode/java/demos/lsn4classes2/Student.java @@ -30,21 +30,59 @@ 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 // 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; } diff --git a/src/org/launchcode/java/demos/lsn5unittesting/main/Car.java b/src/org/launchcode/java/demos/lsn5unittesting/main/Car.java index 1fc03a6d6..6ceaef467 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,7 @@ 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..59d19a08d 100644 --- a/src/org/launchcode/java/demos/lsn5unittesting/test/CarTest.java +++ b/src/org/launchcode/java/demos/lsn5unittesting/test/CarTest.java @@ -1,11 +1,63 @@ package org.launchcode.java.demos.lsn5unittesting.test; +import org.launchcode.java.demos.lsn5unittesting.main.Car; +import org.junit.Test; +import org.junit.Before; +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); + } + + @Test + public void testInitialGasTank2(){ + assertFalse(test_car.getGasTankSize() == 0); + } + + @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 - //TODO: can't have more gas than tank size, expect an exception + @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"); + } } 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; + } +} 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; + } + + +} 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()); + } + + + } + + + } +