Hello, I am hoping that somebody can help to provide some guidance on this for me. I am tasked with writing this class for a Java class I'm taking:
Write a class encapsulating the concept of coins, assuming that coins have the following attributes: a number of quarters, a number of dimes, a number of nickels, and a number of pennies. Include a constructor, the accessors and mutators and methods toString and equals. Also code the following methods: one returning the total amount of money in dollar notation with two significant digits after the decimal point, and others returning the money in quarters ( for instance, 0.75 if there are three quarters ), in dimes, in nickels, and in pennies. Write a class to test all methods in your class.

First, I'm somewhat confused if I need to write two classes, one to encapsulate the concept of coins and one to test all methods in the class or one class to handle all demands.
I'm comfortable with setting the accessor and mutator methods and such but I'm confused on how this should be structured initially to start this. I'm unable to get to campus to work with the tutor due to a sick child so I'm hoping I can get some guidance here to build on. Thanks for the time.

it sounds like you write something like class coins { }
and you make another class to run class coins.

so class testinginterface {

coins mycoins; // i..e coins is a variable of this class so this class can invoke it.

// have methods to do operations with class coins, and test by confirming that the output is correct, which you can see because class testinginterface, does input output.


Mike

Makes sense. I think my biggest hang up at the moment is I'm a bit unsure on how to structure it to state the class coins and then give the attributes of: a number of quarters, a number of dimes, etc... My book is horrible in giving clear examples on this for guidance. I understand it would be the same structure for a number of classes I could create such as weather forecast with the attributes of the temperature and sky conditions (sunny, cloudy, etc..). Thanks for the help, I'm having a hard time initially grasping the concept of writing classes. But I will persist.

it sounds like you write something like class coins { }
and you make another class to run class coins.

so class testinginterface {

coins mycoins; // i..e coins is a variable of this class so this class can invoke it.

// have methods to do operations with class coins, and test by confirming that the output is correct, which you can see because class testinginterface, does input output.


Mike

Hi, I'm in a similar boat - sorry if this has been answered already but yeah, you'll need to write a public class Coins, then a public class TestCoins. My text is clear as mud as well, but basically your structure is going to be ///fields(attributes)///, /// constructors ////, ///// methods ////. You first define your class, ie: public class Coins, then in your fields you're going to declare what attributes each coin object will have, (needs a type, etc), then you move on to your constructors, which initialize the fields, then your methods give each coin object a set of thing it can do. I realize this is very basic in terms of format, but it helps me when I'm starting out with an assignment to remember what I'm doing at each stage. Cheers - Leah

Maybe the way your project was specified doesn't allow this, but...
A class Coins as described is just an old-fashioned program with all the data and methods in one place. It's definitely not a good OO design.
If the spec allows it, I would start by encapsulating the concept of a single Coin - with attributes name (eg "Quarter") and value (eg 0.25).
Then I would have a class Purse that encapsulates the concept of a collection of coins of different values and has methods to add then up etc.
This leads to code fragments like:

Purse myPurse = new Purse();
myPurse.add(new Coin("Quarter", 0.25));
...
System.out.println(myPurse.getTotalValue());
System.out.println(myPurse.getTotalForCoin("Quarter"))

... which can go in the test classes' main method.

ps. I guess you haven't done enums yet - but if you have you can define each type of coin in an enum and get clearer/safer code.If not, don't worry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.