Java Simple math

Lagre Download example files

We can do simple calculations using plus, minus, multiply and divide.

We can this create a program that calculates the area of a rectangle. The formula for the rectangle is:
Area = Length · Height


To create this program we need the following:

package area;
import java.util.Scanner;
public class Area {

        public static void main(String[] args) {
                // Scanner class
                Scanner scanner = new Scanner (;
                // Ask for lenght and witdth
                // Area = lengt*width
                System.out.println("This program calculates the area of a rectangle.");
                System.out.print("Enter length: ");
                double length = scanner.nextDouble();
                System.out.print("Enter width: ");
                double width = scanner.nextDouble();
                // Calcuate result
                double area = length*width;
                System.out.print("Area = " + area + "\n");



1. Create a program that calculates the volume of a prism with formula
Volume = lenght · width · height.

2. Expand the program in question 1 that one should be able to set a new lenght, width and height after the volume has been calculated. The program should not quit after the initial volume has been calculated. Hint: Search for java and while.

3. The surface area of a cylinder is calculated by A=2πr(r+h). Create a program that computes this.

4. Create a program that asks for the length, width and height of a prism. The program should ask whether the user wants to see the surface area or volume.

Add comment






No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.