Do the following problems by hand (without using a computer):
What will each of the following lines print?
System.out.println("12" + 3 + 4);
System.out.println("12" + (3 + 4));
System.out.println(12 + 3 + 4);
System.out.println(12 + "3" + 4);
System.out.println(12
+ 3 + "4");
What will each of the following lines print?
System.out.println(13 / 3);
System.out.println(13 / -3);
System.out.println(3 / 13);
System.out.println(999 / 2);
System.out.println(129
/ 10);
What will each of the following lines print?
System.out.println(13 % 3);
System.out.println(-13 % 3);
System.out.println(3 % 13);
System.out.println(999 % 2);
System.out.println(129
% 10);
The expression (x % 1) will always equal 0 for any integer
value of x. Explain.
If x and y are positive integers where x < y, then (x / y)
will always equal zero and (x % y) will always equal x. Explain.
When we try to evaluate (x % 0), Java signals an error and
stops running the program. The exact error is "division by zero", even
though we are using the remainder operator and not the division
operator. Explain.
* Use algebra to show that (x % y) is equal to (x - (x/y)*y)
as long as y is not zero.
Hint: x/y is integer division, so it truncates.
Hint: Express x as some multiple of y plus some remainder that is less
than y. That is, x = ky + r, where 0 <= r < y.
What will each of the following lines print?
System.out.println(1328538 % 1);
System.out.println(1328538 % 10);
System.out.println(1328538 % 100);
System.out.println(123 % 456);
System.out.println(123456 % 123450);
If x is a positive integer, then ((x % 100) / 10) will equal the ten's digit of x. For example, if x equals 12345, then (x % 100) equals 45, and 45/10 equals 4, which is the ten's digit of 12345.
Explain why this works.
Provide a similar expression that will equal the
hundred's digit of x for any positive integer x.
Write (by hand, without a computer!) a single Java statement
that will result in overflow.
What will each of the following lines print?
System.out.println(1+2*3-4/5);
System.out.println(1+2/3*4-5);
System.out.println(1*2-3/4+5);
System.out.println(1-2+3*4/5);
System.out.println(1/2-3*4+5);
What will each of the following lines print?
System.out.println(5*4%3+2%1);
System.out.println(17%13%8);
System.out.println(17%(13%8));
System.out.println(17/13/8);
System.out.println(17/(13/8));
Do the following problems with a computer (and a Java compiler):
Besides tabs (\t), Java has an escape sequence for newlines.
It is backslash-n (\n). Write a program called Poem1.java that changes
the following program so that it only uses only one println statem\ent.
Do this by embedding three escaped newlines within the one println
statement.
System.out.println("Roses are red.");
System.out.println("Violets are blue.");
System.out.println("Some poems rhyme,");
System.out.println("But not this one.");
Write a program, SumOfThreeDigits.java, that reads in a
positive integer less than 1000 and prints out the sum of its digits.
It should work like this:
Enter an integer
between 0 and 999: 342
The sum of
the digits of 342 is 3 + 4 + 2 which equals 9.
Of course, your
program should work for any legal input!
Hint: one of the written problems from above provides the math you
need here.
Write a program, SumFromOneToN.java, that reads in a
positive integer N and prints out the sum of the integers from 1 to N (that
is, 1 + 2 + ... + N). It should work like this:
Enter a positive
integer: 8
The sum 1 + ... + 8
equals 36.
As usual, your program should work for any legal
input!
Hint: As a child prodigy, the great mathematician Gauss figured out
the math needed here. The sum from 1 to N equals N times (N+1) divided
by 2. So, for example, the sum from 1 to 8 equals 8 * 9 / 2, or 72/2,
which is 36. Check that this equals 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8.
It does!
Write a program, FeetAndInches.java, that reads in a
person's height in inches and prints out the same height in feet and inches.
It should work like this:
Enter a person's
height in inches: 74
74 inches
equals 6 feet and 2 inches.
As usual, your program should work for
any legal input!
Fabric must be purchased in whole yards. So if you
require 24 inches (2 feet) of fabric, you still must purchase one yard,
leaving a foot of scrap left over. Write a program, FabricScrap.java,
that reads in the number of inches of fabric that are required, and prints
out both the number of yards that must be purchased and the number of inches
of scrap that will be left over. It should work like this:
Enter the inches
of fabric required: 74
74 inches
equals 6 feet and 2 inches, requiring 3 yards of fabric, with 34 inches of
scrap left over.
As usual, your program should work for any legal input!