SlideShare una empresa de Scribd logo
1 de 3
Design a class named Rectangle to represent a rectangle. The class contains the following.
1. Two double data fields named width and height that specify the width and height of the
rectangle. The default values are 1 for both fields.
2. A no-arg constructor that creates a default rectangle.
3. A constructor that creates a rectangle with the specified width and height.
4. A method named getArea() that returns the area of the rectangle.
5. A named method getPerimeter() that returns the perimeter.
Draw the UML diagram for the class and include it in your report. Implement the class. Write a
test or application program that creates two Rectangle objects—one with width 4 and height 40,
and the other with width 3.5 and height 35.9. Display the width, height, area and perimeter of
each rectangle in this order
Solution
Rectangle.java
public class Rectangle {
private double width;
private double height;
private final double PI = 3.14;
public Rectangle(){
}
public Rectangle(double width, double height){
this.width = width;
this.height = height;
}
public double getArea(){
return width * height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getPerimeter(){
return 2 * (height + width);
}
public String toString(){
return String.format("Rectangle: Area = %.2f , Perimeter = %.2f ",getArea(),getPerimeter());
}
}
RectangleTest.java
public class RectangleTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle r1 = new Rectangle();
r1.setWidth(4);
r1.setHeight(40);
Rectangle r2 = new Rectangle();
r2.setWidth(3.5);
r2.setHeight(35.9);
System.out.println("Rectangle Width "+r1.getWidth()+" Height :"+r1.getHeight());
System.out.println(r1.toString());
System.out.println("Rectangle Width "+r2Â Â .getWidth()+" Height :"+r2.getHeight());
System.out.println(r2.toString());
}
}
Output:
Rectangle Width 4.0 Height :40.0
Rectangle: Area = 160.00 , Perimeter = 88.00
Rectangle Width 3.5 Height :35.9
Rectangle: Area = 125.65 , Perimeter = 78.80

Más contenido relacionado

Similar a Design a class named Rectangle to represent a rectangle- The class con.docx

Chapter 6.5
Chapter 6.5Chapter 6.5
Chapter 6.5sotlsoc
 
You still work for packaging company that makes boxes and cylindrical.docx
 You still work for packaging company that makes boxes and cylindrical.docx You still work for packaging company that makes boxes and cylindrical.docx
You still work for packaging company that makes boxes and cylindrical.docxajoy21
 
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.docxfaithxdunce63732
 
Csphtp1 10
Csphtp1 10Csphtp1 10
Csphtp1 10HUST
 
Class program and uml in c++
Class program and uml in c++Class program and uml in c++
Class program and uml in c++Osama Al-Mohaia
 
C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)Umar Farooq
 
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7helpido9
 
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7ashhadiqbal
 
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7helpido6
 
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7solutionjug4
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingHock Leng PUAH
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console ProgramHock Leng PUAH
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritanceNurhanna Aziz
 
1.what is the difference between a instance variable and an local va.pdf
1.what is the difference between a instance variable and an local va.pdf1.what is the difference between a instance variable and an local va.pdf
1.what is the difference between a instance variable and an local va.pdfarchgeetsenterprises
 
Assessments
AssessmentsAssessments
Assessmentshccit
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Palak Sanghani
 

Similar a Design a class named Rectangle to represent a rectangle- The class con.docx (20)

Chapter 6.5
Chapter 6.5Chapter 6.5
Chapter 6.5
 
Methods
MethodsMethods
Methods
 
You still work for packaging company that makes boxes and cylindrical.docx
 You still work for packaging company that makes boxes and cylindrical.docx You still work for packaging company that makes boxes and cylindrical.docx
You still work for packaging company that makes boxes and cylindrical.docx
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
 
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
 
Csphtp1 10
Csphtp1 10Csphtp1 10
Csphtp1 10
 
3433 Ch10 Ppt
3433 Ch10 Ppt3433 Ch10 Ppt
3433 Ch10 Ppt
 
Uta005
Uta005Uta005
Uta005
 
Class program and uml in c++
Class program and uml in c++Class program and uml in c++
Class program and uml in c++
 
C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)
 
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7
 
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7
 
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7
 
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem Solving
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console Program
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritance
 
1.what is the difference between a instance variable and an local va.pdf
1.what is the difference between a instance variable and an local va.pdf1.what is the difference between a instance variable and an local va.pdf
1.what is the difference between a instance variable and an local va.pdf
 
Assessments
AssessmentsAssessments
Assessments
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 

Más de earleanp

Create your own variant of both a hiring and a termination policy rela.docx
Create your own variant of both a hiring and a termination policy rela.docxCreate your own variant of both a hiring and a termination policy rela.docx
Create your own variant of both a hiring and a termination policy rela.docxearleanp
 
Determine the valuation of long-term liabilities- Donald Lennon is the.docx
Determine the valuation of long-term liabilities- Donald Lennon is the.docxDetermine the valuation of long-term liabilities- Donald Lennon is the.docx
Determine the valuation of long-term liabilities- Donald Lennon is the.docxearleanp
 
Describe three of the following attack types in the Operation Security.docx
Describe three of the following attack types in the Operation Security.docxDescribe three of the following attack types in the Operation Security.docx
Describe three of the following attack types in the Operation Security.docxearleanp
 
Describes the concept of ADTS and illustrates the concept with three o.docx
Describes the concept of ADTS and illustrates the concept with three o.docxDescribes the concept of ADTS and illustrates the concept with three o.docx
Describes the concept of ADTS and illustrates the concept with three o.docxearleanp
 
Describe- manage- and install Active Directory replication- federation.docx
Describe- manage- and install Active Directory replication- federation.docxDescribe- manage- and install Active Directory replication- federation.docx
Describe- manage- and install Active Directory replication- federation.docxearleanp
 
Describe the process to start and restart apache on CENTOS command lin.docx
Describe the process to start and restart apache on CENTOS command lin.docxDescribe the process to start and restart apache on CENTOS command lin.docx
Describe the process to start and restart apache on CENTOS command lin.docxearleanp
 
Describe- in your own words- the mechanism for establishing a HTTPS co.docx
Describe- in your own words- the mechanism for establishing a HTTPS co.docxDescribe- in your own words- the mechanism for establishing a HTTPS co.docx
Describe- in your own words- the mechanism for establishing a HTTPS co.docxearleanp
 
Describe the process of creating and exporting a schedule report for t.docx
Describe the process of creating and exporting a schedule report for t.docxDescribe the process of creating and exporting a schedule report for t.docx
Describe the process of creating and exporting a schedule report for t.docxearleanp
 
Describe the principal technologies that have shaped contemporary tele.docx
Describe the principal technologies that have shaped contemporary tele.docxDescribe the principal technologies that have shaped contemporary tele.docx
Describe the principal technologies that have shaped contemporary tele.docxearleanp
 
Describe the typical duties of a security manager that are strictly ma.docx
Describe the typical duties of a security manager that are strictly ma.docxDescribe the typical duties of a security manager that are strictly ma.docx
Describe the typical duties of a security manager that are strictly ma.docxearleanp
 
Describe the four categories of international airports in the federal.docx
Describe the four categories of international airports in the federal.docxDescribe the four categories of international airports in the federal.docx
Describe the four categories of international airports in the federal.docxearleanp
 
Describe the major types of VPNs and technologies- protocols- and serv.docx
Describe the major types of VPNs and technologies- protocols- and serv.docxDescribe the major types of VPNs and technologies- protocols- and serv.docx
Describe the major types of VPNs and technologies- protocols- and serv.docxearleanp
 
Describe the different metrics that BGP can use in building a routing.docx
Describe the different metrics that BGP can use in building a routing.docxDescribe the different metrics that BGP can use in building a routing.docx
Describe the different metrics that BGP can use in building a routing.docxearleanp
 
Describe the ethnic city and the benefit of ethnic communiti- (-I need.docx
Describe the ethnic city and the benefit of ethnic communiti- (-I need.docxDescribe the ethnic city and the benefit of ethnic communiti- (-I need.docx
Describe the ethnic city and the benefit of ethnic communiti- (-I need.docxearleanp
 
Describe the different types of qualitative analysis and indicate whic.docx
Describe the different types of qualitative analysis and indicate whic.docxDescribe the different types of qualitative analysis and indicate whic.docx
Describe the different types of qualitative analysis and indicate whic.docxearleanp
 
Describe neo-evolution- What is it and what are its primary tenets- Pr.docx
Describe neo-evolution- What is it and what are its primary tenets- Pr.docxDescribe neo-evolution- What is it and what are its primary tenets- Pr.docx
Describe neo-evolution- What is it and what are its primary tenets- Pr.docxearleanp
 
Describe ip protocol security pros and cons-SolutionIP Protocol Securi.docx
Describe ip protocol security pros and cons-SolutionIP Protocol Securi.docxDescribe ip protocol security pros and cons-SolutionIP Protocol Securi.docx
Describe ip protocol security pros and cons-SolutionIP Protocol Securi.docxearleanp
 
Describe core competencies and their relationship to operations manage.docx
Describe core competencies and their relationship to operations manage.docxDescribe core competencies and their relationship to operations manage.docx
Describe core competencies and their relationship to operations manage.docxearleanp
 
Describe in detail a man-in-the-middle attack on the Diffie-Hellman ke.docx
Describe in detail a man-in-the-middle attack on the Diffie-Hellman ke.docxDescribe in detail a man-in-the-middle attack on the Diffie-Hellman ke.docx
Describe in detail a man-in-the-middle attack on the Diffie-Hellman ke.docxearleanp
 
Describe events that led to the signing of the Homeland Security Act 2.docx
Describe events that led to the signing of the Homeland Security Act 2.docxDescribe events that led to the signing of the Homeland Security Act 2.docx
Describe events that led to the signing of the Homeland Security Act 2.docxearleanp
 

Más de earleanp (20)

Create your own variant of both a hiring and a termination policy rela.docx
Create your own variant of both a hiring and a termination policy rela.docxCreate your own variant of both a hiring and a termination policy rela.docx
Create your own variant of both a hiring and a termination policy rela.docx
 
Determine the valuation of long-term liabilities- Donald Lennon is the.docx
Determine the valuation of long-term liabilities- Donald Lennon is the.docxDetermine the valuation of long-term liabilities- Donald Lennon is the.docx
Determine the valuation of long-term liabilities- Donald Lennon is the.docx
 
Describe three of the following attack types in the Operation Security.docx
Describe three of the following attack types in the Operation Security.docxDescribe three of the following attack types in the Operation Security.docx
Describe three of the following attack types in the Operation Security.docx
 
Describes the concept of ADTS and illustrates the concept with three o.docx
Describes the concept of ADTS and illustrates the concept with three o.docxDescribes the concept of ADTS and illustrates the concept with three o.docx
Describes the concept of ADTS and illustrates the concept with three o.docx
 
Describe- manage- and install Active Directory replication- federation.docx
Describe- manage- and install Active Directory replication- federation.docxDescribe- manage- and install Active Directory replication- federation.docx
Describe- manage- and install Active Directory replication- federation.docx
 
Describe the process to start and restart apache on CENTOS command lin.docx
Describe the process to start and restart apache on CENTOS command lin.docxDescribe the process to start and restart apache on CENTOS command lin.docx
Describe the process to start and restart apache on CENTOS command lin.docx
 
Describe- in your own words- the mechanism for establishing a HTTPS co.docx
Describe- in your own words- the mechanism for establishing a HTTPS co.docxDescribe- in your own words- the mechanism for establishing a HTTPS co.docx
Describe- in your own words- the mechanism for establishing a HTTPS co.docx
 
Describe the process of creating and exporting a schedule report for t.docx
Describe the process of creating and exporting a schedule report for t.docxDescribe the process of creating and exporting a schedule report for t.docx
Describe the process of creating and exporting a schedule report for t.docx
 
Describe the principal technologies that have shaped contemporary tele.docx
Describe the principal technologies that have shaped contemporary tele.docxDescribe the principal technologies that have shaped contemporary tele.docx
Describe the principal technologies that have shaped contemporary tele.docx
 
Describe the typical duties of a security manager that are strictly ma.docx
Describe the typical duties of a security manager that are strictly ma.docxDescribe the typical duties of a security manager that are strictly ma.docx
Describe the typical duties of a security manager that are strictly ma.docx
 
Describe the four categories of international airports in the federal.docx
Describe the four categories of international airports in the federal.docxDescribe the four categories of international airports in the federal.docx
Describe the four categories of international airports in the federal.docx
 
Describe the major types of VPNs and technologies- protocols- and serv.docx
Describe the major types of VPNs and technologies- protocols- and serv.docxDescribe the major types of VPNs and technologies- protocols- and serv.docx
Describe the major types of VPNs and technologies- protocols- and serv.docx
 
Describe the different metrics that BGP can use in building a routing.docx
Describe the different metrics that BGP can use in building a routing.docxDescribe the different metrics that BGP can use in building a routing.docx
Describe the different metrics that BGP can use in building a routing.docx
 
Describe the ethnic city and the benefit of ethnic communiti- (-I need.docx
Describe the ethnic city and the benefit of ethnic communiti- (-I need.docxDescribe the ethnic city and the benefit of ethnic communiti- (-I need.docx
Describe the ethnic city and the benefit of ethnic communiti- (-I need.docx
 
Describe the different types of qualitative analysis and indicate whic.docx
Describe the different types of qualitative analysis and indicate whic.docxDescribe the different types of qualitative analysis and indicate whic.docx
Describe the different types of qualitative analysis and indicate whic.docx
 
Describe neo-evolution- What is it and what are its primary tenets- Pr.docx
Describe neo-evolution- What is it and what are its primary tenets- Pr.docxDescribe neo-evolution- What is it and what are its primary tenets- Pr.docx
Describe neo-evolution- What is it and what are its primary tenets- Pr.docx
 
Describe ip protocol security pros and cons-SolutionIP Protocol Securi.docx
Describe ip protocol security pros and cons-SolutionIP Protocol Securi.docxDescribe ip protocol security pros and cons-SolutionIP Protocol Securi.docx
Describe ip protocol security pros and cons-SolutionIP Protocol Securi.docx
 
Describe core competencies and their relationship to operations manage.docx
Describe core competencies and their relationship to operations manage.docxDescribe core competencies and their relationship to operations manage.docx
Describe core competencies and their relationship to operations manage.docx
 
Describe in detail a man-in-the-middle attack on the Diffie-Hellman ke.docx
Describe in detail a man-in-the-middle attack on the Diffie-Hellman ke.docxDescribe in detail a man-in-the-middle attack on the Diffie-Hellman ke.docx
Describe in detail a man-in-the-middle attack on the Diffie-Hellman ke.docx
 
Describe events that led to the signing of the Homeland Security Act 2.docx
Describe events that led to the signing of the Homeland Security Act 2.docxDescribe events that led to the signing of the Homeland Security Act 2.docx
Describe events that led to the signing of the Homeland Security Act 2.docx
 

Último

Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
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 PractiseAnaAcapella
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Último (20)

Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
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
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

Design a class named Rectangle to represent a rectangle- The class con.docx

  • 1. Design a class named Rectangle to represent a rectangle. The class contains the following. 1. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both fields. 2. A no-arg constructor that creates a default rectangle. 3. A constructor that creates a rectangle with the specified width and height. 4. A method named getArea() that returns the area of the rectangle. 5. A named method getPerimeter() that returns the perimeter. Draw the UML diagram for the class and include it in your report. Implement the class. Write a test or application program that creates two Rectangle objects—one with width 4 and height 40, and the other with width 3.5 and height 35.9. Display the width, height, area and perimeter of each rectangle in this order Solution Rectangle.java public class Rectangle { private double width; private double height; private final double PI = 3.14; public Rectangle(){ } public Rectangle(double width, double height){ this.width = width; this.height = height; } public double getArea(){ return width * height; }
  • 2. public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getPerimeter(){ return 2 * (height + width); } public String toString(){ return String.format("Rectangle: Area = %.2f , Perimeter = %.2f ",getArea(),getPerimeter()); } } RectangleTest.java public class RectangleTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Rectangle r1 = new Rectangle(); r1.setWidth(4); r1.setHeight(40); Rectangle r2 = new Rectangle(); r2.setWidth(3.5); r2.setHeight(35.9); System.out.println("Rectangle Width "+r1.getWidth()+" Height :"+r1.getHeight()); System.out.println(r1.toString()); System.out.println("Rectangle Width "+r2Â Â .getWidth()+" Height :"+r2.getHeight()); System.out.println(r2.toString()); } }
  • 3. Output: Rectangle Width 4.0 Height :40.0 Rectangle: Area = 160.00 , Perimeter = 88.00 Rectangle Width 3.5 Height :35.9 Rectangle: Area = 125.65 , Perimeter = 78.80