+ 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.

19th Dec 2019, 9:21 PM
Nana Kofi
Nana Kofi  - avatar
10 Answers
+ 2
MirielleđŸ¶ [Inactive] You confused it. void setter and <return type> getter. Nana Kofi Show us your code and we will help you.
20th Dec 2019, 12:29 AM
Denise Roßberg
Denise Roßberg - avatar
+ 1
It would be easier if you can show us your try.
19th Dec 2019, 11:10 PM
Michal
Michal - avatar
20th Dec 2019, 9:30 PM
Denise Roßberg
Denise Roßberg - avatar
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(); } }
20th Dec 2019, 8:56 PM
Nana Kofi
Nana Kofi  - avatar
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.
20th Dec 2019, 9:28 PM
Denise Roßberg
Denise Roßberg - avatar
0
please your page cannot be found
20th Dec 2019, 10:06 PM
Nana Kofi
Nana Kofi  - avatar
0
Nana Kofi Strange. The link works for me. Click on Java Tutorial -> click on Objects & Classes -> click on Getters & Setters.
20th Dec 2019, 10:23 PM
Denise Roßberg
Denise Roßberg - avatar
0
Denise Roßberg Thank you. I wasn't using the app at then..
21st Dec 2019, 12:11 AM
Nana Kofi
Nana Kofi  - avatar