{"id":2016,"date":"2017-05-15T06:30:53","date_gmt":"2017-05-15T06:30:53","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2016"},"modified":"2024-09-04T15:40:49","modified_gmt":"2024-09-04T15:40:49","slug":"java-program-to-calculate-area-of-rectangle","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/java-program-to-calculate-area-of-rectangle\/","title":{"rendered":"Java Program to Calculate Area of Rectangle"},"content":{"rendered":"\r\n\r\n\r\n<h1>Java Program to Calculate Area of Rectangle<\/h1>\r\n<p>In this post, we will write a java program to calculate the area of the rectangle and perimeter of the Rectangle. We will have two java classes.<\/p>\r\n<ul>\r\n<li><strong>Rectangle class<\/strong><\/li>\r\n<li><strong>RectangleMain driver class<\/strong><\/li>\r\n<\/ul>\r\n<h2>Eclipse IDE Setup<\/h2>\r\n<p>Some steps and instructions to create a Java application on Eclipse IDE:<\/p>\r\n<ul>\r\n<li>Create Java Project in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/<\/a><\/p>\r\n<ul>\r\n<li>Create Java Package in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/\">https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/<\/a><\/p>\r\n<ul>\r\n<li>Create Java Class in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/\">https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/<\/a><\/p>\r\n<ul>\r\n<li>Run Java Project in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/<\/a><\/p>\r\n<h2>Java Program<\/h2>\r\n<h3>Rectangle class<\/h3>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/**************************************************\r\n * Rectangle.java class\r\n * @program   \t: \r\n * @web        \t: www.TestingDocs.com\r\n * @author      :  \r\n * @version     : 1.0\r\n **************************************************\/\r\n\r\npublic class Rectangle {\r\n    private double height;\r\n  private double width;\r\n\r\n  \/**\r\n   * @param height\r\n   * @param width\r\n   *\/\r\n  public Rectangle(double height, double width) {\r\n    this.height = height;\r\n    this.width = width;\r\n  }\r\n\r\n  \/**\r\n   * @return the height\r\n   *\/\r\n  public double getHeight() {\r\n    return height;\r\n  }\r\n\r\n  \/**\r\n   * @param height the height to set\r\n   *\/\r\n  public void setHeight(double height) {\r\n    this.height = height;\r\n  }\r\n\r\n  \/**\r\n   * @return the width\r\n   *\/\r\n  public double getWidth() {\r\n    return width;\r\n  }\r\n\r\n  \/**\r\n   * @param width the width to set\r\n   *\/\r\n  public void setWidth(double width) {\r\n    this.width = width;\r\n  }\r\n\r\n  \/\/calculating Area of the rectangle\r\n  public double calculateArea() {\r\n    return  width * height;\r\n  }\r\n\r\n  \/\/calculating perimeter of the rectangle\r\n  public double calculatePerimeter() {\r\n    return  2*(width + height);\r\n  }\r\n}\r\n\r\n<\/pre>\r\n<h3>RectangleMain<\/h3>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/**************************************************\r\n * RectangleMain.java\r\n * @program   \t: Demo program of Rectangle class\r\n * @web        \t: www.TestingDocs.com\r\n * @author      :  \r\n * @version     : 1.0\r\n **************************************************\/\r\n\r\npublic class RectangleMain {\r\n  public static void main(String args[]) {\r\n    \/\/ Creating Rectangle object\r\n    Rectangle r1 = new Rectangle(5.0,3.5);\r\n\r\n    System.out.println(\"--------------Output----------\");\r\n    System.out.println(\"Height of r1 in centimeter = \" + r1.getHeight());\r\n    System.out.println(\"Width of r1 in centimeter = \" + r1.getWidth());\r\n    System.out.println(\"Area of r1 = \" + r1.calculateArea() + \" cm ^2\");\r\n    System.out.println(\"Perimeter of r1 = \" + r1.calculatePerimeter() + \" cm\");\r\n\r\n  }\r\n}<\/pre>\r\n<p>&nbsp;<\/p>\r\n<h2>Output<\/h2>\r\n<p>&#8212;&#8212;&#8212;&#8212;&#8211;Output&#8212;&#8212;&#8212;-<br \/>Height of r1 in centimeter = 5.0<br \/>Width of r1 in centimeter = 3.5<br \/>Area of r1 = 17.5 cm ^2<br \/>Perimeter of r1 = 17.0 cm<\/p>\r\n<h2>Screenshot<\/h2>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2020 size-full\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Rectangle-Java-Program.png\" alt=\"\" width=\"1727\" height=\"1024\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Rectangle-Java-Program.png 1727w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Rectangle-Java-Program-300x178.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Rectangle-Java-Program-1024x607.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Rectangle-Java-Program-768x455.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Rectangle-Java-Program-1536x911.png 1536w\" sizes=\"auto, (max-width: 1727px) 100vw, 1727px\" \/><\/p>\r\n<p>&#8212;<\/p>\r\n<p>Java Tutorial on this website: <strong><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/strong><\/p>\r\n<p>For more information on Java, visit the official website :<\/p>\r\n<p><strong><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/strong><\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Java Program to Calculate Area of Rectangle In this post, we will write a java program to calculate the area of the rectangle and perimeter of the Rectangle. We will have two java classes. Rectangle class RectangleMain driver class Eclipse IDE Setup Some steps and instructions to create a Java application on Eclipse IDE: Create [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-2016","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=2016"}],"version-history":[{"count":9,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2016\/revisions"}],"predecessor-version":[{"id":24190,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2016\/revisions\/24190"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}