Question
Task 7. Instance Fields and Conditional Statements Switch roles. The driver should now navigate and the navigator should now drive. We are now going to
Task 7.
Instance Fields and Conditional Statements Switch roles. The driver should now navigate and the navigator should now drive. We are now going to change the maximizeHeight and maximizeWidth methods. So that they can both maximize and restore the window sizes. Create two fields inside the MaxWindow class. A field goes inside the class body but not inside any methods. The first field should be type boolean and given the name heightMaximized. The second field should be type int and given the name originalHeight. Just as with methods, fields need access modifiers, but unlike methods, we like to make fields private. Give heightMaximized the initial value of false and originalHeight the initial value of 0. Change the maximizeHeight method so that it accepts a single boolean as input. You do this by creating a variable declaration for the input value inside the ( ) of the method header. Save, compile your code, and test to see if you must now use boolean values when calling maximizeHeight. There should be no other differences. If there are any errors, fix them before moving on. In the body of the maximizeHeight method, create a conditional statement. A conditional statement is of the form: if (condition) then-statement else else-statement In this case, our then-statement will be a compound statement containing several simple statements. Our else-statement will be another conditional statement. That second conditional statement will have a then-statement that is a compound statement with several simple statements and no else-statement. The form looks like this: if (condition1) { statement1; statement2; statement3; (Note there could be more than 3 statements here) } else if (condition2) { statement4; statement5; } The first condition should check if the input to maximizeHeight is true and the heightMaximized field is false. The simple statements inside the then-statement compound statement should do the following: Store the current height of the window to the field originalHeight. Do the code to maximize the window and move it to the top of the screen. (The code should be moved here so that this is the only place the code exists inside the method.) Set the heightMaximized field to store true. The second condition should check if the input to maximizeHeight is false and the heightMaximized field is true. The second compound statement should contain simple statements to do the following: Set the window height to be the value stored in the originalHeight field while keeping the width the same. Set the heightMaximized field to store false. Compile and test your code. The window's height should be maximized when you call maximizeHeight(true) and it should be restored when you call maximizeHeight(false) (though the window location will still be the top of the screen. Switch roles. The driver should now navigate and the navigator should now drive. Once you have this working, make the same changes to maximizeWidth, and then demonstrate your class to your lab teaching assistant.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started