review sheet

profilesniper87
exam2_review.pdf

CS1160 Exam #1 Review Spring 2014

The exam will be two parts. You will have 1 hour and 50 minutes to complete the exam. The first part (30 points) is 30 multiple choice questions on PILOT for a maximum of 60 minutes, although students are encouraged to complete the multiple choice portion within 40 minutes to have more time to complete the second portion.

The second part (70 points) is programming and will be based on 1-5 problems shown on the next page. Portion 2 of the exam requires Microsoft Word, and/or Netbeans for submitting on PILOT.

General Types of Exercises for Multiple Choice and Programming 1) What is this program or method doing in plain English? 2) What will get printed? 3) What will the result be? 4) Wrong's wrong? How can it be fixed? 5) Create a program with methods to solve a particular type of problem. Chapter 4 (Sections 4-9, Project #2) 1) Be able to solve a problem requiring a nested loop. 2) Perform a simulation using random numbers. 3) Create a loop that uses a sentinel value to stop. 4) Make sure input is valid.

Chapter 5 (Sections 1-8, Project #2) 1) Be able to implement a given method including from the following known classes: Math (max, min, abs, random, pow), String (length, substring, indexOf, charAt) 2) Be able to mimic a method from a known class Math(min, max, pow, abs). 3) Be able to create and implement a method given a particular header. 4) Be able to correctly call a method, get a value and use that value from the main method. 5) Be able to correctly identify the scope of a variable.

Sample Problems Variations of 3 to 5 of the following problems below will appear on your exam: Be able to do hand tracing, input validation (when asked), pseudocode and Java (include comments indicating your steps minimally 1) Getting input (specifying type of input), 2) Processing (specifying type of processing), 3) Displaying output

1. Create the methods and show the results after implementing the following main method:

public static void main(String[] args){ // CONSTANTS final int L_NUM = 1; final int H_NUM = 500;

// VARIABLE DECLARATIONS int mOdds;

// PROCESSING mOdds = multOdds(H_NUM);

// OUTPUT squares(L_NUM, H_NUM); System.out.println(“mOdds result with “ + H_NUM +” :” + mOdds);

}

/** * Multiplies a number of odd numbers. * Sample call from another method: * int mOdds = multOdds(3); * Result: mOdds = 1 * 3 * 5 = 15; * * @param numOdds number of odd numbers to multiply * @return multiplication of the odd numbers */ public static int multOdds(int numOdds)

/** * Print a range of square numbers on the same line. (Prints a newline at the end.) * Sample call from another method: * squares (100, 300); * Result: 100, 121, 169, 196, 225, 256, 289 * * @param lowNum lower number in range, inclusive * @param highNum higher number in range, inclusive */ public static void squares(int lowNum, int highNum)

2. Create a program that displays the average score (with 2 decimal places) for each student in a class. The number of students is not known ahead of time. The teacher will enter the word “END” by itself on a line to indicate when done. The teacher will enter the following for each student from the keyboard: the student's first name, the student's last name, and 3 scores.

The program will print each student last name first, the average score for each student, followed by a summary rating for the class. An average class score 90+ is excellent, 80+ is above average, 70+ is average, 60+ is below average, <60 is poor.

Sample Input: John Doe 45.2 90.7 88 Jane Smith 76.3 80 98.7 END

Sample Output: Doe, John – Average Score 74.63 Smith, Jane – Average Score 85.00

For a class of 2 students, the overall average of the average scores is 79.81. This is AVERAGE.

3. Create a program that simulates randomly flipping a six-sided die a number of times specified by the user. Display the dice and total. (Create and implement methods called flipDie and displayDie.)

Sample run: Enter the number of times to flip the die: 2

---- | 3 | ---- ---- | 5 | ----

Your 2 dice resulted in a total of 8.

4. What gets printed from this program?

public static void main(String[] args){ int num = 5; method2(num); num = method1(num); System.out.println(“Main: “+ num); }

public static int method1(int sillyNum){ System.out.println(“Method 1”); sillyNum = sillyNum + 2; method2(sillyNum); return sillyNum; }

public static void method2(int num){ System.out.println(“Method 2”); num = num * 3; System.out.println(num); }

5. Create a program that gets two non-negative integers (>= 0) from the user (on 2 different lines). Then display the product of those numbers. The program should continue asking until the user provides valid input.

6. Write a program that has a user create and validate a new password, following at least 3 of the 4 rules:

The password must be at least 8 characters long. The password must have at least one uppercase and one lowercase letter The password must have at least one digit. The password must contain at least one of the following special characters: @!* Write a program that asks for a password, then asks again to confirm it. If the passwords don't match or the rules are not fulfilled, prompt again. Your program should include a method that checks whether a password is valid.

7. What does this method do in plain English?

public static char method1(char firstChar, char secondChar){ // Parameters are uppercase letters char someChar = (char)((int)(Math.random() * (secondChar – firstChar + 1)) + firstChar); return someChar; }

8. Create your own version of the Math.abs to get the absolute value of any number.

9. Create a program that displays a randomly generated Tic Tac Toe board. (There may be many more X's than O's or vice versa.)

Sample output: X O O O X O O X O

10. Create a program that gets a word or phrase from the user and then makes calls to methods to display the first and last characters both in uppercase. Create a method called firstChar that returns the first character of a word or phrase and a method called lastChar that returns the last character of a word or phrase. Then implement those methods in the main method. Note: the firstChar and lastChar methods should not change the case of any strings to uppercase. Do that from the main method.

Sample user input: CS1160 Intro to Programming Sample output: CG