Question

Below is a list of things that your program should do and the exact order in...

Below is a list of things that your program should do and the exact order in which they should be done:

  1. Declare two integers (call them firstNumber and secondNumber) and assign them some values of your choice (1 pt).
  2. Compute the result of dividing firstNumber by secondNumber; store it in an int variable. Display the result (2 pts).
  3. Compute the result of dividing firstNumber by secondNumber; store it in a double variable. Display the result (2 pts).
  4. Compute the result of casting firstNumber to a double, then dividing by secondNumber; store it in a double variable. Display the result (2 pts).
  5. Compute the result of dividing firstNumber by secondNumber and then casting to a double (use parentheses to force the order of operations); store it in a double variable. Display the result (2 pts).
  6. Explain the results that you observed in steps 2-5. Why did some of the answers give the correct mathematical result, and others did not? Write your explanation in the comments of the program. (2 pts).
0 0
Add a comment Improve this question Transcribed image text
Answer #1


public class Division {
   public static void main(String[] args) {
//       Declare two integers (call them firstNumber and secondNumber) and assign them some values
       int firstNumber = 8 ;
       int secondNumber = 3 ;
      
//       Compute the result of dividing firstNumber by secondNumber; store it in an int variable. Display the result
       int result1 = firstNumber/secondNumber;
       System.out.println(result1);
       /*Explanation
       * first it will calculate firstNumber/secondNumber which will give quotient only 8/3 = 2 because
       * quotient for 8/3 is 2
       */
      
//       Compute the result of dividing firstNumber by secondNumber; store it in a double variable. Display the result
       double result2 = firstNumber/secondNumber;
       System.out.println(result2);
      
       /*Explanation
       * first it will calculate firstNumber/secondNumber which will give quotient only 8/3 = 2 because
       * quotient for 8/3 is 2 and you are assigning this variable to double variable so it will become 2.0
       */
      
//       Compute the result of casting firstNumber to a double, then dividing by secondNumber; store it in a double variable. Display the result
       double result3 = ((double)firstNumber)/secondNumber;
       System.out.println(result3);
       /*Explanation
       * the cast operator has precedence over division
       * the value of firstNumber is first converted to type double and finally it gets divided by secondNumber yielding a double value.
       * if you cast any one of the variable which doing division it will give you the correct result
       * so it is giving correct result assigning it to double variable
       */
      
//       Compute the result of dividing firstNumber by secondNumber and then casting to a double (use parentheses to force the order of operations); store it in a double variable. Display the result
       double result4 = (double)(firstNumber/secondNumber);
       System.out.println(result3);
       /*Explanation
       * the cast operator has precedence over division
       * the value of firstNumber is first converted to type double and finally it gets divided by secondNumber yielding a double value.
       * if you cast any one of the variable which doing division it will give you the correct result
       * so it is giving correct result assigning it to double variable
       */
      
      
   }
}

Add a comment
Know the answer?
Add Answer to:
Below is a list of things that your program should do and the exact order in...
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
  • 1- Correct the syntax errors in the following program, and rewrite the program so that it...

    1- Correct the syntax errors in the following program, and rewrite the program so that it follows the proper style conventions. What does the program do? (Write it out as a comment). (10 pts) #include <stdio.h> int main() {/* * Calculate and display the difference of two values *) int X, /* first input value */ x, /* second input value */ sum; /* sum of inputs */ X=9, x=7.5 X + x = sum; printf("%d + %d = %d\n";...

  • Write a program that will compute the total sales tax on a $95 purchase. Assume the...

    Write a program that will compute the total sales tax on a $95 purchase. Assume the state sales tax is 4 percent and the county sales tax is 2 percent ... and then display the amounts for the total sales tax, state sales tax, and county sales tax. the pseudo code is: since the tax rates do not vary, we will declare them as constants: const double STATE_SALES_TAX_RATE = 0.04 //copy initialization const double CNTY_SALES_TAX_RATE(0.02) //direct initialization declare variable to...

  • If you’re using Visual Studio Community 2015, as requested, the instructions below should be exact but...

    If you’re using Visual Studio Community 2015, as requested, the instructions below should be exact but minor discrepancies may require you to adjust. If you are attempting this assignment using another version of Visual Studio, you can expect differences in the look, feel, and/or step-by-step instructions below and you’ll have to determine the equivalent actions or operations for your version on your own. INTRODUCTION: In this assignment, you will develop some of the logic for, and then work with, the...

  • Write a Java program to do the following with your name. This can all be done...

    Write a Java program to do the following with your name. This can all be done in the main() method. 1. Create a String variable called myName and assign your personal name to it. Use proper capitalization for a legal name. I.e. String myName = "Billy Bob"; 2. Load myName with the upper case version of itself and display the result. 3. Load myName with the lower case version of itself and display the result. 4. Capitalize the first letter...

  • guys can you please help me to to write a pseudo code for this program and...

    guys can you please help me to to write a pseudo code for this program and write the code of the program in visual studio in.cpp. compile the program and run and take screenshot of the output and upload it. please help is really appreciated. UTF-8"CPP Instruction SU2019 LA X 119SU-COSC-1436- C Get Homework Help With Che X Facebook -1.amazonaws.com/blackboard.learn.xythos.prod/584b1d8497c84/98796290? response-content-dis 100% School of Engineering and Technology COSC1436-LAB1 Note: in the instruction of the lab change "yourLastName" to your last...

  • In order to do Program 6, Program 5 should bemodified.Program 6

    In order to do Program 6, Program 5 should be modified.Program 6-----------------------------------Program 4----------------------------------------------- (abstract class exception handling) Modify program4 to meet the following requirements: I. Make Person, Employee classes to abstract classes. Cl0%) 2. Add an interface interface working with a method teach() (5%) interface working void teach(String course) J 3. Modify Faculty class. (20%) a. a private data field and implement existing b. Modify Faculty class as a subclass of addition to the c. toString or print Info) method to...

  • The following C++ program is a slightly modified version of Program 5.20 from Gary J. Bronson...

    The following C++ program is a slightly modified version of Program 5.20 from Gary J. Bronson textbook. In this program, you compute and display the average of three grades of each student for a class of four students. You need to modify the code to compute and display the class average as well as the standard deviation. Your code changes are as follows: 1. The variable “double grade” should be replaced by a two-dimensional array variable “double grade[NUMSTUDENTS][NUMGRADES].” Also replace...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...

  • CE – Return and Overload in C++ You are going to create a rudimentary calculator. The...

    CE – Return and Overload in C++ You are going to create a rudimentary calculator. The program should call a function to display a menu of three options: 1 – Integer Math 2 – Double Math 3 – Exit Program The program must test that the user enters in a valid menu option. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return the option...

  • import java.util.*; /** Description: This program determines the average of a list of (nonnegative) exam scores....

    import java.util.*; /** Description: This program determines the average of a list of (nonnegative) exam scores. Repeats for more exams until the user says to stop. Input: Numbers, sum, answer Output: Displays program description Displays instruction for user Displays average Algorithm: 1. START 2. Declare variables sum, numberOf Students, nextNumber, answer 3. Display welcome message and program description 4. DOWHILE user wants to continue 5. Display instructions on how to use the program 6. Inititalize sum and number of student...

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
ADVERTISEMENT