Topic 1 - Java Review Exercises and Worksheet

Total Page:16

File Type:pdf, Size:1020Kb

Topic 1 - Java Review Exercises and Worksheet

Topic 1 - Java Review Exercises and Worksheet

Short Answer Questions.

Go through Test.java. There are 30 code snippets in the program. The program does not compile right now due to syntax errors. For each code snippet write what you expect the output to be. Do this before you run the program. Then run the program and write what the output actually was, and explain the actual output. If there is a difference between you expected answer and the actual answer that is okay! Include in your explanation what you didn't understand about the code. If a snippet would result in a syntax or compile error then the expected result is "syntax error" or "compile error". If the code causes a syntax or runtime error explain why the error occurred. You do not have to explain how to correct the error.

For code that gives a compile or runtime error, simply comment out the entire snippet and run the rest of the program; do not attempt to correct the code.

Here is an example of a code snippet, expected and actual answer. (This one is a bit long winded. Your explanations may be more concise.) int x = 5; x = 2 + x + x; System.out.println(x);

/* expected answer: 12 actual answer: 12 explanation: The variable x is assigned 5. The expression 2 + x + x is evaluated. x holds the value 5 so after substitution the expression is 2 + 5 + 5 which evaluates to 12. 12 is assigned to the variable x. */ import java.awt.Rectangle; public class Test {

public static void main(String[] args) { /* For the following expressions answer 3 questions. 1. What do you expect the result (output) to be? 2. What is the actual result? 3. Explain the result. If the code causes a syntax or runtime error explain why the error occurred and how to correct the code so an error does not occur. */

// Number 1 int x1 = 17 / 5 + 2; System.out.println(x1);

// Number 2 int x2 = 31 % 5 + 31 % 6; System.out.println(x2);

Topic 1 - Java Review Exercises and Worksheet

1 Topic 1 - Java Review Exercises and Worksheet

// Number 3 double a3 = 17.0 / 5.0 + 2.0; System.out.println(a3);

// Number 4 double a4 = 17 / 5.0 + 2.0; System.out.println(a4);

// Number 5 double a5 = 17 / 5 + 2.0; System.out.println(a5);

// Number 6 double a6 = 17 / 5.0 + 2; System.out.println(a6);

// Number 7 double a7 = 3 / 2 + 6 / 5; System.out.println(a7);

// Number 8 double a8 = 5 / 3 * 2.0 / 3; System.out.println(a8);

// Number 9 double a9 = 5.0 % 3.0; System.out.println(a9);

// Number 10 int x10 = 2147483647; int y10 = 1; int z10 = x10 + y10; System.out.println(z10);

// Number 11 double a11 = 11.00; double b11 = 10.09; double c11 = a11 - b11; System.out.println(c11);

// Number 12 double a12 = 1000000000; double b12 = 0.00000001; double c12 = a12 - b12; System.out.println(c12);

// Number 13 int x13 = 3; int y13 = 4; y13 += x13 * y13; System.out.println(y13); Topic 1 - Java Review Exercises and Worksheet

2 Topic 1 - Java Review Exercises and Worksheet

// Number 14 int x14 = 5; int y14 = 3; y14 *= y14 + x14; System.out.println(y14);

// Number 15 int x15 = 5; int y15 = 3; y15 *= x15 + y15; System.out.println(y15);

// Number 16 int x16 = 4; x16++; // Okay ++x16; // Okay x16 = x16+++x16; // Absolutely horrible style. // This example shows why it is really bad idea to // embed the increment operator within more complex // expressions. System.out.println(x16);

// Number 17 int x17 = 5; x17 = ++x17 + x17; // Again terrible style which you should NOT emulate. System.out.println(x17);

// Number 18 int x18 = 5; if( x18 = 5) System.out.println("variable x18 equals 5");

// Number 19 int x19 = 12; int y19 = 6 * 2; Rectangle box191 = new Rectangle(200, 100, 5, 10); Rectangle box192 = new Rectangle(200, 100, 5, 10); boolean same191 = ( box191 == box192); boolean same192 = ( box191.equals(box192) ); boolean same193 = (x19 == y19); System.out.println(same191); System.out.println(same192); System.out.println(same193);

// Number 20 int x20 = 0; double a20 = 1.5; x20 = a20; System.out.println(x20); Topic 1 - Java Review Exercises and Worksheet

3 Topic 1 - Java Review Exercises and Worksheet

// Number 21 int x21 = 12; double a21 = x21; System.out.println(a21);

// Number 22 int x22 = 0; double a22 = -1.9; x22 = (int)a22; System.out.println(x22);

// Number 23 double a23 = 1.5; double b23 = Math.rint(a23); System.out.println(b23); double c23 = 2.5; double d23 = Math.rint(c23); System.out.println(d23);

/* Number 23. It is not important that you know what the Math.rint method does. Rather this question forces you to look up a method in the Java standard library documentation, an invaluable skill.

In addition to the answer to the standard 3 questions, answer the following for number 23. The Math class is used, but the statement import java.lang.Math; does not appear at the top of this program. Why doesn't this cause a syntax error? */

// Number 24 int x24 = 10; int[] list24 = new int[10]; for(int i = 0; i < list24.length; i++) list24[i] = i * i * (int)Math.pow(-1, i); System.out.println( list24[7] ); System.out.println( list24[10] );

// Number 25 int[] list25 = {4, 4, 11, 0, 3}; int result = list25[ list25[ list25[2] % list25[1] ] ]; System.out.println( result );

// Number 26 Rectangle box26; box26.setSize(25, 75); System.out.println( box26.toString() );

Topic 1 - Java Review Exercises and Worksheet

4 Topic 1 - Java Review Exercises and Worksheet

// Number 27 String s26 = "We are a \"work for the night " + " is coming.\" culture."; String s261 = s26.substring(25); String s262 = s26.substring(10, 15); System.out.println( "s261: " + s261); System.out.println( "s262: " + s262);

// Number 28. method mustang(int) is shown after main mustang(2, 3);

//Number 29. method mustang(int, int) is shown after main mustang(13);

//Number 30. method bobcat is shown after main bobcats(5, 2); } //end of method main

public static void mustang(int x, int y) { x = x + 3; y = y – x; System.out.println( x + " " + y); }//end of method mustang(int, int)

public static int mustang(int a) { int x = 3 * a; a = a % 7; System.out.println( a + " " + x ); return a; }//end of method mustang(int, int)

public static int hornedFrogs(int x) { int y = mustang( x ); System.out.println( x + " " + y ); mustang(x, y); System.out.println( x + " " + y ); return y + x; }//end of method hornedFrogs

public static void bobcats(int b, int c) { System.out.println(b + " " + c); int d = mustang(b); b = b – 3; c = hornedFrogs( c ); System.out.println(b + " " + c + " " + d); }// end of method bobcats

} // end of class Test

Topic 1 - Java Review Exercises and Worksheet

5 Topic 1 - Java Review Exercises and Worksheet

Programming Exercises.

1. Complete the following method. public int countPunctuationMarks(String s) { /* pre: s != null post: return the number of punctuation marks in s. The following characters are considered punctuation marks: . ? ! : ; - ( ) [ ] ' " , */

2. 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. Each number between 1 and N has a uniform probability of being picked. That is each number has the same chance of being picked.

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 of 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. public int[] pickNums(int N, int S) { /* pre: N > 0, S <= N post: returns an array of length S. The elements are unique values between 1 and N inclusive picked randomly. The elements are not sorted. */

Topic 1 - Java Review Exercises and Worksheet

6

Recommended publications