Wednesday, April 8, 2026

This is the first in a series of Java programs I’d be writing to keep track of my growth and development in the language. The program I’d be writing today is a simple profitability calculator that would be expanded over-time with more functionality and an interface built with Java Swing. The program was created in replit and it can calculate two profitability metrics — Gross Profit and Profit Margin. I call this program darusha.me — after a rich Nigerian man in the 60s/70s who was famous for spending money lavishly.

In Java, every program starts from a class. We can think of a class as a sort of blueprint and a common analogy is cars and brand type e.g. Toyota. For this analogy, we can think of the cars as the blueprint — class as against an object of the class which can be Toyota, Honda, or Subaru. Another analogy can be footwears and boots. In this case, footwears can be seen as a class, a blueprint for items/materials used to protect our feet while the boots are a type of footwear — an object. These are the foundations of Java programming and by extension — Object Oriented Programming.

So hence we have a Main.java file, which is a class that houses the main method. We can think of methods as functions, snippets of code that do something. A sizeable number of times they return something, but they can also return nothing. The code shows the Main class:

class Main {
  public static void main(String[] args) {
    Profitability profitability = new Profitability();
    System.out.println(profitability.getRevenue());
    System.out.println(profitability.getCost());
    System.out.println(profitability.grossProfit());
    System.out.println(profitability.profitMargin());
  }
}

We must be aware that every Java program must have a main method, this is the method that Java would execute. As we can see the main method has a series of various keywords — identifiers and modifiers — that further define it. Let’s briefly examine them — the public keyword tells Java that this method is available beyond the scope of the Main.java file, the static keyword tells Java that the method is available to only the instance of the class and not the objects, the void keyword tells Java that this method returns nothing.

In the body of the main method, there are a series of expressions and methods, before we get into them, let’s look at what’s inside the Profitability.java file where the logic resides.

import java.util.Scanner;

class Profitability {
public int revenue, cost;

public int getRevenue() {
    Scanner revenueScanner = new Scanner(System.in);
    System.out.println("What is your revenue or total sales?");
    revenue = Integer.parseInt(revenueScanner.nextLine());
    return revenue;
  };

public int getCost() {
    Scanner costScanner = new Scanner(System.in);
    System.out.println("What is your Total Cost? ");
    cost = Integer.parseInt(costScanner.nextLine());
    return cost;
  };

public int grossProfit() {
    return revenue - cost;
  };

public double profitMargin( ){
    return (((double) grossProfit() / (double) revenue));
  };

Java has one of the most extensive API along with its documentation in the programming world. In the first line, we are telling Java to import a class from its java.util package — which is Scanner class. The Scanner class has lots of methods in it, but we are most interested in being able to capture user input. In this application, the user would interact with the program by using the command line interface.

In the second line, we create a Profitability class as every java program must start with a blueprint — a class, then we declare two important variables that would be needed from the user — cost and revenue. We declare them public so every method can have access to them then we want them to be of the primitive type: int. There are various primitives in Java. They include:

  1. byte – stores whole numbers from -127 to 127.
  2. short – stores whole numbers from -32,768 to 32,768.
  3. int – stores whole numbers from -2,147,483,648 to 2,147,483,647.
  4. long – stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  5. float – stores floating point numbers with 6 to 7 degrees of precision. Usually ends with an f e.g. 2.675438f.
  6. double – stores floating point numbers with up to 15 degrees of precision. This is the preferred primitive type to use when storing decimal values if accuracy is of great concern.
  7. booleans – true or false values, gotten from Boolean algebra.
  8. char – single characters e.g ‘A’, ‘G’.
TIP
To know more about Java Primitive values along with their size, go here.

Next, I create the method getRevenue(), here what I want to capture is the user’s company’s total revenue — revenue can be loosely defined as the total number of sales or in economic terms total quantity multiplied by price i.e.

tr = p*q
where:
 tr = total revenue 
 p = price
 q = quantity

Inside the method, I create an object from the Scanner class imported in line 1, then using the System.out.println() method to output the next direction to the user — which is to input the value of the revenue. Then I capture this value into the already declared but empty variable, revenue. However, at this point we need to pause. In the previous line that outputs direction to the user, that value entered though it may be a number, it is captured as a String (a non-primitive type in Java) which is an object. And we want the value of revenue to be an integer remember the earlier line:

public int cost, revenue;

In the line above, we have declared revenue to be an integer because this variable is always expressed in a numeric value. So, we need to convert this variable from a String to an integer, an operation called Type Casting. There are other forms of Type Casting in Java namely, Widening and Narrowing but these forms exist amongst primitive types — int, byte, short, long, char, boolean, double, float. But a String is a non-primitive type hence we have the line:

revenue = Integer.parseInt(revenueScanner.nextLine());

The nextLine() is a method in the Scanner class, it captures strings. There are other methods in this class to capture other variable types like nextInt(), nextByte(), nextBoolean() etc. You can view the Java API documentation here to view and refer to the full list of methods in this class. Then the last line simply tells Java to return the revenue integer.

In the getCost() method, the code is like getRevenue() method elaborated above. It involves getting the Total Cost from the user for a specific period and using the Scanner class, saving this input into the variable — cost. Hence:

public int getCost() {
    Scanner costScanner = new Scanner(System.in);
    System.out.println("What is your Total Cost? ");
    cost = Integer.parseInt(costScanner.nextLine());
    return cost;
  }

Then we have the grossProfit() method; this method simply calculates the difference between the revenue and cost and returns this difference. Note that the method has a return type — int.

public int grossProfit() {
    return revenue - cost;
  }

And finally, we have the profitMargin() method; this method was a bit tricky as the profit margin is calculated as the gross profit divided by the revenue. But since this would be a percentage, the profitMargin() must return a double or float(depending on the degree of accuracy that we a after). However, the grossProfit() method returns an integer and the getRevenue() method returns an integer, even the revenue variable has been declared earlier as an integer. This process of type dependency, relationship and consistency is what makes Java a static type language or as some would call it, type-safe language unlike other languages like Python which have dynamic typing. This feature of the Java language helps to prevent bugs in the long-run thereby reducing both maintenance costs and technical debt.

The solution to this is to cast both the grossProfit() and the revenue variable as a double, as follows since we are interested in a percentage or decimal value(1.226, 6.5, 200.345) and not a whole number value(2, 3, 45, 600):

public double profitMargin( ){
    return (((double) grossProfit() / (double) revenue));
  }

And that ends the program, we have finished writing the code for calculating gross profit and profit margin with just having two values — revenue and cost. Later, we can expand on this program by adding further metrics that can be extracted from a user’s given inputs.

In the Main.java file, we have the main() method. This method is the one that Java runs and every Java program must have a main() method. In the main method, we create an object of our Profitability class hence:

Profitability profitability = new Profitability();

This object instance gives the main method access to all the public attributes(variables) and methods in the Profitability.java file. After this, it’s just a simple exercise of copy and pasting the various methods in whatever other you want, ideally you would want to capture the revenue and cost from the user first before calculating the gross profit and profit margin as these two methods need those variables to have values if not you get an error. Hence:

 System.out.println(profitability.getRevenue());
 System.out.println(profitability.getCost());
 System.out.println(profitability.grossProfit());
 System.out.println(profitability.profitMargin());

And that’s it, we have gotten to the end of the first part of the Java Program Series. The next program we would be building is a program that calculates in number of steps, how far a place is; I call it walkingdistance.me.

Tags: , , ,
lare (pronounced as LAH-RAY) is a Computer Scientist and Creative Artist who loves creating different ideas with both technology and design. Whenever I'm learning a new technology or programming language, I like to create a journal of my journey - javabyprojects - is my journal for learning java.

0 Comments

Leave a Comment