+ 2
Java methods please i need help
java methods Write a program to print the area of a rectangle by creating a class named 'Area' having two methods. First method named as 'setDim' takes length and breadth of rectangle as parameters and the second method named as 'getArea' returns the area of the rectangle. Length and breadth of rectangle are entered through keyboard.
8 ответов
+ 2
Mirielle🐶 [Inactive]
You confused it.
void setter and <return type> getter.
Nana Kofi
Show us your code and we will help you.
+ 1
It would be easier if you can show us your try.
+ 1
About setter and getter:
https://www.sololearn.com/learn/Java/2154/?ref=app
0
//Actually this is the question. It was my quiz question but i am not sure about my answer. Here the question goes
//Write a java program to print the area of a rectangle by creating a class RectArea having two methods. The first method setDim takes length and breadth from the user as integer parameters. The second method getArea returns the area of the rectangle and prints it on the screen.
public class RectArea{
int length,breadth;
RectArea(int l, int b){
length = l;
breadth = b;
}
public int setDim(){
int results = length * breadth;
return results;
}
public void getArea(){
System.out.println("RectArea = " + setDim());
}
public static void main(String []args){
RectArea x =new RectArea(4,5);
x.getArea();
}
}
0
Your constructor is okay but in this case you don't need to write one.
Read the description carefully.
setDim() takes length and breath from the user. Not more and not less.
getArea(): calculate this area and return the value and print it.
public void setDim(int length, int breadth){
this.length = length;
this.breadth = breadth;
}
public int getArea(){
System.out.println(length * breadth);
return length * breadth
}
It is a bit confusing that a method which should return a value also have to print it. Normally I only do this to test a method.
0
please your page cannot be found
0
Nana Kofi
Strange. The link works for me.
Click on Java Tutorial -> click on Objects & Classes -> click on Getters & Setters.
0
Denise Roßberg
Thank you. I wasn't using the app at then..