internship ppt on smartinternz platform as salesforce developer
Java program calculates shapes' areas
1. Circle.java
import java.text.DecimalFormat;
public class Circle {
//declaring variable
private double radius;
//Parameterized constructor
public Circle(double radius) {
this.radius = radius;
}
//Setters and getters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//calcArea() method is used to calculate the area of the circle
public double calcArea() {
double area=3.14*radius*radius;
return area;
}
//toString() method is used to display the contents of the Object inside it.
@Override
public String toString() {
DecimalFormat df=new DecimalFormat("#.##");
return " Circle# Radius=" + radius + " Area=" +df.format(calcArea());
}
}
___________________________________________________
Rectangle.java
2. import java.text.DecimalFormat;
public class Rectangle {
//declaring variable
private double length;
private double width;
//Parameterized constructor
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
//Setters and getters
public double getlength() {
return length;
}
public void setlength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
//calcArea() method is used to calculate the area of the Rectangle
public double calcArea() {
double area=width* length;
return area;
}
//calcArea() method is used to calculate the area of the circle
@Override
public String toString() {
3. DecimalFormat df=new DecimalFormat("#.##");
return " Rectangle# length=" + length + " Width=" + width + "
Area="+df.format(calcArea());
}
}
____________________________________________
Triangle.java
import java.text.DecimalFormat;
public class Triangle{
//declaring variable
private double base;
private double height;
//Parameterized constructor
public Triangle(double base,double height) {
this.base=base;
this.height=height;
}
//Setters and getters
public double getBase() {
return base;
}
public void setBase(double base) {
this.base = base;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//calcArea() method is used to calculate the area of the Triangle
4. public double calcArea() {
double area=0.5*base*height;
return area;
}
//calcArea() method is used to calculate the area of the circle
@Override
public String toString() {
DecimalFormat df=new DecimalFormat("#.##");
return " Triangle# Base=" + base + " Height=" + height + "
Area="+df.format(calcArea());
}
}
___________________________________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
int choice;
char ch;
//Scanner Object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//This loop will continue to execute until user enters other than 'y' or 'Y'
do
{
//Displaying the menu
System.out.println(" ::Program Which Calculates the Area of the following Shapes::");
System.out.println("::Menu::");
System.out.println("1.Rectangle");
System.out.println("2.Circle");
System.out.println("3.Triangle");
//Getting the choice entered by the user
System.out.print("Enter Choice :");
5. choice=sc.nextInt();
//Based on the users choice corresponding case will be executed.
switch(choice)
{
case 1:
{
//Getting the length of the Rectangle
System.out.print("Enter the Length :");
double length=sc.nextDouble();
//Getting the width of the Rectangle
System.out.print(" Enter the Width :");
double width=sc.nextDouble();
//Creating the Rectangle Object by passing the length and width as parameters
Rectangle rect=new Rectangle(length, width);
//Displaying the contents of the Rectangle Object
System.out.println(rect.toString());
break;
}
case 2:
{
//Getting the Radius of the Circle
System.out.print("Enter the Radius:");
double radius=sc.nextDouble();
//Creating the Circle Object by passing the radius as parameter
Circle c=new Circle(radius);
//Displaying the contents of the Circle Object
System.out.println(c.toString());
break;
}
case 3:
6. {
//getting the base of the triangle
System.out.print("Enter the Base:");
double base=sc.nextDouble();
//getting the Height of the triangle
System.out.print(" Enter the Height:");
double height=sc.nextDouble();
//Creating the triangle Object by passing the area and height as parameters
Triangle t=new Triangle(base,height);
System.out.println(t.toString());
break;
}
default :
{
//If the user entered choice other than 1 or 2 or 3 then this error message will be displayed
System.out.println("Invalid Choice,Enter Valid choice");
break;
}
}
//Getting the character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print("Do you want to continue(Y/N) ::");
ch = sc.next(".").charAt(0);
}while(ch=='y'|| ch=='Y');
}
}
______________________________________________
Output:
::Program Which Calculates the Area of the following Shapes::
::Menu::
1.Rectangle
2.Circle
3.Triangle
Enter Choice :1
7. Enter the Length :4
Enter the Width :5
Rectangle#
length=4.0
Width=5.0
Area=20
Do you want to continue(Y/N) ::y
::Program Which Calculates the Area of the following Shapes::
::Menu::
1.Rectangle
2.Circle
3.Triangle
Enter Choice :2
Enter the Radius:5.5
Circle#
Radius=5.5
Area=94.98
Do you want to continue(Y/N) ::y
::Program Which Calculates the Area of the following Shapes::
::Menu::
1.Rectangle
2.Circle
3.Triangle
Enter Choice :3
Enter the Base:6
Enter the Height:7
Triangle#
Base=6.0
Height=7.0
Area=21
Do you want to continue(Y/N) ::n
________________________________________________Thank You
Solution
Circle.java
8. import java.text.DecimalFormat;
public class Circle {
//declaring variable
private double radius;
//Parameterized constructor
public Circle(double radius) {
this.radius = radius;
}
//Setters and getters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//calcArea() method is used to calculate the area of the circle
public double calcArea() {
double area=3.14*radius*radius;
return area;
}
//toString() method is used to display the contents of the Object inside it.
@Override
public String toString() {
DecimalFormat df=new DecimalFormat("#.##");
return " Circle# Radius=" + radius + " Area=" +df.format(calcArea());
}
}
___________________________________________________
Rectangle.java
import java.text.DecimalFormat;
public class Rectangle {
9. //declaring variable
private double length;
private double width;
//Parameterized constructor
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
//Setters and getters
public double getlength() {
return length;
}
public void setlength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
//calcArea() method is used to calculate the area of the Rectangle
public double calcArea() {
double area=width* length;
return area;
}
//calcArea() method is used to calculate the area of the circle
@Override
public String toString() {
DecimalFormat df=new DecimalFormat("#.##");
return " Rectangle# length=" + length + " Width=" + width + "
10. Area="+df.format(calcArea());
}
}
____________________________________________
Triangle.java
import java.text.DecimalFormat;
public class Triangle{
//declaring variable
private double base;
private double height;
//Parameterized constructor
public Triangle(double base,double height) {
this.base=base;
this.height=height;
}
//Setters and getters
public double getBase() {
return base;
}
public void setBase(double base) {
this.base = base;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//calcArea() method is used to calculate the area of the Triangle
public double calcArea() {
double area=0.5*base*height;
11. return area;
}
//calcArea() method is used to calculate the area of the circle
@Override
public String toString() {
DecimalFormat df=new DecimalFormat("#.##");
return " Triangle# Base=" + base + " Height=" + height + "
Area="+df.format(calcArea());
}
}
___________________________________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
int choice;
char ch;
//Scanner Object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//This loop will continue to execute until user enters other than 'y' or 'Y'
do
{
//Displaying the menu
System.out.println(" ::Program Which Calculates the Area of the following Shapes::");
System.out.println("::Menu::");
System.out.println("1.Rectangle");
System.out.println("2.Circle");
System.out.println("3.Triangle");
//Getting the choice entered by the user
System.out.print("Enter Choice :");
choice=sc.nextInt();
12. //Based on the users choice corresponding case will be executed.
switch(choice)
{
case 1:
{
//Getting the length of the Rectangle
System.out.print("Enter the Length :");
double length=sc.nextDouble();
//Getting the width of the Rectangle
System.out.print(" Enter the Width :");
double width=sc.nextDouble();
//Creating the Rectangle Object by passing the length and width as parameters
Rectangle rect=new Rectangle(length, width);
//Displaying the contents of the Rectangle Object
System.out.println(rect.toString());
break;
}
case 2:
{
//Getting the Radius of the Circle
System.out.print("Enter the Radius:");
double radius=sc.nextDouble();
//Creating the Circle Object by passing the radius as parameter
Circle c=new Circle(radius);
//Displaying the contents of the Circle Object
System.out.println(c.toString());
break;
}
case 3:
{
//getting the base of the triangle
13. System.out.print("Enter the Base:");
double base=sc.nextDouble();
//getting the Height of the triangle
System.out.print(" Enter the Height:");
double height=sc.nextDouble();
//Creating the triangle Object by passing the area and height as parameters
Triangle t=new Triangle(base,height);
System.out.println(t.toString());
break;
}
default :
{
//If the user entered choice other than 1 or 2 or 3 then this error message will be displayed
System.out.println("Invalid Choice,Enter Valid choice");
break;
}
}
//Getting the character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print("Do you want to continue(Y/N) ::");
ch = sc.next(".").charAt(0);
}while(ch=='y'|| ch=='Y');
}
}
______________________________________________
Output:
::Program Which Calculates the Area of the following Shapes::
::Menu::
1.Rectangle
2.Circle
3.Triangle
Enter Choice :1
Enter the Length :4
Enter the Width :5
14. Rectangle#
length=4.0
Width=5.0
Area=20
Do you want to continue(Y/N) ::y
::Program Which Calculates the Area of the following Shapes::
::Menu::
1.Rectangle
2.Circle
3.Triangle
Enter Choice :2
Enter the Radius:5.5
Circle#
Radius=5.5
Area=94.98
Do you want to continue(Y/N) ::y
::Program Which Calculates the Area of the following Shapes::
::Menu::
1.Rectangle
2.Circle
3.Triangle
Enter Choice :3
Enter the Base:6
Enter the Height:7
Triangle#
Base=6.0
Height=7.0
Area=21
Do you want to continue(Y/N) ::n
________________________________________________Thank You