CodeGym /Courses /New Java Syntax /Practice with integer type conversions

Practice with integer type conversions

New Java Syntax
Level 12 , Lesson 4
Available

"Hi, Amigo. Here are some tasks involving integer type conversion. Add cast operators where necessary:"

12
Task
New Java Syntax, level 12, lesson 4
Locked
From a small bucket to a big one
When two long numbers are multiplied, the result may exceed the capacity of the long type. If precision is not important, then one option could be to store the result as a floating point number. Implement the getSquare method. It should return the square of the input argument.
12
Task
New Java Syntax, level 12, lesson 4
Locked
Approximate value
When converting from integers to floating point numbers, the lower order parts of numbers may be discarded. Let's test this in practice. In the getDifference method, the incoming argument has already been converted to a float. Determine what was discarded during the conversion and return the missing
12
Task
New Java Syntax, level 12, lesson 4
Locked
Conscious choice
When a type is narrowed, part of a number might be discarded. Or it might not. Implement the isByte, isShort, and isInt methods. They should determine whether the input argument can be respectively converted to the byte, short, and int types "without serious consequences".
12
Task
New Java Syntax, level 12, lesson 4
Locked
Task No. 1 about integer type conversions
This task begins a series of tasks about integer type conversions. This is not a very difficult topic, but it often bewilders noobies because instructors sometimes put in at the very beginning, which is fundamentally wrong. But on Level 10, you're ready. Arrange the cast operators correctly to get the required result: d > 0. The operators are in the conditions.
12
Task
New Java Syntax, level 12, lesson 4
Locked
Task No. 3 about integer type conversions
"Nothing organizes your thinking as much as performing integer type conversions in your head. Of course, a compiler will also suffice". And now, the third task in the "Integer Type Conversion" series. We have some variables converted to another type, but we don't have enough of them. We need to add one type cast operation to obtain the require answer.
12
Task
New Java Syntax, level 12, lesson 4
Locked
Task No. 6 about integer type conversions
Here's the sixth task in the "Integer Type Conversions" series. What are we shooting for here? We want you to master these techniques so you don't have to relearn them. This time our task has too many cast operators. Superfluous conversions have crept in. Find and remove them to get the required answer.
Comments (56)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
FelixUjo Level 35, Gijón, Spain
16 July 2023
I cannot validate tasks with IntelliJ IDEA. I receive an error from a previous task. I have already uninstalled and reinstalled it.I cannot validate tasks with IntelliJ IDEA. I receive an error from a previous task.
John Squirrels Level 41, San Francisco, Poland
17 July 2023
There are several ways to make IDEA to solve the task you need: 1. To press CTRL+SHIF+F10 when the cursor is active in the code window of the new task. 2. Next to the main method of the new task, on the left, where the line numbers are, click the green triangle and select the top item, which is called by the ClassName.main type, for example ("Run Solution.main CTRL + SHIFT + F10") 3. To the left of the class name which we want to run, there is also a green triangle next to the line number. Click on it and select the top item, which is called by the type ClassName.main, for example ("Run Solution.main CTRL + SHIFT + F10"). 4. In the window with the code of the new task, right-click. Select the menu item of the form: "Run ClassName.main", for example - "Run Solution.main CTRL + SHIFT + F10").
FelixUjo Level 35, Gijón, Spain
17 July 2023
It remains the same. I can't run tasks with IntelliJ IDEA it shows me errors from previous solved tasks.
John Squirrels Level 41, San Francisco, Poland
18 July 2023
If all of the above does not work, you can resort to more drastic measures:  1. Mark all used tasks as excluded. To do this, left-click on the used task and select Mark Directory as -> excluded in the drop-down menu. The task#### folder will turn orange. 2. If the method is not implemented but should return something, put stubs (return 1, return "", return new HashMap<>()); 3. Comment out the problematic code in the previous tasks in order for the code in the required task would compile and wouldn't interfere with other tasks. How to do it? Select the problematic piece of code of the previous task and press CTRL + /. When you return to this task, then simply remove it from the comments.
FelixUjo Level 35, Gijón, Spain
18 July 2023
By commenting the code I can execute the tasks although it does not show me the result.
John Squirrels Level 41, San Francisco, Poland
18 July 2023
Perhaps your code has errors. Please refer to the Help section.
b n raju Level 17, irving, United States
8 June 2023
Task No. 1 about integer type conversions This should give ArthimeticException right because it is divided by 0
6 May 2023
Este ejercicio parece destinado a que los alumnos abandonen el aprendizaje de la programación.
Hoist Level 4, San Diego, United States
4 April 2023
--- important section in JAVA and C++ for those that a decent at JavaScript programming -- dynamic wild and crazy Type conversion is at the core of JS ... and is the causation for very odd happenings to debug
Xavi Martin Level 22 Expert
8 December 2022
Me vais a perdonar pero estos ejercicios no tienen demasiado sentido, no se programa asi y hay maneras mas logicas de practicar el upcasting y el downcasting.
Anonymous #10428383 Level 12, Seattle, United States
2 January 2024
Usted esta 100% acertado. Es pura perda del tiempo. Exercises like that are like as puzzle solution and has a little to learn/practice about cast. I don't think I need to solve such a personal math enigma to learn java. Please content Administrator be more clever on your exercises. Thanks
Romant Level 15, Russia, Russian Federation
10 April 2022
More or less easy. The only trouble is with task 4 'Task No. 1 about integer type conversions' - still dont understand how it worked =))
Anonymous #10960894 Level 19, Arnold, United Kingdom
27 March 2022
It seems task 2 Approximate value is misstated, or I am missing something? Conversion from int to float is not lossy and doesn't require the casting operator. Conversion from float to int is what's lossy.
Korlat Level 25, Gasteiz, Basque Country, Spain
8 April 2022
I think you are right but, since they have made the change from int big to float approx, when you make the change to int approx again there will be losses.
Anonymous #10843013 Level 24, Germany, Germany
1 March 2022
Using debug mode in intellij really helped me with all of these tasks. Add a breakpoint to each line. anouther useful hint(https://en.wikipedia.org/wiki/IEEE_754#Exception_handling): Division by zero: an operation on finite operands gives an exact infinite result, e.g., 1/0 or log(0). By default, returns ±infinity.
Stanislav Mayer Level 15, Czech Republic
9 February 2022
Task No. 6 was hard as hell. I demand extra Dark matter!
whoseunassailable Level 28, India, India
25 January 2022
Someone explain me the conscious choice code please or provide me with some alternative understandable solution. Also, these conversions are they really necessary i mean we are trying to save a lil bit of data while coding but how much is it saved exactly. Also, why not just use not int or something according to out requirement and not indulge ourself into any conversions. That way the code is more readable.
Brentachii Level 13, United States of America, United States
26 January 2022
you can use the capital of the variable in the argument to get for example System.out.println(Byte.MIN_VALUE) == 127 System.out.println(Byte.MAX_VALUE) == -128 As for why it's necessary for us to learn. I believe it's because not all devices that run java programs have the memory capacity to run programs that aren't minimalistic. another reason is that lots of businesses have programs that have millions of lines of code so if it's not optimized it could take forever to process requests.