#Not working

1 messages · Page 1 of 1 (latest)

gusty raven
#

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.

south patrolBOT
#

<@&987246399047479336> please have a look, thanks.

south patrolBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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?

gusty raven
#

I just want to return the area but its not printing

bitter sandal
#

@gusty raven could you provide some context about the problem? im confused with second half of your program.

#

about this bit

gusty raven
#

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

bitter sandal
gusty raven
#
/**
 * 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

    }

    
    
}```
south patrolBOT
# gusty raven ```import java.util.Scanner; /** * Programming with methods */ public class My...

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
  }
}
blissful grove
#

u can call methods in other methods, just not define them

gusty raven
#

How?

bitter sandal
#

are you calling the method somewhere?

gusty raven
#

As of now I am just trying to get the area

#

So length X Width just something simple

blissful grove
#

not what alpha is asking

#

are u using that method that u made somewhere ?

gusty raven
#

no

blissful grove
#

u made a method, but if u do not call ( use ) it, its just wasted code lines

gusty raven
#

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

blissful grove
#

look at your main

#

there your calling the runMyProgram method

gusty raven
#

Ahhh

blissful grove
#

hence the code in that method gets executed

gusty raven
#

I see

#

Sorry this is practice code so it was already made that main

#

It was a template

bitter sandal
#

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
  }```
gusty raven
#

Oh so doing the prog.runmyprogram is invoking it

#

Cause it said to do that after

bitter sandal
#

same way you'd call rectangleArea(val1,val2)

bitter sandal
#

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?

gusty raven
#

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

bitter sandal
#

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)

gusty raven
#

How come its incorrect?

#

Putting it as 5?

bitter sandal
#

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

gusty raven
bitter sandal
#

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.

blissful grove
#

also, your not doing it correct

bitter sandal
#

So you should pass different values of L,W

gusty raven
#

Idk why im actually so confused with this

gusty raven
#

Like value1 should be 5 or whatever?

bitter sandal
#

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

gusty raven
#

public void rectangleArea (int length, int width) So Length and Width is the parameter

bitter sandal
#

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

gusty raven
#

Yeah so I didnt need to do length = 5

#

And width = 5

bitter sandal
#

correct, that means hey doesnt matter what i pass as arguements. Make length and width equal to 5 and mulitply that only.

gusty raven
#
    {
                int area = width * length;
                System.out.println("The area of the rectangle = " + area);```
#

So i dont need a value anywhere?

bitter sandal
#

not in the method definition

gusty raven
#

So the method definition is just the public void part?

bitter sandal
#

when you call the method, it will pass those values in place of these placeholders

bitter sandal
gusty raven
#

Ok so its all goodH

#

How do I call it now?

bitter sandal
#

you can just use the name of method and pass the values that you wanna calculate area of

gusty raven
#

Where do I do it?

#

In the top method?

bitter sandal
#

where do you need it?

#

the part where you want to print the area

gusty raven
#

Invoke it in run my program

#

Thats the first method

bitter sandal
#

yes you can

gusty raven
#

prog.runMyProgram(); //invoke the top method

#

Would I do this but call it rectangleArea?

bitter sandal
#

nope, not if you need it inside your runMyProgram

gusty raven
#

So how do I invoke in it?

bitter sandal
#

lets take a step back

#

what does this method do? rectArea method

gusty raven
#

Gives a length and width

#

Which I need to multpy

#

mutiply*

#

To get the area

bitter sandal
#

incorrect, it calculates the area based on passed values.

gusty raven
#

Oh

bitter sandal
#

also prints it

#

now where in your code you are doing this same thing but without any method

gusty raven
#

In myprogram

#

the main method#

bitter sandal
#

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.

gusty raven
#

I could but Im now meant to leave it

#

Like all the things I done above

#

Needs to be kept the same#

#

Apparently

bitter sandal
#

ah

#

so i don't see a reason for this new method

#

unless im missing something

gusty raven
#

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

bitter sandal
#

ah oki then you can call any method inside your main method

gusty raven
#

Call any method

#

So I dont need to do all of this?

bitter sandal
#

wait nvm let me read it

#

oki they want you to use this method inside runMyProgram to calculate and print area

gusty raven
#

So I needed to do what I did so far?#

bitter sandal
#

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?

gusty raven
#

Where the print is?

bitter sandal
#

do you understand why tho?

gusty raven
#

Because calling the method is printing it? Like we want the call the method which is already made which will give us the area?

bitter sandal
#

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.

gusty raven
#

Yeah

bitter sandal
#

Now i assume you know where else you need to call this method

gusty raven
#

Either down the botton or in the method we made

bitter sandal
#

ye it does say return

bitter sandal
#

You might wanna look into "return" or "print", these are different things. I believe your question talked about return the area.

gusty raven
errant ibex
#

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

gusty raven
#

Nice thank you

#

Would that be correct as it prints the area now

bitter sandal
#

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.

gusty raven
#

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?

bitter sandal
#

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.

gusty raven
#

Thank you

south patrolBOT
#

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 👍