Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dk.norfeldt.lasse.assignment2;
- /**
- *
- * @author Norfeldt <lasse@norfeldt.dk>
- */
- import java.util.Scanner;
- public class TestVars {
- public void runMe() {
- //Part 2
- /*2. In method runMe() defines three integer variables x, y and z and
- set them to the initial values of 0, 6 and 10, respectively.
- */
- int x = 0, y = 6, z = 10;
- //2a. Print out the value of z as “z = 10”.
- System.out.println("z = "+z);
- /*2b. Add the following code “z++;” and try to figure out the value
- of z before you print it out in Java.
- Then print it out to check if you assumption was correct.
- */
- z++; //Expect it to be the same as "z = z + 1;" so it prints out 11.
- System.out.println("z after z++; = "+z);
- /*2c. Set the value of z back to 10 and add this line of code “++z;”.
- Then try to figure out the value of z before you print it out in Java.
- Then print it out to check if you assumption was correct.
- */
- z = 10;
- ++z; /*Expect it to be the same because it is still just one variable
- * that the increment operator is beeing used on.
- */
- System.out.println("\"z = 10;\" and \"++z;\" gives "+z);
- /*2d. Define two int variables x and y and initialize them as x=0 and
- y=6”. Then add the following code “x = ++y;” and try to figure out the
- values before printing them out in Java
- */
- x = ++y; /*Expect it to do the incremation first (so y is assiged to
- the value 6 + 1) and then assign that value to the
- variable x. This will give a print out that says
- x = 7 and y = 7
- */
- System.out.println("x = "+x);
- System.out.println("y = "+y);
- /* 2e. Now set x and y back to their original values 0 and 6,
- respectively. Add the following line of code “x = y++”. Do the same
- as in 2d.
- */
- x = 0; y = 6;
- x = y++; /*Expect it to do assign the value of y to x and THEN do the
- * incremation of y. This means that the print out will be
- * x = 6 and y = 7
- */
- System.out.println("x = "+x);
- System.out.println("y = "+y);
- /*Was expecting this because in the prefix form, the operand is
- incremented (or decremented) before the value is obtained for use in the
- expression. In postfix form, the previous value is obtained for use in
- the expression, and THEN the operand is incremented (or decremented).
- */
- /*2f. Add the lines of code you see below and explain the semantic
- difference between the + operators in the System.out.println() methods.
- Afterwards remove the blank space after the first + in the first
- System.out.println() method and see what happens.
- z = 6;
- System.out.println("z : " + ++ z);
- z = 6;
- System.out.println("z: " +z++);
- */
- z = 6;
- System.out.println("z : " + ++ z);
- //System.out.println("z : "+++z);
- /*Gives a syntax error because the operator + requeres a variable and
- * does not work on an int - the ++ operator always return integer value.
- */
- z = 6;
- System.out.println("z: " +z++);
- /*2g. Set x and y back to their original values, i.e. 0 and 6,
- respectively. Then print out the result of the division y/x.
- */
- x = 0; y = 6;
- //System.out.println(y/x);
- // It will not print it out. Because division by zero cannot be done.
- /* 2i. Declare two new float variables float_x and float_y and assign
- them the values 14 and 4, respectively. Then print out the result of
- the division float_x / float_y and the result of the rest of that
- division, if possible*/
- float float_x = 14;
- float float_y = 4;
- System.out.println(float_x/float_y);
- /* 3. Before testing out the following line in Java, please try to think
- whether it will generate a compile-error or not:
- */
- // Have no idea of what
- String myStrangeString = + 1 + - - + - - + + + + + 1 + " ";
- System.out.println(myStrangeString);
- /*4. You should know by now that System.out.println(whatever) can be
- used to print out variables or literals or a combination thereof.
- What you might don’t know is however, how you can read in data: here
- you will learn one way for doing so. You will make use of the class
- Scanner that you have to import into your java code by adding this line
- before the class declaration line of TestVars i.e. like:
- */
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter your age: ");
- int age = sc.nextInt();
- System.out.println("So, you are " + age + " years old");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement