Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.12 KB | None | 0 0
  1. package dk.norfeldt.lasse.assignment2;
  2.  
  3. /**
  4.  *
  5.  * @author Norfeldt <lasse@norfeldt.dk>
  6.  */
  7. import java.util.Scanner;
  8.  
  9. public class TestVars {
  10.  
  11.     public void runMe() {
  12.         //Part 2
  13.     /*2. In method runMe() defines three integer variables x, y and z and
  14.          set them to the initial values of 0, 6 and 10, respectively.
  15.         */
  16.         int x = 0, y = 6, z = 10;
  17.  
  18.         //2a. Print out the value of z as “z = 10”.
  19.         System.out.println("z = "+z);
  20.  
  21.         /*2b. Add the following code “z++;” and try to figure out the value
  22.          of z before you print it out in Java.
  23.          Then print it out to check if you assumption was correct.
  24.          */
  25.         z++;    //Expect it to be the same as "z = z + 1;" so it prints out 11.
  26.         System.out.println("z after z++; = "+z);
  27.  
  28.         /*2c. Set the value of z back to 10 and add this line of code “++z;”.
  29.          Then try to figure out the value of z before you print it out in Java.
  30.          Then print it out to check if you assumption was correct.
  31.          */
  32.         z = 10;
  33.         ++z;    /*Expect it to be the same because it is still just one variable
  34.                  * that the increment operator is beeing used on.
  35.                  */
  36.         System.out.println("\"z = 10;\" and \"++z;\" gives "+z);
  37.        
  38.         /*2d. Define two int variables x and y and initialize them as x=0  and  
  39.          y=6”. Then add the following code “x = ++y;” and try to figure out the
  40.          values before printing them out in Java
  41.         */
  42.         x = ++y;    /*Expect it to do the incremation first (so y is assiged to
  43.                       the value 6 + 1) and then assign that value to the
  44.                       variable x. This will give a print out that says
  45.                       x = 7 and y = 7
  46.                     */
  47.         System.out.println("x = "+x);
  48.         System.out.println("y = "+y);
  49.  
  50.         /* 2e. Now set x and y back to their original values 0 and 6,
  51.          respectively. Add the following line of code “x = y++”. Do the same
  52.          as in 2d.
  53.          */
  54.         x = 0; y = 6;
  55.  
  56.         x = y++;    /*Expect it to do assign the value of y to x and THEN do the
  57.                      * incremation of y. This means that the print out will be
  58.                      * x = 6 and y = 7
  59.                      */
  60.        
  61.         System.out.println("x = "+x);
  62.         System.out.println("y = "+y);
  63.         /*Was expecting this because in the prefix form, the operand is
  64.          incremented (or decremented) before the value is obtained for use in the
  65.          expression. In postfix form, the previous value is obtained for use in
  66.          the expression, and THEN the operand is incremented (or decremented).
  67.         */
  68.  
  69.         /*2f. Add the lines of code you see below and explain the semantic
  70.          difference between the + operators in the System.out.println() methods.
  71.          Afterwards remove the blank space after the first + in the first
  72.          System.out.println() method and see what happens.
  73.                 z = 6;
  74.                 System.out.println("z : " + ++ z);
  75.                 z = 6;
  76.                 System.out.println("z: " +z++);
  77.          */
  78.         z = 6;
  79.         System.out.println("z : " + ++ z);
  80.  
  81.         //System.out.println("z : "+++z);
  82.         /*Gives a syntax error because the operator + requeres a variable and
  83.          * does not work on an int - the ++ operator always return integer value.
  84.          */
  85.  
  86.         z = 6;
  87.         System.out.println("z: " +z++);
  88.  
  89.         /*2g. Set x and y back to their original values, i.e. 0 and 6,
  90.          respectively. Then print out the result of the division y/x.
  91.          */
  92.         x = 0; y = 6;
  93.         //System.out.println(y/x);
  94.         // It will not print it out. Because division by zero cannot be done.
  95.  
  96.         /* 2i. Declare two new float variables float_x and float_y and assign
  97.          them the values 14 and 4, respectively. Then print out the result of
  98.          the division float_x / float_y and the result of the rest of that
  99.          division, if possible*/
  100.         float float_x = 14;
  101.         float float_y = 4;
  102.         System.out.println(float_x/float_y);
  103.  
  104.         /* 3. Before testing out the following line in Java, please try to think
  105.          whether it will generate a compile-error or not:
  106.          */
  107.         // Have no idea of what
  108.         String myStrangeString = + 1 + - - + - - + + + + +  1 + " ";
  109.         System.out.println(myStrangeString);
  110.  
  111.         /*4. You should know by now that System.out.println(whatever) can be
  112.          used to print out variables or literals or a combination thereof.
  113.          What you might don’t know is however, how you can read in data: here
  114.          you will learn one way for doing so. You will make use of the class
  115.          Scanner that you have to import into your java code by adding this line
  116.          before the class declaration line of TestVars i.e. like:
  117.         */
  118.         Scanner sc = new Scanner(System.in);
  119.         System.out.print("Enter your age: ");
  120.         int age = sc.nextInt();
  121.         System.out.println("So, you are " + age + " years old");
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement