CS61B HW0 A Java Crash Course

Creative Exercise 1a: Drawing a Triangle

Your goal is to create a program that prints the following figure. Your code should use loops (i.e. shouldn’t just be five print statements, that’s no fun).

1
2
3
4
5
*
**
***
****
*****

Considerations:

1 * in row 1,
2 * in row 2,

5 * in row 5.

i is row number.
j is number of * in a row.
First while loop to limit row number in to 5.
while j (number of *) is less than i (row number), print *.
when i,j are equal, print the last *, i + 1 and move to the next row, the j roll back to 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class DrawingaTriangle {
public static void main(String[] args) {
int i=1;
int j=1;

//limit row number into 5
while (i<=5) {
//draw i-1 "*" in each row
while (j <= i) {
if (j < i) {
System.out.print("*");
//statment for i=j condition
} else {
System.out.println("*");
}
j++;
}
i++;
j = 1;
}
}
}

Creative Exercise 1b: DrawTriangle

Name this new method drawTriangle and give it a return type of void (this means that it doesn’t return anything at all).

The drawTriangle method should take one parameter named N, and it should print out a triangle exactly like your triangle from exercise 1a, but N asterisks tall instead of 5.

After writing DrawTriangle, modify the main function so that it calls DrawTriangle with N = 10.

Considerations:

This is similar to the previous Exercise 1a code, but need to change 5 into a variable n in the function drawTriangle(int n).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class DrawTriangle {
public static void drawTriangle(int n) {
int i=1;
int j=1;
//limit row number into n
while (i<=n) {
//draw i-1 "*" in each row
while (j <= i) {
if (j < i) {
System.out.print("*");
//statment for i=j condition
} else {
System.out.println("*");
}
j ++;
}
i ++;
j = 1;
}
}

public static void main(String[] args) {
drawTriangle(10);
}
}

Exercise 2

Using everything you’ve learned so far on this homework, you’ll now create a function with the signature public static int max(int[] m) that returns the maximum value of an int array. You may assume that all of the numbers are greater than or equal to zero.

Modify the code below (also found here) so that max works as described. Furthermore, modify main so that the max method is called on the given array and its max printed out (in this case, it should print 22).

1
2
3
4
5
6
7
8
9
public class ClassNameHere {
/** Returns the maximum value from m. */
public static int max(int[] m) {
return 0;
}
public static void main(String[] args) {
int[] numbers = new int[]{9, 2, 15, 2, 22, 10, 6};
}
}

Considerations:

Set first interger as the initial max value, use While loop to compare max with other intergers in the array, any other one bigger than max will instead it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class MaxValue {

public static int max(int[] m) {
int n = m.length;
int max = m[0];
int i = 1;
while (i < n) {
if (m[i] > max) {
max = m[i];
}
i ++;
}
return max;
}

public static void main(String[] args) {
int[] m = new int[]{9, 2, 15, 2, 22, 10, 6};
System.out.println(max(m));
}
}

Exercise 3

Rewrite your solution to Exercise 2 so that it uses a for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class MaxValue_for {
/** Returns the maximum value from m using a for loop. */
public static int forMax(int[] m) {
int mv = m[0];
for (int i = 1; i < m.length; i += 1) {
if (m[i] > mv) {
mv = m[i];
}
}
return mv;
}
public static void main(String[] args) {
int[] numbers = new int[]{9, 2, 15, 2, 22, 10, 6};
System.out.println(forMax(numbers));
}
}

Exercise 4

This is a particularly challenging exercise, but strongly recommended.

Write a function windowPosSum(int[] a, int n) that replaces each element a[i] with the sum of a[i] through a[i + n], but only if a[i] is positive valued. If there are not enough values because we reach the end of the array, we sum only as many values as we have.

For example, suppose we call windowPosSum with the array a = {1, 2, -3, 4, 5, 4}, and n = 3. In this case, we’d:

Replace a[0] with a[0] + a[1] + a[2] + a[3].
Replace a[1] with a[1] + a[2] + a[3] + a[4].
Not do anything to a[2] because it’s negative.
Replace a[3] with a[3] + a[4] + a[5].
Replace a[4] with a[4] + a[5].
Not do anything with a[5] because there are no values after a[5].
Thus, the result after calling windowPosSum would be {4, 8, -3, 13, 9, 4}.

As another example, if we called windowPosSum with the array a = {1, -1, -1, 10, 5, -1}, and n = 2, we’d get {-1, -1, -1, 14, 4, -1}.

Hint 1: Use two for loops.
Hint 2: Use continue to skip negative values.
Hint 3: Use break to avoid going over the end of the array.

Considerations:

  1. For loop each element;
  2. For each element, if it is positive, use sum of this element and all elements behind instead itself;
  3. The sum can be calculated by another for loop, each loop need to check if it is over the array, if so, break;
  4. For each element, if it is negative, use continue to skip.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class BreakContinue {

public static void windowPosSum(int[] a, int n) {
for (int i = 0; i < a.length; i += 1) {
if (a[i] >= 0) {
for (int j = 1; j <= n; j += 1) {
if (i + j > a.length-1) {
break;
}
a[i] = a[i] + a[i + j];
}
} else {
continue;
}
}
}

public static void main(String[] args) {
int[] a = {1, 2, -3, 4, 5, 4};
int n = 3;
windowPosSum(a, n);

// Should print 4, 8, -3, 13, 9, 4
System.out.println(java.util.Arrays.toString(a));
}
}