Today we will be concluding the set of posts I have been sharing on how to bring the pieces of our previous lessons together and write a meaningful code. We will focus on User Interaction but before we go ahead, let us have a recap of all we have done so far.

  • Setting up of IDEs,
  • Creation of packages, classes, methods, variables and identifies,
  • Brief overview of access modifiers
  • Declaration of constructors
  • Use of Java statements: If-Else and return statements
  • Implementation of an interface – declaration of an interface, addition of abstract methods to an interface and implementation of an interface.
  • Working with objects by creating instance of a class,
  • Encapsulation – making use of setters and getters and we were able to set parameters and get some parameters using this concept.
  • Calling up methods from implementing classes of an interface by creating object of the implementing class with reference to the interface

Without doubt, it has been a long lesson. If you are just joining us, you should start from the beginning. In this post, we will be concluding the set of tutorials by looking at the most important part of an application which is user interaction. A user of an application is anybody that makes use of an application, interaction is the communication between two or more objects therefore we can say that user interaction in an application is anything built into the application that makes it usable and understandable for human. No, we are not designing an interface but we will see how our application can communicate effectively with the user to ensure the user can easily navigate the application and use our AreaCalculator effectively.

Let us consider our application layout again as we usually do,


A lot of ground have been covered so far. We have discussed about the purpose of each package and class and we have seen the code on ShapeVariableBean class of package here. In our second lesson, we discussed package, the interface and classes in this package here. In our third lesson however, we discussed package and we have seen the code on all the classes within this package. With these, we are left with only one class that has not been talked about which is the AreaCalculator class under the package as shown above. In the first post, I pointed at it as the main class and explained what makes a main class, click here if you missed it as today’s  discussion will be on this.

AreaCalculator. Java class

AreaCalculator class is the main class within the application as it contains the main method which means this is the first class that will run when the application is executed. It handles the communication with the user, finds out what the user wants, sends the request to the appropriate method and returns the output to the user. See source code below:

The class makes use of the Java Scanner to receive input from the user of the application see on line16 below (to be discussed in details).

  1 package;
  3 import;
  4 import;
  5 import;
  6 import;
  7 import;
  8 import java.util.Scanner;
 10 /**
 11  *
 12  * @author 'beecodeblog
 13  */
 14 public class AreaCalculator {
 16     static Scanner reader = new Scanner(;
 18     public static void main(String[] args) {
 19         AreaCalculator calculator = new AreaCalculator();
 20         String response = "Y";
 22         while (response.equalsIgnoreCase("Y")) {
 23             calculator.computeArea();
 24             System.out.println("Do you want to compute the area of another shape. Type 'Y' to continue and 'N' to exit.");
 25             response =;
 27         }
 28     }
 30     public void computeArea() {
 31         double area = 0.0;
 33         System.out.println("Welcome Math Professor: Please\n Type '1' - for Area of Parrallelogram\n Type '2' - for Area of Rectangle \n Type '3' - for Area of Square\n Type '4' - for Area of Triangle  ");
 34         System.out.println("Enter a number: ");
 35         final int n = reader.nextInt();
 37         switch (n) {
 38             case 1:
 39                 System.out.println("You selected area of a Parrallelogram, Type 'Y' to proceed and 'N' to terminate:");
 40                 String parrallelogram =;
 41                 if (parrallelogram.toUpperCase().equals("Y")) {
 42                     System.out.println("Enter base value: ");
 43                     double base = reader.nextDouble();
 44                     System.out.println("Enter height value: ");
 45                     double height = reader.nextDouble();
 46                     area = new Parrallelogram().doArea(base, height);
 47                     break;
 48                 } else {
 49                     return;
 50                 }
 51             case 2:
 52                 System.out.println("You selected area of a Rectangle, Type 'Y' to proceed and 'N' to terminate:");
 53                 String rectangle =;
 54                 if (rectangle.toUpperCase().equals("Y")) {
 55                     System.out.println("Enter base value: ");
 56                     double base = reader.nextDouble();
 57                     System.out.println("Enter height value: ");
 58                     double height = reader.nextDouble();
 59                     area = new Rectangle().doArea(base, height);
 60                     break;
 61                 } else {
 62                     return;
 63                 }
 64             case 3:
 65                 System.out.println("You selected area of a Square, Type 'Y' to proceed and 'N' to terminate:");
 66                 String square =;
 67                 if (square.toUpperCase().equals("Y")) {
 68                     double lenght = reader.nextDouble();
 69                     System.out.println("Enter lenght value: ");
 70                     area = new Square().doArea(lenght);
 71                     break;
 72                 } else {
 73                     return;
 74                 }
 75             case 4:
 76                 System.out.println("You selected area of a Triangle, Type 'Y' to proceed and 'N' to terminate:");
 77                 String triangle =;
 78                 if (triangle.toUpperCase().equals("Y")) {
 79                     System.out.println("Enter base value: ");
 80                     double base = reader.nextDouble();
 81                     System.out.println("Enter height value: ");
 82                     double height = reader.nextDouble();
 83                     area = new Triangle().doArea(base, height);
 84                     break;
 85                 } else {
 86                     return;
 87                 }
 88             default:
 89                 System.out.println("You have not selected any option- Is the shape a regular polygon? Type 'R' for Regular Polygon,  'I' for Irregular Polygon and 'N' to terminate:");
 90                 String regularPolygon =;
 91                 if (regularPolygon.toUpperCase().equals("R")) {
 92                     System.out.println("Enter base value: ");
 93                     double base = reader.nextDouble();
 94                     System.out.println("Enter height value: ");
 95                     double height= reader.nextDouble();
 96                     area = new RegularPolygon().doArea(base, height);
 97                     break;
 98                 } else if (regularPolygon.toUpperCase().equals("I")) {
 99                     System.out.println("Sorry, no implementation for Irregular Polygons...");
100                     break;
101                 } else {
102                      System.out.println("Thank you for using area of shapes calculator...");
103                     return;
104                 }
105         }
106         if(area!=0.0){
107         System.out.println("Area is : " + area);
108         }
110     }
111 }

The following interactions happen between the user of the application and the application when the application is executed.

  • Once the application runs, the application will ask the user to specify the shape the user is trying to calculate its area. See snippet below:


  • Once a user enters the right number for the operation needed, the application pops up a message to confirm the option selected by the user


  • Once the user confirms his option by typing “Y”, the message below displays asking for the necessary parameter .


  • Then this message displays to request for the second value


  • Once the required parameters have been supplied and the users clicks on enter, the area is calculated and the output is displayed as shown below.


  • Once the output is displayed, the message above follows asking the user if he would like to continue as shown above. And the cycle continues as long as the customer response remains “Y”.


No matter how complex the application you are building is, it is very important that you have a very good user interaction/interface as an unnecessarily complicated interface is of no good use to the user if they don’t know how to go about using it. Like my boss will always say, “always have it at the back of your mind whenever you are building an application as if it’s targeted at a 5 year old this way, you will make it as dummy proof as possible”. I hope you enjoyed the tutorial, please don’t forget to get in touch should you require any assistance on any of the tutorials. Happy coding!!!


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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s