0% found this document useful (0 votes)
5 views3 pages

Java Data Types and Arithmetic Exercises

The document contains a series of Java code segments and questions related to data types and arithmetic operations. It requires the reader to determine the exact values of variables after executing the code, as well as the output of specific print statements. Additionally, it includes problems involving mathematical functions from the Java Math library.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Java Data Types and Arithmetic Exercises

The document contains a series of Java code segments and questions related to data types and arithmetic operations. It requires the reader to determine the exact values of variables after executing the code, as well as the output of specific print statements. Additionally, it includes problems involving mathematical functions from the Java Math library.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Types & Arithmetic Name: _____________________________

Use the Java segments of code below to answer the following, if error write ”error”, given:
int i = 14;
int x;
double d= 4.0;
double e = 3.5;
double y;

1. x = (int)e; What is the exact value of x? 3


______________

2. y = i; What is the exact value of y? 14.0


______________

3. x = i/8; What is the exact value of x? 1


______________

4. y = d/4; What is the exact value of y? 1.0


______________

5. y = i/8; What is the exact value of y? 1.0


______________

6. y = i/8.0; What is the exact value of y? 1.75


______________

1.75
7. y = (double)(i/8); What is the exact value of y? ______________

Determine the exact output to the following code segment:


int sum1 = 10+5;
double sum2 = 10+5.0;
int quotient1 = 15/4;
double quotient2 = 15/4;
double quotient3 = 15/4.0;
int remainder1 = 15 % 4;
int remainder2 = 17 % 2;

15/4 is 3
8. [Link]("15/4 is " + quotient1); ___________________________

9. [Link]("15/4 is " + quotient2); 15/4 is 3.0


___________________________

15/4.0 is 3.75
10. [Link]("15/4.0 is " + quotient3); ___________________________

11. [Link]("15 % 4 is " + remainder1); 15 % 4 is 3


___________________________

12. [Link]("17 % 2 is " + remainder2); 17 % 2 is 1


___________________________
For the following problems, use these variables. If a problem changes a variable, assume that it has been reset before
the next problem.

int a = 2, b = 9, c = -19;

double x = 4.7, y = 5.8, z = -9.6;

13. What is the value of n? int n = [Link](c); 19

14. What is the value of p? int p = [Link](b, c); 9

15. What is the value of s? double s = [Link](b, a); 81.0

16. What is the value of t? int t = (int)[Link](a, 4); 16

17. What is the value of u? double u = [Link](z); 9.6

18. What is the value of v? double v = [Link](b); 3.0

19. What is the value of w?

double w = [Link]([Link](4,2) + [Link](3,2)); 5.0

20. What is the value of d? int d = (int)([Link](x)); 2

21. What is the value of r? double r = [Link](y,z); -9.6


_____________

Common questions

Powered by AI

The function 'Math.abs' takes a number and returns its absolute value, which converts a negative number to its positive counterpart. Therefore, 'int n = Math.abs(c);' where c = -19 results in n being 19. Meanwhile, 'Math.sqrt' computes the square root of a number, implying that 'double v = Math.sqrt(b);' where b = 9 results in v equating to 3.0. These functions thus transform variable values to comply with specific mathematical principles: absolute magnitude and root extraction.

In integer division, any fractional part is discarded. For 'x = i/8;' with i being 14, the division 14/8 results in 1.75, but since x is an integer, only the whole number part is kept. Thus, the value of x is 1.

Using 'double s = Math.pow(b, a);' as an example, where b = 9 and a = 2, the Math.pow function computes the power of a base by an exponent, yielding s = 81.0. This function allows for more complex arithmetic operations compared to basic arithmetic, enabling operations such as exponentiation with high precision, supporting a variety of mathematical computations beyond standard arithmetic.

The type casting from double to int in the expression 'x = (int)e;' causes the fractional part of the double value to be truncated. Therefore, the value of e, which is 3.5, becomes 3 when cast to an int. The exact value of x is 3.

The expression 'double w = Math.sqrt(Math.pow(4, 2) + Math.pow(3, 2));' effectively computes the hypotenuse of a right triangle with legs 4 and 3 units long by applying the Pythagorean theorem: a² + b² = c². Here, 4² and 3² are computed and summed, then the square root is taken, yielding w = 5.0. This demonstrates how fundamental mathematical principles can be implemented in programming languages, allowing for the calculation of distances, lengths, and other metrics relying on such mathematical foundations.

The statement 'double d = Math.abs(z);' where z = -9.6 ensures that the absolute value retained accuracy as a double, equivalent to 9.6. Omitting proper data type conversion, such as treating z as an integer, could lead to incorrect rounding or truncation, potentially resulting in less precise outcomes. This highlights the importance of ensuring compatible data types to maintain computational integrity and precision, especially with floating-point operations.

Assigning the division result to a double variable allows the fractional part to be retained. In 'y = i/8.0;' where i is 14, this operation results in 1.75 because dividing by a double (8.0) allows the division to yield a double result, retaining the fractional part. This contrasts with integer division like 'x = i/8;' where the result would be truncated.

'System.out.println("15/4.0 is " + quotient3);' results in '15/4.0 is 3.75', reflecting the ability of floating-point division to handle fractions, thus providing more precision. Conversely, 'System.out.println("15 % 4 is " + remainder1);' displays '15 % 4 is 3', showing that the modulus operation returns the remainder of division, solely focusing on the integer remainder without any fraction component. This contrast highlights the differences in the handling of numeric operations between division methods and modular arithmetic.

In the expression 'int d = (int)(Math.sqrt(x));' where x = 4.7, the Math.sqrt function computes approximately 2.167. However, casting to an integer truncates the fractional part, resulting in d being 2. This illustrates a loss of precision due to integer conversion, an important consideration when precise values are necessary in computations. Financial or scientific calculations, for example, might suffer from such truncation if using an integer instead of a floating-point result.

The output 'System.out.println("15/4 is " + quotient1);' results in displaying '15/4 is 3'. Since integer division discards the fractional part, '15/4' evaluates to 3, demonstrating how Java handles integer division by truncating towards zero. This is critical when predicting outputs or performing precise calculations.

You might also like