CSPP51036-2 Java Programming Prof. Gerber Fall 2011 Homework 2 Due: Mon. Oct 24, 5:30pm 1.CurrencyMaker: (10 points) Use the java.util.Currency class to write a program called CurrencyMaker that takes as input a valid ISO 4217 currency code (Google it -- e.g. "USD", etc.) and prints out the currency "symbol" (e.g. "$"). You can use either a Scanner or pass in args[] directly to main. The class with the executable main method should be called CurrencyMaker. Exceptions/Errors handled: Currency.getInstance(strCode) throws an exception if you supply a bad code. If thrown, catch this exception, print out a nice message reminding the user what he must suppy, and return. No need to have a loop which asks the user over and over until exit is typed. ------------------- 2. ArrayListPrinter: (20 points) Create an array of String called strSports; Declare and initilaize the array in one statement like this: String[] strSports = {"Squash", "Baseball", "Football", "Golf", "Badminton"}; Iterate over this array using a foreach loop printing each element to the console. Iterate over this array using a for loop (e.g. use nC as a counter) printing each element to the console. Iterate over this array but only print those sports that end with the letters "ball". Iterate over this array, but only print elements with even indices (e.g. 0,2,4). clue: better to use a for loop. Declare and instantiate an ArrayList of Strings like this: ArrayList strCountries = new ArrayList(); add at least 5 countries to this ArrayList, of which at least two of which must end with "stan" such as Afganistan, Kazakhistan, Kyrgystan, Pakistan, Turkmeinistan,Uzbekistan, Tajakistan. Iterate over this ArrayList using a foreach loop and print out the elements to the console. Iterate over this ArrayList using a for loop (e.g. use nC as a counter) and print out the elements to the console. Iterate over this array (using either for or foreach) but only print those countries that end with the letters "stan". Iterate over this ArrayList, but only print elements with even indices (e.g. 0,2,4). clue: better to use a for loop. The class with the executable main method should be called ArrayListPrinter. Exceptions/Errors handled: none ------------------- 3. RollCall: (30points) Create an abstract Person class Fields: private String strName Constructors: Person(String strName) Methods: public abstract String vocalize(); getter and setter for field With abstract methods, you do NOT write the implmentation, just the signature. This forces any extending class to implement this method. Also, you can never instantiate a Peron object (but you can store sub-class objects in a Person reference). Create a Student class and extend Person Fields: private String strNationality private String strSport Constructors: Student(String strName, String strNationality, String strSport) //call super(name) within this constructor Methods: public String vocalize(); //this method will return "My name is " + name + " I'm " + nationality + " and I play " + sport. getters and setters for fields Create a Teacher class and extend Person Fields: none Constructors: Teacher(String strName) //call super(strName) within this constructor Methods: public String vocalize(); //this method will return "My name is " + name + ". Roll call please: " In the RollCall class (which has an executable main method), Create an ArrayList of Person, e.g. ArrayList Add one Teacher, and at least 5 Students. Iterate over this ArrayList calling the vocalize() method of each element and printing the results to the console. Exceptions/Errors handled: none ------------------- 4.MyAsciify: (40 points) Copy the Asciify class (located in edu.chicago.cs.java.lec03.ascii) into your homework package. change all methods defined with the private keyword to protected. Create a class called FlexAsciify which extends the Asciify class you just modified. Within FlexAsciify: Create the following constants (public static final) int CONTRAST_LOW = 0; int CONTRAST_MED = 1; int CONTRAST_HIGH = 2; //define a method called getAsciiChars() with the following signature: public static char[][] getAsciiChars(String strClipURL, int nDimW, int nDimH, int nContrast) //you can copy and paste the code from directly within the Asciify method body for public static char[][] getAsciiChars(String strClipURL, int nDimW, int nDimH) //within your getAsciiChars() method of the FlexAsciify class, instead of calling getAsciiValueChar(nGreyValue), you will call a method called getAsciiValueChar(nGreyValue, nContrast); //now you need to define a method whose signature looks like this: private static char getAsciiValueChar(int nGrey, int nContrast) in your FlexAsciify class. //again, you can copy and paste code directly from getAsciiValueChar(int nGrey) in the Asciify class //Depending on the contrast you supply this method, it will return low, med, high contrast asciified image. //You can see that the gates are more or less evenly distributed along a continuum between 0 and 255 with gates at 50, 70, 100, 130, etc. Change these gates to produce a character of low, med, and high contrast depending on the nConstrast parameter supplied. A gate distribution which is narrow in the middle ranges and wide on the extremes will produce high contrast. The opposite will produce low contrast. The med contrast can the same gate distribution as is already defined in the Asciify class. Create a class called MyAsciify (with an executable main method). Within this class, use a Scanner to ask the user for a url to an image on the internet -- can be jpg, gif, png, etc. Then ask user for an image dimension. Use the Scanner for this. Then ask the user for a contrast, high, medium or low. Use the Scanner again. declare and initialize a two-dimensional array of chars like this, using the values you got from your user: char cImage[][] = FlexAsciify.getAsciiChars(strClipURL, nDimW, nDimH, nContrast); //the output image will be square, so nDimW and nDimH are the same dimension //remember, FlexAsciify.getAsciiChars() calls FlexAsciify.getAsciiValueChar() //and you will pass your nContrast first into getAsciiChars() and then into getAsciiValueChar() both of which are defined //inside your FlexAsciify class. In MyAsciify, iterate over this two-dimensional array of chars using a nested for loop; here's one possible way to do it: for (int nRow = 0; nRow < cImage.length; nRow++) { for (int nCol = 0; nCol < cImage[nRow].length; nCol++) { System.out.print(...some code here } System.out.println(); } .... to print the asciified image to the console. Exceptions/Errors handled: inputs from user; if dimension not an integer or contrast not an appropriately defined contrast (see constants), then print out a nice message reminding the user what he must supply, and return. No need to have a loop which asks the user over and over until exit is typed.