Hello I need help figuring out this code and casting numbers

Hello I need help figuring out this code and casting numbers into this program, following the directions should help solve this code

Introduction For this lab we\'ll be exploring numerical data types and casting. Numerical Data Types Java has a few basic numerical data types, as you\'ve seen. These represent integer values of increasing sizes: byte short int long These represent real number (decimal) values of increasing sizes: float double These are just a few of the primitive data types Introduction to Data Type Conversions Casting is the process of converting a variable from one data type to another. For example, going from an int (an integer value) to a double (a real number) The process of conversion can be done either implicitly or explicitly In the case of implicit conversions, it is done automatically without the programmer having to direct the change in data. In Java, the only types of implicit conversions that occur are the ones that are promoting the data type. This is often seen when performing arithmetic: int i 7 double d 8.9 *i In this situation, the int value is promoted to double for the duration of the multiplication process. Basically, the values of lesser precision are raised to the level of the most precise (in this case double). You can also assign less precise values to variables of higher precision For example, a byte has less precision than an int so you can type: byte b 1; int i bi with no trouble. However, if you tried int i 5; byte b i You will get a complaint about mismatching types and a loss of precision. This brings us to explicit casting. This is where programmers direct the change of data type by specifying the new type in parenthesis before what is to be converted In the previous example, the issue was going from a more precise data type to a less precise one. If we cast the int to a byte like so:

Solution

import java.lang.Math; // headers MUST be above the first class
import java.util.Scanner;

// one class needs to have a main() method
class Eggs
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
int dozens,extras,eggs;
   double amount;
Scanner keyboard = new Scanner(System.in);
System.out.println(\"Enter Number of eggs: \ \");
eggs = keyboard.nextInt();   
dozens = eggs / 12;
extras = eggs % 12;
   amount= dozens*3.25+extras*.45;
   Double d = new Double(amount);
int i = d.intValue();
   System.out.println(\"You ordered \"+eggs+ \" eggs.That\'s \"+dozens+\" dozen at $3.25 per dozen and \"+extras+
   \" loose eggs at 45 cents per each for a total of \"
   +amount + \". With special promotion,you pay $\" + i+\"!!\");
  
}
}

Hello I need help figuring out this code and casting numbers into this program, following the directions should help solve this code Introduction For this lab w

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site