ToString method for my change class

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 441 Views - Last Post: 25 June 2012 - 04:13 PM Rate Topic: -----

#1 Cybersix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 12

ToString method for my change class

Posted 24 June 2012 - 08:05 PM

I have this code that calculate the return change but i don't know how i can add a ToString to my class

public class Change
{
    private int hundred,fifty,twenty,ten,five,twoDollar,oneDollar,fiftyCent,twentyCent,tenCent,fiveCent;
    private String[] note = {"Hundred ","Fifty","Twenty","Ten","Five","Two Dollar","one Dollar","Fifty Cent","Twenty Cent","ten Cent","Five Cent"};

    public Change()
    {
        hundred = 10000;
          fifty = 5000;
         twenty = 2000;
            ten = 1000;
           five = 500;
         twoDollar = 200;
         oneDollar = 100;
         fiftyCent = 50;
        twentyCent = 20;
           tenCent = 10;
           fiveCent = 5;
    }
    public  void calcChange(int amount)
    {
        int[] money = {hundred,fifty,twenty,ten,five,twoDollar,oneDollar,fiftyCent,twentyCent,tenCent,fiveCent};
        int count = 0;

        for(int i=0 ; i < money.length ; i++)
        {
            while(amount >= money[i])
            {
                amount = amount - money[i];
                count++;
            }
            if(count > 0)
            {
                System.out.println(count + " number of  "+ note[i] );
                count =0;
            }


        }

    }

}



this is my driver class
import java.util.Scanner;

public class ChangeTest
{
    public static void main(String[] args)
    {
        int cash;

        Change cashRegister = new Change();

        Scanner kbd = new Scanner(System.in);
        System.out.print("Enter an amount in Cents: ");
        cash = kbd.nextInt();

        cashRegister.calcChange(cash);
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: ToString method for my change class

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,310
  • Joined: 06-March 08

Re: ToString method for my change class

Posted 24 June 2012 - 08:10 PM

and what the toString() method should be returning ?
Was This Post Helpful? 0
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 734
  • View blog
  • Posts: 1,890
  • Joined: 23-December 08

Re: ToString method for my change class

Posted 24 June 2012 - 08:11 PM

Just create a method called toString in your change class?

This post has been edited by atraub: 24 June 2012 - 08:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,310
  • Joined: 06-March 08

Re: ToString method for my change class

Posted 24 June 2012 - 08:11 PM

in a big dispenser I could image returning the number of quarters, dimes, nickels left in the automate
Was This Post Helpful? 0
  • +
  • -

#5 Cybersix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 12

Re: ToString method for my change class

Posted 24 June 2012 - 08:18 PM

I want the toString to return the same message and get rid of the System.out
it print out something like this

Enter an amount in Cents: 125
1 number of one Dollar
1 number of Twenty Cent
1 number of Five Cent

but i can not figure out how to add a toString method to my code
Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 734
  • View blog
  • Posts: 1,890
  • Joined: 23-December 08

Re: ToString method for my change class

Posted 24 June 2012 - 08:20 PM

Quote

but i can not figure out how to add a toString method to my code


Just add a method called toString and make it output whatever you want

EDIT:
lol at down-rep.
What part of adding a toString method can't you figure out?

This post has been edited by atraub: 24 June 2012 - 08:30 PM

Was This Post Helpful? 0
  • +
  • -

#7 Cybersix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 12

Re: ToString method for my change class

Posted 24 June 2012 - 08:54 PM

View Postatraub, on 24 June 2012 - 08:20 PM, said:

Quote

but i can not figure out how to add a toString method to my code


Just add a method called toString and make it output whatever you want

EDIT:
lol at down-rep.
What part of adding a toString method can't you figure out?

the -rep was because you keep posting the same reply
if i knew how to add the toString to it i wouldn't ask it in here
i tried everything i knew but it doesn't print out the same
Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 734
  • View blog
  • Posts: 1,890
  • Joined: 23-December 08

Re: ToString method for my change class

Posted 24 June 2012 - 09:00 PM

Quote

if i knew how to add the toString to it i wouldn't ask it in here

You'll probably down rep me again but what the hell? What part of creating a method called
public String toString()
don't you understand?

Quote

i tried everything i knew but it doesn't print out the same

Is it that you don't know how to make the method or are you saying that you can create the method but the formatting is off? These are two different questions. If the output doesn't look right, what does it look like? How should it look?

This post has been edited by atraub: 24 June 2012 - 09:08 PM

Was This Post Helpful? 0
  • +
  • -

#9 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 196
  • View blog
  • Posts: 1,628
  • Joined: 13-March 10

Re: ToString method for my change class

Posted 24 June 2012 - 09:03 PM

toString() returns a string representation of the object.

Dog
      name
      noise

      toString() - > return its name and its noise


This post has been edited by darek9576: 24 June 2012 - 09:13 PM

Was This Post Helpful? 0
  • +
  • -

#10 Cybersix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 12

Re: ToString method for my change class

Posted 24 June 2012 - 09:07 PM

I want the same out out using toString
i want to get rid of the System.out
but i don't know how
Was This Post Helpful? 0
  • +
  • -

#11 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 196
  • View blog
  • Posts: 1,628
  • Joined: 13-March 10

Re: ToString method for my change class

Posted 24 June 2012 - 09:09 PM

toString() returns a String object. So you do get rid of print statement.
Was This Post Helpful? 0
  • +
  • -

#12 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 734
  • View blog
  • Posts: 1,890
  • Joined: 23-December 08

Re: ToString method for my change class

Posted 24 June 2012 - 09:10 PM

wtf? toString is suppose to return a string. It does NOT print anything, if you want to print something, you still need System.out. System.out and toString are not the same, they fulfill completely different needs. You're not making a whole lot of sense right now.

This post has been edited by atraub: 24 June 2012 - 09:12 PM

Was This Post Helpful? 0
  • +
  • -

#13 Cybersix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 12

Re: ToString method for my change class

Posted 24 June 2012 - 09:15 PM

I know what toString returns
I want to know if i can use toString to get the same damn out put from my code
public class Change
{
    private int hundred,fifty,twenty,ten,five,twoDollar,oneDollar,fiftyCent,twentyCent,tenCent,fiveCent,count = 0;;
    private String[] note = {"Hundred ","Fifty","Twenty","Ten","Five","Two Dollar","one Dollar","Fifty Cent","Twenty Cent","ten Cent","Five Cent"};

    public Change()
    {
        hundred = 10000;
          fifty = 5000;
         twenty = 2000;
            ten = 1000;
           five = 500;
         twoDollar = 200;
         oneDollar = 100;
         fiftyCent = 50;
        twentyCent = 20;
           tenCent = 10;
           fiveCent = 5;
    }
    public  void calcChange(int amount)
    {
        int[] money = {hundred,fifty,twenty,ten,five,twoDollar,oneDollar,fiftyCent,twentyCent,tenCent,fiveCent};


        for(int i=0 ; i < money.length ; i++)
        {
            while(amount >= money[i])
            {
                amount = amount - money[i];
                count++;
            }
            if(count > 0)
            {
                System.out.println(count + " number of  "+ note[i] );
                count =0;
            }


        }

    }


}



why is it so hard to understand ... i want to know if i can use toString to get the same out put and not have the
            if(count > 0)
            {
                System.out.println(count + " number of  "+ note[i] );
                count =0;
            }



if it s possible
that s all
Was This Post Helpful? 0
  • +
  • -

#14 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 734
  • View blog
  • Posts: 1,890
  • Joined: 23-December 08

Re: ToString method for my change class

Posted 24 June 2012 - 09:22 PM

using the toString method in that context is not good programming.

This post has been edited by atraub: 24 June 2012 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#15 Cybersix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 12

Re: ToString method for my change class

Posted 24 June 2012 - 09:28 PM

I just wanted to know if it can be done
my stupid teacher said that all the classes needs to have toString method so i am just trying to see if i can implemented in this class or not
t
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2