SlideShare una empresa de Scribd logo
1 de 14
Descargar para leer sin conexión
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
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() {
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
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 :");
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:
{
//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
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
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 {
//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 + "
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;
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();
//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
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
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

Más contenido relacionado

Similar a Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf

assignmentTwoCar.javaassignmentTwoCar.javapackage assignmentTw.docx
assignmentTwoCar.javaassignmentTwoCar.javapackage assignmentTw.docxassignmentTwoCar.javaassignmentTwoCar.javapackage assignmentTw.docx
assignmentTwoCar.javaassignmentTwoCar.javapackage assignmentTw.docx
ssuser562afc1
 
1- Create a class called Point that has two instance variables, defi.pdf
1- Create a class called Point that has two instance variables, defi.pdf1- Create a class called Point that has two instance variables, defi.pdf
1- Create a class called Point that has two instance variables, defi.pdf
jeeteshmalani1
 
Hi,I have updated your code. It is working fine now. Highllighted .pdf
Hi,I have updated your code. It is working fine now. Highllighted .pdfHi,I have updated your code. It is working fine now. Highllighted .pdf
Hi,I have updated your code. It is working fine now. Highllighted .pdf
aniyathikitchen
 
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docxCSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
faithxdunce63732
 
please help with java questionsJAVA CODEplease check my code and.pdf
please help with java questionsJAVA CODEplease check my code and.pdfplease help with java questionsJAVA CODEplease check my code and.pdf
please help with java questionsJAVA CODEplease check my code and.pdf
arishmarketing21
 
3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf
atozshoppe
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
PRN USM
 

Similar a Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf (13)

Ch3
Ch3Ch3
Ch3
 
assignmentTwoCar.javaassignmentTwoCar.javapackage assignmentTw.docx
assignmentTwoCar.javaassignmentTwoCar.javapackage assignmentTw.docxassignmentTwoCar.javaassignmentTwoCar.javapackage assignmentTw.docx
assignmentTwoCar.javaassignmentTwoCar.javapackage assignmentTw.docx
 
1- Create a class called Point that has two instance variables, defi.pdf
1- Create a class called Point that has two instance variables, defi.pdf1- Create a class called Point that has two instance variables, defi.pdf
1- Create a class called Point that has two instance variables, defi.pdf
 
Csphtp1 10
Csphtp1 10Csphtp1 10
Csphtp1 10
 
3433 Ch10 Ppt
3433 Ch10 Ppt3433 Ch10 Ppt
3433 Ch10 Ppt
 
Class program and uml in c++
Class program and uml in c++Class program and uml in c++
Class program and uml in c++
 
Hi,I have updated your code. It is working fine now. Highllighted .pdf
Hi,I have updated your code. It is working fine now. Highllighted .pdfHi,I have updated your code. It is working fine now. Highllighted .pdf
Hi,I have updated your code. It is working fine now. Highllighted .pdf
 
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docxCSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
 
Abstract Classes Interface Exceptional Handling Java
Abstract Classes Interface Exceptional Handling JavaAbstract Classes Interface Exceptional Handling Java
Abstract Classes Interface Exceptional Handling Java
 
please help with java questionsJAVA CODEplease check my code and.pdf
please help with java questionsJAVA CODEplease check my code and.pdfplease help with java questionsJAVA CODEplease check my code and.pdf
please help with java questionsJAVA CODEplease check my code and.pdf
 
Csphtp1 06
Csphtp1 06Csphtp1 06
Csphtp1 06
 
3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
 

Más de ANJALIENTERPRISES1

17-The Y-Chromosome DNA testing helps in the examination of the male.pdf
17-The Y-Chromosome DNA testing helps in the examination of the male.pdf17-The Y-Chromosome DNA testing helps in the examination of the male.pdf
17-The Y-Chromosome DNA testing helps in the examination of the male.pdf
ANJALIENTERPRISES1
 
#includestdio.h#includestring.h#includestdlib.h#define M.pdf
#includestdio.h#includestring.h#includestdlib.h#define M.pdf#includestdio.h#includestring.h#includestdlib.h#define M.pdf
#includestdio.h#includestring.h#includestdlib.h#define M.pdf
ANJALIENTERPRISES1
 
goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
  goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf  goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
ANJALIENTERPRISES1
 

Más de ANJALIENTERPRISES1 (20)

H2SO4 may act as 1) an acidexample 2NaOH + H2SO4 - Na2SO4 +.pdf
H2SO4 may act as 1) an acidexample 2NaOH + H2SO4 - Na2SO4 +.pdfH2SO4 may act as 1) an acidexample 2NaOH + H2SO4 - Na2SO4 +.pdf
H2SO4 may act as 1) an acidexample 2NaOH + H2SO4 - Na2SO4 +.pdf
 
Computer graphics are pictures and movies produced use computers fre.pdf
Computer graphics are pictures and movies produced use computers fre.pdfComputer graphics are pictures and movies produced use computers fre.pdf
Computer graphics are pictures and movies produced use computers fre.pdf
 
C is correct. Only the host that the unicast message is addressed to.pdf
C is correct. Only the host that the unicast message is addressed to.pdfC is correct. Only the host that the unicast message is addressed to.pdf
C is correct. Only the host that the unicast message is addressed to.pdf
 
According to the seriousness level, the following organs areThymu.pdf
According to the seriousness level, the following organs areThymu.pdfAccording to the seriousness level, the following organs areThymu.pdf
According to the seriousness level, the following organs areThymu.pdf
 
Among 40 subjects randomly choose 20 subjects and assign themSol.pdf
Among 40 subjects randomly choose 20 subjects and assign themSol.pdfAmong 40 subjects randomly choose 20 subjects and assign themSol.pdf
Among 40 subjects randomly choose 20 subjects and assign themSol.pdf
 
AdvantagesThe main objective of business combination is to elimina.pdf
AdvantagesThe main objective of business combination is to elimina.pdfAdvantagesThe main objective of business combination is to elimina.pdf
AdvantagesThe main objective of business combination is to elimina.pdf
 
a. Germ cell speciication in Drosophila is primarily a maternlly con.pdf
a. Germ cell speciication in Drosophila is primarily a maternlly con.pdfa. Germ cell speciication in Drosophila is primarily a maternlly con.pdf
a. Germ cell speciication in Drosophila is primarily a maternlly con.pdf
 
A is correct. The packets to be filtered would be heading into the r.pdf
A is correct. The packets to be filtered would be heading into the r.pdfA is correct. The packets to be filtered would be heading into the r.pdf
A is correct. The packets to be filtered would be heading into the r.pdf
 
1). A) ipsilateralBelow the point of spinal cord, the nerve paths .pdf
1). A) ipsilateralBelow the point of spinal cord, the nerve paths .pdf1). A) ipsilateralBelow the point of spinal cord, the nerve paths .pdf
1). A) ipsilateralBelow the point of spinal cord, the nerve paths .pdf
 
-FSolution-F.pdf
-FSolution-F.pdf-FSolution-F.pdf
-FSolution-F.pdf
 
17-The Y-Chromosome DNA testing helps in the examination of the male.pdf
17-The Y-Chromosome DNA testing helps in the examination of the male.pdf17-The Y-Chromosome DNA testing helps in the examination of the male.pdf
17-The Y-Chromosome DNA testing helps in the examination of the male.pdf
 
#includestdio.h#includestring.h#includestdlib.h#define M.pdf
#includestdio.h#includestring.h#includestdlib.h#define M.pdf#includestdio.h#includestring.h#includestdlib.h#define M.pdf
#includestdio.h#includestring.h#includestdlib.h#define M.pdf
 
The pH and pOH of a solution are defined as pH .pdf
                     The pH and pOH of a solution are defined as  pH .pdf                     The pH and pOH of a solution are defined as  pH .pdf
The pH and pOH of a solution are defined as pH .pdf
 
goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
  goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf  goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
 
7 steps are required as 9 is the 7th element and the search is line.pdf
 7 steps are required as 9 is the 7th element and the search is line.pdf 7 steps are required as 9 is the 7th element and the search is line.pdf
7 steps are required as 9 is the 7th element and the search is line.pdf
 
(D) the number of moles of hydroxide ion added and the number of mol.pdf
  (D) the number of moles of hydroxide ion added and the number of mol.pdf  (D) the number of moles of hydroxide ion added and the number of mol.pdf
(D) the number of moles of hydroxide ion added and the number of mol.pdf
 
Look for changes in oxidation numbers. These occ.pdf
                     Look for changes in oxidation numbers.  These occ.pdf                     Look for changes in oxidation numbers.  These occ.pdf
Look for changes in oxidation numbers. These occ.pdf
 
D) Insulin .pdf
                     D) Insulin                                       .pdf                     D) Insulin                                       .pdf
D) Insulin .pdf
 
Use Daltons Law of partial pressures. P(Total).pdf
                     Use Daltons Law of partial pressures.  P(Total).pdf                     Use Daltons Law of partial pressures.  P(Total).pdf
Use Daltons Law of partial pressures. P(Total).pdf
 
The Br was originally neutral, but picks up an ex.pdf
                     The Br was originally neutral, but picks up an ex.pdf                     The Br was originally neutral, but picks up an ex.pdf
The Br was originally neutral, but picks up an ex.pdf
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Último (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 

Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf

  • 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