![]() Length and breadth of rectangle are entered through keyboard. We should define a method to calculate the area of the rectangle and call it from the main method. Secondly: First method named as setDim takes length and breadth of rectangle as parameters. Previously In the Java program to find the area of Rectangle, we have written logic to find the area of the rectangle inside the main method. ![]() ![]() This class will take the height and width of the rectangle in its constructor. ("Area of Rectangle = "+ area) Īrea of Rectangle = 145.0 Area of the rectangle in Java using method Steps for the program: We will follow the following steps: Create one Rectangle class. Java program to find Area of Rectangle Java programming Example, this program will read length and width from the user and calculate area of rectangle. If we know the width and height of a rectangle, we can calculate the area of a rectangle using the formula: Area Width Height. take them as parameters, then calculate and output the area of the rectangle. create Scanner class object to read input The program you are given takes length and width of a rectangle as input. Program 1: User would provide the length and width values during execution of the program. We want to use Scanner class to read // data from console on the system, So we are using // a constructor which is taking an System.in object Scanner scanner new Scanner (System.in) // Create three variables which will hold the // values for length, breadth, area and perimeter of Rectangle. ![]() The final result will be a floating-point number hence, declare the area as the double data type. In this tutorial we will see how to calculate Area of Rectangle. The program asks the user for length and width values and then outputs the result in the console. So, it is a better idea to take width and length variable as double data type, because it can store both types of values. The following program calculates area of a rectangle in Java. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more. The value of width and height may be an integer or floating-point value. W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Procedures to write a Java program to calculate the area of the rectangle:-Ģ) Declare variables for taking inputs:- width and heightģ) Import Scanner class of util package to read inputsĤ) Read inputs from end-user and store them in the declared variablesĥ) Calculate area using the formula, and store it in a variable We will take length and width variables to store the value of length and width respectively. Here we will write a Java program to find the area of the rectangle. This also helps to achieve data hiding.The area of the Rectangle is given by length * width. ![]() It prevents outer classes from accessing and changing fields and methods of a class. Encapsulation refers to the bundling of fields and methods inside a single class. How to implement the java program to find out the area of a rectangle First, we have to declare the class AreaRect. Encapsulation is one of the key features of object-oriented programming. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |