• TestingDocs
TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

JUnit

Calculator Java class

In this post, we will learn how to create a Calculator Java class with simple features like add, subtract, multiply and divide methods. Later, we will learn how to test this class with JUnit framework.

Things you need

1.JDK installed on your computer.

2. IDE like Eclipse or IntelliJ.

(Note that you can also create a class using a source or text editor and
save with a .java file extension. This approach has several drawbacks. You tend
to know the disadvantages when you work with more than two classes.
So let’s use an IDE)

Steps to follow:

Create a Java Project

For example : Calculator Project

File >> New >> Project

 

Create a source package

For example: com.testingdocs.calculator

Create a class

src >> right click >> New >> Java class

Eclipse

IntelliJ IDEA

 

 

3.Create a Java class and name it Calculator

Calculator Java class

This class models some of the functions of a simple calculator which handles mathematical operations.

package com.testingdocs.calculator;
/**
* Calculator class:
* Basic Mathematical functions like
* Add,Subtract,Multiply,Divide.
*
*/
public class Calculator {
//no-arg constructor
public Calculator() {
}
/**
* Sum method.
*/
public int add(int a, int b) {
return a + b;
}
/**
* Subtract method.
*/
public int subtract(int a, int b) {
return a - b;
}

/**
* Multiply method.
*/
public long multiply(int a, int b) {
return a * b;
}

/**
* Divide method.
* Note that this method throws an exception when
* b is zero.
*/
public double divide(int a, int b) {
double result;
if (b == 0) {
throw new IllegalArgumentException("Divisor cannot divide by zero");
} else {
result = Double.valueOf(a)/Double.valueOf(b);
}
return result;
}
}

Calculator Java Class

 

JUnit Tests for the Calculator class can be found here:

Calculator JUnit Tests

 

JUnit Tutorial on this website : https://www.testingdocs.com/junit-tutorial/

More information on JUnit can be found on the official website: https://junit.org

Related Posts

Getting Started with JUnit Framework

JUnit /

Getting Started with JUnit Framework

Run JUnit tests from command line

JUnit /

Run JUnit tests from command line

Working with JUnit in Eclipse IDE

JUnit /

Working with JUnit in Eclipse IDE

Adding JUnit5 library to a Project

JUnit /

Adding JUnit5 library to a Project

Test Failure JUnit

JUnit /

Debug JUnit Tests using IDE

‹ JUnit : Calculator Test Case Example› Unit testing Questions

Recent Posts

  • How to secure your SQL Database: Tips and Tricks
  • Shaping the Future of Development: Exploring Key Trends in Software Engineering
  • Improving Java Performance with Multithreading
  • Difference between PHP and JavaScript?
  • Bing Conversation Styles
  • ChatGPT Introduction
  • Open Source AI Frameworks
  • Artificial Intelligence Tools
  • Top AI Music Applications
  • Top AI Website Design Tools

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com