04a. WS Rev U1a Java Basics W Methods SOL
04a. WS Rev U1a Java Basics W Methods SOL
Name______________________
Review for Test U1a: Basic Java (Conditionals, Loops, Methods, Strings, DeMorgan's)
For #1-2, evaluate the expression.
// Exercise #4: What's the output?
1.) 10 + 28 % 6 - (7 - 10 / 4)
int a = 21, b = 10, c = 32;
2.) 36 / 5 / 2.0 - (4.2 + 7.8) - 22 /
5 if (c > a)
{
// Exercise #3: What's the output? c = a - 4;
}
int a = 21, b = 11, c = 8; else
{
if (c > a) if ( b < a )
{ {
c = a - 4; c = c - 2;
} }
else else
{ {
if ( b < a ) c = a + 22;
{ }
c = c - 2; }
} if (c <= a)
else {
{ b = b % 4;
c = a + 22; }
} System.out.print( a + " " + b + " " + c);
}
if (c <= a)
{
b = b % 4;
}
System.out.print( a + " " + b + " " + c);
// Exercise #5: What's the output? // Exercise #6: What's the output?
int x = 35, y = 21, z = 14;
if (y * 2 < x) int x = 21, y = 15, z = 14;
{ if (y * 2 < x)
z = x + 23; {
} z = x + 23;
else if ( y > z ) }
{ else if ( y < z )
x /= 4; {
if ( x > y ) x /= 4;
{ }
x += 11; else
} {
else if ( z > y ) if ( z + 1 <= y )
{ {
x -= 3; z = 25 - z;
} }
} if ( x >= y )
else {
{ z -= 6;
z = 19 - z; }
} }
if (z <= x) if (z <= x)
{ {
z += 3; x += 3;
1
} }
System.out.print( x + " " + y + " " + z); System.out.print( x + " " + y + " " + z);
7.) What's the output? 8.) What’s the output?
Do #21-31 (required). You may do #13-20, 24.) Write the code to generate a random
but those problems are optional. integer from 762 to 819.
For #21-28, identify the type of answer you 25.) Write the code to generate a random
get (integer or decimal), and state the range of
integer from -18 to -7.
values. (Do not use interval notation.)
2
31.) (z < x) && (z > y || x >= y)
For #32-50, given the Strings below, evaluate each of the expressions.
one.indexOf("nilla") ); // #40
one.indexOf('a') // #41
53.) Use nested loops to print the output on the right. Each print statement only prints out
one character at a time. So you can only use these print statements:
System.out.print("n");
System.out.println();
Here’s the output (Each row has nine n's):
nnnnnnnnn
nnnnnnnnn
nnnnnnnnn
nnnnnnnnn
54.) Use nested loops to print the output on the 55.) Use nested loops to print the
3
right. Each print statement only prints out one output on the right. Each print
number at a time. statement only prints out one
number at a time.
333
4444 7777777
55555 666666
666666 55555
7777777 4444
88888888
56.) Write a method named slope that accepts four integers as parameters: x1, y1,
x2, and y2. This method will return the slope based on the parameters. For example,
the call
threeFourths( 7 )
58.) Write a method called averageOdds that takes five integers as parameters and
returns the average of all the odd numbers. For example,
59.) Write a method pear that takes an integer as a parameter, returning true if the number is
NOT a multiple of 6 and is divisible by 5, or if it is more than 5 digits, returning false otherwise.
You may assume that the parameter is positive. For example,
Method Call Returned Reason
value
4
pear( 6000 ); false 6000 is divisible by 5, but it IS a multiple of 6.
Also, 6000 is not more than 4 digits long.
60.) Write a method myDawg that takes a dog's name, a number of spots (numSpots), and a
decimal weight (dogWeight) as parameters. It will return the following sentence:
"My dog name weighs dogWeight pounds and has numSpots spots."
For example,
Method call Returned value
myDawg("Skippy", 5, "My dog Skippy weighs 32.6 pounds and has 5 spots."
32.6);
myDawg("Dali", 60, 97.1); "My dog Dali weighs 97.1 pounds and has 60 spots."
61.) Write a method called skipPrint that accepts a String as its parameter and prints the
characters in order, skipping every other letter. For example, a call of skipPrint("zoidberg
lobster"); should print the following output:
zibr ose
If the empty string is passed, no output is produced. Your method should produce a
complete line of output.
62.) Write a method called reverse that accepts a String as its parameter and returns a
String with the characters in opposite order. For example, a call of reverse("hello
there!"); should return:
"!ereht olleh"
63.) Write a method called printPowersOfN that accepts a base and an exponent as
arguments and prints each power of the base from base0 (1) up to that maximum power,
inclusive. For example, consider the following calls:
Method call Output in the terminal window
5
printPowersOfN(4, 3); 1 4 16 64
64.) OPTIONAL, FOR EXTRA PRACTICE: Write a method called artificial that takes
two integers as parameters. If the first integer is divisible by 7 and second integer is smaller
than the first, then return the difference of the two integers (the first integer minus the second).
Otherwise, if the both integers are odd, then return the product of the two integers. Otherwise,
return the second integer. For example,
Method Call Returned Reason
value
65.) OPTIONAL, FOR EXTRA PRACTICE: Write a method countNegatives that takes
three decimals as parameters and returns how many of them are negative numbers. For
example
countNegatives( 8.2, -7.8, -3 ) returns 2.
countNegatives( -2.4, 30, 81 ) returns 1.
countNegatives( 2.4, 8, 3.14 ) returns 0.
66.) OPTIONAL, FOR EXTRA PRACTICE: Write a method wizowski that accepts an integer
as a parameter and returns true if the parameter is NOT divisible by 7, is odd, and greater
than 15, returning false otherwise. For example:
Method Call Returned value Reason
wizowski(9); false 9 is not divisible by 7, and it’s odd, but it’s NOT greater
than 15, so it doesn’t meet the criteria.
67.) OPTIONAL, FOR EXTRA PRACTICE: Write a method krattbros that accepts two
integers as parameters. The method returns true if the sum of both parameters is divisible
by 5 or if the second parameter is even. For example,
6
Method Call Returned Reason
value
krattbros( 18, 5 ); false 18 + 5 == 23, which is not divisible by 5. Also, the second parameter
is 5, which is not even. So it meets none of the criteria.
Answers: Review for Test of Basic Java (Conditionals, Loops, Methods, Strings, DeMorgan's)
1.) 9 15.) integer: -501 to -322 32.) 15
33.) b
2.) -12.5 16.) 364
34.) r
17.) 200 35.) l
3.) 21 3 6
36.) tte
4.) 21 2 17 18.) 63 37.)
5.) 8 21 14 19.) 0, 11, 22, 33, ... , 77 mvanilla
38.)
6.) 24 15 5
20.) 4, 11, 18 upcakewars
14.) integer: 33 to 76
Answers: Review for Test of Basic Java (Conditionals, Loops, Methods) - (continued):
7
for (int i = -3; i <= 25 ; i += 7) { for (int i = 1; i <= 5; i++) {
System.out.print( i + " " ); System.out.print( (7 * i - 10) + " ");
} }
for (int i = 44; i >= -16; i -= 12) { for (int i = 1; i <= 6; i++) {
System.out.print( i + " " ); System.out.print( (-12 * i + 56) + " " );
} }
53.) 54.)
for (int i = 1; i <= 4; i++) for (int i = 3; i <= 8; i++)
{ {
for (int j = 1; j <= 9; j++) for (int j = 1; j <= i; j++)
{ {
System.out.print("n"); System.out.print(i);
} }
System.out.println(); System.out.println();
} }
55.) 56.)
for (int i = 7; i >= 4; i--) public double slope( int x1, int y1, int x2, int y2 )
{ {
for (int j = 1; j <= i; j++) return (double) (y2 - y1) / (x2 - x1);
{ }
System.out.print(i);
} 57.)
System.out.println(); public double threeFourths( int num )
} {
return num * 3 / 4.0 ;
}
8
}
60.)
61.)
public void skipPrint( String str )
{
for ( int i = 0; i < str.length(); i += 2 )
{
System.out.print( str.charAt(i) );
}
}
// #66 (Method 1)
// #66 (Method 2)
// #67 (Method 1)
// #67 (Method 2)
10
}
}
11