Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: No Output in Program:

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default No Output in Program:

    I am working on a Object Oriented Program that gives change for a certain Purchase. You give it the purchase amount and the coins and amount of coins that you are paying with.

    The problem is that even though I'm not getting any errors, I'm also not getting any output.

    Tester:
    /**
       This program tests the CashRegister class.
    */
    package application;
    import classes.*;
    public class CashRegisterCoinTester
    {
       public static void main(String[] args)
       {
    //define constants for PENNY_VALUE,NICKEL_VALUE,DIME_VALUE,QUARTER_VALUE
     
     final double QUARTER_VALUE = 0.25;
     final double DIME_VALUE = 0.1;
     final double NICKEL_VALUE = 0.05;
     final double PENNY_VALUE = 0.01;
     
     
    //create new cash register
    CashRegister register = new CashRegister();
    //do some purcahse
    register.recordPurchase(20.00);
     //enter some payment usign pennies,
    Coin penny=new Coin(PENNY_VALUE,"penny");
    Coin quarter=new Coin(QUARTER_VALUE,"quarter");
    Coin dime=new Coin(DIME_VALUE,"dime");
    Coin nickel=new Coin(NICKEL_VALUE,"nickel");
     register.enterPayment(5,penny );
     register.enterPayment(1,dime );
     register.enterPayment(5,quarter );
     register.enterPayment(5,nickel );
     
     
     // new Coin(QUARTER_VALUE,"quarter");
     //enter some payments using other coins too
     
     
     //get change
     
     register.giveChange();
     //display change
     
       }
    }

    Cash Register:

    /**
       A cash register totals up sales and computes change due.
    */
    package classes;
    public class CashRegister
    {
       // add two attributes (variables) purchase and payment
    private double purchase;
    private double payment;
     
     
       /**
          Constructor that constructs a cash register with no money in it.
       */
       public CashRegister()
       {
     
           //initialize the purchase and payment to 0.0
          double purchase = 0.0;
         double payment = 0.0;
     
       }
     
       /**
          Records the sale of an item.
          @param amount the price of the item
       */
       public void recordPurchase(double amount)
       {
     
           //add the amount of purchase to purchase
           purchase = purchase + amount;
     
       }
     
       /**
          Enters the payment received from the customer; should be called once
          for each coin type.
          @param coinCount the number of coins
          @param coinType the type of the coins in the payment
       */
     
     
     
     
     
       public void enterPayment(int coinCount, Coin coinType)
       {
     
     
          //calculate the payment
           payment = payment+ coinCount * coinType.getValue();
     
       }
     
       /**
          Computes the change due and resets the machine for the next customer.
          @return the change due to the customer
       */
       public double giveChange()
       {
         double change = payment - purchase;
     purchase = 0;
     payment = 0;
     return change;
     
     //calculate change,change is the difference between payment and purchase, return change
     
       }    
    }

    Coin:

    /**
       A coin with a monetary value.
    */
    package classes;
    public class Coin
    {
        //declare coin attributes/data/variable value & name
     
        double value;
        String name;
     
     
     
       /**
          Constructs a coin.
          @param aValue the monetary value of the coin.
          @param aName the name of the coin
       */
       public Coin(double aValue, String aName) 
       { 
          //initialize the coin value and name to the arguments passed
        value = aValue;
        name = aName;
     
       }
     
       /**
          Gets the coin value.
          @return the value
       */
       public double getValue() 
       {
        return value;
     
           //return value
     
       }
     
       /**
          Gets the coin name.
          @return the name
       */
       public String getName() 
       {
          //return name,
         return name;
       }
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: No Output in Program:

    You might kick yourself for this one .
    At a glance everything seems fine, but when you call
    register.giveChange(); it only returns the change amount. In its current state, it simply performs the calculation.
    For you as the client to see it, you need to print it out.
    So replace " register.giveChange();" with
    "System.out.println(register.giveChange());" and it should be ok.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: No Output in Program:

    Lol wow. Thank you very much Newbie, I appreciate it.

Similar Threads

  1. what's the output?
    By dcshoecousa in forum Java Theory & Questions
    Replies: 3
    Last Post: November 27th, 2010, 11:11 AM
  2. How to capture standard output from another program ?
    By ni4ni in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2010, 11:00 AM
  3. Simple Input/Output program Acting weird
    By drexasaurus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 02:15 PM
  4. Object Oriented program, no output
    By boardbreaker in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 17th, 2009, 11:11 PM
  5. Redirect error and output stream using java program
    By leenabora in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 16th, 2009, 04:12 AM