So I have this code. And the 2nd part isnt working where I am trying to have the rectangle area be a method. It prints like I want it to apart from the 2nd part. I feel like im doing something obvouis that is wrong but I have no idea.
#Not working
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
It seems like you have encountered an issue with your code. To better understand the problem and provide a solution, could you please provide the code you are referring to?
I just want to return the area but its not printing
@gusty raven could you provide some context about the problem? im confused with second half of your program.
about this bit
Alright
So thats all fine that part is just me giving a length and the user inputting the width which prints the area. Then another area but adding 3 to the legth and printing it 4 times. And also saying its small if its under 20 and big if its over 20t
This all works perfectly fine
Now I made a method to get the area I just want the area printed
so 5 X 5
But nothing else prints
Can I put a method in the first method
Or Im just not sure
method is fine, but since you assigned length and width as 5, it will always print 25
/**
* Programming with methods
*/
public class MyProgram
{
public void runMyProgram()
{
double length1;
length1 = 6.5;
double width1;
double area1;
System.out.println("Pick a width");
Scanner input = new Scanner(System.in);
width1 = input.nextDouble();
System.out.println("The area of the rectangle = " + length1 * width1);
int length2 = 0;
int width2;
width2 = 5;
int counter = 0;
while(counter < 4) {
length2 += 3;
int area = length2 * width2;
System.out.println("The area of the rectangle = " + area);
if (area >= 20){
System.out.println("Large Rectangle");
}else{
System.out.println("Small Rectangle");
}
counter++;
}
}
//part 2
//method RectangleArea
public void rectangleArea (int length, int width)
{
length = 5;
width = 5;
int area = width * length;
System.out.println("The area of the rectangle = " + area);
}
//Define as many subordinate methods as you like
public static void main(String[] args)
{
MyProgram prog = new MyProgram(); //create an object
prog.runMyProgram(); //invoke the top method
}
}```
Detected code, here are some useful tools:
Formatted code
import java.util.Scanner;
/**
* Programming with methods
*/
public class MyProgram {
public void runMyProgram() {
double length1;
length1 = 6.5;
double width1;
double area1;
System.out.println("Pick a width");
Scanner input = new Scanner(System.in);
width1 = input.nextDouble();
System.out.println("The area of the rectangle = " + length1 * width1);
int length2 = 0;
int width2;
width2 = 5;
int counter = 0;
while (counter < 4) {
length2 += 3;
int area = length2 * width2;
System.out.println("The area of the rectangle = " + area);
if (area >= 20) {
System.out.println("Large Rectangle");
}
else {
System.out.println("Small Rectangle");
}
counter++;
}
}
//part 2
//method RectangleArea
public void rectangleArea(int length, int width) {
length = 5;
width = 5;
int area = width * length;
System.out.println("The area of the rectangle = " + area);
}
//Define as many subordinate methods as you like
public static void main(String[] args) {
MyProgram prog = new MyProgram();
//create an object
prog.runMyProgram();
//invoke the top method
}
}
u can call methods in other methods, just not define them
How?
Yeah but nothing else prints
are you calling the method somewhere?
no
u made a method, but if u do not call ( use ) it, its just wasted code lines
I thought doing the print would just print it
Because thats how it worked on the other method
This is why I am confused im not sure how to call it on this one
Ahhh
hence the code in that method gets executed
I see
Sorry this is practice code so it was already made that main
It was a template
no worries, your main method is responsible for calling stuff
public static void main(String[] args) {
MyProgram prog = new MyProgram();
//create an object
prog.runMyProgram();
//invoke the top method
}```
same way you'd call rectangleArea(val1,val2)
yes, if you define your rectangleArea inside MyProgram then you'd have to do use object of class to call that method
prog.rectangleArea(val1,val2)
idk if you are familiar with objects and classes, if not then no need to worry too much
I assume your goal was to create a method which can generate area for you instead of doing it inside your program?
Yeah that was my gaol
goal*
In a suitable part of MyProgram, define a method called rectangleArea that calculates the area of a
rectangle. The method has two parameters which represent the length and width of a rectangle (and
can be real numbers e.g. 4.5) and returns the area. Formula for the area of a rectangle is length *
width
See it told me to do it in my program
you got the method right except you defined values as 5, which is incorrect. Apart from that, to use the method anywhere you just rectangleArea(value1,value2)
it will always assume length and width as 5, 5
but we wanna use it for different values of L,W
so if you don't define then L,W will be used from passed in values aka arguements
it doesn have to be value1 and value2, i meant to say whatever you define in your definition for ex L,W. You pass values in that order.
also, your not doing it correct
So you should pass different values of L,W
Idk why im actually so confused with this
So I shoudl write the number?
Like value1 should be 5 or whatever?
you should pass whatever variable holds the value of L,W
Ok let me give you an example
Let's say i have a method called printNumber(int val){ //print val}
this is how i defined my method, where val is the paramater
which means it's a placeholder for whatever value you pass when you actually call the method
so i can do printNumber(1)-->prints 1
printNumber(2)-->prints 2
and so on
you're gonna have to let me know if this makes sense
public void rectangleArea (int length, int width) So Length and Width is the parameter
yes
you can pass any value of length and width and it will print area for you based on that
when you call teh method
correct, that means hey doesnt matter what i pass as arguements. Make length and width equal to 5 and mulitply that only.
{
int area = width * length;
System.out.println("The area of the rectangle = " + area);```
So i dont need a value anywhere?
not in the method definition
So the method definition is just the public void part?
when you call the method, it will pass those values in place of these placeholders
from that to everything that's in {}
you can just use the name of method and pass the values that you wanna calculate area of
yes you can
prog.runMyProgram(); //invoke the top method
Would I do this but call it rectangleArea?
nope, not if you need it inside your runMyProgram
So how do I invoke in it?
incorrect, it calculates the area based on passed values.
Oh
also prints it
now where in your code you are doing this same thing but without any method
can you print the area using this method instead of those lines of code, look for lines that you can replace with this new method.
I could but Im now meant to leave it
Like all the things I done above
Needs to be kept the same#
Apparently
Yeah Im not sure pretty much this is a sample test for us to practice
Like its not a real one its just cause we have a real one next week
This is legit what it asked I assume part 1 is completly fine
Part 2 is where Ive started to actually struggle
ah oki then you can call any method inside your main method
wait nvm let me read it
oki they want you to use this method inside runMyProgram to calculate and print area
this is fine, it's defintion of your method
you need to call it where you want to calculate the area
lets look at this, can you see where you should call the method?
Where the print is?
do you understand why tho?
Because calling the method is printing it? Like we want the call the method which is already made which will give us the area?
correct
we made a method to calculate and print area, and that's why we can use it anywhere we want to calculate the area.
Yeah
Now i assume you know where else you need to call this method
Either down the botton or in the method we made
ye it does say return
anywhere if you need to calculate and print area, rectArea is your guy.
You might wanna look into "return" or "print", these are different things. I believe your question talked about return the area.
u specified ur method return type to be void
that's incorrect if u want to return something
to return area, it must be int
yes, seems alright to me. Your method calculates and returns the area and you are just printing it here.
Methods are cool, you can make all sorts of methods which can do some stuff and then you just call em. Lots of benefits but you will learn with time.
Yeah hopefully I can get better at them lol
Thank you very much for the help
One last question is there any need for this
When I have this
Or do I need them both?
First one is your method definition, without definition it won't know what rectangleArea is
You define things first only then you get to use em. So to answer your question yes you need both parts.
Thank you
Closed the thread due to inactivity.
If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍