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

1 Expert Approved Answer
Step: 1 Unlock

To add an integer value to a float value you can simply use the addition operator without the need f... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!