Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Using & Understanding the Main() Method

Thread view: 
psmith@mcwy.com - 21 Mar 2007 16:09 GMT
Could somebody please explain how to use the main() method properly.
I have read numerous books but none seem to help.

I have two classes - Payment and TestPayment.

In TestPayment I have the following code:

public class TestPayment
{
   public static void main(String[] args)
   {
       // Create four Payment instances
       Payment p1 = new Payment(2490, "Euros");
       Payment p2 = new Payment(2490, "Euros");
       Payment p3 = new Payment(2491, "Euros");
       Payment p4 = new Payment(2490, "Pesos");
       Payment p5 = new Payment(49, "Pesos");
       Payment p6 = new Payment(9, "Pesos");
       Payment p7 = new Payment(10, "Pesos");
   }
}

In the Payment class I have the following method to create:

  public boolean equals(Object o)
  {
  }
In here I have to check if the payments are the equal to each other,
firstly the amount, followed by the currency.

The amount is an int value(anAmount) and the currency a String value
(aCurrency)

How do I iterate through each Payment to see if they are equal or not?

Thanks in advance,

Paul
Robert Klemme - 21 Mar 2007 16:55 GMT
> Could somebody please explain how to use the main() method properly.
> I have read numerous books but none seem to help.
>
> I have two classes - Payment and TestPayment.

It seems you have one class - a Java class (pun intended).

> In TestPayment I have the following code:
>
[quoted text clipped - 25 lines]
>
> How do I iterate through each Payment to see if they are equal or not?

What does your textbook say about dealing with multiple instances of the
same type?

    robert
Michael Rauscher - 21 Mar 2007 17:10 GMT
> Could somebody please explain how to use the main() method properly.

Could you explain what your question (see below) has to do with the
main-method?

> I have read numerous books but none seem to help.

None of the books you read told you how to use the main method? I don't
believe that.

> I have two classes - Payment and TestPayment.
>
> In TestPayment I have the following code:

...

> In the Payment class I have the following method to create:
>
[quoted text clipped - 3 lines]
> In here I have to check if the payments are the equal to each other,
> firstly the amount, followed by the currency.

Remember to override hashCode, too.

> The amount is an int value(anAmount) and the currency a String value
> (aCurrency)
>
> How do I iterate through each Payment to see if they are equal or not?

To iterate you'd need something to iterate on, e. g. an array or a List.

You could create such List e. g. via:

List<Payment> payments = Arrays.asList( new Payment(1234, "EUR"),
                                        new Payment(5678, "EUR"),
                                        ... );

Then you could write a method that compares all payments in the list
with each other:

boolean areAllPaymentsEqual( List<Payment> payments ) {
    if ( payments.size() == 0 )
        return true;

    Iterator<Payment> it = payments.iterator();
    boolean result = true;
    Payment firstPayment = it.next();
    while ( result && it.hasNext() ) {
        Payment secondPayment = it.next();
        result = firstPayment.equals(secondPayment);
    }
    return result;
}

Bye
Michael


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.