Question
Import java.util.Scanner; public class Main { public static void main(String[] args) { // 1. See what happens with Mary. // Mary had 6
Import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 1. See what happens with Mary.
// Mary had 6 dollars 50 cents. She puts them all into a bank. But the bank only understands numbers as integers! What happens to the extra 50 cents?
float deposit = 6.50f;
int balance = 0;
// We need to cast the float value to an int because the float value is a larger type than integer. Thus we need to manually convert it.
balance = balance + (int) deposit;
System.out.println("Mary started with: " + deposit + ". This is the amount the bank registered: " + balance);
// The extra 50 cents have disappeared because of downcasting! This is called data loss.
// 2. Now try adding an integer value to a float value. You don't need to do the casting yourself because the float value is larger and the conversion from int to float happens automatically!
System.out.println("This is the amount the bank registered");
//Create an integer variable that holds the value of 10 and another float variable that hold the value of 3.755. Add them and print the result. See how the float value assimilates the integer one?
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
To add an integer value to a float value you can simply use the addition operator without the need f...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started