Lab 7 Solution

$30.00

Category: Tag:

Description

This lab involves the appropriate application of decision statements (if, if-else, etc.), loops, and arrays to solve problems.

  1. Add the following method to your program and call it from main with parameters that cause the output to be: It’s a mad mad mad mad world. Do not modify the method in any way.

public static void decisionStatementPractice(

int a, int b, double c, char d, boolean e) {

a += 4;

  1. = (char) (d+1);

if (a + 2 < c || e) {

System.out.print(“It’s “);

}

if (a <= b && d == ‘q’) {

System.out.print(“a “);

} else {

if (d == ‘q’ && c > 4) {

System.out.print(“a “);

} else {

System.out.print(“not such a “);

}

}

for (int i=b; i<=7; i+=2) {

System.out.print(“mad “);

}

if (!e) {

System.out.println(“world”);

}

}

  1. Write a method that draws an X of the desired height (indicated as a parameter to the method) out of asterisks. For instance, when called like this from main:

drawX(6);

the method should display:

*

*

*

*

**

*

**

*

*

*

  1. Pascal’s Triangle is a set of numbers arranged in a triangle in which the sides have the value 1 and each interior value is the sum of the two values above it (a picture is worth a thousand words here; see below).

Write a method to draw Pascal’s Triangle for any height specified (the height of the triangle should be a parameter of the method). Hint: you might want to store the previous row in an array, so that you can use that to compute the values for the next row. For example, if the method is called from main as follows:

displayPascalsTriangle(5);

the result should be:

1

1

1

1

1

2

1

1

3

3

1

1

4

6

4

  1. Write a method called removeZeros to remove all of the zeros from an array of integers. The array that is returned from this method should not be any longer than necessary. For example, if your method is called from main with the following code:

int[] test = {1, 0, 4, 7, 0, 2, 10, 82, 0};

System.out.println(Arrays.toString(test) +

“: length = ” + test.length);

int[] result = removeZeros(test);

System.out.println(Arrays.toString(result) +

“: length = ” + result.length);

the output should be:

[1, 0, 4, 7, 0, 2, 10, 82, 0]: length = 9

[1, 4, 7, 2, 10, 82]: length = 6

  1. Write a method that takes two arrays containing doubles and returns a new array that contains the larger of the two values at each index. The method should handle cases in which one array is longer than the other. For example, when called from main with the following code:

double[] test1 = {3.3, 8.2, 19.0, 38.1, 2.1, 3.7}; double[] test2 = {4.8, 2.1, 27.3, 6.0}; double [] maxResult = maximize(test1, test2);

System.out.println(Arrays.toString(test1));

System.out.println(Arrays.toString(test2));

System.out.println(Arrays.toString(maxResult));

Your code should produce:

[3.3, 8.2, 19.0, 38.1, 2.1, 3.7]

[4.8, 2.1, 27.3, 6.0]

[4.8, 8.2, 27.3, 38.1, 2.1, 3.7]

Rubric

For each question (20 points total):

  • Computes the correct answer: 2 point

  • Complies with all directions: 1 point

  • Follows style guidelines and commented as appropriate: 1 point


error: Content is protected !!