import java.awt.Rectangle; /* Ben Ni Unique Number: 51860 Section ID: 18 Assignment 1 */ /** * Simple class for Assignment 1. Testing of expressions and simple * Java mechanics. * * @author(of main only) Mike Scott * @version 8/26/02 */ public class Test { // Complete the following two methods /* pre: source != null, target != null post: prints out number of comparisons to standard output and all indices in source that are the starting point for the String target. */ public static void matches(String source, String target) { boolean isMatch = true; int matchCount = 0; int compCount = 0; String indices = ""; for (int i = 0; i < source.length() - target.length(); i++) { int temp = i; int j = 0; while ( isMatch && j < target.length() ) { if ( target.charAt(j) == source.charAt(temp) ) isMatch = true; j++; temp++; compCount++; } if ( isMatch ) { matchCount++; indices += temp + " "; } else i += (temp - i); } System.out.println ( "Number of comparisons when searching for \"" + target + "\" in \"" + source + "\" was " + compCount ); } /* pre: limit >= 2 post: all primes n, such that 2 <= n <= limit are printed to standard output */ public static void findAndPrintPrimes(int limit) { } 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. */ // Number 1 int x1 = 17 / 5 + 2; System.out.println(x1); // Number 2 int x2 = 31 % 5 + 31 % 6; System.out.println(x2); // 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); // 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. System.out.println(x16); // Number 17 int x17 = 5; x17 = ++x17 + x17; // Again terrible style which you should not copy. 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 = (int)a20; System.out.println(x20); // 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); /* 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] ); // Number 25 int[] list25 = {4, 4, 11, 0, 3}; System.out.println( list25[ list25[ list25[2] % list25[1] ] ] ); // Number 26 Rectangle box26 = new Rectangle(); box26.setSize(25, 75); System.out.println( box26.toString() ); // Number 27 // remember, predict the result BEFORE running the code! 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); } /* * Last digit of SSN: 8 * Joke from class newgroup corresponding * with last digit of yoru SSN: * 8: The best part about computers is that they make very fast accurate mistakes. * * What is your USL's name? Ian Mosher * What is your USL's email address? imosher@cs.utexas.edu * * Write the predicted answer, actual answer, and explanation * for answer for the 27 questions above: 1. Expected answer: 5 Actual answer : 5 Explanation : 17 / 5 is an integer division, resulting in 3. 3 + 2 = 5. 2. Expected answer: 2 Actual answer : 2 Explanation : 31 % 5 = 1 and 31 % 6 = 1. 1 + 1 = 2. 3. Expected answer: 5.4 Actual answer : 5.4 Explanation : 17.0 / 5.0 is a floating-point division, resulting in 3.4. 3.4 + 2.0 = 5.4. 4. Expected answer: 5.4 Actual answer : 5.4 Explanation : 17 / 5.0 is a floating-point division, where 17 is cast to double, resulting in 3.4. 5. Expected answer: 5.0 Actual answer : 5.0 Explanation : 17 / 5 is an integer division, resulting in 3. 3 + 2.0 = 5.0, where the 3 is automatically cast to double. 6. Expected answer: 5.4 Actual answer : 5.4 Explanation : 17 / 5.0 = 3.4. 3.4 + 2 = 5.4, where the 2 is cast to double. 7. Expected answer: 2.0 Actual answer : 2.0 Explanation : 3 / 2 = 1 and 6 / 5 = 1. 1 + 1 = 2.0, where the total is cast to double. 8. Expected answer: 0.67 Actual answer : 0.666666666666666 Explanation : 5 / 3 = 1. 1 * 2.0 = 2.0, where 1 is cast to double. 2.0 / 3 = 0.67, where the 3 is cast to double. 9. Expected answer: 2.0 Actual answer : 2.0 Explanation : The numbers are cast to int in order to perform the modulus operation, 5 % 3 = 2, and cast back to double. 10. Expected answer: -2147483648 Actual answer : -2147483648 Explanation : 2147483647 is the max value for an int, so when 1 is added, it "wraps around" to the negative end of the range. 11. Expected answer: 0.91 Actual answer : 0.9100000000000001 Explanation : No idea. 12. Expected answer: 999999999.99999999 Actual answer : 1.0E9 Explanation : c12 is rounded off to the nearest whole number. 13. Expected answer: 16 Actual answer : 16 Explanation : 3 * 4 = 12. 4 += 12 = 16. THe expression on the right side of += is evaluated first 14. Expected answer: 24 Actual answer : 24 Explanation : 3 + 5 = 8. 3 *= 8 = 24. 15. Expected answer: 24 Actual answer : 24 Explanation : 5 + 3 = 8. 3 *= 8 = 24. 16. Expected answer: 13 Actual answer : 13 Explanation : 4++ = 5 and ++5 = 6. x16 = 6 + ++6 = 13. The + sign takes precedence over the ++ increment, thus the ++ is assumed to be a preincrement. 17. Expected answer: 12 Actual answer : 12 Explanation : x17 = ++5 + 6 = 6 + 6 = 12, where ++5 is preincremented, raising the value permanently before the second call. 18. Expected answer: Syntax error Actual answer : Syntax error Explanation : Java automatically weeds out single '='s in if statements. 19. Expected answer: False True True Actual answer : False True True Explanation : Object variables are pointers, and box191 and box192 have different memory addresses, thus ( box191 == box192 ) is false. 20. Expected answer: Syntax error Actual answer : Syntax error Explanation : There is loss of information associated with assigning a double to an int. The compiler does not let you do that. 21. Expected answer: 12.0 Actual answer : 12.0 Explanation : Doubles can take int values just fine. 22. Expected answer: -1 Actual answer : -1 Explanation : When a number is cast from double to int, the numbers after the decimal point are truncated, thus -1.9 becomes -1. 23. Expected answer: 2.0 2.0 There is no syntax error because java.lang is automatically loaded. Explanation : Math.rint() rounds a number to the nearest integer, and when it is equally close to both the upper and lower integer, it rounds to the even one. 24. Expected answer: -49 Runtime error Actual answer : -49 Runtime error Explanation : 7 * 7 * (-1)^7 = -49. System.out.println( list24[10] ); caused an array out of bounds exception, because the highest element in a 10 element array is 9. 25. Expected answer: 4 Actual answer : 4 Explanation : list25[2] = 11, list25[1] = 4, and 11 % 4 = 3. list25[3] = 0, and list25[0] = 4. 26. Expected answer: Syntax error Actual answer : Syntax error Explanation : box26 was not initialized as a new Rectangle with the new modifier. 27. Expected answer: s261: ght is coming." culture. s262: work Actual answer : s261: ght is coming." culture. s262: work Explanation : substring() with one parameter takes the part of another string from that point on. substring() with two parameters takes the part of another string between those two points. */ }