1a. Write a method named *total* for computing the sum of three
given real numbers s, m, and f.

1b. Call (invoke) your method *total* in the *main* method
to compute the sum of the three numbers s = 2.0, m = 2.0, and f =
2.0. (You do not need to write the entire *main* method, just
the part that assigns values to s, m, and f, and calls your method.)

1c. Trace, step by step, how the computer will perform the needed computations, and check that the result is indeed correct.

Solution: public double total(double start, double mainpart, double final){ return start + mainpart + final;} in the method main: double s = 2.0; double m = 2.0; double f = 2.0; double t = total(s, m, f); Tracing: first, --------- --------- --------- | 2.0 | | 2.0 | | 2.0 | --------- --------- --------- s m f -------------------------------------- | -------- --------- --------- | | | 3.0 | | 5.0 | | 2.0 | | | -------- --------- --------- | | start mainpart finish | |--------------------------------------| | | | | | | | | -------------------------------------- total --------- | 6.0 | --------- t2a. Define a class

2b. Use your class in the *main* method to define a new
process "test" of type *Process* with s = 10, m = 30, and f = 10.
Compute and print the overall time of the test (the result
should be 10 + 30 + 10 = 50); then,
replace s and f with 15 minutes, m with 50 minutes,
and compute and print the new total process time.

2c. Trace your program step-by-step.

Solution: public class Process{ private String name; private double s; private double m; private double f; public Circle(String aname, double as, double am; double af){ name = aname, s = as; m = am; f = af;} public String getName(){return name;} public double getS(){return s;} public double getM(){return m;} public double getF(){return f;} public void setName(String newName){name = newName;} public void setS(double newS){s = newS;} public void setM(double newM){m = newM;} public void setF(double newF){f = newF;} public double time(){return s + m + f;} } In the main method: Process test = new Process("Test", 10, 30, 10); double total = test.time(); System.out.println(total); test.setS(15); test.setF(15); test.setM(50); total = test.time(); System.out.println(total); Tracing: on the first line, we call a constructor method: -------------- | ---------- | | | Test | | | ---------- | | aname | | ---------- | | | 10 | | | ---------- | | as | | ---------- | | | 30 | | | ---------- | | am | | ---------- | | | 10 | | | ---------- | | af | -------------- | | | | | | | | | | | | | | | | -------------- Process after the first line, we get a new object -------------- | ---------- | | | Test | | | ---------- | | name | | ---------- | | | 10 | | | ---------- | | s | | ---------- | | | 30 | | | ---------- | | m | | ---------- | | | 10 | | | ---------- | | f | -------------- test after the second line, we get ---------- | 50 | ---------- total at the third line, we print 50 to the screen after the fourth through sixth lines, the object changes to: -------------- | ---------- | | | Test | | | ---------- | | aname | | ---------- | | | 15 | | | ---------- | | as | | ---------- | | | 50 | | | ---------- | | am | | ---------- | | | 15 | | | ---------- | | af | -------------- | | | | | | | | | | | | | | | | -------------- Process after the seventh line, we change the value of the variable total: ---------- | 90 | ---------- total on the eighth line, we print 90 to the screen3a. Write a piece of code that, given an array

3b. To check the correctness of the code you wrote in Part 3a, write
a piece of code that defines a new array *h* consisting of 3
elements of type integer and assigns, to elements of this array,
values 10, 30, and 10. Trace step-by-step how the piece of code you
wrote in Part 3a will compute the sum of all the elements of this
array.

Solution: int sum = 0; for (int i = 0; i < h.length; i++) {sum = sum + h[i];} Testing: int[] h = {10, 30, 10}; Tracing: The fact that we have defined an array h with 3 elements means, in effect, that we have defined three variables h[0], h[1], and h[2]: ---- | \| ---\ h \ \| - ------- | 10 | h[0] ------- | 30 | h[1] ------- | 10 | h[2] ------- Then, we define a new variable sum and assigning to it a value 0: ------- | 0 | ------- sum Next, we get to the for-loop. First, we define a new integer variable i and assign to it the value 0: ------- ------- | 0 | | 0 | ------- ------- sum i We check the condition: the length of our array h is 3, so i is smaller than this length. Thus, the condition i < h.length is satisfied, and we implement the operation in the body of the loop. Since i = 0, we thus perform the operation sum = sum + h[0]; In other words, we add the values of sum (0) and h[0] (10) and place the result 0 + 10 = 10 into the variable sum: ------- ------- | 10 | | 0 | ------- ------- sum i Then, we go to the update statement i++; this statement adds 1 to 2, so we get: ------- ------- | 10 | | 1 | ------- ------- sum i The condition i < h.length is still satisfied, so we again go inside the loop. This time, i is 1, so the statement in the body of the loop becomes sum = sum + h[1]; In other words, we add the values of sum (1) and h[1] (30) and place the result 10 + 30 = 40 into the variable sum: ------- ------- | 40 | | 1 | ------- ------- sum i Then, we go to the update statement i++; this statement adds 1 to i, so we get: ------- ------- | 40 | | 2 | ------- ------- sum i The condition i < h.length is still satisfied, so we again go inside the loop. This time, i is 2, so the statement in the body of the loop becomes sum = sum + h[2]; In other words, we add the values of sum (40) and h[2] (10) and place the result 40 + 10 = 50 into the variable sum: ------- ------- | 50 | | 2 | ------- ------- sum i Then, we go to the update statement i++; this statement adds 1 to i, so we get: ------- ------- | 50 | | 3 | ------- ------- sum i The condition i < h.length is no longer satisfied, since i = 3 and the length of h is also 3. Thus, we get out of the loop.4a. Write a method that, given an array

4b. To check the correctness of your method,
write a piece of code that defines a new array *z*
consisting of 3 elements of type integer and assigns, to elements of this
array, values 2, 4, and 0. Trace step-by-step
how the piece of code you wrote
in Part 4a will compute the smallest element of this array. *Hint:*
the desired smallest element is min(2, 4, 0) = 0.

public int smallest(int[] h){ int smallestSoFar = h[0]; for(int i = 1; i < h.length; i++) {if (h[i] < smallestSoFar) smallestSoFar = h[i];} return smallestSoFar; } in the main method: int[] z = {2, 4, 0}; int small = smallest(z); tracing: First, we define an array ---- | \| ---\ z \ \| - ------- | 2 | z[0] ------- | 4 | z[1] ------- | 0 | z[2] ------- Then, we call a method: --------------------------- | ---- | | | \| | | ---\ | | h \ | | \| | | - ------- | | | 2 | h[0] | | ------- | | | 4 | h[1] | | ------- | | | 0 | h[2] | | ------- | |---------------------------| | | | ... | Within this method, we define a new variable smallestSoFar and assign it a value h[0] = 2: --------------------- | 2 | --------------------- smallestSoFar Then, we start a loop by defining a new variable i and assigning it a value 1: --------------------- --------- | 2 | | 1 | --------------------- --------- smallestSoFar i Since 1 = i < h.length = 3, we go inside the loop. Since i = 1, the condition 4 = h[1] < smallestSoFar = 2 is not satisfied, so we do not do anything inside the loop. Next, we increase i by 1: --------------------- --------- | 2 | | 2 | --------------------- --------- smallestSoFar i Since 2 = i < h.length = 3, we go inside the loop. Since i = 2, the condition 0 = h[2] < smallestSoFar = 2 is satisfied, so change the value of smallestSoFar to h[2] = 0: --------------------- --------- | 0 | | 2 | --------------------- --------- smallestSoFar i Next, we increase i by 1: --------------------- --------- | 0 | | 3 | --------------------- --------- smallestSoFar i Now, 3 = i is not smaller than h.length = 3, so we exit the loop, and return the value smallestSoFar = 0 to the line int small = smallest(z); according to which the value 0 is assigned to the new variable small: ------------- | 0 | ------------- small5. Name main GUI components, and briefly explain their use.

Windows create actual windows on the screen Labels indicate the meaning of different parts of the window Text area is a place where the user types in the input and/or the program places the output A button is what a user pressed to initiate a new process, like "Calculate" or "Exit"