You are on page 1of 6

PRO-LCU Programming in Java Assignment 5. G.

Fleischer, instructor

Submit for marking: (Exercise 1 OR Exercise 2) AND Exercise 3. Advice. Whatever your choice, make a good effort to write them all, as preparation for Test #2. Exercise 0. (background and warmup. Help available in the next Lab) Study the posted Ellipse, Circle, Rectangle classes, and the Animal Hierarchy classes of a recent lab. Now create a class of your own called Square which is derived from the Rectangle class (i.e. inherits from it). Add a public method to Rectangle that computes the length of the diagonal of the rectangle, and show in a client test program how Square inherits this method, as well as the area() and semiPerimeter() methods. Now add extra capabilities to Square as follows: IF NOT PRESENT IN THE Rectangle class, define a boolean method called containsPoint (Point p) that returns true if and only if the current square object contains the given point p on or inside it Carefully define the other three corners of the square (top right, bottom left, bottom right), then write a boolean method called intersects(Square s) , that returns true if and only if the given square object (this) intersects the square s.

This method must call the containsPoint () method (a few times). Careful with your and-s and or-s ! To get the idea, think about the following pictures (next page)

Exercise 1. a. Write a client program that creates an array of Square objects, all of which have the top left corner at the origin (0, 0). All you have to specify are the lengths of their sides. To make it interesting, create a data file containing these lengths. This is a simple text file you can create with Notepad, or whatever you use if you have a Mac. Make the data file long-ish. You can have several integers per line, and lines of varying lengths. In numeric input Java doesnt care. Separate the numbers by spaces. There is no restriction on the lengths of each line in the file. Indicate end-of-file with a negative number, obviously an impossible length. Unlike the text-processing example shown in the online pack, here use inp.nextInt() instead of nextLine(). You are processing numbers, not text. Once this is done, write an additional public method into the Square class, that takes an array of Square object s as a parameter, and returns (to the nearest integer), the side of a new, bigger square whose area equals the sum of the areas of all the squares in the array. Call it what you like. Note that at first calculation, this length will be of double type. You must then round it to the nearest integer. Have your client program call this method for demonstration.

b. Add another public void method to your Square class: this method should swap two elements of an array of Squares, given as a parameter to your method. The indices of the elements to be swapped are to be integer parameters of the method. See class notes. Have your client program verify that the elements did indeed get swapped. Exercise 2. Prepare a text file with a few meaningful lines of text in it. A large example called lines.txt is on Omnivox in the zip package Files for Asgt5. Look up your notes or earlier labs if necessary, then do the following. Create a public class called TextProcessing to which you will add various array-of-char manipulation methods. Do NOT import the Arrays class from java.utils in your final production copy. You may use this class during program development. More on the Arrays class at the end of this Exercise.

The TextProcessing class might not contain any fields (member variables), except possibly some private booleans(?) and Strings, if you see the need for housekeeping purposes. Now add the following two methods to your TextProcessing class: (1) A static method that turns a String parameter into all upper case, then extracts all the letters from it and stores them in a char[] array. Note: The String class has a built-in toCharArray() method. Use this only to verify your own code! Remove it from your production copy, and write your own code instead. This method built into the the String class, is an instance method, it is attached to a String object. In other words itt works like this: Assume String st is already defined. Now declare char [] a = new char[ st.length() ]; a = st.toCharArray(); By contrast, your toCharArray() is static and takes the String as a parameter. Thus its method call would read something like char[] cArr = TextProcessing . toCharArray( myString );

(2) An int-valued method based on Linear Search that returns the index, in the array of part (1) of a letter given as a parameter, and -1 if the letter is not present in the array. If you want to make it interesting:

Write a protective method that takes the letter as keyboard input, and has an input validation loop that keeps prompting the user till the character entered is actually a letter. The protective method should then call the method of part (1). (3) A boolean valued method that decides, for each line of the input file, whether it is a palindrome or not. Endnote to Exercise 2. About the Arrays class The java.utils.Arrays class contains a number of useful static methods for array handling. The arrays may have any basetype, i.e. int, double, or any primitive type, or even objects of a class. Here are the APIs of two of the useful utilities of this class: static String toString(TypeName [] arr) : allows direct output of the array; the components are printed within a pair of square beackets [, ] , separated by commas. Example: System.out.println( +Arrays.toString( myArray ) ); Note how the syntax differs from the common use of the toString() method. static boolean equals (TypeName [] arr1, TypeName [] arr2): returns true iff the two arrays are equal in content. Example: if ( Arrays.equals( myArr, yourArr) doSomething(); else doSomethingElse();

There is of course a lot more to the Arrays class, e.g. various built-in sorting routines. For further details, if interested, see the Oracle website for a very thorough documentation, or take one of the Java tutorials there. Warning: the tutorials are quite clear but they move fast!

Exercise 3.
A Mini Spreadsheet, implemented using a two dimensional array.

Write a Java program to simulate a small spreadsheet. You are the teacher in course XYZ-101 01. You gave 6 assignments, 2 midterm tests and a final exam. The final grade is computed as in your present Java course, i.e. it is the better of two marking options, A and B.

In both options the assignments count for 30% of the final mark. To arrive at the assignment average for an individual student, you drop the weakest of the six assignments and average out whats left. Formula to use:( Sum of the assignment marks - (the minimum of the six marks))/5

Option A: 30% of the average of the assignments+15% of each midterm test mark + 40% Final Exam mark.

Option B: 30% of the average of the assignments+ 15% of the higher midterm test mark + 55% Final Exam.

See attached Excel spreadsheet for a sample output. Your output should look like it, as much as possible.

At the bottom of each numeric column display the average of the entries in that column. For the second table, the one with the column headings AsgtAvge to Option B, laso display the standard deviation of the scores. This is computed according to the formula

Standard Deviation =

, where x denotes the average of the scores, the xi are the

individual scores, and n is the number of scores.

Reminder: when computing averages of integer scores, be sure to cast the sum into (double) before dividing by the number of students.

Your job: the spreadsheet functions that you activate by simple mouse clicks when using an actual spreadsheet program are now Java methods which you must write yourself. There is no need for a separate class. You can keep the main() method and your other methods inside one single class. Your methods must be static, because you will be calling them from within the static method main().

Tips. Keep the headings and the list of student names in separate arrays.

To avoid tedious reentry of data in the testing stage, you may hard-code the numeric data. This implies that you will declare and initialize your two-dimensional array in the style { {number, number,..,number},..., {number,..,number} }; See the textbook and class notes for a readable layout. The Final Mark column should be all integers, hence yet another separate array. The most realistic solution uses an input file. See the recent lecture on this. This implementation will require a bit of thought, but it is well worth it for your learning.

*** End of Assignment 5 ***

You might also like