java coding adventure

Potato wants to use a calculator

Java is powerful. You can do many things with Java.

Java is ugly too. Really. You don’t want to use Java to write the part of the application that your user will see. That part is called UI – User Interface or GUI – Graphical User Interface.

That’s why I started with tests. You can write a test to check if your code is working before you actually connect it with what you or the user will see or what you or the user will use.

But Potato wants to use the calculator!

But The Potato asked me how can she run the application (it’s like telling your Java code to start and do something) and see what the result is. The potato wanted to use the calculator. And The Potato is right. What for are you writing the application if you can’t use it? You write the application first for the user and only a second for the computer.

Java program can be run thanks to the main method that is in one of the classes in the application (more about classes in Adventure First hut on the trail: Basics of Object Oriented Programming (OOP)). Yes, the main method is called ‘main ()’:

public static void main(String[] args) {

How to create a class with the main method in eclipse?

  1. Go to your Calculator project in Eclipse.
  2. Right-click on package blog.jumpstart.learningPath > New > ClassnewClass
  3. Make sure the package is set to blog.jumpstart.learningPath.
  4. Write a class name: I wrote Potato (because it’ll be The Potato using the Calculator)
  5. In section “Which method stub would you like to create?” check “public static void main(String[] args)newClassFinish
  6. Finish
  7. The new class should be created:
    package blog.jumpstart.learningPath.potato;
    public class Potato {
         * @param args
        public static void main(String[] args) {
            // TODO Auto-generated method stub
  8. You can also see the class in the project explorer.newClappInProjectExp

Let’s fill the main method with a new code.

The main method you just created doesn’t do anything. Let’s fill in some code.

  1. The potato wants to use a calculator so let’s first create the object:
    Calculator calculator = new Calculator();
  2. When the calculator is created you can use it. For example, use a method add of calculator class this way:
  3. However, if you just wrote:

    you won’t see anything.

  4. In Eclipse, you have a built-in console where you can see the code results. To do it you can, for example, print a line of text using a method
  5. To print a result of add function just put it inside a println() method:
  6. The final code will look like this:
    package blog.jumpstart.learningPath;
    public class Potato {
         * @param args
        public static void main(String[] args) {
            Calculator calculator = new Calculator();
            System.out.println(calculator.add(1, 2));
  7. Now you can run the application
  8. In the project explorer, right-click on class Potato > Run As > Java ApplicationrunAsJavaApp
  9. After you run Potato as Java Application you can see the result in Eclipse console (under the code)consoleOutput

I updated the code in the GitHub repository for the Calculator so you can take a look. I’ll explain how to get the changes from there next week. (If you want to see it right now you can download a zipped code and view it in a Notepad, or Notepad++ it’s colorful in the latter!>)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s