Write a program in Java by using a class with the following specifications class name :- rectangle data members/instance variables :- int length and breadth of the rectangle
Member functions:-
void input data () :- to accept length and breadth of the rectangle
void calculate () :- to find area, perimeter and diagnol of the rectangle
void output data () :- to print the results
Answers
import java.util.*;
import java.lang.*;
import java.io.*;
class rectangle
{
int length, breadth, a, p, d;
void input_data()
{
Scanner Sc=new Scanner(System.in);
System.out.print("Enter length : ");
length = Sc.nextInt();
System.out.print("Enter breadth : ");
breadth = Sc.nextInt();
}
void calculate()
{
a = length*breadth;
p = 2*(length+breadth);
d = (int)Math.sqrt(length*length + breadth*breadth);
}
void output_data()
{
System.out.println("Area : "+a);
System.out.println("Perimeter : "+p);
System.out.println("Diagonal :"+d);
}
public static void main (String[] args) throws java.lang.Exception
{
rectangle rect = new rectangle();
rect.input_data();
rect.calculate();
rect.output_data();
}
}