Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Lab02.java: Throttle.java Question: Write a Java program which will simulate the behavior of an array of Throttles as described in the file Lab02.java 2. **
Lab02.java: Throttle.java Question: Write a Java program which will simulate the behavior of an array of Throttles as described in the file Lab02.java 2. ** Using Throttle class * Test the function guessSize // Create an array t of Throttles. t.length equals 5 int [] size = {29, 23, 19, 17, 13); Throttle [] t = new Throttle[5]; for (int i-0 ; 1 * Java Source Code for this class (www.cs.colorado. edu/-main/edu/colorado/simulations/Throttle.java) * @author Michael Main * (main@colorado.edu) * eversion Feb 10, 2016 public class Throttle private int top; / The topmost position of the throttle private int position; / The current position of the throttle * Construct a Throttle with a specified number of Rfon positions." t @param size Mthe number of "on positions" for this new Throttle @precondition size > 0 epostcondition This Throttle has been initialized with the specified number of "on positions" above the shutoff position, and it is number of "on positions" above the shutoff position, and it is * currently shut off * eexception IllegalArgumentException Indicates that size is not positive. public Throttle(int size) if (size 0) throw new IllegalArgumentException("Size: size); top size; position -0; // No assignment needed for position -it gets the default value of 0. k* * Get the current flow of this Throttle. * @return the current flow rate Calways in the range C0.0. 1.0] as a proportion of the maximum flow public double getFlow return (double)position/ (double)top; Check whether this Throttle is on. ereturn If this Throttle's flow is above zero, then the return value is true; otherwise the return value is false. public boolean isonC return (position > 0); public boolean isOn() return (position > 0); k* * Move this Throttle's position up or down. * @param amount the amount to move the position up or down (a positive amount *moves the position up, a negativ * epostcondition * This Throttle's position has been moved by the specified amount. If the result is more than the topmost position, then the position stays at the topmost position. If the result is less than the zero position, then the position stays at the zero position. k* public void shiftCint amount) if (amount top position) // Adcing amount would put the position above the top. position top; else if Cposition + amount 0) // Adding amount would put the position below zero. position 0; else /7 Adding amount puts position in the range Co...top] position + amount; * Turn off this Throttle. *@post condition This Throttle has been turned off. k* Z public void shift(int amount) if Camount> top position) // Adding amount would put the position above the top. position = top; else if (position + amount * Java Source Code for this class (www.cs.colorado. edu/-main/edu/colorado/simulations/Throttle.java) * @author Michael Main * (main@colorado.edu) * eversion Feb 10, 2016 public class Throttle private int top; / The topmost position of the throttle private int position; / The current position of the throttle * Construct a Throttle with a specified number of Rfon positions." t @param size Mthe number of "on positions" for this new Throttle @precondition size > 0 epostcondition This Throttle has been initialized with the specified number of "on positions" above the shutoff position, and it is number of "on positions" above the shutoff position, and it is * currently shut off * eexception IllegalArgumentException Indicates that size is not positive. public Throttle(int size) if (size 0) throw new IllegalArgumentException("Size: size); top size; position -0; // No assignment needed for position -it gets the default value of 0. k* * Get the current flow of this Throttle. * @return the current flow rate Calways in the range C0.0. 1.0] as a proportion of the maximum flow public double getFlow return (double)position/ (double)top; Check whether this Throttle is on. ereturn If this Throttle's flow is above zero, then the return value is true; otherwise the return value is false. public boolean isonC return (position > 0); public boolean isOn() return (position > 0); k* * Move this Throttle's position up or down. * @param amount the amount to move the position up or down (a positive amount *moves the position up, a negativ * epostcondition * This Throttle's position has been moved by the specified amount. If the result is more than the topmost position, then the position stays at the topmost position. If the result is less than the zero position, then the position stays at the zero position. k* public void shiftCint amount) if (amount top position) // Adcing amount would put the position above the top. position top; else if Cposition + amount 0) // Adding amount would put the position below zero. position 0; else /7 Adding amount puts position in the range Co...top] position + amount; * Turn off this Throttle. *@post condition This Throttle has been turned off. k* Z public void shift(int amount) if Camount> top position) // Adding amount would put the position above the top. position = top; else if (position + amount
Lab02.java:
Throttle.java
Question:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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