Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/exercises/class2/Alice.java
Original file line number Diff line number Diff line change
@@ -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);

}
}
13 changes: 13 additions & 0 deletions src/exercises/class2/HelloWorld.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
109 changes: 109 additions & 0 deletions src/exercises/class3/ControlFlowAndCollections.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<Integer> arr) {
int sum = 0;

for (int num : arr) {
if ( num % 2 == 1) {
sum += num;
}
}
return sum;
}

public static void printFiveLetterWord( List<String> strList) {
for (String word: strList) {
if (word.length() == 5 ) {
System.out.println(word);
}
}
}

// 3.7.3 HashMap
public static void studentLogHashMap() {
HashMap<Integer, String> 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<Integer, String> student: studentLog.entrySet()) {
System.out.println(student.getValue() + "'s ID: " + student.getKey());
}

System.out.println("Number of students in the roster: " + studentLog.size());

}
}
2 changes: 2 additions & 0 deletions src/org/launchcode/java/demos/lsn1datatypes/HelloMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
34 changes: 34 additions & 0 deletions src/org/launchcode/java/demos/lsn3classes1/Course.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 36 additions & 3 deletions src/org/launchcode/java/demos/lsn3classes1/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
11 changes: 11 additions & 0 deletions src/org/launchcode/java/demos/lsn3classes1/Teacher.java
Original file line number Diff line number Diff line change
@@ -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;



}
45 changes: 35 additions & 10 deletions src/org/launchcode/java/demos/lsn4classes2/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/org/launchcode/java/demos/lsn5unittesting/main/Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -85,4 +88,8 @@ public void drive(double miles)
this.odometer += milesAbleToTravel;
}

public void addGas (double gas) {
this.setGasTankLevel(gas + this.getGasTankLevel());
}

}
Loading