Data and Operators-Exercises
Data and Operators-Exercises
2) Define a Java variable named NewLine storing the new line character. Print it.
3) Declare two float variables (x and y) and assign them values 2.5 and 5.4 (with separated
declaration and initialization)
4) Declare two float variables (x and y) and assign them values 2.5 and 5.4 (declaration and
initialization in the same sentence)
b) System.out.println('ab');
c) System.out.println( (3 + 2) – 1) );
d) int a = 3.2;
e) float a = 2.1;
int c = a;
f) int i = 10;
float b;
i = b;
g) int x;
{
x = 10;
}
System.out.println(x);
h) boolean a, b;
a = false, b = true;
i) int x;
x = 1;
r = x + 1;
Arrays
6) Declare an array to store the following data, in such a way that the array mirrors the
structure of the data (initialization is not required):
E.g.: The names of the people living a building with 4 floors and 5 apartments (1 person
per apartment)
Operators
7) Find the value of the variable result after executing the following sentences:
int a;
int b;
int result;
a) a = 4;
b = 12;
result = a + b / 3;
b) a = 3;
a = a + 3;
b = 2;
result = a – b;
c) a = 2;
b = a + 1;
a = b + 2;
result = a + b + a;
result = -result;
d) a = 3;
b = 11;
result = (b % a) + 1;
e) a = 3;
b = a++;
result = 1;
result += a – b;
Programming Data and operators, Exercises
f) String s = “jjj”;
String t = “xxx”;
String result2 = s + s + t;
8) Write an application that displays a box, an oval, an arrow and a diamond using asterisks
(*), as follows:
9) Develop a Java program that reads a temperature value in Celsius degrees from the
keyboard and transforms it to Fahrenheit degrees. The program must print the two values
in the form: X Celsius degrees are Y Fahrenheit degrees.
(Remember: )
10) Develop a Java program to calculate the following statistical parameters of three values
read from the keyboard. Print the results.
( )
a) Mean:
( ) ( ) ( )
b) Variance:
c) Standard deviation: √
11) Develop a Java program to exchange the values of two integer variables (e.g.: if x is equal
to 10 and y is equal to 5, at the end of the program, x must be equal to 5 and y equals to
10). (Initialize the variables in the code; do not read from the keyboard.)
12) Using only the programming techniques you learned in this lesson, write an application
that calculates the squares and cubes of the numbers from 0 to 10 and prints the resulting
values in table format, as shown below.
9 81 729
10 10 1000
13) Develop a Java program that reads the three coefficients (a, b, c) of a 2nd grade polynomial
( ) and obtains and prints the values of x.
√
(Remember: )
14) Develop a Java program that, given an integer value representing a number of seconds,
transform it to an expression in hours, minutes, and seconds (e.g. 3680 seconds are 1
hour, 1 minute, and 20 seconds).