Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java (please help) Part 1: Make a box that can hold anything This first part checks that you know how to use generics, write JavaDocs,

java (please help)

Part 1: Make a box that can hold anything This first part checks that you know how to use generics, write JavaDocs, and use the basic command line tools youll use throughout the semester. You need to write a class called SingleItemBox (in a file called SingleItemBox.java that you create). This class has a constructor that takes a single item (of any type) and puts it in the box. You also need a method called getItem() which provides the item back to the user but does not remove it from the box (this is an accessor, if you remember, sometimes called a getter). Make sure to comment your code as you go in proper JavaDoc style. To check that youve got the right idea, weve provided a class called BoxUsageDemo.java which shows creating three different boxes that store different things (an apple, a banana, and a cat). You should not alter this class to make it work, but rather alter your box to allow the provided code to work. You can use the command "java BoxUsageDemo" to run the testing code defined in main(). You could also edit this main() to perform additional testing (we wont be using BoxUsageDemo for testing, its just a demo for you). Note that JUnit test cases will not be provided for SingleItemBox, but feel free to create JUnit tests for yourself. A part of your grade will be based on automatic grading using test cases made from the specifications provided.

TL;DR Youre making a box (SingleItemBox) and using JavaDoc comments. Theres test code in BoxUsageDemo.

BoxUsageDemo.java

/** * This is a demo of using the SingleItemBox class. * @author K. Raven Russell */ public class BoxUsageDemo { /** * This is a main method with demo code. * @param args command line args (not used) */ public static void main(String[] args) { //demo putting an apple in a box /** * Class for apples. */ class Apple { } //make an apple Apple a1 = new Apple(); //put the apple in a box SingleItemBox appleBox = new SingleItemBox<>(a1); //check that the apple was put in the box if(appleBox.getItem().equals(a1)) { System.out.println("yay 1"); } //demo putting a banana in a box /** * Class for bananas. */ class Banana { } //make a banana Banana b1 = new Banana(); //put the banana in a box SingleItemBox bananaBox = new SingleItemBox<>(b1); //check that the banana was put in the box if(bananaBox.getItem().equals(b1)) { System.out.println("yay 2"); } //demo putting a banana in a box /** * Class for cats. */ class Cat { } //make a banana Cat c1 = new Cat(); //put the banana in a box SingleItemBox catPlayBox = new SingleItemBox<>(c1); //check that the banana was put in the box if(catPlayBox.getItem().equals(c1)) { System.out.println("yay 3"); } } }

Checking That Youve Got Style Every professional developer needs to adhere to a coding style (indentation, variable naming, etc.). This is non-optional in 99.99999% of professional settings (aka. jobs). Normally, in school, a grader checks your style manually, but this is very inefficient since you only get feedback after your project is completed (not while youre writing), and its very hard for someone to manually check these things. Were going to help move you along the path to coding with style this semester. We have provided you with a command line tool that checks your style for you: checkstyle (https://checkstyle.org). This tool has plugins for Eclipse, NetBeans, jGRASP, and many others (see their website), but there is also a command line interface (CLI) which has been provided with this project (checkstyle.jar). This automatic checker is similar to ones used at large companies (like Google, Oracle, and Facebook). If youre curious, were using a subset of Googles style requirements for this class. The provided cs310code.xml checks for common coding convention mistakes (such as indentation and naming issues). You can use the following to check your style: java -jar checkstyle.jar -c [style.xml file] [userDirectory]/*.java For example, for a user directory 001-krusselc-p0 checking for JavaDoc mistakes I would use: java -jar checkstyle.jar -c cs310code.xml 001-krusselc-p0/*.java Note: if you have a lot of messages from checkstyle, you can add the following to the end of the command to output it to a file called out.txt: > out.txt If checkstyle finds nothing wrong youll see Starting audit... Audit done. and nothing else. Checking Your JavaDoc Comments You should verify that your JavaDoc comments adhere to the proper format (especially if youre new to writing JavaDocs). Weve provided a second checkstyle file (cs310comments.xml) that looks for JavaDoc issues and in-line comment indentation issues. You can run it the same way as cs310code.xml, for example: java -jar checkstyle.jar -c cs310comments.xml 001-krusselc-p0/*.java

Part 2: Fruit and Numbers This second part of the project checks that you know how to use command line arguments and write basic Java code (including exception handling), and it gives you more practice writing JavaDocs and using checkstyle. Youll also be able to see and use JUnit tests (such as the ones we use when grading). Specification: Write a program (AppleOrange, that lives in AppleOrange.java). This program should print the numbers from 1 to X (inclusive), space separated, but for multiples of 3 print apple instead of the number, for multiples of 7 print orange instead of the number, and for the multiples of both 3 and 7 print appleorange (no space) instead of the number. X is a command line argument to the program. Dont forget to document as you go.

Example program run (coloring added for readability):

> java AppleOrange 10

1 2 apple 4 5 apple orange 8 apple 10

> java AppleOrange 25

1 2 apple 4 5 apple orange 8 apple 10 11 apple 13 orange apple 16 17 apple 19 20 appleorange 22 23 apple 25

> java AppleOrange 50

1 2 apple 4 5 apple orange 8 apple 10 11 apple 13 orange apple 16 17 apple 19 20 appleorange 22 23 apple 25 26 apple orange 29 apple 31 32 apple 34 orange apple 37 38 apple 40 41 appleorange 43 44 apple 46 47 apple orange 50

Checking for Invalid Input Your program should print the following (exactly) if the command line argument is missing, if the argument is not a positive number, or if too many arguments are given. This message should be sent to System.err not System.out.

Example commands which generate this error:

> java AppleOrange

> java AppleOrange 0

> java AppleOrange 1 1

> java AppleOrange apple

Error message: One positive number required as a command line argument.

Example Usage: java AppleOrange [number]

Exactly Matching Output

Seven unit tests have been provided to automate checking that you are exactly matching the output required (AppleOrangeTest.java) since it is hard to eyeball differences in text. To run these tests, navigate to the directory above your user directory (from your user directory type cd .. to go up one directory) and do the following...

Compile and run the tests with the following in Windows:

javac -cp .;junit-4.11.jar;[userDirectory] AppleOrangeTest.java

java -cp .;junit-4.11.jar;[userDirectory] AppleOrangeTest

Or for Linux/Mac, replace all the semicolons wit colons in the classpath:

javac -cp .:junit-4.11.jar:[userDirectory] AppleOrangeTest.java

java -cp .:junit-4.11.jar:[userDirectory] AppleOrangeTest

Check Your Style and JavaDocs Run both checkstyle files again on this new program to verify you got those basics down.

TL;DR You need to make a program (AppleOrange) with exactly output. JUnit tests are provided (AppleOrangeTest).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building Database Driven Catalogs

Authors: Sherif Danish

1st Edition

0070153078, 978-0070153073

More Books

Students also viewed these Databases questions

Question

10. Is the statement easy to read?

Answered: 1 week ago

Question

Explain in detail how the Mughal Empire was established in India

Answered: 1 week ago

Question

Problem: Evaluate the integral: I - -[ze dx

Answered: 1 week ago

Question

Problem: Evaluate the integral: I = 1- 1 dx 9

Answered: 1 week ago

Question

Are the rules readily available?

Answered: 1 week ago

Question

Are these written ground rules?

Answered: 1 week ago