Question

Task 5 (20 pts.) import java.util.Scanner; public class Casting { public static void main(String[] args) {...

Task 5 (20 pts.)

import java.util.Scanner;

public class Casting {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.printf("Please enter a double number: ");
    double a = in.nextDouble();
    int a1 = a;
    System.out.printf("a cast into an int becomes %d.\n", a1);
  }
}

The above program is an incorrect attempt to do the following:

  • Ask the user to enter a double number (i.e., a floating-point number).
  • Store that number into a variable called a.
  • Cast variable a into an integer, and store the result into a variable called a1.
  • Print out the value of a1.

Fix the above program, so that it runs correctly. Save your corrected program as Casting.java. You do NOT have to explain what is wrong, it is sufficient to submit your corrected Casting.java.

For example: if the user enters 15.23, your program output should look EXACTLY like this:

Please enter a double number: 15.23
a cast into an int becomes 15.

As another example: if the user enters -12.87, your program output should look EXACTLY like this:

Please enter a double number: -12.87
a cast into an int becomes -12.

Your program's output should match EXACTLY the format shown above. There should be no deviations, no extra spaces or lines, no extra punctuation in your output. What you see above as uppercase letters should remain uppercase in your output, what you see as lowercase letters should remain as lowercase in your output, what you see as spaces and punctuation should remain exactly as spaces and punctuation in your output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class Casting {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.printf("Please enter a double number: ");
        double a = in.nextDouble();
        int a1 = (int)a;
        System.out.printf("a cast into an int becomes %d.\n", a1);
    }
}

Add a comment
Know the answer?
Add Answer to:
Task 5 (20 pts.) import java.util.Scanner; public class Casting { public static void main(String[] args) {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT