Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java An Introduction To Problem Solving And Programming

Authors: Walter Savitch

8th Edition

0134462033, 978-0134462035

More Books

Students also viewed these Programming questions

Question

relatively high socioeconomic development

Answered: 1 week ago