Discussion Section Handout 1 - Java Basics

This is the first discussion section handout. (DiSH. Being in the military for 8 years gave me a love for acronyms.) These will normally be handed out on Fridays. The DiSHes serve many purposes. 

  1. Give you more practice in the topics the course covers.

  2. Serve as a guide for discussion sections, although there is far more material here than can be covered in 30 minutes (10 minutes of discussion section for quiz, 10 for questions on assignment, 30 for some DiSH problems.)

  3. Give you material to work on in preparation of exams. The best way to prepare for exams is by being active. Doing problems instead of looking at your notes. 

It is okay share work on the DiSHes. 

The goal of discussion section this week is  to get more practice with basic Java syntax and problem solving.

1.    Create a method that prints the representation of its integer parameter as a Roman numeral.  Thus, if the parameter is 1998, the output is MCMXCVIII.

2. Write a method that accepts two arguments, an array of integers, and a target number. The method shall print out all combinations of exactly three values in the array that when added together equal the target number. Print out the index and values for each combination. Consider this example. The array consists of the following values {1, 3, 5, 8, 10, -2, 5, 0} and the target is 13.

Target: 13
Combinations:
Indexes: 1 2 6 Values: 3 5 5
Indexes: 1 4 7 Values: 3 10 0
Indexes: 2 3 7 Values: 5 8 0
Indexes: 2 4 5 Values: 5 10 -2
Indexes: 3 6 7 Values: 8 5 0
Indexes: 4 5 6 Values: 10 -2 5

3. Write a method to determine the number of punctuation marks in a String.

4. A checksum is the 32-bit integer that is the sum of all Unicode characters in a String or file (we allow silent overflow ). Two identical Strings or files have the same checksum. Write a program to compute the checksum of a String that is supplied as a parameter. What number of ASCII characters would it take to cause an overflow in the checksum value?

5. Write a method that simulates picking numbers for a lottery. The possible numbers in the lottery are between 1 and N, a parameter to the method. The method shall pick S, another parameter to the method, numbers from the range 1 to N. No number may be picked more than once for the given set of numbers S. What is the required relationship between N and S so the method will always be able to find a solution? The method should return an array if ints with a length of S, containing the numbers picked. The first number picked will stored in the element with index 0, the second number picked will be stored in the element with index 1 and so forth.

You may use the Random class from the Java standard library. Random is contained in the java.util package.

6. Write out one tenth (1/10) in base 10. Now do it in base 2.

7. The following problems are from Rich Pattis, a lecturer at CMU. I thank him for the permission to use them here:

 Classify each of the following as a legal or illegal identifier. If it is illegal, propose a legal identifier that can take its place (a homophone (something that sounds the same but is spelled differently) or homoglyph (something that looks the same but is spelled differently))

packAge x12 2Lips
xOrY sum of squares %Raise
termInAte u235 $Bill
x_1

8. Classify each of the following numeric literals as int, or double, or illegal (neither); write the equivalent value of each double without using E notation; for each illegal literal, write a legal one with the "same" value.

5. 3.1415 17
17.0 1E3 1.E3
.5E-3 5.4x103 50E-1
1,024 0.087 .087

9. What is the difference between 5, 5., five, '5', and "5"? What is the difference between true and "true"?

10. Write a String literal that includes the characters I've said a million times, "Do not exaggerate!"

11. Choose an appropriate type to represent each of the following pieces of information

12. Write prototypes for each of the following methods. Infer the information needed in the prototype from each function's description.

13. What are the results of each of the the following operators?

    7/10   7./10   7/10.   7./10.
    7/10   57/10   157/10   2157/10
    7%10   57%10   157%10   2157%10
14. Show the result produced by each of the following operators/methods (or indicate that some syntax error is present). If the state of some variable is changed, show that too (and indicate any implicit conversions). Assume int x = 0; String s = "ABC"; before each call
  false == false          'a' < 'Z'          'a'-'A'
  true  != true           true && false      true || false
  x++                     ++x                1 = x
  x = 1                   x == 1             x = 'A'
  Math.abs(-3.5)          Math.abs(3.5)      Math.max(3.5, 8)
  Math.sqrt(-1.)          Math.pow(-3.,2.)   Math.pow(-3.,.5)

  s = System.out.println("Hi");

15. What is printed in each of the following method calls.

  System.out.println("" + 1 + 1 + 1);
  System.out.println(1 + 1 + 1 + "");
  System.out.println('1' + 1 + "");

16. Assume that we declare double a,b; evaluate the expression (a+b - Math.abs(a-b))/2. when

Try a few other example values for a and b. Describe in general terms what this expression evaluates to.

17. Translate each of the following mathematical formulas (7 on the top line, 3 on the middle and bottom lines) into Java expressions. Assume that all variables (some have subscripts) are declared to be of type double. Writing |x| means the absolute value of x.

18. Suppose that we declare int attendance = 3000, capacity=10000; the number of fans attending an event at a stadium and the maximum number of fans possible at that stadium respectively. Which of the following Java expressions evaluates to 30, the percentage of fans in the stadium? What do the "incorrect" expressions evaluate to?

        attendance/capacity
        100*attendance/capacity
        100*(attendance/capacity)
        attendance/capacity*100        

19. Assume that we declare int x; Write an expression whose result is X rounded to the nearest 10: e.g., if x were 1432 the result would be 1430; if x were 1437 the result would be 1440; if x were 1435 round up so the result would be 1440 also. Use only the standard arithmetic operators and casting.

20. Assume that we declare double x; Write an expression whose result is the int value closest to x: e.g., if x were 3.2 the result would be 3; if x were 3.9 the result would be 4; if x were 3.5 round up so the result would be 4 also. Just casting doesn't work, because of truncation: 3.9 would be cast to 3. (no if statements)

21. Assume that we declare int x,y,z; Write an expression that computes whether. (no if statements)..

Assume that zero is a positive number. Write an expression that computes whether... Write an expression that computes whether...

22. Assume that we specify two points in the place with the declaration double x1,y1, x2,y2; Write an expression that computes...

23. Assume that specify an interval by a pair of int values (the ones at the beginning and end of the interval in the place with the declaration): 5 and 8 would specify the interval containing the numbers 5, 6, 7, and 8 inclusive. We declare int b1,e1, b2,e2; to represent the beginning and end of two intervals, and int x; so represent some value. Note that we will guarantee that the intervals are "well formed": b1 <= e1 and b2 <= e2.

Draw pictures to help you visualize the relationships; choose your relational and logical operators carefully, and try a few examples to convince yourself that your expressions are correct. For example, the following picture shows the first interval inside the second.

24.  Nitty-Gritty Java. If you just can't get enough here are some more syntactical snippets. I would not consider these very important for evaluation purposes, but some people just love to get into the language. 

Will method foo compile? Why or why not?

public int foo(int x)
{  int foo;
   foo = x;
   if(foo % 2 == 0)
      return 100;
   if(foo % 2 == 1)
      return 200;
}

What is the output of the following Java statements?

float f1 = 1.121223;
System.out.println(f1);

double d1 = 1.12f;
System.out.println(d1);

double d2 = 12345678901111l; // that's a lower case l at the end
System.out.println(d2);

int i1 = 4000000000;
System.out.println(i1);

int i2;
char c1 = 'I';
i2 = c1;
System.out.println(i2);

byte b1 = 125;
int i3 = b1 * 10;
System.out.println(i3);

byte b2;
int i4 = -1000;
b2 = (byte)i4;
System.out.println(b2);

boolean bo1 = 131 % 7 == 0;
int i5 = 12;
bo1 = i5;
System.out.println(bo1);

bo1 = (boolean)i5;
System.out.println(b01);

int i6 = 12;
if( i6 )
    System.out.println(i6);