Computer Applications

Define a class called Triangle , Class and Objects based program

Define a class called Triangle with the following description:

Instance variables/Data members:          three side of triangle ( a, b, c )

Member methods:

  • input( ) – to input the data members values
  • display( )             – to display the values of data members
  • area( ) – to calculate the area of Triangle

Write a main( ) method where user will create an object of the class and call all the member functions.

import java.io.*;
import java.util.*;

class Triangle
{
	int a;
	int b;
	int c;

	void input( )
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a =");
		a = sc.nextInt();
		System.out.println("Enter b =");
		b = sc.nextInt();
		System.out.println("Enter c =");
		c = sc.nextInt();
	}

	void display( )
	{
		System.out.println(" a = "+a);
		System.out.println(" b = "+b);
		System.out.println(" c = "+c);
	}

	void area( )
	{
		double s = (a+b+c)/2;
		double ar = Math.sqrt(s*(s-a)*(s-b)*(s-c));
		System.out.println(" Area of Triangle = "+ar);
	}

	public static void main(String args[ ])
	{
		Triangle tn = new Triangle( );
		tn.input( );
		tn.display( );
		tn.area( );
	}
}

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top