A08

Due Date: Friday, 19 March 2021
File(s) to be submitted: A08.java, AddLineFunction.java, FactorialLineFunction.java, MaxLineFunction.java, MinLineFunction.java, MultiplyLineFunction.java, TooManyArgumentsException.java
Sample Output: SampleOutput.html
Starter Files:


SUBMIT   /   Check

Exceptions, Lists, Interfaces

Summary

Complete the program A08. Create five classes that implement the LineFunction interface:

Create the class TooManyArgumentsException to represent having too many arguments.

For this assignment, you do not need to include messages on the exceptions you create.

Details

I have given you (most of) a program that reads and executes commands. Its intended use is to allow the user to calculate some simple multi-argument functions. For example:

>>> add 2 8 5 13 6 -2 9 Answer: 41
Here the user asks the program to add up a line of integers (including possibly some negative numbers).

Right now it only understands two commands:

There are three sections in the program that are marked "TODO". Each section requires you to add code so that the program can complete its part of the total package.

The first TODO is the part where you add more commands by creating LineFunction objects and adding them to commandList.

I explain more about LineFunctions below.

The second TODO section passes the rest of the line off to the LineFunction object that matched the command and prints the answer that comes back.

Of course there are exceptional circumstances to deal with:

The third TODO section is in the method findFunction. You need to complete the method definition (right now it's just a stub) and revise the javadoc comment to reflect the design decisions you've taken.

The matching must be done by name, and it's perfectly acceptable to simply match the name in its entirety (ignoring case). Thus, it's perfectly OK if you require the user to enter "maximum" if they want the maximum function, and "multiply" if they want the multiply function. You can do that and get full score on the assignment.

Alternatively, you can get some bonus points (up to ten) by allowing the user to abbreviate the function names.

NOTE: no bonus points for hard-coded abbreviations! In order to get the bonus points, your code has to work with other functions I might add to your program. So I might have a function called "ackers", and your code would have to recognize a suitable abbreviation of that.
In order to get all ten bonus points, your code needs to: For example, your code should: You can get partial credit for dealing with abbreviations even if you don't satisfy all those conditions.

LineFunctions

Note that LineFunction is an interface, so you can't just use new LineFunction() to create one. Note also that it has two abstract methods in it, so you won't be able to use a lambda expression, either.

You will need to create five classes, each one implementing the LineFunction interface. In each class, you need to implement the getName method, which just returns the name of the function (add, multiply, ...), and the processLine method, which reads the function's arguments from the String given to it and returns the value calculated.

For example, the LineFunction that adds up numbers might be given the String
" 2 8 5 13 6 -2 9"
From which it would read the numbers 2, 8, 5, 13, 6, -2 and 9. It would then return their sum (which is 41).

The five LineFunction classes you need to create are:

In addition to the exceptions mentioned above, the processLine method throws an InputMismatchException if any argument is not an integer.

Hint: The Scanner method nextInt throws an InputMismatchException if the input is not an int.

It might also interest you to know that it throws a NoSuchElementException if there is nothing there when it tries to read.

You might want to read my hints on reading from a String below.

The Exception Class

Note that one of the exceptions mentioned above (the TooManyArgumentsException) doesn't already exist in Java (the others do). That means you're going to have to create it and submit it with the rest of your code. You are also going to have to write the code to throw it yourself, since Scanner isn't going to throw it for you.

Reading from a String

We learned how to read from a String back in 1226 when we started discussing object oriented programming. The easy way to do it is to create a Scanner that reads from the String -- rather than reading from System.in, like our keyboard Scanner does.

The commands to read from the String are exactly the same as the commands to read from the keyboard -- next, nextLine, nextInt, nextDouble, and so on -- but there is one difference, and one new piece of helpful information based on that difference.

The difference between reading from a String and from System.in is that we expect the String to end after a while. We don't expect System.in to end.

Note that it is possible for System.in to come to an end, but we don't usually expect it to happen. Also, NetBeans makes it very hard (if not impossible) to end System.in in a program running under its control.

When you try to read from a Scanner, and there's no more data there, a NoSuchElementException will be thrown.

Remember, sometimes we will want our LineFunction object to throw that exception; and if the Scanner is already throwing it for us, well then what's left for us to do except step out of the way!

Because we expect the input to come to an end, we might want to peek ahead every once in a while to check whether there's more data there. For that we can use the Scanner's hasNext method. That method returns true if there is more data, and false otherwise. Thus we can read all the numbers from a String by using a while loop that runs until there is no next element.

This loop looks kind of like a ListIterator loop:
while (it.hasNext()) { Integer value = it.next(); // use value here // }
The differences are:

Final Words

There's a lot to do on this assignment, and several files to work on and submit. You should get started early, and work on stuff you know how to do before moving on to stuff you don't know how to do.

For example, the exception-handling code is only a small part of the assignment. Get the other stuff written, then figure out how to add on the exception-handling bits. (That part is pretty small.)

The five LineFunction classes are going to be pretty similar. You might want to start with AddLineFunction and add it to your program before you work on any of the others. Once you've got it working, MultiplyLineFunction is a very simple modification.

From there the minimum and maximum methods are pretty simple variations on each other, and not terribly different from AddLineFunction.

FactorialLineFunction is the most complicated, because it's the only one that you need to do error checking on yourself. Still, we've dealt with throwing exceptions before, so you should know how to do that.

The only bits that are new for this week are the exception class I want you to create and the exception-handling I want the program to do. That's stuff you can put off till after we've covered it in class.


SUBMIT   /   Check