GLS653 – LAB 3
Java Program to Calculate Area of Rectangle
In this tutorial we will see how to calculate Area of Rectangle.
Program 1:
User would provide the length and width values during execution of the
program and the area would be calculated based on the provided values.
/**
* @programmer: Saharuddin bin Lin
* @description: Program to Calculate Area of rectangle
*/
import java.util.Scanner;
class AreaOfRectangle {
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
double width = scanner.nextDouble();
//Area = length*width;
double area = length*width;
System.out.println("Area of Rectangle is:"+area);
}
}
Output:
Output:
Enter the length of Rectangle:
2
Enter the width of Rectangle:
8
Area of Rectangle is:16.0
GLS653 – LAB 3
Program 2:
In the above program, user would be asked to provide the length and width
values. If you do not need user interaction and simply want to specify the
values in program, refer the below program.
/**
* @programmer: Saharuddin bin Lin
* @description: Calculation with no user interaction
*/
class AreaOfRectangle2 {
public static void main (String[] args)
{
double length = 4.5;
double width = 8.0;
double area = length*width;
System.out.println("Area of Rectangle is:"+area);
}
}
Output:
Area of Rectangle is:36.0
GLS653 – LAB 3
Java program to calculate area of Square
In this tutorial we will learn how to calculate area of Square. Following are the
two ways to do it:
1) Program 1: Prompting user for entering the side of the square
2) Program 2: Side of the square is specified in the program’ s source code.
Program 1:
/**
* @programmer: Saharuddin bin Lin
* @description: Program to Calculate Area of square.Program
* will prompt user for entering the side of the square.
*/
import java.util.Scanner;
class SquareAreaDemo {
public static void main (String[] args)
{
System.out.println("Enter Side of Square:");
//Capture the user's input
Scanner scanner = new Scanner(System.in);
//Storing the captured value in a variable
double side = scanner.nextDouble();
//Area of Square = side*side
double area = side*side;
System.out.println("Area of Square is: "+area);
}
}
Output:
Enter Side of Square:
2.5
Area of Square is: 6.25
GLS653 – LAB 3
Program 2:
/**
* @programmer: Saharuddin bin Lin
* @description: Program to Calculate Area of square.
* No user interaction: Side of square is hard-coded in the
* program itself.
*/
class SquareAreaDemo2 {
public static void main (String[] args)
{
//Value specified in the program itself
double side = 4.5;
//Area of Square = side*side
double area = side*side;
System.out.println("Area of Square is: "+area);
}
}
Output:
Area of Square is: 20.25
GLS653 – LAB 3
Java program to calculate area of Triangle
Here we will see how to calculate area of triangle. We will see two following
programs to do this:
1) Program 1: Prompt user for base-width and height of triangle.
2) Program 2: No user interaction: Width and height are specified in the
program itself.
Program 1:
/**
* @programmer: Saharuddin bin Lin
* @description: Program to Calculate area of Triangle in Java
* with user interaction. Program will prompt user to enter the
* base width and height of the triangle.
*/
import java.util.Scanner;
class AreaTriangleDemo {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the width of the Triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the Triangle:");
double height = scanner.nextDouble();
//Area = (width*height)/2
double area = (base* height)/2;
System.out.println("Area of Triangle is: " + area);
}
}
Output:
Enter the width of the Triangle:
2
Enter the height of the Triangle:
2
Area of Triangle is: 2.0
GLS653 – LAB 3
Program 2:
/**
* @programmer: Saharuddin bin Lin
* @description: Program to Calculate area of Triangle
* with no user interaction.
*/
class AreaTriangleDemo2 {
public static void main(String args[]) {
double base = 20.0;
double height = 110.5;
double area = (base* height)/2;
System.out.println("Area of Triangle is: " + area);
}
}
Output:
Area of Triangle is: 1105.0
GLS653 – LAB 3
Java Program to calculate area and
circumference of circle
In this tutorial we will see how to calculate area and circumference of circle in
Java. There are two ways to do this:
1) With user interaction: Program will prompt user to enter the radius of the
circle
2) Without user interaction: The radius value would be specified in the
program itself.
Program 1:
/**
* @programmer: Saharuddin bin Lin
* @description: Program to calculate area and circumference of
circle
* with user interaction. User will be prompt to enter the radius
and
* the result will be calculated based on the provided radius
value.
*/
import java.util.Scanner;
class CircleDemo
{
static Scanner sc = new Scanner(System.in);
public static void main(String args[])
{
System.out.print("Enter the radius: ");
/*We are storing the entered radius in double
* because a user can enter radius in decimals
*/
double radius = sc.nextDouble();
//Area = PI*radius*radius
double area = Math.PI * (radius * radius);
System.out.println("The area of circle is: " + area);
//Circumference = 2*PI*radius
double circumference= Math.PI * 2*radius;
System.out.println( "The circumference of the circle
is:"+circumference) ;
}
}
Output:
Enter the radius: 1
The area of circle is: 3.141592653589793
The circumference of the circle is:6.283185307179586
GLS653 – LAB 3
Program 2:
/**
* @programmer: Saharuddin bin Lin
* @description: Program to calculate area and circumference of
circle
* without user interaction. You need to specify the radius value
in
* program itself.
*/
class CircleDemo2
{
public static void main(String args[])
{
int radius = 3;
double area = Math.PI * (radius * radius);
System.out.println("The area of circle is: " + area);
double circumference= Math.PI * 2*radius;
System.out.println( "The circumference of the circle
is:"+circumference) ;
}
}
Output:
The area of circle is: 28.274333882308138
The circumference of the circle is:18.84955592153876