![]() ("Enter the width of Rectangle " + (i + 1) ("Enter the length of Rectangle " + (i + 1) Scanner console = new Scanner(System.in) Create a Scanner object for keyboard input. ![]() Create an array to hold rectangle's data.ĭouble length //to hold length of rectangleĭouble width //to hold width of rectangle You can use a loop to create objects for each element. You must create a Rectangle object reference to array element like : rectangles = new Rectangle() Each element in this array is a reference variable. The variable that references the array is named rectangles. The following code declares an array of five Rectangle objects: For example, in section 6.3, we created a Rectangle class to implement the basic properties of a Rectangle. can create arrays of objects like any other datatype. Public static double getArea(double length, double width) 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 ) 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 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
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |