Program 1: Using Scanner Class
/*
Algorithm:
---------------
1. Get the base of the parallelogram from the user.
2. Get the altitude or height of the parallelogram from the user.
3. Calculate the area of parallelogram.
4. Print the area.
*/
//importing java package
import java.util.Scanner;
public class AreaOfParallelogram1{
public static void main (String[] args){
//creating new Scanner
Scanner sc = new Scanner (System.in);
System.out.println ("Please enter the base of the parallelogram :: ");
double base = sc.nextDouble();
System.out.println ("Please enter the height of the parallelogram :: ");
double height = sc.nextDouble();
//calculate area of parallelogram
double area = base * height;
//print the area
System.out.println ("Area of the Parallelogram is :: "+area);
}
}
Save this file as 'AreaOfParallelogram1.java'
Compile: $javac AreaOfParallelogram1.java
Execute: $java AreaOfParallelogram1
Output:
Please enter the base of the parallelogram ::
4
Please enter the height of the parallelogram ::
18
Area of the Parallelogram is :: 72.0
Program 2: Using Java BufferedReader Class
/*
Algorithm:
---------------
1. Get the base of the parallelogram from the user.
2. Get the altitude or height of the parallelogram from the user.
3. Calculate the area of parallelogram.
4. Print the area.
*/
Algorithm:
---------------
1. Get the base of the parallelogram from the user.
2. Get the altitude or height of the parallelogram from the user.
3. Calculate the area of parallelogram.
4. Print the area.
*/
//importing java packages
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class AreaOfParallelogram2{
public static void main (String[] args)
throws IOException{
//creating new InputStreamReader
InputStreamReader is = new InputStreamReader (System.in);
//creating new BufferedReader
BufferedReader br = new BufferedReader (is);
System.out.println ("Please enter the base of the parallelogram :: ");
double base = Double.parseDouble (br.readLine());
System.out.println ("Please enter the height of the parallelogram :: ");
double height = Double.parseDouble (br.readLine());
//calculate area of parallelogram
double area = base * height;
//print the area
System.out.println ("Area of the Parallelogram is :: "+area);
}
}
Save this file as 'AreaOfParallelogram2.java'
Compile: $javac AreaOfParallelogram2.java
Execute: $java AreaOfParallelogram2
Output:
Please enter the base of the parallelogram ::
5
Please enter the height of the parallelogram ::
15
Area of the Parallelogram is :: 75.0
0 Comments