|
-
Sep 22nd, 2006, 08:20 AM
#1
Thread Starter
Junior Member
I need some help
I need some one to tell me if I am doing these 2 programs right. And if not can you please tell me what I am doing wrong.
// ********************************************************
// DeliFormat.java
//
// Computes the unit price of a deli item given the weight
// (in ounces) and price per pound -- prints a label,
// nicely formatted, for the item.
//
// ********************************************************
// import all needed classes
import java.util.Scanner;
import java.text.NumberFormat;
import java.text.DemcialFormat;
public class Deli
{
// ---------------------------------------------------
// main reads in the unit 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 unitPricePerPound; // unit price per pound
double weightOunces; // weight in ounces
double weight; // weight in pounds
double totalPrice; // total price for the item
DecimalFormat df = new DecimalFormat("0.###");
NumberFormat nfc = NumberFormat.getCurrencyInstance();
Scanner scan = new Scanner(System.in);
// Declare money as a NumberFormat object and use the
// getCurrencyInstance method to assign it a value
System.out.println("The formatted variable var (currency) = " + nfc.format(var));
// 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.
System.out.println("The formatted variable var = " + df.format(var));
// prompt the user and read in each input
System.out.println("Welcome to the CS Deli!!\n ");
// Read the unit price of the item
System.out.print("Enter the unit price per pound of your item: ");
unitPricePerPound = scan.nextDouble();
// Read the weight of the item
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 = unitpricePerPound * weight;
// Print the label using the formatting objects
// fmt for the weight in pounds and money for the prices
System.out.println("unitPricePerPound: " + fmt.format(unitPricePerPound));
System.out.println("weight: " + fmt.format(weight) + " per " + (ounces_per_pound));
System.out.println("TotalPrice: " + fmt.format(totalPrice));
}
}
Second Program
1. Prompt for and read in an integer, then print the binary, octal and hexadecimal representations of that integer.
2. Print the maximum and minimum possible java integer values
3. Prompt the user to enter two decimal integers, one per line. Use the next method of the Scanner class to read each of them in. Now convert the strings to ints and add them together, and print the sum.
//testing NumberFormat, DecimalFormat classes
//and also Wrapper classes
public class TestUtilities
{
public static void main (String[] args)
{
String s;
int num;
//lets's try the Integer wrapper class
num = 123;
System.out.println(" \n\n num = " + num);
//using the Integer wrapper class to convert num to binary
System.out.println(" binarynum = " + Integer.toBinaryString(num));
//using the Integer wrapper class to convert num to Hexadecimal
System.out.println(" Hexadecimal num = " + Integer.toHexString(num));
//using the Integer wrapper class to convert num to Octal
System.out.println(" Octal num = " + Integer.toOctalString(num));
//the wrapper classes can convert strings to number types (parsing)
s = "56935";
//conversion means we "make" an int out of a string
num = Integer.parseInt(s);
System.out.println("Sting version: " + s);
System.out.println("Integer version: " + num);
num +=134;
System.out.println("modified Int version: " + num);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|