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 | * |
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 | public class DrawingaTriangle { |
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 | public class DrawTriangle { |
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 | public class ClassNameHere { |
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 | public class MaxValue { |
Exercise 3
Rewrite your solution to Exercise 2 so that it uses a for loop.
1 | public class MaxValue_for { |
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:
- For loop each element;
- For each element, if it is positive, use sum of this element and all elements behind instead itself;
- The sum can be calculated by another for loop, each loop need to check if it is over the array, if so, break;
- For each element, if it is negative, use continue to skip.
1 | public class BreakContinue { |