Starting from:

$30

CPT120-Tutorial 2 Solved

1.                Create a new Eclipse project then a new Java class named TypeDemo inside that project.

2.                Now, inside the main method of your TypeDemo class, create 8 variables; one for each primitive data type. The names of the variables should be descriptive of the type of information. E.g. We store the age of something in a variable called ‘age’ as shown below:

int age;

I.e. do not name the variable a, b, etc. Use your imagination and try and give the variable a “purpose”. When you do it, your int variable must be named differently what’s shown in the example here. (Gayan will discuss what makes a bad variable name during the live lecture)

3.                Next, for variables of integer (whole number) data types (hint: there are four, not just int), assign the largest value that you are allowed to assign to each (refer to values given under Oracle Java Tutorials Primitive Data Types page for exact values). E.g: For int, the largest value is given as 231-1, we can then use a calculator to find out that this value is 2147483647 and hard-code it to the variable as follows:

age=2147483647;

or

int age=2147483647;

For variables of other data types that you create, assign compatible values for each. (Gayan will discuss how you can find out the maximum values for numerical data types without calculating them)

4.                Finally, compose a single String variable to contain a summary of the field/variable (one per line) names and their values and then display it (you may also use System.out… instead of JOptionPane...). E.g.

Of course, the above example only has the int field/variable and its value.

Yours must have all 8 and just use your imagination for the variable names!
Page 1 of 2

5.2: Continue to modify the TypeDemo class (from step 5) above as per the following instructions (there is no need to show/submit the version from 5.1 separately): After displaying the String (from exercise 1), increment the existing values of the number variables by 1. You can do this by saying either:

age+=1; or age++;

For the non-numerical variables (e.g. boolean, etc.), assign completely new new values.

Hint: When re-assigning values, we do not mention the data type (int, in this case) as we have already declared the variable with a data type before.

Finally, create a similar message to the one that you created at the end of exercise 1, create a second message that shows the new values, after the new values were re-assigned. E.g.  

Again, yours must show all 8 fields/variable names and their new values (and you can use System.out… instead). If you do this right, the integer values will now show negative values (Optional: How does a positive number suddenly become a negative one? 


In summary, after completion of exercise 2, your program should produce two outputs, showing “before” and “after” summaries of the variables and their contents.
Optional Exercise: You have seen the += and ++ operators (as in age+=1 or age++). Have you seen the rest of the Java operators? Which of these are important in everyday programming? What is operator precedence? 

More products