diff --git a/U6/L1/U6_L1_Activity_One.java b/U6/L1/U6_L1_Activity_One.java new file mode 100644 index 0000000..2e09b12 --- /dev/null +++ b/U6/L1/U6_L1_Activity_One.java @@ -0,0 +1,16 @@ +import java.util.Scanner; + +public class U6_L1_Activity_One { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + double[] arr = new double[3]; + arr[0] = scan.nextDouble(); + arr[1] = scan.nextDouble(); + arr[2] = scan.nextDouble(); + + System.out.println("Contents: " + arr[0] + " " + arr[1] + " " + arr[2]); + System.out.println("Sum: " + (arr[0] + arr[1] + arr[2])); + } +} diff --git a/U6/L1/U6_L1_Activity_Two.java b/U6/L1/U6_L1_Activity_Two.java new file mode 100644 index 0000000..626b767 --- /dev/null +++ b/U6/L1/U6_L1_Activity_Two.java @@ -0,0 +1,23 @@ +import java.util.Scanner; + +public class U6_L1_Activity_Two { + + public static void main(String[] args) { + int[] h = new int[10]; + + h[0] = 1; + h[1] = h[0] + 2; + h[2] = h[1] + 3; + h[3] = h[2] + 4; + h[4] = h[3] + 5; + h[5] = h[4] + 6; + h[6] = h[5] + 7; + h[7] = h[6] + 8; + h[8] = h[7] + 9; + h[9] = h[8] + 10; + + Scanner scan = new Scanner(System.in); + int i = scan.nextInt(); + if (i > 0 && i <= 10) System.out.println(h[i - 1]); + } +} diff --git a/U6/L2/U6_L2_Activity_One.java b/U6/L2/U6_L2_Activity_One.java new file mode 100644 index 0000000..afe854e --- /dev/null +++ b/U6/L2/U6_L2_Activity_One.java @@ -0,0 +1,13 @@ +public class U6_L2_Activity_One { + + // Write your containsNeg method here + + public static boolean containsNeg(double[] arr) { + for (int i = 0; i < arr.length; i++) { + if (arr[i] < 0) { + return true; + } + } + return false; + } +} diff --git a/U6/L2/U6_L2_Activity_Three.java b/U6/L2/U6_L2_Activity_Three.java new file mode 100644 index 0000000..e69de29 diff --git a/U6/L2/U6_L2_Activity_Two.java b/U6/L2/U6_L2_Activity_Two.java new file mode 100644 index 0000000..0732a36 --- /dev/null +++ b/U6/L2/U6_L2_Activity_Two.java @@ -0,0 +1,12 @@ +public class U6_L2_Activity_Two { + + public static int numDivisibleBy3(int[] arr) { + int count = 0; + for (int i = 0; i < arr.length; i++) { + if (arr[i] % 3 == 0) { + count++; + } + } + return count; + } +} diff --git a/U6/L2/runner_U6_L2_Activity_One.java b/U6/L2/runner_U6_L2_Activity_One.java new file mode 100644 index 0000000..fe2a7a7 --- /dev/null +++ b/U6/L2/runner_U6_L2_Activity_One.java @@ -0,0 +1,20 @@ +import java.util.Scanner; + +public class runner_U6_L2_Activity_One { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.println("Enter array length:"); + + int len = scan.nextInt(); + double[] vals = new double[len]; + System.out.println("Enter values:"); + for (int i = 0; i < len; i++) { + vals[i] = scan.nextDouble(); + } + + System.out.println( + "Contains negative: " + U6_L2_Activity_One.containsNeg(vals) + ); + } +} diff --git a/U6/L2/runner_U6_L2_Activity_Three.java b/U6/L2/runner_U6_L2_Activity_Three.java new file mode 100644 index 0000000..05d2139 --- /dev/null +++ b/U6/L2/runner_U6_L2_Activity_Three.java @@ -0,0 +1,23 @@ +import java.util.Scanner; + +public class runner_U6_L2_Activity_Three { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.println("Enter a value for num:"); + int num = scan.nextInt(); + System.out.println("Enter array length:"); + int len = scan.nextInt(); + int[] vals = new int[len]; + System.out.println("Enter values:"); + for (int i = 0; i < len; i++) { + vals[i] = scan.nextInt(); + } + System.out.println( + "Num divisible by " + + num + + ": " + + U6_L2_Activity_Three.numDivisible(num, vals) + ); + } +} diff --git a/U6/L2/runner_U6_L2_Activity_Two.java b/U6/L2/runner_U6_L2_Activity_Two.java new file mode 100644 index 0000000..a22871e --- /dev/null +++ b/U6/L2/runner_U6_L2_Activity_Two.java @@ -0,0 +1,20 @@ +import java.util.Scanner; + +public class runner_U6_L2_Activity_Two { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.println("Enter array length:"); + int len = scan.nextInt(); + int[] vals = new int[len]; + + System.out.println("Enter values:"); + for (int i = 0; i < len; i++) { + vals[i] = scan.nextInt(); + } + System.out.println( + "Number divisible by three: " + + U6_L2_Activity_Two.numDivisibleBy3(vals) + ); + } +} diff --git a/U6/L3/U6_L3_Activity_One.java b/U6/L3/U6_L3_Activity_One.java new file mode 100644 index 0000000..16ee043 --- /dev/null +++ b/U6/L3/U6_L3_Activity_One.java @@ -0,0 +1,12 @@ +public class U6_L3_Activity_One { + + public static String findShortest(String[] words) { + String shortest = words[0]; + for (int i = 1; i < words.length; i++) { + if (words[i].length() < shortest.length()) { + shortest = words[i]; + } + } + return shortest; + } +} diff --git a/U6/L3/U6_L3_Activity_Three.java b/U6/L3/U6_L3_Activity_Three.java new file mode 100644 index 0000000..42dd8ca --- /dev/null +++ b/U6/L3/U6_L3_Activity_Three.java @@ -0,0 +1,12 @@ +public class U6_L3_Activity_Three { + + public static void printUn(String[] words) { + for (int i = 0; i < words.length; i++) { + if ( + words[i].length() >= 2 && words[i].substring(0, 2).equals("un") + ) { + System.out.println(words[i]); + } + } + } +} diff --git a/U6/L3/U6_L3_Activity_Two.java b/U6/L3/U6_L3_Activity_Two.java new file mode 100644 index 0000000..c27e2d1 --- /dev/null +++ b/U6/L3/U6_L3_Activity_Two.java @@ -0,0 +1,21 @@ +public class U6_L3_Activity_Two { + + public static void removeVowels(String[] words) { + for (int i = 0; i < words.length; i++) { + String result = ""; + for (int j = 0; j < words[i].length(); j++) { + String letter = words[i].substring(j, j + 1); + if ( + !letter.equals("a") && + !letter.equals("e") && + !letter.equals("i") && + !letter.equals("o") && + !letter.equals("u") + ) { + result += letter; + } + } + System.out.println(result); + } + } +} diff --git a/U6/L3/runner_U6_L3_Activity_One.java b/U6/L3/runner_U6_L3_Activity_One.java new file mode 100644 index 0000000..8547409 --- /dev/null +++ b/U6/L3/runner_U6_L3_Activity_One.java @@ -0,0 +1,19 @@ +import java.util.Scanner; + +public class runner_U6_L3_Activity_One { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.println("Enter array length:"); + int len = scan.nextInt(); + scan.nextLine(); + String[] wordList = new String[len]; + System.out.println("Enter values:"); + for (int i = 0; i < len; i++) { + wordList[i] = scan.nextLine(); + } + System.out.println( + "Shortest word: " + U6_L3_Activity_One.findShortest(wordList) + ); + } +} diff --git a/U6/L3/runner_U6_L3_Activity_Three.java b/U6/L3/runner_U6_L3_Activity_Three.java new file mode 100644 index 0000000..e320f76 --- /dev/null +++ b/U6/L3/runner_U6_L3_Activity_Three.java @@ -0,0 +1,18 @@ +import java.util.Scanner; + +public class runner_U6_L3_Activity_Three { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.println("Enter array length:"); + int len = scan.nextInt(); + scan.nextLine(); + String[] wordList = new String[len]; + System.out.println("Enter values:"); + for (int i = 0; i < len; i++) { + wordList[i] = scan.nextLine(); + } + System.out.println(); + U6_L3_Activity_Three.printUn(wordList); + } +} diff --git a/U6/L3/runner_U6_L3_Activity_Two.java b/U6/L3/runner_U6_L3_Activity_Two.java new file mode 100644 index 0000000..d3dd4ba --- /dev/null +++ b/U6/L3/runner_U6_L3_Activity_Two.java @@ -0,0 +1,17 @@ +import java.util.Scanner; + +public class runner_U6_L3_Activity_Two { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.println("Enter array length:"); + int len = scan.nextInt(); + scan.nextLine(); + String[] wordList = new String[len]; + System.out.println("Enter values:"); + for (int i = 0; i < len; i++) { + wordList[i] = scan.nextLine(); + } + U6_L3_Activity_Two.removeVowels(wordList); + } +} diff --git a/U6/L4/U6_L4_Activity_One.java b/U6/L4/U6_L4_Activity_One.java new file mode 100644 index 0000000..86a1b65 --- /dev/null +++ b/U6/L4/U6_L4_Activity_One.java @@ -0,0 +1,19 @@ +public class U6_L4_Activity_One { + + public static String insert(String[] words, String newWord, int place) { + if (place < 0 || place >= words.length) { + return "you need a valid number"; + } + + for (int i = words.length - 1; i > place; i--) { + words[i] = words[i - 1]; + } + words[place] = newWord; + + String result = ""; + for (int i = 0; i < words.length; i++) { + result += words[i]; + } + return result; + } +} diff --git a/U6/L4/U6_L4_Activity_Two.java b/U6/L4/U6_L4_Activity_Two.java new file mode 100644 index 0000000..9694f33 --- /dev/null +++ b/U6/L4/U6_L4_Activity_Two.java @@ -0,0 +1,14 @@ +public class U6_L4_Activity_Two { + + public static void swap(int[] arr, int i, int j) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + + public static void allReverseSwap(int[] arr) { + for (int i = 0; i < arr.length / 2; i++) { + swap(arr, i, arr.length - 1 - i); + } + } +} diff --git a/U6/L4/runner_U6_L4_Activity_One.java b/U6/L4/runner_U6_L4_Activity_One.java new file mode 100644 index 0000000..710d685 --- /dev/null +++ b/U6/L4/runner_U6_L4_Activity_One.java @@ -0,0 +1,28 @@ +import java.util.Scanner; +public class runner_U6_L4_Activity_One +{ + public static void main(String[] args) + { + Scanner scan = new Scanner(System.in); + System.out.println("Enter array length:"); + int len = scan.nextInt(); + scan.nextLine(); + String[] wordList = new String[len]; + System.out.println("Enter values:"); + for(int i = 0; i < len; i++) + { + wordList[i] = scan.nextLine(); + } + System.out.println("Enter new String:"); + String insWord = scan.nextLine(); + System.out.println("Enter place:"); + int pos = scan.nextInt(); + System.out.println("Method return: " + U6_L4_Activity_One.insert(wordList, insWord, pos)); + System.out.print("Array contents: {"); + for(int i = 0; i < len-1; i++) + { + System.out.print(wordList[i] + ", "); + } + System.out.println(wordList[len-1]+"}"); + } +} diff --git a/U6/L4/runner_U6_L4_Activity_Two.java b/U6/L4/runner_U6_L4_Activity_Two.java new file mode 100644 index 0000000..7330f08 --- /dev/null +++ b/U6/L4/runner_U6_L4_Activity_Two.java @@ -0,0 +1,32 @@ +import java.util.Scanner; + +public class runner_U6_L4_Activity_Two { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.println("Enter array length:"); + int len = scan.nextInt(); + int[] nums = new int[len]; + System.out.println("Enter values:"); + for (int i = 0; i < len; i++) { + nums[i] = scan.nextInt(); + } + System.out.println("Which method?\n1 - swap\n2 - allReverseSwap"); + int mtdNum = scan.nextInt(); + if (mtdNum == 1) { + System.out.println("Enter two indices:"); + int pos1 = scan.nextInt(); + int pos2 = scan.nextInt(); + U6_L4_Activity_Two.swap(nums, pos1, pos2); + } else if (mtdNum == 2) { + U6_L4_Activity_Two.allReverseSwap(nums); + } else { + return; + } + System.out.print("Array contents: {"); + for (int i = 0; i < len - 1; i++) { + System.out.print(nums[i] + ", "); + } + System.out.println(nums[len - 1] + "}"); + } +} diff --git a/U6/L5/U6_L5_Activity_One.java b/U6/L5/U6_L5_Activity_One.java new file mode 100644 index 0000000..49decf1 --- /dev/null +++ b/U6/L5/U6_L5_Activity_One.java @@ -0,0 +1,12 @@ +public class U6_L5_Activity_One { + + public static void reverse(String[] words) { + for (String s : words) { + String reversed = ""; + for (int i = s.length() - 1; i >= 0; i--) { + reversed += s.substring(i, i + 1); + } + System.out.println(reversed); + } + } +} diff --git a/U6/L5/U6_L5_Activity_Three.java b/U6/L5/U6_L5_Activity_Three.java new file mode 100644 index 0000000..5f84e47 --- /dev/null +++ b/U6/L5/U6_L5_Activity_Three.java @@ -0,0 +1,10 @@ +public class U6_L5_Activity_Three { + + public static double avg(int[] arr) { + double s = 0; + for (int n : arr) { + s += n; + } + return s / arr.length; + } +} diff --git a/U6/L5/U6_L5_Activity_Two.java b/U6/L5/U6_L5_Activity_Two.java new file mode 100644 index 0000000..0c747e0 --- /dev/null +++ b/U6/L5/U6_L5_Activity_Two.java @@ -0,0 +1,10 @@ +public class U6_L5_Activity_Two { + + public static int product(int[] arr) { + int p = 1; + for (int k : arr) { + p *= k; + } + return p; + } +} diff --git a/U6/L5/runner_U6_L5_Activity_One.java b/U6/L5/runner_U6_L5_Activity_One.java new file mode 100644 index 0000000..34dcc86 --- /dev/null +++ b/U6/L5/runner_U6_L5_Activity_One.java @@ -0,0 +1,20 @@ +import java.util.Scanner; + +public class runner_U6_L5_Activity_One { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + System.out.println("Enter array length:"); + int len = scan.nextInt(); + scan.nextLine(); + + String[] wordList = new String[len]; + System.out.println("Enter values:"); + for (int i = 0; i < len; i++) { + wordList[i] = scan.nextLine(); + } + + U6_L5_Activity_One.reverse(wordList); + } +} diff --git a/U6/L5/runner_U6_L5_Activity_Three.java b/U6/L5/runner_U6_L5_Activity_Three.java new file mode 100644 index 0000000..069fd72 --- /dev/null +++ b/U6/L5/runner_U6_L5_Activity_Three.java @@ -0,0 +1,16 @@ +import java.util.Scanner; + +public class runner_U6_L5_Activity_Three { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.println("Enter array length:"); + int len = scan.nextInt(); + int[] nums = new int[len]; + System.out.println("Enter values:"); + for (int i = 0; i < len; i++) { + nums[i] = scan.nextInt(); + } + System.out.println(U6_L5_Activity_Three.avg(nums)); + } +} diff --git a/U6/L5/runner_U6_L5_Activity_Two.java b/U6/L5/runner_U6_L5_Activity_Two.java new file mode 100644 index 0000000..bbb7df9 --- /dev/null +++ b/U6/L5/runner_U6_L5_Activity_Two.java @@ -0,0 +1,17 @@ +import java.util.Scanner; + +public class runner_U6_L5_Activity_Two { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.println("Enter array length:"); + int len = scan.nextInt(); + scan.nextLine(); + int[] nums = new int[len]; + System.out.println("Enter values:"); + for (int i = 0; i < len; i++) { + nums[i] = scan.nextInt(); + } + System.out.println(U6_L5_Activity_Two.product(nums)); + } +} diff --git a/U6/Student.java b/U6/Student.java new file mode 100644 index 0000000..85fb776 --- /dev/null +++ b/U6/Student.java @@ -0,0 +1,36 @@ +public class Student { + + private String name; + private double gpa; + private int year; + + public Student(String name, double gpa, int year) { + this.name = name; + this.gpa = gpa; + this.year = year; + } + + public String getName() { + return this.name; + } + + public double getGpa() { + return this.gpa; + } + + public int getYear() { + return this.year; + } + + public String toString() { + return ( + "{\n\tname: " + + this.name + + ",\n\tgpa: " + + this.gpa + + ",\n\tyear: " + + this.year + + "\n}" + ); + } +} diff --git a/U6/StudentStatsArray.java b/U6/StudentStatsArray.java new file mode 100644 index 0000000..05704fd --- /dev/null +++ b/U6/StudentStatsArray.java @@ -0,0 +1,92 @@ +public class StudentStatsArray { + + private final Student[] students; + + public StudentStatsArray(Student[] students) { + this.students = students; + } + + // Returns the average gpa of the students + public double averageGpa() { + double sum = 0; + for (Student s : students) { + sum += s.getGpa(); + } + return sum / students.length; + } + + // Returns the gpa range of the students + public double getGpaRange() { + double min = students[0].getGpa(); + double max = students[0].getGpa(); + for (Student s : students) { + if (s.getGpa() < min) min = s.getGpa(); + if (s.getGpa() > max) max = s.getGpa(); + } + return max - min; + } + + // Returns the name of the student that has the longest length + public String getLongestName() { + String longest = students[0].getName(); + for (Student s : students) { + if (s.getName().length() > longest.length()) { + longest = s.getName(); + } + } + return longest; + } + + // Returns a count of the number freshman students + public int getNumFreshman() { + int count = 0; + for (Student s : students) { + if (s.getYear() == 1) count++; + } + return count; + } + + // Returns the index of the first student with a name equal to name. + // Returns -1 if not found + public int search(String name) { + for (int i = 0; i < students.length; i++) { + if (students[i].getName().equals(name)) return i; + } + return -1; + } + + // Returns the index of the first student with a gpa greater than or equal to the gpa + // Returns -1 if not found + public int search(double gpa) { + for (int i = 0; i < students.length; i++) { + if (students[i].getGpa() >= gpa) return i; + } + return -1; + } + + // Returns 1 if the students are sorted in ascending order by their gpa; -1 if they + // are sorted in descending order; 0 otherwise. + public int sortStatus() { + boolean ascending = true; + boolean descending = true; + for (int i = 0; i < students.length - 1; i++) { + double curr = students[i].getGpa(); + double next = students[i + 1].getGpa(); + if (curr > next) ascending = false; + if (curr < next) descending = false; + } + if (ascending) return 1; + if (descending) return -1; + return 0; + } + + // Returns the array of students in JSON like format + public String toString() { + String result = "[\n"; + for (int i = 0; i < students.length; i++) { + result += students[i].toString() + ",\n"; + } + result += "]"; + return result; + } +} diff --git a/U6/runner_StudentStatsArray.java b/U6/runner_StudentStatsArray.java new file mode 100644 index 0000000..95fc1bd --- /dev/null +++ b/U6/runner_StudentStatsArray.java @@ -0,0 +1,75 @@ +import java.util.Scanner; + +public class runner_StudentStatsArray { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + String choice = ""; + while (!choice.equals("n")) { + System.out.print("Would you like to test StudentStats? (y/n): "); + choice = scan.nextLine().trim().toLowerCase(); + + if (choice.equals("y")) { + testStudentStats(scan); + } else if (!choice.equals("n")) { + System.out.println("Invalid choice."); + } else { + System.out.println("Bye!"); + } + } + } + + public static void testStudentStats(Scanner scan) { + System.out.print("Enter the length of the Student array: "); + int len = scan.nextInt(); + scan.nextLine(); + + Student[] arr = new Student[len]; + for (int i = 0; i < len; i++) { + System.out.println( + "\nPlease enter the info for student " + (i + 1) + "" + ); + arr[i] = studentBuilder(scan); + } + + System.out.print("Enter a students name to search for: "); + String name = scan.nextLine().trim(); + System.out.print("Enter a minimum gpa to search for: "); + double gpa = scan.nextDouble(); + scan.nextLine(); + + StudentStatsArray statsArray = new StudentStatsArray(arr); + System.out.println(statsArray); + System.out.println("\nMethod return values: "); + System.out.println("averageGpa(): " + statsArray.averageGpa()); + System.out.println("getGpaRange(): " + statsArray.getGpaRange()); + System.out.println("getLongestName(): " + statsArray.getLongestName()); + System.out.println("getNumFreshman(): " + statsArray.getNumFreshman()); + System.out.println("search(" + name + "): " + statsArray.search(name)); + System.out.println("search(" + gpa + "): " + statsArray.search(gpa)); + int sortStatus = statsArray.sortStatus(); + System.out.println( + "sortStatus(): " + + (sortStatus == 1 + ? "Ascending" + : (sortStatus == -1 ? "Descending" : "Not sorted")) + ); + + System.out.println(); + } + + public static Student studentBuilder(Scanner scan) { + System.out.print("Enter the students name: "); + String name = scan.nextLine().trim(); + System.out.print("Enter the students gpa: (0.0-4.0) "); + double gpa = scan.nextDouble(); + scan.nextLine(); + System.out.print("Enter the students year (1-4): "); + int year = scan.nextInt(); + scan.nextLine(); + Student student = new Student(name, gpa, year); + + return student; + } +}