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 git ives you more...

1 answer below »
Need help with simple java project


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 git ives you more practice writing JavaDocs and using checkstyle. You’ll 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. Don’t 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
Answered Same DayApr 25, 2021

Answer To: Part 2: Fruit and Numbers This second part of the project checks that you know how to use command...

Mohd answered on Apr 27 2021
145 Votes
completed solution/AppleOrange.java
completed solution/AppleOrange.java
public class AppleOrange {
    public static void main(String[] args) {
        try
    {
            int len = args.length;
            if(len == 1)
            {
                int number = Integer.parseInt(args[0]);
                if(number == 0)
                {
                    System.err.println("One positive number required as a command line argument.");
                    System.err.println("Example Usage: java AppleOrange [number] ");
                }
                else
                {
                    for(int i=1;i<=number;i++)
                    {
                        if(i%3 == 0 && i%7 == 0)
                            System.out.print("appleorange ");
                        else if(i%3 == 0)
                            System.out.print("apple ");
                        else if(i%7 == 0)
                            System.out.print("orange ");
                        else
                            System.out.print(i+" ");
                    }
                }
            }
            else
            {
                System.err.println("One positive number required as a command line argument.");
                System.err.println("Example Usage: java AppleOrange [number] ");
            }
        }
        catch(Exception ex)
        {
            System.err.println("One positive number required as a command line argument.");
            System.err.println("Example Usage: java AppleOrange [number] ");
        }
    }
}
completed solution/AppleOrangeTest.java
completed...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here