### CoSTARS Worksheet for Note #4: More "int" Variables

Do the following problems by hand (without using a computer):

1. What will each of the following lines print?

1. System.out.println("12" + 3 + 4);

2. System.out.println("12" + (3 + 4));

3. System.out.println(12 + 3 + 4);

4. System.out.println(12 + "3" + 4);

5. System.out.println(12 + 3 + "4");

2. What will each of the following lines print?

1. System.out.println(13 / 3);

2. System.out.println(13 / -3);

3. System.out.println(3 / 13);

4. System.out.println(999 / 2);

5. System.out.println(129 / 10);

3. What will each of the following lines print?

1. System.out.println(13 % 3);

2. System.out.println(-13 % 3);

3. System.out.println(3 % 13);

4. System.out.println(999 % 2);

5. System.out.println(129 % 10);

4. The expression (x % 1) will always equal 0 for any integer value of x.  Explain.

5. 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.

6. 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.

7. * 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.

8. What will each of the following lines print?

1. System.out.println(1328538 % 1);

2. System.out.println(1328538 % 10);

3. System.out.println(1328538 % 100);

4. System.out.println(123 % 456);

5. System.out.println(123456 % 123450);

9. 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.

1. Explain why this works.

2. Provide a similar expression that will equal the hundred's digit of x for any positive integer x.

10. Write (by hand, without a computer!) a single Java statement that will result in overflow.

11. What will each of the following lines print?

1. System.out.println(1+2*3-4/5);

2. System.out.println(1+2/3*4-5);

3. System.out.println(1*2-3/4+5);

4. System.out.println(1-2+3*4/5);

5. System.out.println(1/2-3*4+5);

12. What will each of the following lines print?

1. System.out.println(5*4%3+2%1);

2. System.out.println(17%13%8);

3. System.out.println(17%(13%8));

4. System.out.println(17/13/8);

5. System.out.println(17/(13/8));

Do the following problems with a computer (and a Java compiler):

13. 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.");

14. 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.

15. 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!

16. 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!

17. 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!