Results 1 to 3 of 3

Thread: I need some help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    19

    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);




    }
    }

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: I need some help

    The second program works fine, but the first one has lots of compile-time errors
    1- Replace "import java.text.DemcialFormat;" with "import java.text.DecimalFormat;"
    2- Fields (var, unitpricePerPound, fmt, ounces_per_pound) are not declared or initialized
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: I need some help

    Code:
    import java.util.* ;
    
    public class TestUtilities
    {
      public static void main ( String[] args )
      {
        Scanner scanner = new Scanner( System.in ) ;
        int num = scanner.nextInt() ;
        System.out.println( " \n\n num = " + num ) ;
        //Print in Binary format
        System.out.println( " Binary Num = " + Integer.toBinaryString( num ) ) ;
        //Print in Hexadecimal format
        System.out.println( " Hexadecimal Num = " + Integer.toHexString( num ) ) ;
        //Print in Octal format
        System.out.println( " Octal Num = " + Integer.toOctalString( num ) ) ;
        // print maximum and minumu integer numbers
        System.out.println( "Maximum integer value= " + Integer.MAX_VALUE +
    			"\r\nMinimum integer value= " + Integer.MIN_VALUE ) ;
        //Read 2 numbers
        System.out.println( "Enter 2 numbers please" ) ;
        int n1 = Integer.parseInt( scanner.next() ) ;
        int n2 = Integer.parseInt( scanner.next() ) ;
        //Print the sum
        System.out.printf( "The sum of %d + %d = %d", n1, n2, n1 + n2 ) ;
      }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width