common/conf common/conf
Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp, Stuart Reges and Whitaker Brand
Goals for today:
S.o.pln()
/ S.o.p()
for System.out.println()
/ System.out.print()
.
For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g., 7.0 rather than 7 for a double, Strings in quotes).
If you've forgotten how to tackle expressions problems, check out lab 2 for a recap!
12/5 + 8/4
|
4 |
|
2.5 * 2 + 17/4
|
9.0 |
|
41 % 15 % 7 + 17 % 3
|
6 |
|
21/2 + "7 % 3" + 17 % 4
|
"107 % 31" |
|
46/3/2.0/3 * 4/5
|
2.0 |
(Review this type of problem in lab 3!) Fill in the boxes with the output that each method call produces:
public static void main(String[] args) { String hear = "bad"; String song = "good"; String good = "hear"; String walk = "talk"; String talk = "feel"; String feel = "walk"; claim(feel, song, feel); // to walk the walk is good claim(good, hear, song); // to hear the good is bad claim(talk, "song", feel); // to feel the walk is song claim("claim", talk, walk); // to claim the talk is feel } public static void claim(String hear, String good, String song) { System.out.println("to " + hear + " the " + song + " is " + good); }
if
/else
mystery
public static void ifElseMystery(int a, int b) { if (a < b) { a = a * 2; } if (a > b) { a = a - 10; } else { b++; } System.out.println(a + " " + b); }(Review this type of problem in lab 4!) Fill in the boxes with the output produced by each of the method calls.
ifElseMystery(10, 3);
|
0 3 |
|
ifElseMystery(6, 6);
|
6 7 |
|
ifElseMystery(3, 4);
|
-4 4 |
|
ifElseMystery(4, 20);
|
8 21 |
while
loop mystery
Fill in the boxes at right with the output produced by each method call. (Review how to do this type of problem in lab 5!)
public static void mystery4(int n) { int x = 1; int y = 2; while (y < n) { if (n % y == 0) { n = n / y; x++; } else { y++; } } System.out.println(x + " " + n); } |
|
Write always/never/sometimes true
at each point. If unsure how to, check out the assertions tutorial!
x > y |
z == 0 |
x == y |
|
---|---|---|---|
A | SOMETIMES | ALWAYS | SOMETIMES |
B | SOMETIMES | SOMETIMES | NEVER |
C | ALWAYS | NEVER | NEVER |
D | NEVER | NEVER | NEVER |
E | NEVER | SOMETIMES | ALWAYS |
public static void mystery(int x, int y) { int z = 0; // Point A while (x != y) { // Point B z++; if (x > y) { // Point C x = x / 10; } else { // Point D y = y / 10; } } // Point E System.out.println(x + " " + y + " " + z); }
You can also solve this problem in Practice-It by clicking on the check-mark above.
Write a method named makeGuesses
that will output random
numbers between 1 and 50 inclusive until it outputs one of at least 48.
Output each guess and the total number of guesses made. Below is a sample
execution:
guess = 43 guess = 47 guess = 45 guess = 27 guess = 49 total guesses = 5
If you're not sure how make/use a Random, review them here.
Write a static method called printMultiples
that takes an
integer n
and an integer m
as parameters and that
prints a complete line of output reporting the first m
multiples of n
. For example, the following calls:
printMultiples(3, 5); printMultiples(7, 3);should produce this output:
The first 5 multiples of 3 are 3, 6, 9, 12, 15 The first 3 multiples of 7 are 7, 14, 21
Notice that the multiples are separated by commas. You are to exactly reproduce this format. Also notice the order of the parameters: the first parameter is the base number and the second parameter is the number of multiples to generate.
You may assume that the number of multiples you will be asked to generate is greater than or equal to one.
Solve this problem in Practice-It by clicking on the check-mark above.
Write a static method called digitsInARow
that takes an
integer n
as a parameter and that returns the highest number
of digits that appear in a row in the base-10 representation
of n
. For many numbers the answer will be 1 because they
don't have adjacent digits that match. But for a number like 3555585, the
answer is 4 because there are four occurrences of the digit 5 that appear
in a row. Below are sample calls on the method.
Method Value Method Value Call Returned Call Returned ------------------------------- ------------------------------- digitsInARow(0) 1 digitsInARow(8823) 2 digitsInARow(18) 1 digitsInARow(777) 3 digitsInARow(394) 1 digitsInARow(82888) 3 digitsInARow(99) 2 digitsInARow(7111171) 4 digitsInARow(8229) 2 digitsInARow(233333888) 5
You are not allowed to use a string to solve this problem. You may assume that the value passed to the method is greater than or equal to 0.
Hint: You can pull apart a number into its digits using /
10
and % 10
.
Solve this problem in Practice-It by clicking on the check-mark above.
Write a method named allDigitsOdd
that returns whether every
digit of a positive integer is odd. Your method should
return true
if the number consists entirely of odd digits
and false
if any of its digits are even. 0, 2, 4, 6, and 8
are even digits, and 1, 3, 5, 7, 9 are odd digits.
For example, allDigitsOdd(135319)
returns true
but allDigitsOdd(9145293)
returns false
.
Hint: You can pull apart a number into its digits using /
10
and % 10
.
season
that takes two integers as
parameters representing a month and day and that returns a String
indicating the season for that month and day. Assume that months are
specified as an integer between 1 and 12 (1 for January, 2 for February,
and so on) and that the day of the month is a number between 1 and 31.
"Winter"
. If the date falls between 3/16 and 6/15,
you should return "Spring"
. If the date falls between 6/16
and 9/15, you should return "Summer"
. And if the date falls
between 9/16 and 12/15, you should return "Fall"
.
firstDigit
that returns the first digit
of an integer. For example, firstDigit(3572)
should
return 3
. It should work for negative numbers as well. For
example, firstDigit(-947)
should return 9
.
If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.
You can view an exercise, type a solution, and submit it to see if you have solved it correctly.
Choose some problems from the book and try to solve them!