## CPSC 120 -- Test #2 Review

### Topics

• Machine Representation of Integers (Class notes, handout on twos complement, Lab 5 Prelab and Lab, exercise handouts, Section 2.3 and Appendix B of the text)
• Be able to convert a number in any base to its equivalent base 10 representation.
• Be able to convert a base 10 number to any other base.
• Be able to convert among bases 2 (binary), 8 (octal), and 16 (hexadecimal) without using base 10.
• Be able to find the n-bit (where n is specified) twos complement representation of a base 10 integer (positive or negative).
• Be able to find the base 10 value of a number represented in twos complement.
• Be able to find the largest (and smallest) base 10 unsigned number that can be stored in n-bits; be able to find the largest (and smallest) base 10 signed twos complement number that can be stored in n-bits.
• Be able to add base 2 and twos complement numbers; understand the problem of overflow and be able to demonstrate it.

• Objects and Classes (Sections 4.0-4.5, Labs 5, 6, and 7, class notes and handouts)
• Understand the difference between objects and primitive values, including the difference in how they are stored (directly vs. references).
• Be able to create an object given the type signature of one or more constructors of the appropriate class.
• Be able to use an object appropriately given the type signatures and reasonable documentation for its methods.
• Understand the meaning of the public and private modifiers and when each is appropriate.
• Understand the meaning of encapsulation and how visibility modifiers are used to enforce it.
• Understand the difference between instance data (variables) and local data (variables.
• Know what a constructor is and how a constructor differs from other methods (when a constructor is executed and other differences).
• Be able to write a class given a description of the capabilities required of it.

• Methods (Sections 4.2-4.4, 5.0 - 5.1, Labs 9 and 10, class notes and handouts)
• Be able to write a method to perform a given task.
• Understand all components of a method's signature -- public/private, static/instance, return type, method name, formal parameters.
• Understand what it means for a method to return a value, and be able to both write and use such methods. In particular, understand the difference between a method that has void return type and one that returns a value.
• Understand the difference between formal and actual parameters.
• Be able to write methods (including main) that call other methods as appropriate. Understand the difference between calling a method from a client program (when you must use the dot operator on an object, or in the case of a static method, on the class name) and calling a method from the class the method is defined in (when you just use the method name along with its parameter list).
• Understand the purpose of a toString method - what should such a method do and how is the method used.

• Selection Statements (if ... else ...) and Boolean Expressions (Sections 5.1 - 5.4 of the text plus labs and prelabs)
• Know how to write if ... else ... statements in their various forms (with no else clause, nested, cascading)
• Know how to write and evaluate boolean expressions using relational operators (such as == and <=) and boolean operators (&&, ||, and !); know that the relational operators such as == work only on primitive data not on objects (to compare objects you must use operators - equals or compareTo).
• Know about the boolean data type -- how to declare a boolean variable and assign the result of a boolean expression to the variable.
• Know how to write a method with a boolean return type.
• Know how to use the equals method of the String class to compare String objects.
• Know that the switch statement is another selection statement that can be used when your selection condition is to test to see if a single variable is equal to one of several different possible values (the cases in the switch). Understand the role of the break statement in the switch -- without it once a condition is true all remaining statements in the switch would be executed rather than just the one where the condition is true. You will not have to write a switch statement on the test but you may be asked some general question or given a switch statement and asked something about it.

• Logic (Handouts)
• Know the logic operators AND, OR, and NOT and both the mathematical and Java symbols for these.
• Be able to construct truth tables for boolean expressions (propositions/ logic statements).
• Know the basic laws of logic (DeMorgan's laws, distributive law, associative law, commutative law, idempotent law, etc.).
• Be able to show two boolean expressions are equivalent using truth tables and the laws of logic.
• Understand the connection between logic and computer circuits -- be able to write a logic statement equivalent to a given circuit, be able to simplify the statement using the laws of logic and then draw the simpler circuit, be able to construct circuits given either a truth table for the output or a verbal description of the output of the circuit.

### Some Additional Practice Exercises

1. Self-Review questions 4.1, 4.2, 4.4 - 4.9 on pages 194-5 (answers are provided on pages 197-199) plus exercise 4.1; Self-Review questions 5.1, 5.3, 5.6 - 5.8 on page 274 plus exercises 5.1 - 5.4, 5.26, 5.28, 5.30.

2. Write the Card class and associated program described in Programming Project 4.3 in the text (page 196). The Card class should include a toString method to print Card objects, and accessor methods for the suit and the face in addition to a constructor.

3. Write a segment of code that takes an integer between 0 and 15 and assigns the character variable hexDigit the corresponding hex digit (as a character -- so if the integer is 1 hexDigit will be '1'; if the integer is 11 hexDigit will be 'B'). You should use a cascading if ... else... statement (you could also try a switch).

4. Trace the execution of the following segment of code and determine what is printed for each of the following values of answer: (a) 3; (b) 0; (c) 8; (d) 1 (NOTE: This requires a very careful trace because it doesn't quite do what expected due to missing braces.)
```
System.out.print("Enter a number: ");
if (answer < 5)
if (answer == 1)
System.out.println ("Hello");
if (answer = 0)
System.out.println("There");
else
System.out.println("You");
if (answer > 2)
System.out.println("What's");
else
System.out.println("Up");

```
5. Is the following if equivalent to the if in the MinOfThree.java program on page 219 of the text? (NOTE: Exercise 5.1 also asks about that program.)
```          if (num1 < num2 && num1 < num3)
min = num1;
else if (num2 < num1 && num2 < num3)
min = num2;
else
min = num3;
```

6. Assume the variable seconds has been declared to be of type int and a value has been assigned to it. Write a declaration of a variable named validSeconds of type boolean and assign to this variable a boolean expression that determines whether or not seconds contains a valid number of seconds (a number between 0 and 59 inclusive).

7. Assume the variable yearsExperience (type int) contains the number of years computer experience of a person and the variable passed120 (type boolean) is true if the person passed CPSC 120 at Roanoke College. Assume a person is eligible for a computer related job if they have at least 5 years computer experience or they passed CPSC 120. (a) Write a declaration of a boolean variable eligible and assign it the boolean expression that is true if the person is eligible for the job. (DON'T use IFs!!) (b) Write a boolean expression that would be true if the person is NOT eligible for the job -- write this two different ways (one way with ANDS and one with ORS -- and of course some NOTS).