0% found this document useful (0 votes)
34 views

Constructor Programs

The first document demonstrates how to calculate the area of a triangle using user-defined methods in Java. It defines a main method that gets the base and height of a triangle from the user and calls a static AOT method to calculate and return the area. The second document similarly calculates the area of a triangle, but when given the lengths of all three sides, using a formula involving the semi-perimeter and Heron's formula. The third document defines an Employee class with constructor and methods to set and print employee details like name, age, designation and salary for two sample Employee objects. The last document uses method overloading to calculate the volumes of different shapes like a cube, rectangular prism and cone by defining volume methods that

Uploaded by

bhavaniselwyn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Constructor Programs

The first document demonstrates how to calculate the area of a triangle using user-defined methods in Java. It defines a main method that gets the base and height of a triangle from the user and calls a static AOT method to calculate and return the area. The second document similarly calculates the area of a triangle, but when given the lengths of all three sides, using a formula involving the semi-perimeter and Heron's formula. The third document defines an Employee class with constructor and methods to set and print employee details like name, age, designation and salary for two sample Employee objects. The last document uses method overloading to calculate the volumes of different shapes like a cube, rectangular prism and cone by defining volume methods that

Uploaded by

bhavaniselwyn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

To find the area of a Triangle Using User-Defined Method

import java.util.Scanner;

class Xyz

public static void main(String args[])

Scanner s= new Scanner(System.in);

System.out.println("Enter the width of the Triangle:");

double b= s.nextDouble();

System.out.println("Enter the height of the Triangle:");

double h= s.nextDouble();

double area=AOT(b,h);

System.out.println("Area of Triangle is: " + area);

static double AOT(double b,double h)

return ((b*h)/2);

Output:
Enter the width of the Triangle:
5
Enter the height of the Triangle:
3
Area of Triangle is: 7.5

---------------------------------------------------------------------------------------------

To Find the Area of a Triangle Using when three sides are given

import java.util.Scanner;

class AreaOfTriangle3

public static void main(String args[])

Scanner s1= new Scanner(System.in);


System.out.println("Enter the 1st side:");

int a= s1.nextInt();

System.out.println("Enter the 2nd side:");

int b= s1.nextInt();

System.out.println("Enter the 3rd side:");

int c= s1.nextInt();

if((a+b)>c && (a+c)>b && (b+c)>a)

int s=(a+b+c)/2;

double area=Math.sqrt(s*(s-a)*(s-b)*(s-c));

System.out.println("Area of Triangle is: " + area);

else

System.out.println("Area of Triangle not exit");

} }

Enter the 1st side:


10
Enter the 2nd side:
10
Enter the 3rd side:
10
Area of Triangle is:43.30

To print the details of an employee using constructor

public class Employee {

String name;
int age;
String designation;
double salary;

// This is the constructor of the class Employee


public Employee(String name) {
this.name = name;
}

// Assign the age of the Employee to the variable age.


public void empAge(int empAge) {
age = empAge;
}

/* Assign the designation to the variable designation.*/


public void empDesignation(String empDesig) {
designation = empDesig;
}

/* Assign the salary to the variable salary.*/


public void empSalary(double empSalary) {
salary = empSalary;
}

/* Print the Employee details */


public void printEmployee() {
System.out.println("Name:"+ name );
System.out.println("Age:" + age );
System.out.println("Designation:" + designation );
System.out.println("Salary:" + salary);
}
}
public class EmployeeTest {

public static void main(String args[]) {


/* Create two objects using constructor */
Employee empOne = new Employee("James Smith");
Employee empTwo = new Employee("Mary Anne");

// Invoking methods for each object created


empOne.empAge(26);
empOne.empDesignation("Senior Software Engineer");
empOne.empSalary(1000);
empOne.printEmployee();

empTwo.empAge(21);
empTwo.empDesignation("Software Engineer");
empTwo.empSalary(500);
empTwo.printEmployee();
}
}

Java Program to find the volume of cube, rextangular prism , cone using
polymorphism

class Main{
int volume(int side)
{
return side * side * side;
}
int volume(int length, int breadth, int height)
{
return length*breadth*height;
}
double volume(int radius, int height)
{ return 3.14 * (radius * radius)* height / 3;
}
public static void main(String[] args)
{
Main oe = new Main();
System.out.println("Volume of Cube " +oe.volume(10));
System.out.println("Volume of Rectangular prism " +oe.volume(10,12,30));
System.out.println("Volume of Cone "+oe.volume(5,10));
} }

Output:

You might also like