Notice: Assignments are currently being rewritten for CSAlpha. This assignment was created for an old version of the curriculum and may no longer align with the current curriculum.
If you have any questions regarding the assignment or see any errors in the assignment writeup, please let me know.
Create a text file called assign5.txt
. You will be using this to respond to questions in the assignment writeup and paste your code.
Arrays are extremely helpful tools to store lists of data at one time. Instead of creating one variable for everything you want to store, it may be more efficient to store the data in array, depending on what the use of the data is. The syntax for the two ways of creating an array is shown below:
xxxxxxxxxx
int[] firstArray = {1, 2, 3, 4};
String[] secondArray = new String[4];
//general formats:
//datatype[] name = {list of contents separated by commas};
//datatype[] name = new datatype[size];
Many uses of arrays require you to know the length of array. You can access the length of the array as shown below:
xxxxxxxxxx
System.out.println(firstArray.length);
System.out.println(secondArray.length);
//Program output:
//4
//4
Is length
a property (i.e. a variable) or method of arrays? Please write your answer in assign5.txt
.
Note that although secondArray might not hold any elements at this moment, the size of the array is still 4. If you access an element that hasn't been defined within secondArray yet, you will get the value of null
. However, if you access an index out of bounds of either of the arrays, you will receive an ArrayOutOfBounds exception error, which will cause your program to terminate no matter what.
In this assignment, you will be using the Random class to generate random numbers. The Random class has the ability to generate random values of any datatype, so don't forget about it! Here is an example of how to use the Random class:
xxxxxxxxxx
import java.util.Random;
Random generator = new Random();
for (int i = 0; i < 100; i++) {
System.out.println(generator.nextInt(5)); //generates Ints in the range of 0-4, inclusive
}
System.out.println(generator.nextInt(5) + 1); //generates Ints in the range of 1-5, inclusive
//Program output:
//0
//1
//3
//4
//2
//95 more lines...
//5
Note that the Random method nextInt
will always start with a range from 0
to n - 1
inclusive, n
being the parameter that you pass into the function. You can add or subtract numbers outside of the function call to change where the range starts; for example, in the example above, adding 1 outside of the function call results in the range starting at 1 and going through 5. If you instead added 10, then the range would be 10 to 14.
What is the range of this call: generator.nextInt(20) - 5
? Please write your answer in assign5.txt
.
You and your friend have opened a new smoothie shop that serves customers smoothies that are combinations of exclusively three fruits at a time. You, as the programmer of the duo, decide to take on a software project that will allow you to manage the creation of smoothies easily. This way, you won't have to make too many decisions about which fruits to pick.
Your friend doesn't really think it will be that helpful, but you are ready to prove them wrong. You come up with a framework that you think would be the best design for the software, for which you wrote the details below:
You will have four classes named Main
, Shop
, Smoothie
, and Fruit
.
Main
will manage the shop.You decide to tackle this parts in the following order:
The Fruit
class will contain two fields: a deliciousness
variable and a name
variable. The deliciousness rating of a fruit is an integer from 0 to 100, 0 being absolutely disgusting and 100 being a fruit from heaven. The name variable is simply the name of the fruit. These properties should be set upon the creation of a Fruit
object.
The Smoothie
class will be more complicated than the simple Fruit
class. It must have:
A Fruit
array of size 3. This elements of the array (i.e. the fruits) will be set upon the creation of the Smoothie
. A client creating a Smoothie
should have the ability to pass in three fruits to make the smoothie.
An orderName
property. This variable represents the name of the person who ordered the smoothie. This field should be able to be set upon creation of the Smoothie
as well.
A getDeliciousness
method that returns the total deliciousness of the smoothie. The deliciousness of a smoothie is calculated by summing all the individual deliciousness ratings of the fruits inside the smoothie. Do not do the following:
xxxxxxxxxx
sum = fruit1deliciousness + fruit2deliciousness + fruit3deliciousness
Implement the function so that you can calculate the total deliciousness no matter the size of the Fruit
array.
A printOrder
method that prints out the smoothie details in the following format:
xxxxxxxxxx
orderName: fruit1, fruit2, fruit3.
You do not have to account for the size of the array in this method; you can hardcode the way you access the fruits in the array for this method.
Any other methods that you think would be helpful for the Smoothie
class. It's your project!
The Shop
class requires the following:
A Smoothie
array of size 100. This is the limit of smoothies that your shop can hold at one time. This array should not contain any elements upon the creation of a shop, but it should be initialized to a size of 100 upon creation.
smoothieIndex
field that keeps track of where the "end" of the array is currently at, or in other words, the index after the last element you put into the array. This variable should start at 0.An addSmoothie
method. This method creates a new smoothie and adds it to the smoothie array. It has no return value. Make sure that you are not hardcoding the way you create a new smoothie; a user of this function should be able to pass in the fruits that they want in the smoothie.
You will be using smoothieIndex
for this method, and will also need to increment this variable. You must take into account that the smoothieIndex
may try to access outside of the Smoothie
array; in that case, print an error message and do not attempt to add or create a new smoothie.
A serve
method. This method removes the very first smoothie in the smoothie array (index 0) by setting it to value of null
. Then it must shift the remaining smoothies in the array to the left by one. Don't forget to decrement smoothieIndex
as well.
A generateSmoothie
method. This method takes in an order name and returns nothing. First, you will hardcode an array of 10 or so fruits that you pick/create. Then, you will use the Random
class to pick three random fruits from this hardcoded array. Finally, you use the three random fruits and order name that you received and call the addSmoothie
function to add your generated smoothie to the list of smoothies.
This class has no requirements except for containing the main method. You should be using this class to test your classes above. If you want, you can develop a console program that allows you to manage your shop through user input.
Paste all your code into assign5.txt
In lesson, you learned that you cannot change the size of an array once it has been created. However, you can create a class that acts like an array, but is able to increase its size.
Call this class DynamicArray
. Although arrays supports many datatypes, your dynamic array class will only support Int
. It should have the following properties:
Int
array. Although you can't change the size of an array, you will be setting this array variable to an array of a different size once you want to increase the size.Int
array of the class to the initialization of a new array of this size.arrayIndex
field. This variable always keeps track of the index after the last element of the array. It should start at 0.doubleSize
method. This method creates a new array that is twice the size of the original array, moves all the elements from the original array to the new one, and then sets the original array to the new array.addInt
method. This method adds an Int
to the internal array using the arrayIndex
field. However, if you notice that arrayIndex
is greater or equal to the array length, you should call doubleSize
first.get
method. This method takes in an index and returns the Int
value that is stored in the internal array at that index. If the index is invalid, print an error message and return -1.If you are brave enough, you can adapt and use this DynamicArray
class for your smoothie shop so that you can have as many smoothies as you want. Make sure to save your code somewhere else before attempting to make major modifications to your code. This is the primary use of Git and GitHub!
Paste all your code into assign5.txt