Create a new folder for all of this lab exam's code. You may call this folder whatever you'd like. Inside of the folder, create five Java text files with these exact names: "Comp110Lab2.java"

computer science

Description

Exam Prompt

Create a new folder for all of this lab exam's code. You may call this folder whatever you'd like. Inside of the folder, create five Java text files with these exact names: "Comp110Lab2.java", "Stinging.java","Insect.java", "Bee.java"and "Wasp.java". You are to write a program that will create an array of insects (either bees or wasps) and will allow the user to choose from a number of different actions that they can perform on this array. For this project, Comp110Lab2 is a static class, Stinging is an interface, Insect is an abstract class, and Bee & Wasp are both object classes.

 

Start by creating the Stinging interface first. This interface contains just one method stub: the "sting" method, which has no input arguments, and returns an integer. Next, create the Insect abstract class. This abstract class contains five members: an integer variable "height", a constructor, a getter method, a setter method, and an abstract method. The constructor has one input argument, which is assigned to the height variable. The "getHeight" method has no input arguments and returns an integer: height. The "setHeight" method has one input argument, returns nothing, and assigns the argument to the height variable. The abstract method stub is called "wingspan". It has no input arguments, and returns an integer.

 

Next are the Bee and Wasp object classes. Both object classes extend Insect and implement Stinging. The Bee class contains seven members: an integer variable "pollen", a constructor, a getter method, a setter method, the implementation of Insect's "wingspan", the implementation of Stinging's "sting", and an overriding "toString" method. The constructor takes in two arguments, where the first is passed into the "super" constructor call, and the second is assigned to pollen. The "getPollen" method has no input arguments and returns an integer: pollen. The "setPollen" method has one input argument, returns nothing, and assigns the argument to pollen. The Wasp class also contains seven members: an integer variable "venom", a constructor, a getter method, a setter method, the implementation of Insect's "wingspan", the implementation of Stinging's "sting", and an overriding "toString" method. The constructor takes in two arguments, where the first is passed into the "super" constructor call, and the second is assigned to venom. The "getVenom" method has no input arguments and returns an integer: venom. The "setVenom" method has one input argument, returns nothing, and assigns the argument to venom.

 

For Bee and Wasp, the method signatures of wingspan, sting, and toString are identical in both classes. All six have no input arguments. Both wingspan methods return an integer, both sting methods return an integer, and both toString methods return a string. However, the exact values returned are different for each class.

 

Here is a table with the expected return values for each class's integer methods:

 

 

wingspan()

sting()

Bee

height - pollen

height + pollen

Wasp

height / venom

height * venom

 

In all four cases, the object classes combine their inherited Insect variable, along with their own individual variable. Next is a table with the expected strings for each class's toString method. These variables are also utilized in each string output:

 

Bee

"This is a bee that is [height] cm tall, and carries [pollen] kg of pollen."

Wasp

"This is a wasp that is [height] cm tall, and carries [venom] kg of venom."

 

At this point, all that remains is the static class: Comp110Lab2. Start by importing the Scanner and Random classes above the class name, as we will be using the Scanner to scan for keyboard input, and the Random to generate random numbers. This class will also be using a constant, and a global variable as part of its execution. The constant is an integer "EXIT_OPTION", and is assigned the value of 4. The global variable is an array of Insect objects "insects". This is declared, but not assigned. Next, outline (with comments) all of the static methods that will be used in the static class: The Main Method, Create Insects, Print Insects, Find Insect, and Change Insects. That's five static methods total, all of which do not return any output.

 

The main method begins by declaring and assigning a Scanner object "input" for scanning the keyboard. Traditionally, this is the part where we type out a prompt to the user, like "Enter a number between X to Y". For this exam, there is no prompting at this part. Instead, there is a method call to the "createInsects" method. This method call will create the insect array for us, using a hardcoded length. With both of those statements done, we are now ready to write the program's main menu loop. This loop uses an integer ("option", initially assigned 0), and checks to see if it is not equal to the exit option constant. If it isn't, then the loop proceeds. Also, before writing statements inside the loop, make sure to "close" the scanner object AFTER the loop's closing bracket (at the end of the main method).

 

Inside of the loop, a menu is printed out to let the user know what to enter:

"Select an option:"

"1) Print out every insect"

"2) Find a random insect from the array"

"3) Change every insect's variables"

"[EXIT_OPTION]) Exit"

Next, the user is prompted to "Enter a number: ", which is followed by an if/else statement. If the scanner object has a "next int", then assign that "next int" into option. If not, then assign option to 0, where it will be error-checked later. Following the if/else statement, the scanner object flushes the input out. We can now proceed with the evaluation of option using either a switch/case (or if/else) statement.

 

For case 1, a method call to "printInsects" is made. For case 2, a method call to "findInsect" is made. This method call is the only one that takes in arguments. Two such arguments are supplied here: 0, and "insects.length - 1". For case 3, a method call to "changeInsects" is made. For case EXIT_OPTION, the word "Goodbye."is printed out. And for the default case, the sentence "Bad input, try again."is printed out. Following the switch/case (or if/else) statement, make sure to print out an empty line, so that each iteration's output can be separated from one another.

 

At this point, the main method is complete, which means we can now begin working on the other static methods, where the majority of the program's calculations take place. Start first with "createInsects". The purpose of this method is to create the insect array using randomly generated numbers. This method begins by declaring and assigning a Random "generator". Next, assign "insects" as a new Insect array of length 4. Usually, this is where we plug in a user-typed length. But for this exam, we are keeping things simple by sticking to just the number 4. Once the array has its length, we can now use a for-loop to iterate through it, from 0 up to before "insects.length". A new insect (Bee or Wasp) will be created and assigned into the array at each index "insects[i]". Inside of the loop, start by declaring and assigning two integers ("number1" and "number2"). Both of these numbers will be passed in as input arguments when creating the current insect in the array. The range of number1 is between 11 to 20, and the range of number2 is between 2 to 6. Once both numbers are generated, create an if/else statement. The Random object should generate a random boolean (true or false) which is what the if-statement checks. If "true", then the current insect is created as a new Bee. Otherwise, the current insect is created as a new Wasp. Once the if/else statement is complete, the last statement to include inside the method is a println statement that outputs the following line: "[insects.length] insects have been created.". This statement lets the user know that the array has been successfully created.

 

The next static method to create is the "printInsects" method. The purpose of this method is to make use of every insect's "toString", "wingspan", and "sting" methods, in order to print all of their variables to the screen. This method begins with the same for-loop from the createInsects method. Inside of the loop, start with three println statements: the first prints a line that describes the current insect, using its position (aka, its index +1):"Insect [i + 1]:". The second prints the insect directly (which is possible because of toString). And the third prints a statement that uses the abstract class's method: "It has a wingspan of [insects[i].wingspan()] cm.". No casting is necessary because the array 's data type is Insect already. However, casting is necessary for using the sting method. (The abstract class and interface are not directly connected via inheritance.) Create a single if-statement that checks if the current insect is an "instanceof" the Stinging interface. If it is, then cast the current insect into a Stinging variable "stinger", and print out a statement that uses the interface's method: "And a sting that lasts for [stinger.sting()] seconds.". Once the if-statement is complete, then the for-loop is complete, which also means that the method is complete.

 

The fourth static method to create is the "findInsect" method. This method features two integer input arguments ("bottom" and "top"), both of which represent a number range. The purpose of this method is to find a random insect in the array by randomly shrinking the number range to a single value. (This method is very similar to the second recursive method from project 4, but with a few minor differences.) Start with a println statement that informs the user that the recursive method has been entered: "Finding an insect… ([bottom]-[top])". Because the method is recursive, this statement will print out multiple times: once for every time the method is called upon. Recursive also means that an if/else statement is necessary for determining which way to travel. The bottom and top input arguments represent indices within the insect array (which is why 0 and insects.length - 1 are passed in from the main method). As the method travels recursively, the bottom increases, and the top decreases, until they are both equal in value. Therefore, if the bottom is less than the top, then the recursive case occurs. Otherwise, the base case occurs. Inside of the recursive case, declare and assign three integers ("bottomHeight", "topHeight", and "combinedHeight"). First, use the "bottom" index to find the bottom insect's height. Second, use the "top" index to find the top insect's height. And third, add both of these heights together. The third integer is what determines this method's non-linear recursive traveling (as opposed to Project 4's random boolean). If the number is even (combinedHeight mod 2 equals 0), then a recursive method call to findInsect occurs, with "bottom" and "top - 1" passed in. Otherwise, a recursive method call to findInsect occurs, with "bottom + 1" and "top" passed in. Inside of the base case, the method has succeeded and can backtrack out of its numerous method calls. The base case features two println statements. The first describes the success: "Found this insect at index [top] (position [top + 1]):". And the second prints out the insect from index "top" directly. Remember that top is equal to bottom inside of the base case. Either one can be used in both statements.

 

The final static method to create is the "changeInsects" method. The purpose of this method is to call upon every insect's getter and setter methods in order to modify all of their variables (inherited and individual). The method begins with the same for-loop from the createInsects and printInsects methods. Inside of the loop, declare an integer variable "newHeight". Assign it to the value of the current insect's height (getHeight), multiplied by 3. After the operation is performed, assign the variable as the current insect's new height (setHeight). It doesn't matter if the current insect is a Bee or a Wasp, as both utilize the abstract class's height variable. However, this isn't the case for their individual variables (pollen and venom). The current insect needs to be cast as a Bee or Wasp, in order to access these variables. Create an if/else-if statement (with no "else" at the end) to achieve these casts. If the current insect is an instance of Bee, then cast it into a Bee variable "bee". Next, declare an integer variable "newPollen" and assign it the bee's pollen (getPollen), multiplied by 2. After the operation is performed, assign the variable as the bee's new pollen (setPollen). Otherwise, if the current insect is an instance of a Wasp, then perform these same three statements. Except that this time, replace all instances of the word "Bee" with "Wasp", and all instances of the word "pollen" with "venom."

 

When modifying the variables within the bee or wasp object variables, they also modify the variables within the current insect object as well. Once the if/else-if statement is completed, then the for-loop is also completed. After the for-loop's closing brace, add a single println statement at the very end of this method: "[insects.length] insects have been changed.". This statement lets the user know that the array has been successfully changed. As a result, every insect variable has multiplied in size, which also causes the "wingspan" and "sting" results to change as well. Once the statement is written, the static method is completed, which means that the static class is also completed.

 

At this point, there is nothing additional to type out. Make sure to compile your static class, and fix any errors that show up in any of your code files. Once the compiling stage is done, run the code and use the sample inputs located on the following page of this prompt. The output numbers are randomly generated, so you cannot check the results number-for-number. Instead, check to see that the structure of your output statements matches the output statements down below. If it does, then you can safely assume that your lab exam code is complete.

 

Don’t forget to add comments to your program! This includes the block comment header, and multiple line comments at areas of the code that you felt were hard or complicated.

 

/* Your name

Comp 110L – Spring 2020

Lab Exam 2 */

 

//Constant

//Global Variable

//The Main Method

//Storage

//Main Loop

//Input

//Calculation and Output

//Close the Scanner

//Create Insects

//Print Insects

//Find Insect

//Change Insects

 

Please take note of the time that this exam is due as you are writing your code! Once your code is fully commented and running perfectly, make sure to compress your five Java files into a ZIP file. This ZIP file must be submitted into the Canvas submission portal BEFORE the timer runs out!

 

As a reminder, you are ALLOWED to look at the Java code files from any of your previous projects from this semester. Everything that is covered in this exam has been remixed from all of these assignments. Use them to your advantage in writing out your code. But that is it. No outside notes, no books, and NO SEARCH ENGINES!

 

In addition, you are NOT allowed to make any sort of contact (physical or digital) with any other student while you are taking the online exam. If you have a question, you may either direct message me (the professor) in Slack, or send me an email, and I will answer your question immediately.

 

I wish you all good luck with this exam.

 

Sample Inputs/Outputs

4 insects have been created.

Select an option:

1) Print out every insect

2) Find a random insect from the array

3) Change every insect's variables

4) Exit

Enter a number: 1

Insect 1:

This is a bee that is 13 cm tall, and carries 6 kg of pollen.

It has a wingspan of 7 cm.

And a sting that lasts for 19 seconds.

Insect 2:

This is a wasp that is 18 cm tall, and carries 3 kg of venom.

It has a wingspan of 6 cm.

And a sting that lasts for 54 seconds.

Insect 3:

This is a bee that is 11 cm tall, and carries 6 kg of pollen.

It has a wingspan of 5 cm.

And a sting that lasts for 17 seconds.

Insect 4:

This is a wasp that is 16 cm tall, and carries 2 kg of venom.

It has a wingspan of 8 cm.

And a sting that lasts for 32 seconds.

 

Select an option:

1) Print out every insect

2) Find a random insect from the array

3) Change every insect's variables

4) Exit

Enter a number: 2

Finding an insect... (0-3)

Finding an insect... (1-3)

Finding an insect... (1-2)

Finding an insect... (2-2)

Found this insect at index 2 (position 3):

This is a bee that is 11 cm tall, and carries 6 kg of pollen.

 

Select an option:

1) Print out every insect

2) Find a random insect from the array

3) Change every insect's variables

4) Exit

Enter a number: 3

4 insects have been changed.

 

Select an option:

1) Print out every insect

2) Find a random insect from the array

3) Change every insect's variables

4) Exit

Enter a number: 1

Insect 1:

This is a bee that is 39 cm tall, and carries 12 kg of pollen.

It has a wingspan of 27 cm.

And a sting that lasts for 51 seconds.

Insect 2:

This is a wasp that is 54 cm tall, and carries 6 kg of venom.

It has a wingspan of 9 cm.

And a sting that lasts for 324 seconds.

Insect 3:

This is a bee that is 33 cm tall, and carries 12 kg of pollen.

It has a wingspan of 21 cm.

And a sting that lasts for 45 seconds.

Insect 4:

This is a wasp that is 48 cm tall, and carries 4 kg of venom.

It has a wingspan of 12 cm.

And a sting that lasts for 192 seconds.

 

Select an option:

1) Print out every insect

2) Find a random insect from the array

3) Change every insect's variables

4) Exit

Enter a number: 2

Finding an insect... (0-3)

Finding an insect... (1-3)

Finding an insect... (1-2)

Finding an insect... (2-2)

Found this insect at index 2 (position 3):

This is a bee that is 33 cm tall, and carries 12 kg of pollen.

 

Select an option:

1) Print out every insect

2) Find a random insect from the array

3) Change every insect's variables

4) Exit

Enter a number: 0

Bad input, try again.

 

Select an option:

1) Print out every insect

2) Find a random insect from the array

3) Change every insect's variables

4) Exit

Enter a number: 5

Bad input, try again.

 

Select an option:

1) Print out every insect

2) Find a random insect from the array

3) Change every insect's variables

4) Exit

Enter a number: hello

Bad input, try again.

 

Select an option:

1) Print out every insect

2) Find a random insect from the array

3) Change every insect's variables

4) Exit

Enter a number: 4

Goodbye.


Related Questions in computer science category