Chapter:Classes - main - Exercises

From Juneday education
Jump to: navigation, search

Exercises on the Java main method

The starting point for a class is the main method, which has to be declared exactly as Java requires (with the exception that you may chose the name of the parameter).

We have written some exercises to let you played around with the main method and with two main methods.

Q1.

Write a simple class, called Game, in the package org.gamefactory.textgame. This means that you should have a directory structure as follows:

`-- org
    `-- gamefactory
        `-- textgame
            `-- Game.java

The class should have no main method.

Expand using link to the right to see a suggested answer/solution.

The class could look something like this:

package org.gamefactory.textgame;

public class Game{

}

Q2.

Compile and execute the class. Did it work? Why?

Expand using link to the right to see a suggested answer/solution.

It compiles but will not execute. It compiles because it is a valid Java class but fails to execute since the class does not contain a valid main method.

Q3.

Add a main method to the class. The main method could simply output the class' name, using println. The code looks something like this:

  public static void main(String[] args) {
    System.out.println("Game");
  }

Expand using link to the right to see a suggested answer/solution.

The class could now look something like this:

package org.gamefactory.textgame;

public class Game{

  public static void main(String[] args) {
      System.out.println("Game");
  }

}

Q4.

Compile and execute the class. Did it work? Why?

Expand using link to the right to see a suggested answer/solution.

It both compiles and executes. It can be executed since it has a valid main method.

Q5.

Write one more simple class, called Gamer, in the package org.gamefactory.textgame. This means that you should have a directory structure as follows:

`-- org
    `-- gamefactory
        `-- textgame
            |-- Game.java
            `-- Gamer.java

Expand using link to the right to see a suggested answer/solution.

The class could look something like this:

package org.gamefactory.textgame;

public class Gamer{

}

Q6.

Add a main method to the new class (Gamer). The main method could simply output the class' name, using println.

Expand using link to the right to see a suggested answer/solution.

The class could now look something like this:

package org.gamefactory.textgame;

public class Gamer{

  public static void main(String[] args) {
      System.out.println("Gamer");
  }

}


Q7.

Compile and execute the Gamer class. Did it work? Why?

Expand using link to the right to see a suggested answer/solution.

It both compiles and executes. It can be executed since it has a valid main method.

Q8.

Execute the Game class (the first class you wrote). Did it work? Why?

Expand using link to the right to see a suggested answer/solution.

It worked now as it worked before. Nothing has changed so why shouldn't it work.

Q9.

Change the name of the method main to maine in the Game class. Compile and execute. Did it work? Why?

Expand using link to the right to see a suggested answer/solution.

Changing the name to maine does not satisfy the requirements Java sets on a main method so, although the class compiles fine, the class can't be executed.

Make a mental (or physical) note of the error message from java when it couldn't find the main method. Recognizing error messages is part of the skills you need to develop, so that you quickly can recognize errors and mistakes and remember how to fix them.

Q10.

Change the name of the method back to main. Change the main methods's access modifier public to private in the Game class. Compile and execute. Did it work? Why?

Expand using link to the right to see a suggested answer/solution.

Changing the access modifier to private does not satisfy the requirements Java sets on a main method so, although the class compiles fine, the class can't be executed.

Again, make a note of the error message from java.

Also, make a note of the fact that just because a class or program compiles, it doesn't automatically mean that it works.

Q11.

Make the main method, in the Game class, public again. Compile and execute. Did it work? Why?

Expand using link to the right to see a suggested answer/solution.

Now it compiles and can be executed again, since the class is valid Java and the main method satisfies the requirements.

Q12.

Ok, one more time. Let's rename the method main to maine in the Game class. In the main method of the Gamer class we want to invoke the maine method in the Game class. You can do it like this:

 
package org.gamefactory.textgame;

public class Gamer{

  public static void main(String[] args) {
      Game.maine(null); // using a class in the same package works without import!
      System.out.println("Gamer");
  }
        
}

Expand using link to the right to see a suggested answer/solution.

The Game and Gamer classes compile. Gamer class executes fine. We can see that the Gamer class can invoke the maine method in the Game class.

Q13.

Change the name of the maine back to main. Compile both classes and execute the Gamer class. Did it work? Why?

Expand using link to the right to see a suggested answer/solution.

The Game class compiles and executes fine. The Gamer class will not compile since it invokes a method (maine) that does not exist (any more) in the Game class.

Q14.

Change the main method of the Game class, so that it accepts an argument (which will end up in args[0]) and uses this argument to print a customized message. The message should include the argument, which can be your name.

You should be able to run the program and get a message like the following, if you give the argument "PacMan":

Welcome, PacMan

Hints:

  • You can create a String like this: String message = "Welcome, " + args[0];
  • You will crash the program if it is run without an argument, because args[0] won't work with no arguments (make a note of the error message you get, if you run the program without arguments)
  • The argument is a string you type in after the name of the class when you run it on the command line
  • You can use an if-statement to check if any arguments were given (if no arguments were given, the following expression will evaluate to true: args.length == 0 )

We won't provide a solution to this exercise, to challenge you to read instructions carefully and to experiment and also to ask for help if you get stuck!

Links

Further reading

Where to go next

The next page is: Classes_-_Commenting_code

« PreviousBook TOCNext »