Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS242 – Algorithms and Computing Theory – Spring 2023

Assignment #1

Programming Exercises

Section 1: FooBar.java [10 pts]

1. Fill in the method foobar that does the following: 1) take nonnegative integers, x, p, q as inputs, 2) print the number from 0 (inclusive) to x  (not inclusive) – if the number to be printed is divisible by p, print “foo”; if the number to be printed is divisible by q, print “bar”; if the number to be printed is divisible by both p and q, print “foobar”. 3) the method doesn’t return anything. Handle the error cases gracefully.

Section 2: GpaCalculator.java [30 pts]

1. Fill in the method lettergrade that does the following: 1) take a nonnegative integer between 0 (inclusive) and 100 (inclusive) as an input, 2) return a letter grade as a character, 3) if the input value is outside the valid range, lettergrade must return ‘F’. Use if statements.
A: 90-100 (inclusive), B: 80-89 (inclusive), C: 70-79 (inclusive), D: 60-69 (inclusive), F: 0-59 (inclusive)

2. Fill in the method gradepoint that does the following: 1) take a character as a letter grade, 2) return a grade point as an integer, 3) if the input value is outside the valid range, gradepoint must return 0. Use switch-case.
A: 4, B: 3, C: 2, D: 1, F: 0

3. Fill in the method parseScores that does the following: 1) take a String as an input; the string is delimited by ‘|’ character dividing individual scores, 2) return an ArrayList<Integer> that contains the scores provided in the String input, 3) if the input isn’t formatted correctly, return an empty ArrayList<Integer>.
Sample Input: 25|30|90|99|30
Sample Output: ArrayList consisting of the integers – 25, 30, 90, 99, 30

4. Fill in the method calculateGpa using lettergrade, gradepoint, parseScores that does the following: 1) take a String as an input (the same format with the input of parseScores), 2) return a double as a GPA calculated as an average of the grade points.
Sample Input: 25|20|90|99|30
Sample Output: 1.6

Section 3: SetOperations.java [30 pts]

1. Fill in the method removeDuplicates that does the following: 1) take a LinkedList<Integer> as an input; assume the list is sorted, 2) remove duplicate values from the list, 3) return the list that only contains unique values.
Sample Input: 2 à 5 à 5 à 7 à 7 à 10
Sample Output: 2 à 5 à 7 à 10

2. Fill in the method intersection that does the following: 1) take two LinkedList<Integer> as input; 2) use removeDuplicates to ensure both lists only have unique values, 3) create a new LinekdList<Integer> that contains the values in the intersection of both lists, 4) return the new list of intersection.
Sample Input: 1 à 2 à 2 à 3 à 7, 3 à 3à 7 à 10 à 12
Sample Output: 3 à 7

3. Fill in the method union that does the following: 1) take two LinkedList<Integer> as input; 2) use removeDuplicates to ensure both lists only have unique values, 3) create a new LinekdList<Integer> that contains the values in the union of both lists, 4) return the new list of union.
Sample Input: 1 à 2 à 2 à 3 à 7, 3 à 3à 7 à 10 à 12
Sample Output: 1 à 2 à 3 à 7 à 10 à 12

Section 4: ArrayStack.java [15 pts]

1. Implement push, peek, pop, size methods in ArrayStack class using Array (Not ArrayList). Do not modify the constructor of ArrayStack with the initial Array size of 5; however, the stack can contain more than 5 objects.  Do not modify the method signatures.

Section 5: ArrayQueue.java [15 pts]

1. Implement enqueue, dequeue, size methods in ArrayQueue class using Array (Not ArrayList). Do not modify the constructor of ArrayQueue with the initial Array size of 3; however, the queue can contain more than 5 objects. Do not modify the method signatures.