-
Define a method named makeStars( ), which has as input a single
integer, say numStars, and returns a String that is 5 characters
wide and contains numStars many asterisks in its center. For example,
makeStars(3) should return the String " *** ". If you use
Format.justify in your method, you will avoid some subtle problems,
such as whether makeStars(4) should be " ****" or "****
". If the input is larger than 5, or less than zero, your method should
return the String "error".
-
Assuming that your makeStars method is defined and that output
is a TextArea, complete the following code fragment so that it
draws the arrow above. Each (non-empty) case in your switch
block MUST have the form:
output.append( makeStars(x) + "\n" );
break;
for some value of x (an integer). Here is the start of your arrow drawing
code fragment:
output.setText("");//erase the old text
for (int row = 1; ???; ???) {
switch(row) {
case ?
?
}// end switch
}// end for loop
-
A company wants to transmit data over the telephone, but they are concerned
that their phones may be tapped. All of their data is transmitted as four-digit
integers. They have asked you to write a method, named encrypt,
that will encrypt (encode) a single four-digit integer so that it may be
transmitted more securely. Your method should have one (four-digit or less)
integer input and return the encrypted integer, which is defined as follows:
-
Replace each digit by (the sum of that digit plus 7) modulus 10. Thus,
if the digit is d, the encrypted digit is (d + 7 ) % 10.
-
Then swap the first digit with the third digit.
-
Finally swap the second digit with the fourth digit.
Write a separate method named decrypt. that inputs an encrypted
four-digit integer and returns the decrypted version (i.e the input is
a coded number and the output is the original number). For both methods,
you should check to insure that the inputs are not more than 4 digits long
(think about integer division by 1000 or 10000 to complete this test).
If they are, return the value -1 (a common programming technique is to
return -1, when outputs should be positive, to indicate an error).
-
What is returned by (double) (3 / 4);
how about (double) 3 / 4; explain.
-
In Java, how do you say "if N is negative or N is even, add 5 to N, otherwise
decrease N by 1"?
-
The following code is neither intuitive nor well designed, but contains
many of the constructs we've studied. To the best of your ability, trace
the flow of the following code. That is, walk through the commands which
Java executes, in the order that Java executes them, keeping track of the
values of the variables. Hint: create a table showing the value
of each data field or variable as you go through the program. Assume that
the GUI is defined, that results is a TextArea, and trace what happens
as the user clicks the button.
public class TraceMe extends GBFrame {
public static int methodA(int x) {
if (x > 5)
return
(x-4);
else
return
(x+2);
} // end methodA
public static void buttonClicked(Button
myButton) {
int n = 4;
int x = 6;
while (n > 0) {
switch (x) {
case 1:
n *= 3;
break;
case 3:
n -= 6;
break;
default:
n++;
break;
}//end switch
x = methodA(n);
results.append("x= " + x + " and n = " + n);
}// end while
} // end buttonClicked
} // end TraceMe
-
Write a method named isLessThan that takes two inputs, x
and y, and returns true when x < y, and false when x >= y.
-
Assuming the following definition of the method scramble:
public static int scramble(int egg)
egg = (egg * 35) % 4;
return egg;
}
what are the final values of the variables liquid and yolk,
after the following lines of code are executed?
int liquid, yolk = 5;
liquid = scramble(yolk);