Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
53 views

Data and Operators-Exercises

This document discusses data types, variables, operators, and arrays in Java programming. It provides 14 exercises to practice these concepts, including writing programs to: 1) Declare and initialize different variable types, 2) Declare multi-dimensional arrays to represent various data structures, 3) Perform calculations using operators and order of operations, 4) Display shapes using asterisks, 5) Convert between Celsius and Fahrenheit temperatures, 6) Calculate statistical values from user input numbers, 7) Exchange the values of two variables, 8) Generate and print tables of squares and cubes, 9) Solve quadratic equations from user-input coefficients, and 10) Convert seconds to a formatted hours, minutes, seconds string.

Uploaded by

Subhra Khan 9C
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Data and Operators-Exercises

This document discusses data types, variables, operators, and arrays in Java programming. It provides 14 exercises to practice these concepts, including writing programs to: 1) Declare and initialize different variable types, 2) Declare multi-dimensional arrays to represent various data structures, 3) Perform calculations using operators and order of operations, 4) Display shapes using asterisks, 5) Convert between Celsius and Fahrenheit temperatures, 6) Calculate statistical values from user input numbers, 7) Exchange the values of two variables, 8) Generate and print tables of squares and cubes, 9) Solve quadratic equations from user-input coefficients, and 10) Convert seconds to a formatted hours, minutes, seconds string.

Uploaded by

Subhra Khan 9C
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming Data and operators, Exercises

Lesson 2. Data and Operators in Java

Data types and variables


1) Which data type would you use to represent the following values?
a) Child age
b) Employee salary
c) Whether somebody has children or not
d) Apartment letter

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)

5) Find the errors in the following Java sentences:


a) int a, b, c
a = 0

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;

j) String s = 'This is a string';


Programming Data and operators, Exercises

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)

int [][] peopleInTheBuilding = new int[4][5];


peopleInTheBuilding[0][1] = “John Lennon”;
peopleInTheBuilding[0][2] = “George Harrison”;

(According to the definition, "John Lennon" is living in floor 0, apartment 1.)

a) The results of the sports-lottery (quiniela) (1 day, 15 results)


b) The results of the sports-lottery (1 season of 38 match days, each one with 15 results)
c) The results of the sports-lottery (10 seasons, each one with 38 match days)
d) The name of the people in a hospital. The hospital has 5 floors, each one with 20
rooms, and each room with two patients.
e) The painting in the figure of exercise 8.

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.

number square cube


0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
Programming Data and operators, Exercises

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: )

What happens when a is equal to 0?

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).

Programming – Grado en Ingeniería Informática


Authors
Of the English version:
Juan Gómez Romero

Based on the work by:


Ángel García Olaya
Manuel Pereira González
Silvia Castro García
Gustavo Fernández-Baillo Cañas
Daniel Pérez Pinillos
Javier Ortiz Laguna
Álvaro Torralba Arias de Reyna

You might also like