Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

Applications

The AddSub Applet as a console application using the IBIO methods.

 

 

 

 

 

 

data members

 

All applications start with a public static void main(String[] args) method.

 

It is possible to do all the work of an application in the constructor .

 

This application follows the input-process-output model.

 

 

 

 

 

 

 

 

 

 

 

input and output methods are defined below.

These methods are assumed in all algorithms presented in the examination.

You do NOT need to study or understand these methods.

Their sole purpose is to simplify examination questions.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The AddSub Applet as a console application using the awt.

 

 

 

 

 

data members

 

All applications start with a public static void main(String[] args) method.

 

 

 

Using methods inherited from the Frame parent Class here to set up the application window:

user-defined method to add awt objects

Inner Classes can be used since Java 1.2 to ease the task of interacting with Application windows. It justs detects when the user clicks the Close window cross and exits the application.

This acts just like an init method of an Applet.

 

 

 

 

 

The ActionListener interface can be used equally well with applications.

 

All this looks pretty familiar.

 

 

 

 

 

 

 

Applications are distinct from the Applets we have seen so far, in that: :

  • Applications can access files on the host computer
  • Applications do not run in a web page
  • Applications are contained within a Frame
  • Applications have a main method to start them up

/**
* Adding/Subtracting two numbers as a console application
*
* @author Mr J
* @version 20040216
*/

public class AddSubApp
{
   // integers to be added/subtracted
   private int number1;
   private int number2;
   private int total;
   private char choice;

   public static void main(String[] args)
   {
     new AddSubApp();
   }
   /**
   * Constructor for objects of class AddSubApp
   */

   public AddSubApp()
   {
     // get the input data
     number1 = inputInt("Input the first number: ");
     number2 = inputInt("Input the second number: ");
     choice = inputChar("Do you want to add or subtract these (a/s):");

     // process the data
     if ( (choice == 'a') || (choice == 'A') )
     {
       total = number1 + number2;
     }
     else
     {
       total = number1 - number2;
     }

     // output the result
     output("The answer is: " + total);
   }

   /**
   * IBIO methods, (c) International Baccalaureate 2003
   * Computer Science Subject Guide, Appendix 2.
   */


   static void output(String info) { System.out.println(info); }
   static void output(double info) { System.out.println(info); }
   static void output(int info) { System.out.println(info); }

   static int inputInt(String Prompt)
   {
     int result=0;
     try{result=Integer.parseInt(input(Prompt).trim());}
     catch (Exception e){result = 0;}
     return result;
   }
   static double inputDouble(String Prompt)
   {
     double result=0;
     try{result=Double.valueOf(input(Prompt).trim()).doubleValue();}
     catch (Exception e){result = 0;}
     return result;
   }
   static char inputChar(String Prompt)
   {
     char result;
     try{result = input(Prompt).charAt(0);}
     catch(Exception e){result = '\0';}
     return result;
   }
   static String input(String prompt)
   {
     String inputLine = "";
     System.out.print(prompt);
     try
     {
       java.io.InputStreamReader sys = new       java.io.InputStreamReader(System.in);
       java.io.BufferedReader inBuffer = new java.io.BufferedReader(sys);
       inputLine = inBuffer.readLine();
     }
     catch (Exception e)
     {
       String err = e.toString();
       System.out.println(err);
     }
     return inputLine;
   }
   static String input() { return input(""); }
}

Sample output of the application (in BlueJ, right-click the Class name and select main() from the menu):

Input the first number: 23
Input the second number: 22
Do you want to add or subtract these (a/s):a
The answer is: 45Input the first number: 33
Input the second number: 22
Do you want to add or subtract these (a/s):s
The answer is: 11Input the first number: 22
Input the second number: 33
Do you want to add or subtract these (a/s):
The answer is: -11

There is no reason why an Application cannot use the awt library Classes if required:

import java.awt.*;
import java.awt.event.*;
/**
* An application using GUI objects
*
* @author Mr J
* @version 20040216
*/

public class AddSubGuiApp extends Frame implements ActionListener
{
  
// awt objects
   private TextField number1 = new TextField("Input number 1");
   private TextField number2 = new TextField("Input number 2");
   private Button add = new Button("+");
   private Button sub = new Button("-");
   private Label answer = new Label("Answer appears here");

   public static void main(String[] args)
   {
     Frame theApp = new AddSubGuiApp();
     theApp.show();
   }
  
/**
   * Constructor for objects of class AddSubGuiApp
   * Initialization code can go here
   */

   public AddSubGuiApp()
   {
     
// Setup the application frame
     setTitle("Add-Subtract Application");
     setSize(160,200);
     setLayout(new FlowLayout());
     addComponents();

     // window listener inner class to detect application exit by user
     addWindowListener( new WindowAdapter()
     {
       public void windowClosing(WindowEvent e){ System.exit(0); }
      } );
   }


   public void addComponents()
   {
     add(number1);
     add(number2);
     Panel buttonPanel = new Panel();
     buttonPanel.setLayout(new GridLayout(1,2));
     buttonPanel.add(add);
     buttonPanel.add(sub);
     add(buttonPanel);
     add(answer);
     add.addActionListener(this);
     sub.addActionListener(this);
   }
   /**
   * As for Applets, listens for button presses and other events
   */

   public void actionPerformed(ActionEvent e)
   {
     int n1 = Integer.parseInt(number1.getText().trim());
     int n2 = Integer.parseInt(number2.getText().trim());
     int total;

     if (e.getSource()==add)
     {
       total = n1 + n2;
     }
     else
     {
       total = n1 - n2;
     }
     answer.setText("" + total);
   }
}

Remember that these applications do not run in web pages or the AppletViewer. If you are not using BlueJ then your IDE will provide a method for running main().

If you are using a simple editor like Notepad then you will have to:

  • Compile the code using javac <sourcefile>
  • Run the code using java <classfile>

If anyone's interested we might add a page showing how to do this using the Windows Command Line and the Ant application.

Related: [ Java Core | previous Finally ]

See also this page.


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009