Java SE Assignments Basic

Assignment 101: The calculator (preferably made after lesson: 004) Write a command line calculator that accepts two whole number operands and a math symbol (totaling three arguments). Our calculator must support at least these symbols: “+, -, *, /, %”. The output of the program will be the result of the math operation applied to his operands. The calculator is only required to work with whole integer numbers and no floating point values. Try to write a JUnit test class to go along with your project (use Mockito and System.setOut(PrintStream)). After having finished the calculator implementation, see if you find the time to experiment with how double and float value types affect the outcome (does 10. / 0. throw an ArithmeticException?). Tip: Java 1.7 support switch statement with strings. Example usage (note that on Windows, you need to dubbelfnutt the asterisk): java com.mypackage.Calculator 2 + 3  5 java com.mypackage.Calculator 5 + -5  0 java com.mypackage.Calculator 1 “*” 1  1

Assignment 102: CharAnalyzer (preferably made after lesson: 004) Write a program that accepts an arbitrary length string and then output to the user which indexes of the char sequence is not an alphabetic letter and which indexes is upper- or lower case alphabetic letters. You don’t have to write JUnit tests for this class. Example usage: java com.mypackage.CharAnalyzer eS0me  1: Is lowercase 2: Is uppercase 3: Is not a letter 4: Is lowercase 5: Is lowercase

As we haven’t talked much about looping constructs just yet, I’ll give you a hint: char[] chars = args[0].toCharArray(); for (int ii = 0; ii < chars.length; ++ii) { ... // Now what?? }

Assignment 103: Food stores (preferably made after lesson 006) This assignment will practice your object oriented design (OOD) skills. The assignment will incorporate the single one most important (and probably most overused too) OOD principle: Separate interface from implementation. Step one will be to write two interfaces: Store and Product. For starters, we also want two example implementations of each interface. Say COOP and ICA, Milk and Cheese. The client code, say a Customer where your static main(String[]) method resides, requires a console (read the JavaDoc of System.console()). If a console is attached to the JVM, the program continues the program flow. What exactly the program does is up to you. It is not written in stone that the client code has to be “a customer”. The client might be a product supplier, a governmental food inspection office or whatever. You might even have another concept in mind than food stores and products! But, three requirements must be fulfilled: 1) The human user uses the console to interact with the program, making choices. These choices will 2) demonstrate the functional differences between different implementations that share one single interface (at least two different interfaces exist). Your written code must 3) demonstrate a minimized dependency on class types and a maximized dependency on interface types. Tip #1: The product has a reference price, yet different stores charge different prices? Tip #2: When you’re done and if your application is based on buying products with the main difference between stores being the price, feel free to write a ShoppingCart as a middle man between the customer’s logic and the many different possible stores. The UI could go through a twostep process: Put items in a shopping cart. Then buy the shopping cart from a selected store.

Assignment 104: A typed value-store (made after lesson 007; TypeParameter_PartII.java) Write a generic class that wraps a value the client code can get and set. public class GenericExample { public static void main(String... ignored) { XXX myStore1 = new XXX(); myStore1.set("Hello World!"); String helloWorld = myStore1.get(); XXX myStore2 = new XXX(); myStore2.set(myStore1); myStore1 = myStore2.get(); } } class XXX ...

Assignment 105: Decrypting Java generics (made after lesson 007) The utility class Collections is distributed together with the JDK and offers a whole lot of static methods that will ease your everyday life when working with collections. Find the source code of this class and find the method that copies the elements of one List into another List. 1) Put down in your own words an explanation as to what the hell this method does and why it has the signature it do. At least two people must understand what you are talking about for an approval of this assignment. 2) What is the acronym of the principle behind the pattern used by the copy-method? What does the acronym stand for? What are the synonyms you could use for the copy-method’s parameters dest and src?

Assignment 106: Relationships (made after lesson008.relationships) Here’s a picture of a cut orange fruit:

Obviously, the orange fruit is composed of a few things, one of them - the seed - is an unwanted property for high-quality juice factories. Here’s a factory that must pick the seeds from an orange before making the fruit into juice: public class JuiceFactory { public static void main(String... ignored) { Orange orange = new Orange(); System.out.println("Taste of orange: " + orange.getTaste()); while (orange.hasSeeds()) { Seed seed = orange.pickSeed(); System.out.println("Picked seed from orange: " + seed); } ... squeeze orange and stuff } }

A run of the factory yields this output: Taste of orange: Picked seed from Picked seed from Picked seed from Picked seed from Picked seed from Picked seed from Picked seed from

SWEET orange: orange: orange: orange: orange: orange: orange:

Seed[volume=10] Seed[volume=10] Seed[volume=7] Seed[volume=9] Seed[volume=10] Seed[volume=10] Seed[volume=8]

The factory is done in the sense that you don’t need to implement the squeezing stuff. Your task is to write the Orange class and all orange-related types. Important: In this assignment, we will not use any kind of access modifiers unless we need to (toplevel classes must be public). All fields, methods, package classes and other things shall have no access modifier declared (adding them is the next assignment). Also, put all types including the factory in the same package. Here is what I had in mind. The Orange class is-a Fruit. Fruit has-a Taste (enum of SWEET, BITTER, SOUR, SALTY). Of course, a fruit cannot exist unless it has a taste. In this assignment, we will keep it simple and let all oranges taste sweet. The orange has-a Seed[]. The Seed has-a volume (an imaginary unit of int). It would be kind of strange if all the oranges on this earth had the exact same amount of seeds and it would be equally strange if all seeds had the same volume. Therefore, when the factory creates a new orange (yes, it is strange, we should probably have a farmer for this), the amount of seeds and each seed’s volume is randomized and may fluctuate between an arbitrary chosen interval. Use a provided static method in com.martinandersson.utils.Utils to get a random int or write your own logic for doing so. To get a pretty print of a picked seed in the output window, one has to override Object.toString(). It is possible to do so for the Taste enumeration too, but not necessarily. If you make a type nested or not, if you put the type in his own .java file or not, is up to you. Hopefully, all things we do have a motive and as a group, we will discuss the various possibilities once we are all done.

Assignment 107: Visibility (made after lesson008.visibility) This assignment builds on the last. In assignment 106, we didn’t add any access modifiers and we put everything in the same package. I bet everything seem to have been working just fine. Separate the factory from the Orange class and

all orange-related types by moving the factory java file into his own package (or the other way around if you will). Surely, the factory will no longer compile. Task 1 is to make the code compile by making the symbols used by the factory visible. Task 2 is to add access modifiers to just about everything you have (types, fields, methods). How-to distribute the access is a good thought exercise and when the exercise is done, we will discuss all the possibilities in the group.

Assignment 108: The Blackjack game This assignment will put all of your Java skills into one fun experience. Implement this version of a simplified Blackjack game: http://programmingbydoing.com/a/project-blackjack.html