CSAlpha Assign5: Arrays and the Random class

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

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:

Many uses of arrays require you to know the length of array. You can access the length of the array as shown below:

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.

Random

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:

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.

Core Assignment: Smoothie Shop

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 decide to tackle this parts in the following order:

Fruit

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.

Smoothie

The Smoothie class will be more complicated than the simple Fruit class. It must have:

Shop

The Shop class requires the following:

Main

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

Assignment Extension: Dynamic Allocation

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:

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