+ 1
Java Area of rectangle calculator. Please help!
I have no idea how to do this 😬😬 I tried but it never works. "The program you are given takes length and width of a rectangle as input. Complete the method to take them as parameters, then calculate and output the area of the rectangle." import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int width = read.nextInt(); int height = read.nextInt(); printArea(width, height); } public static void printArea() { System.out.println(width*height); } }
9 ответов
+ 4
Emma Watson thank you very much for help! But I don't know why I should write 'int width, int height' again in the method. Isn't it already done from 'printArea(width, height);' in the 'main' method?
+ 3
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int width = read.nextInt();
int height = read.nextInt();
printArea(width, height);
}
//Please Subscribe to My Youtube Channel
//Channel Name: Fazal Tuts4U
public static void printArea(int width, int height) {
System.out.println(width*height);
}
}
+ 2
Enter the parameters/function signature in the printArea method as mentioned by Emma Watson
+ 1
Jay Kay
You didn't pass parameters in defined method.
+ 1
Because it is mentioned in the main method to input in int
+ 1
0
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner width = new Scanner(System.in);
Scanner height = new Scanner(System.in);
double The_height = height.nextDouble();
double The_width = width.nextDouble();
area(The_width, The_height);
}
static void area(double width, double height){
System.out.println("area = " + width * height);
}
}
0
A rectangle is a four-sided shape with right angles. Its opposite sides have equal length, and diagonals bisect at right angles.
Formula:
Area = Length * Width
Example:
Length = 10
Width = 8
Area of Rectangle is 80
Find Area of Rectangle in Java
//Area of Rectangle in Java
import java.util.Scanner;
public class RectangleArea {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the length : ");
double length = scanner.nextDouble();
System.out.print("Enter the width : ");
double width = scanner.nextDouble();
scanner.close();
// calculate area of rectangle
double area = length * width;
System.out.println("The area of the rectangle is: " + area);
}
}
Output
C:\CodeRevise\java>javac RectangleArea.java
C:\CodeRevise\java>java RectangleArea
Enter the length : 10
Enter the width : 7
The area of the rectangle is: 70.0
for more java programs kindly visit:- https://coderevise.com/java-programs/
- 1
Jay Kay
In main method you are calling that defined method so 1 is defined method and 1 is called method. Both should be same. In calling method we just pass same datatype value in same order in which we have defined.
Calling method should be same as defined method otherwise compiler will not understand like If I call you with other name then you can not understand that I am calling you or someone else.