www.webdeveloper.com
Results 1 to 2 of 2

Thread: java coding help

  1. #1
    Join Date
    Aug 2004
    Posts
    61

    java coding help

    hello.i am not really having a problem, just a problem understanding wha my teacher wants. she assigned us a basic project, nothing special, for beginners. i got everything down, and this is where i get confused.

    question 8.
    in the coinChanger class, comment out the constructor that was provided. when a class does not have a constructor the compiler will provide a default constructor which will store zero values in the numeric data fields and blanks in char and string fields. wriote a four setter methods(mutators) to set the value of each one member variables.

    ok so i have no clue what she means when she says comment the constructor? i dont see one, and she said comment it. so im not sure. also the mutators have to be in the same class? i provided the class's below, there are 2 classes so dont get confused liek me.

    thnks for the help guys..

    -------------------------------------------------------------------

    1st class

    public class CoinChanger{


    //data fields
    private int pennies;
    private int nickels;
    private int dimes;
    private int quarters;

    //methods

    //method to return the total value in cents

    public int findCentsValue(){

    return pennies + 5*nickels + 10* dimes + 24* quarters;
    }

    //method to return dollar value

    public int findDollars(){

    return findCentsValue()/100;
    }

    //Method to return the leftover change

    public int findChange(){

    return findCentsValue()%100;
    }

    //method to convert the coinchanger data to a printable string

    public String toString(){

    return pennies + " pennies, " + nickels + " nickels, " + dimes + " dimes, "+ quarters + " quarters. ";
    }
    }

    ---------------------------------------------------------------------

    2nd Class

    import javax.swing.JOptionPane;

    public class ChangeCoinsApp{

    public static void main(String[] args){

    //declare variables

    String numStr, message;

    CoinChanger coins = new CoinChanger();

    numStr = JOptionPane.showInputDialog("Enter number of pennies.");
    coins.pennies = Integer.parseInt(numStr);

    numStr = JOptionPane.showInputDialog("Enter number of nickels");
    coins.nickels = Integer.parseInt(numStr);

    numStr = JOptionPane.showInputDialog("Enter number of dimes.");
    coins.dimes = Integer.parseInt(numStr);

    numStr = JOptionPane.showInputDialog("Enter number of quarters.");
    coins.quarters = Integer.parseInt(numStr);

    message = coins.toString() + "\nCoin collection value is $" + coins.findDollars() + "." + coins.findChange();

    JOptionPane.showMessageDialog(null, message);

    }
    }

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    I don't see a default constructor in the class CoinChanger

    This line CoinChanger coins = new CoinChanger();
    is using a default constructor to create an instance of CoinChanger and is doing exactly what she has asked.
    I think its that she wanted to you to set all of the instacne variables be initialised in the default constructor and when you will comment it out they will be initialised by JVM to standard values for any of th elegacy data ypes....
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles