Java Student Project         

The Cube
Ability Level: Beginner  
Estimated Time: 40 minutes
Objectives:
  • To learn how to create methods with no arguments.
  • To learn how to create methods that require a single argument.
  • To learn how to create methods that require multiple arguments.
Materials & Resources:
  • Java Software
  • Textbook
Overview: Write a program that displays the result of cubing a number. Pass a number from one method to another method that cubes a number and returns the result. The display should execute within the method that calls the cube method. 
Instructions:

Use the following template to help you get started:
public class theCube
{
   public static void main(String [] args)
   {
      intro(); 
          code here ....
    }
   public static int cubeNumber(int x) // calculate 
   {
      code here
    }
    public static void intro() // print intro here
    {
        code
     }
}

 

Hints: Some important points to keep in mind in doing this project:

1. A method is a series of statements that carry out some task. Using a method makes programs easier to follow. Methods are easily reusable.

2. Any class can contain any number of methods.

3. Methods must include a declaration (or header or definition), an opening curly bracket, a body, and a closing curly bracket.

4. A method declaration contains optional access modifiers, the return type for the method, the method name, an opening parenthesis, an optional list of method arguments, and a closing parenthesis.

5. The access modifier for a method can be any of the modifiers public, protected, private, abstract, static, final, synchronized, or native. Most often, methods are given public access.

6. Any method that is a class-wide method requires the keyword modifier static.

7. A method is placed within a program that will use it, but not within any other method. If you place a method call within a class that does not contain the method, to call the method you must use the class name, followed by a dot, followed by the method.

8. Some methods require a message or argument.

9. Implementation hiding (abstraction), concealing details of how the method is executed, is an important principal of object-oriented programming.

10. When writing the method declaration for a method that can receive an argument, the type of the argument and a local name for the argument need to be included within the method declaration parentheses.

11. A method can be called within a program using either a constant value or a variable to an argument.

12. Multiple arguments can be passed to methods by listing the arguments and separating them by commas within the call to the method.

13. The arguments sent to the method must match in both number and type the parameters listed in the method declaration.

14. The return type for a method (the method's type) can be any Java type, including void.

15. A return statement is used to send a value back to a program that calls a method.

Sample:
public class demoAddress
{
     public static void main (String[ ] args)
     {
         meAndMyAddress( ); // calling the method named "meAndMyAddress"
         System.out.println ("Come visit me!");
     }
     public static void meAndMyAddress ( )
    {
         System.out.println ("Swinging A. Freshman");
         System.out.println ("3131 Stone Valley Road");
         System.out.println ("Danville, CA 94526");
     }
}