August 28, 2017
Write a for-loop to solve snippet problem #4 below
// Reads temperatures from the user, computes average and # days above average.
import java.util.*;
public class Weather {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("How many days' temperatures? ");
int days = console.nextInt();
/// 1. Create temperature array
int sum = 0;
for (int i = 0; i < days; i++) { // read/store each day's temperature
System.out.print("Day " + (i + 1) + "'s high temp: ");
/// 2. Store temperature in array
/// 3. Update sum
}
double average = (double) sum / days;
int count = 0;
/// 4. Count days over average
// Report results
System.out.printf("Average temp = %.1f\n", average);
System.out.println(count + " days above average");
}
}
August 30, 2017 (Section 01)
August 31, 2017 (Section 02)
Read Chapter 1
September 1, 2017
September 6, 2017 (Section 01)
September 7, 2017 (Section 02)
\[ \sqrt{ (x_2 - x_1)^2 + (y_2 - y_1)^2 } \]
1). Using the formula above, write a method distance
that computes the distance between a Point
and another Point
parameter. Note: this should be an accessor method part of the Point
class.
2). Write an additional method, distanceFromOrigin
that returns the distance between a Point
and the origin, (0,0).
September 8, 2017
September 11, 2017
September 13, 2017 (Section 01)
September 14, 2017 (Section 02)
September 15, 2017
What is the output of the following code:
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 10; i++) {
list.add(10 * i); // [10, 20, 30, 40, ..., 100]
}
for (int i = 0; i < list.size(); i++) {
list.remove(i);
}
System.out.println(list);
What is the output of the following code:
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 5; i++) {
list.add(2 * i); // [2, 4, 6, 8, 10]
}
int size = list.size();
for (int i = 0; i < size; i++) {
list.add(i, 42); // add 42 at index i
}
System.out.println(list);
In the code snippet above, what happens if you get rid of the int size
variable above and replace it with list.size()
?
September 18, 2017
September 20, 2017 (Section 01)
September 21, 2017 (Section 02)
September 22, 2017
IntArrayBag
Specification
September 25, 2017
IntArrayBag
Implementation
Point
Implementation
October 4, 2017 (Section 01)
October 5, 2017 (Section 02)
DoubleArraySeq
Javadoc
DoubleArraySeq
Source Code Template
October 11, 2017 (Section 01)
October 12, 2017 (Section 02)
October 13, 2017
October 16, 2017
Linked Lists (no slides)
October 20, 2017
October 23, 2017
Doubly-Linked Lists (no slides)
October 27, 2017
November 7, 2017
November 8, 2017
November 9, 2017
Recursion (no slides)
November 20, 2017
November 27, 2017
Factorial
public static int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
Fibonacci
// note: this is not the most efficient
public static int fib(int n) {
if (n <= 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
Tower of Hanoi
public static void hanoi(int discs, int src, int dest, int temp) {
if (discs > 0) {
hanoi (discs - 1, src, temp, dest);
System.out.printf("Move disc %d from %d to %d\n", discs, src, dest);
hanoi (discs - 1, temp, dest, src);
}
}
Digit Operations
public static int sumDigits(int i) {
if (i == 0)
return 0;
return (i % 10) + sumDigits(i / 10);
}
private static int place(int n) {
if (n == 0)
return 1;
return 10 * place(n / 10);
}
public static int reverse(int n) {
if (n < 10)
return n;
return (n % 10) * place(n / 10) + reverse(n / 10);
}
Binary Search Trees
December 1, 2017
December 4, 2017
December 8, 2017