Question

In: Computer Science

Java How to Program Exercise 4.11 Explain what happens when a Java program attempts to divide...

Java How to Program Exercise 4.11
Explain what happens when a Java program attempts to divide one integer by another. What happens to the fractional part of the calculation? How can you avoid that outcome?

My instructor want me to do that exercise with these details and I am so confuse. I don't know where to start.
"Random number of Trips"
"Enter gallon and miles as whole numbers"
"NPG decimal number"

Solutions

Expert Solution

Java does integer division, as the regular real division, It takes only the quotient and throw away the remainder.
Hence 4/3 gives 1.

However, if you don't want to lose the fractional part and want the result as float/double number, you need to type case any of the number of division to double.

So, division of 4.0/3 will give 1.333333333333 or double(4)/3 will result 1.333333333333

For the project , I guess, it is asking the user input from users as a whole number. and there by calculating NPG in decimal number. However about Random No of Trips, I am not sure. You ask in detail.


Below , I demonstrate this with some java code. Please check it

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
       int a = 8;
       int b = 4;
       int c = 3;
       float d = b/c;
       System.out.printf("a=%d b=%d c=%d\n",a,b,c);
       System.out.println("a/b : " + (a/b));
       System.out.println("b/c : " + (b/c));
       System.out.println("(double)b/c : " + (double)b/c);
       System.out.println("b*1.0/c : "+ b*1.0/c);
  
   Scanner in = new Scanner(System.in);
System.out.print("Enter Gallon in whole no: ");
int gallon = in.nextInt();
System.out.print("Enter Miles in whole no: ");
int mile = in.nextInt();
System.out.print("Mile per gallon is: " + (double)mile/gallon);
   }
}

It gives following output:

To get correct value of Miles /per gallon, you need to use double cast the numerator. You would get the result as 177, if you would simple do the division mile/gallon .

Hope this helps.

a=8 b=4 c=3 a/b : 2 b/c : 1 (double)b/c : 1.3333333333333333 b*1.0/c : 1.3333333333333333 Enter Gallon in whole no: 2 Enter Miles in whole no: 355 Mile per gallon is: 177.5


Related Solutions

Python 1.Explain what happens when a program receives a non-numeric string when a number is expected...
Python 1.Explain what happens when a program receives a non-numeric string when a number is expected as input, and explain how the try-except statement can be of use in this situation. 2.What is meant by the state of an object, and how does the programmer access and manipulate it? 3.Explain the differences between instance variables and temporary variables. Focus on their visibility in a class definition, and on their roles in managing data for an object of that class. 4.Explain...
Explain why segmentation efforts based on attempts to divide the mass market using a few demographic...
Explain why segmentation efforts based on attempts to divide the mass market using a few demographic dimensions may be very disappointing.
Explain how nervous signal transmission is propagated along the axon and what happens when the signal...
Explain how nervous signal transmission is propagated along the axon and what happens when the signal reaches its effector organ or tissue. In your discussion include a brief explanation of the refractory period. How can a signal be made stronger?
Mechanic’s and Artisan’s Liens What if someone attempts to exercise a lien wrongfully, and there is...
Mechanic’s and Artisan’s Liens What if someone attempts to exercise a lien wrongfully, and there is a dispute on whether any payment is due? What remedies does the person whose property has been wrongfully possessed have?
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide)...
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide) on binary numbers using only logical operations (i.e., not using the actual mathematical operators thatJava already supports).A skeleton for the implementation is provided and can be downloaded from Canvas.In this source file  (BinaryCalculator.java), there is already code to read stringsfrom the keyboard.  The program will exit if the string “QUIT” is received, otherwiseit will attempt to process commands of the form: <binary operand 1> <operator> <binary...
a-Explain what happens when the magnet moves through the coil at different speeds b-How this affects...
a-Explain what happens when the magnet moves through the coil at different speeds b-How this affects the brightness of the bulb and the magnitude & sign of the voltage. c-Explain the difference between moving the magnet through the coil from the right side versus the left side. d-Explain the difference between moving magnet through the big coil versus the smaller coil.
Address the following and Explain: a) what happens when a strong base is added to a...
Address the following and Explain: a) what happens when a strong base is added to a solution that only has significant amounts of a weak base b) what happens when a strong base is added to a solution initially made up of significant amounts of a weak base and its conjugate acid
Explain what happens to Na+ concentration in the nephron when GFR increases.
Explain what happens to Na+ concentration in the nephron when GFR increases.
Explain what happens in the Lomuto partition when there is a lot of the same elements...
Explain what happens in the Lomuto partition when there is a lot of the same elements in the input and justify the consequences it has for the time complexity of Quicksort.
Explain why the preemption issue is important and what happens when there is an ambiguity in...
Explain why the preemption issue is important and what happens when there is an ambiguity in a contract term?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT