/* ShortCodeExamples.java * Anne DeFrance */ //imports import java.awt.Rectangle; /** * Contains 30 small code snippets for a review of Java syntax, variables, * and expressions */ public class ShortCodeExamples { /** * main method with the 30 code snippets. Execute main after commenting * out snippets that have syntax errors to see the results. * @param args not used */ public static void main(String[] args) { /* For each of the following snippets of code answer these 3 questions. * 1. Record what you expect the output to be. * Do this before you run the program. * 2. Run the program and record what the output actually was. * 3. Provide a brief explanation 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. */ // 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 // What is the reason this does not give the specific answer? // You do not need to get into the specifics of this particular case. double a11 = 11.00; double b11 = 10.09; double c11 = a11 - b11; System.out.println(c11); // Number 12 // What is the reason this does not give the specific answer? // You do not need to get into the specifics of this particular case. double a12 = 1000000000; double b12 = 0.00000001; double c12 = a12 - b12; System.out.println(c12); c12 = 0; for(int i = 0; i < 1000000000; i++) c12 += b12; System.out.println(c12); c12 = a12; for(int i = 0; i < 1000000000; i++) c12 += 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 = 3; x15++; ++x15; x15 = x15 + 1; x15 += 1; System.out.println(x15); // Number 16 int x16 = 4; x16++; // Okay System.out.println(x16); ++x16; // Okay System.out.println(x16); int x16_1 = 3; int x16_2 = 3; x16 = x16_1+++x16_2; //Absolutely horrible style. System.out.println( x16 + " " + x16_1 + " " + x16_2 ); // Number 17 int x17 = 5; System.out.println(++x17); System.out.println(x17++); 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); // 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() ); System.out.println( box26 ); // 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); // 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 /** * a "toy code" method to illustrate behaviors of methods and parameters */ 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) /** * a "toy code" method to illustrate behaviors of methods and parameters * @param a != 0 */ 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) /** * a "toy code" method to illustrate behaviors of methods and parameters * @param x != 0 */ 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 /** * a "toy code" method to illustrate behaviors of methods and parameters * @param b != 0 * @param C != 0 */ 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 ShortCodeExamples