![]() ![]() Causes any code underneath the return statement to be ignoredĬombining all of that should allow the compiler errors to stop, and make your program work correctly. Return width // Returns the value to your main function ![]() To fix the compiler error, a return statement needs to be added in to the helper functions, such as: Scanner keyboard = new Scanner( System.in ) ĭouble width = keyboard.nextDouble() // Sets the value to return to your main function If that value will never be sent, the program will most likely be unable to compile - an error will be thrown stating something along the lines of "expected type double" when you try to call that function. Therefore, when you set length to equal getLength(), what you're trying to do is set the value of your local length variable to equal the value coming back from your helper function. In the case of a void function, such as main() or displayData(), the method states that it will not return a variable of any specific type. The other thing to consider is that when a method has a return value, such as a double in the case of getLength(), getWidth(), and getArea(), the piece of code calling the function is expecting some variable of that type to be returned. While passing the Scanner to each function separately would allow your methods to work as intended, the first option is slightly more readable. Initialized within the class, but outside of any methods Scanner keyboard = new Scanner( System.in ) instead of: def comparisonarea (): if area1 > area2: print () print ('The area of rectangle one is: ', area1) print ('The area of rectangle two is: ', area2) print ('Rectangle one\'s area is. Since your program is broken up into several methods, the data inside each method is local unless you store it inside the class itself.įor example, your helper functions for getLength() and getWidth() wouldn't be able to access your keyboard Scanner unless you declared it outside of the main method, as such: import Not an answer to your question but a hint for lazy people ) Often if you have to write the exact same line many times, it can be done simpler. What am I screwing up on and how would I go about fixing it? I am a beginner in programming so please bear with me :D. Public static void displayData(double length, double width, double area) Scanner keyboard = new Scanner(System.in) Īrea = getArea(double length, double width) I don't know how to complete this code because right now what I have is this: import displayArea – This method should accept the rectangle’s length, width, and area as arguments, and display them in an appropriate message to the screen.The area is calculated by multiplying the length by width. ![]() getArea – This method should accept the rectangle’s length and width as arguments, and return the rectangle’s area.getWidth – This method should ask the user to enter the rectangle’s width, and then return that value as a double.getLength – This method should ask the user to enter the rectangle’s length, and then return that value as a double.The program should call the following methods: Write a program that asks the user to enter the width and length of a rectangle, and then display the rectangle’s area. The question that I am really stuck on is this: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |