Click to See Complete Forum and Search --> : Calling Methods in Java


antonio_142
February 14th, 2003, 06:41 PM
I am just learing Java programming, but a little confused on how to call your methods. I have attached some code and i know that you have to create a main in order to call the methods, but unsure on how to do it.

public class Dollars
{
/***************************************************************
*Outputs amount in dollars and cents notation. Rounds after two
*decimal points. Does not advance to the next line after output.
***************************************************************/
public static void write(double amount)
{
if (amount >= 0)
{
System.out.print('$');
writePositive(amount);
}
else
{
double positiveAmount = -amount;
System.out.print('$');
System.out.print('-');
writePositive(positiveAmount);
}
}

//Precondition: amount >= 0;
//Outputs amount in dollars and cents notation. Rounds
//after two decimal points. Omits the dollar sign.
private static void writePositive(double amount)
{
int allCents = (int)(Math.round(amount*100));
int dollars = allCents/100;
int cents = allCents%100;

System.out.print(dollars);
System.out.print('.');
if (cents < 10)
{
System.out.print('0');
System.out.print(cents);
}
else
System.out.print(cents);
}


/***********************************************************
*Outputs amount in dollars and cents notation. Rounds after
*two decimal points. Advances to the next line after output.
***********************************************************/
public static void writeln(double amount)
{
write(amount);
System.out.println();
}





}

dlorde
February 21st, 2003, 07:18 PM
The 'main()' method is what the Java runtime needs to run a program, so you only really need one per application, and you generally put it in the class that drives the application. In the 'main()' method, you usually create an instance of your core application class and, if necessary, call a method on it to set the application going. For testing purposes, you can write a 'main()' method for every class you write, which instantiates and tests the class. This allows you to run each class stand-alone as a class tester, but there are better ways of testing classes (e.g. JUnit). You don't need a 'main()' method to call other methods, all you need is a class instance or (for static methods) a class.

For the 'Dollars' class you posted, which appears to be just a utility class that outputs a value as a dollar amount, you could write 'main()' method that tests those methods, but normally you would call the methods from some other classes in your application, so 'Dollars' doesn't really need a 'main()' method. However, you could do this:public class Dollars {
...
public static void main(String[] args) {
// test the methods here:
System.out.println("Method 'write(double)'. This should output '$1234.65'");
Dollars.write(1234.56);
.... etc.
}
...
}I recommend the Java Tutorial (http://java.sun.com/docs/books/tutorial/index.html) and Thinking in Java (http://www.codeguru.com/java/tij/) as good introductions to Java.

Be nice to people on your way up because you'll meet 'em on your way down...
Wilson Mizner