Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Formatting Output

File Deli.java contains a partial program that computes the cost of buying an item at the deli. Save the program to your directory and do the following:

  1. Study the program to understand what it does.
  1. Add the import statements to import the DecimalFormat and NumberFormat classes.
  1. Add the statement to declare money to be a NumberFormat object as specified in the comment.
  1. Add the statement to declare fmt to be a DecimalFormat object as specified in the comment.
  1. Add the statements to print a label in the following format (the numbers in the example output are correct for input of $4.25 per pound and 41 ounces). Use the formatting object money to print the unit price and total price and the formatting object fmt to print the weight to 2 decimal places.

      ***** CS Deli *****

     Unit Price: $4.25 per pound

     Weight: 2.56 pounds

     TOTAL: $10.89

// ********************************************************

// DeliFormat.java

//

// Computes the price of a deli item given the weight

// (in ounces) and price per pound -- prints a label,

// nicely formatted, for the item.

//

// ********************************************************

import java.util.Scanner;

public class Deli

{

    // ---------------------------------------------------

    // main reads in the price per pound of a deli item

    // and number of ounces of a deli item then computes

    // the total price and prints a "label" for the item

    // --------------------------------------------------

    public static void main (String[] args)

    {

        final double OUNCES_PER_POUND = 16.0;

        double pricePerPound; // price per pound

        double weightOunces;   // weight in ounces

        double weight;               // weight in pounds

        double totalPrice;       // total price for the item

        Scanner scan = new Scanner(System.in);

        // Declare money as a NumberFormat object and use the

        // getCurrencyInstance method to assign it a value

        // Declare fmt as a DecimalFormat object and instantiate

        // it to format numbers with at least one digit to the left of the

        // decimal and the fractional part rounded to two digits.

        // prompt the user and read in each input

        System.out.println ("Welcome to the CS Deli!!\n ");

        System.out.print ("Enter the price per pound of your item: ");

        pricePerPound = scan.nextDouble();

        System.out.print ("Enter the weight (ounces): ");

        weightOunces = scan.nextDouble();

        // Convert ounces to pounds and compute the total price

        weight = weightOunces / OUNCES_PER_POUND;

        totalPrice = pricePerPound * weight;

        // Print the label using the formatting objects

        // fmt for the weight in pounds and money for the prices

    }

}

Java, Programming

  • Category:- Java
  • Reference No.:- M9450535

Have any Question?


Related Questions in Java

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Can someone help me please with those question1what is the

Can someone help me please with those question 1:what is the best data type for student id datatime,currency,number,decimal 2:which relationshipis preferable? one to one,one to many,many to many 3:if you add table A's pr ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Assignment - java program using array of objectsobjectives

Assignment - JAVA Program using array of objects Objectives - This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details - For this assignment, you are required to develop a Menu ...

Assessment instructionsin this assessment you will complete

Assessment Instructions In this assessment, you will complete the programming of two Java class methods in a console application that registers students for courses in a term of study. The application is written using th ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

Answer the following question whats the difference public

Answer the following Question : What's the difference public inheritance and private inheritance? What can derived classes inherit from base classes? What cannot be inherited from base classes?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As