Question
Question 10 Item 10 Question 1 Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. Notes: Assume that the
Question 10
Item 10
Question 1
Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Notes:
- Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
- Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.
- In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.
1. A bicycle shop maintains information about bicycles it has in stock. A bicycle is represented by the following Bicycle class.
public class Bicycle
{
/** Returns the type of bicycle (for example, "road" or "mountain") */
public String getType()
{ /* implementation not shown */ }
/** Returns true if the bicycle is assembled and returns false otherwise */
public boolean isAssembled()
{ /* implementation not shown */ }
// There may be instance variables, constructors, and methods that are not shown.
}
Information about the inventory of bicycles at the shop is stored in a BicycleInventory class, which contains a list of the bicycles in stock at the shop. You will write two methods of the BicycleInventory class.
public class BicycleInventory
{
/** A list of the bicycles the shop has in stock
* Guaranteed to contain at least one Bicycle and all entries are non-null
*/
private ArrayList
/** Returns an array of bicycles, as described in part (a)
* Precondition: n > 0
*/
public Bicycle[] getChoices(int n, String type, boolean assembled)
{ /* to be implemented in part (a) */ }
/** Returns a randomly selected bicycle, as described in part (b)
* Precondition: n > 0
*/
public Bicycle chooseOne(int n, String type, boolean assembled)
{ /* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods that are not shown.
}
(a) Write the BicycleInventory method getChoices. The method returns an array of at most n bicycles from those in stock where the bicycle type is equal to the type parameter and the bicycle assembly status is equal to the assembled parameter. If fewer than n bicycles meet the criteria, the unused array elements should be null.
For example, assume that classicBike has been declared as a BicycleInventory object and bike1, bike2, bike3, bike4, and bike5 are properly declared and initialized Bicycle objects. The following table represents the contents of bicycleList.
Note that the bicycles can be in any order in the array, but the null values must always be at the end of the array.
Write method getChoices.
/** Returns an array of bicycles, as described in part (a)
* Precondition: n > 0
*/
public Bicycle[] getChoices(int n, String type, boolean assembled)
(b) Write the BicycleInventory method chooseOne. The method randomly selects one bicycle from an array of at most n bicycles for which the bicycle type is equal to the type parameter and the bicycle assembly status is equal to the assembled parameter.
The method returns the randomly selected bicycle if the array contains at least one bicycle matching the criteria or returns null if there are no matching bicycles. Each matching bicycle in the array must have an equal chance of being selected.
Assume that getChoices works as specified, regardless of what you wrote for part (a). You must use getChoices appropriately to receive full credit.
Write method chooseOne.
/** Returns a randomly selected bicycle, as described in part (b)
* Precondition: n > 0
*/
public Bicycle chooseOne(int n, String type, boolean assembled)
(c) A programmer would like to add a method called getBicyclesByWheelSize, which returns an array of all bicycles in the inventory with a specified wheel size.
Write a description of how you would change the Bicycle and BicycleInventory classes in order to support this modification.
Make sure to include the following in your response.
- The method header for the getBicyclesByWheelSize method
- Identify any new or modified variables, constructors, or methods aside from the getBicyclesByWheelSize method. Do not write the program code for this change.
- Describe, for each new or revised variable, constructor, or method, how it would change or be implemented, including visibility and type. You do not need to describe the getBicyclesByWheelSize method. Do not write the program code for this change.
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