32
LAMAR UNIVERSITY SYLLABUS COSC 1337 PRINCIPLES OF COMPUTER SCIENCE II Mr. Myers L. Foreman Last Revised Fall 2007

cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

  • Upload
    vanmien

  • View
    224

  • Download
    0

Embed Size (px)

Citation preview

Page 1: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

LAMAR UNIVERSITY

SYLLABUS

COSC 1337

PRINCIPLES OF COMPUTER SCIENCE II

Mr. Myers L. Foreman

Last Revised Fall 2007

Page 2: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

COSC 1337 -01 Class Schedule Fall 2007 Semester

Monday Tue Wednesday Thu Friday

Aug 24 First Class

Aug 27 28 Aug 29 30 Aug 31

Sep 3 Holiday 4 Sep 5 6 Sep 7

Sep 10 Prog 1 due 11 Sep 12 13 Sep 14

Sep 17 18 Sep 19 20 Sep 21 Prog 2 Due

Sep 24 25 Sep 26 Test 1 27 Sep 28

Oct 1 2 Oct 3 First Drop 4 Oct 5 Prog 3 Due

Oct 8 9 Oct 10 11 Oct 12

Oct 15 16 Oct 17 18 Oct 19 Prog 4 Due

Oct 22 23 Oct 24 25 Oct 26

Oct 29 30 Oct 31 Nov 1 Nov 2 Prog 5 Due

Nov 5 Test 2 6 Nov 7 8 Nov 9 Last Drop

Nov 12 13 Nov 14 15 Nov 16 Prog 6 Due

Nov 19 20 Nov 21 22 Holiday Nov 23 Holiday

Nov 26 27 Nov 28 29 Nov 30 Prog 7 Due

Dec 3 Last Class Prog 7 accepted late until 5:00

Dec 10 Final Exam 11:00-1:30

Page 3: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)
Page 4: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

COSC 1337 FALL 2007 General

Information

TEXTS: Absolute Java, 3 rd ed. , Savitch COSC 1337 Example Booklet (Kampus Korner Bookstore)Linux User’s Guide Using the Command line and Gnome with Red Hat Linux ,

Gillay

OFFICE HOURS : 9:00 - 10:00 MWF (office 75 Maes)11:15 - 12:00 MWF

GRADING :programs 1/4 Test 1 1/4 Wed, Sep 26 ( TENTATIVE )

Test 2 1/4 Mon, Nov 5 ( TENTATIVE )

Final exam 1/4 Mon, Dec 10 (11:00 - 1:30) ( FIXED )

LATE PROGRAMS:Programs are due at the beginning of class on the date specified. Late programs will be accepted until the first class meeting past the date due. If turned in late but on the due date, 10% will be taken off. From then to the next class meeting 20% will be taken off. After that, the program will not be accepted except in special circumstances. Many program assignments will have an optional extra credit component However, extra credit cannot produce an average higher than 100%. In addition to a paper copy of your program, data, and output, you will send a copy of your source code and any data files as attachments to my grader using yahoo email.

MAKE - UP TESTS:Except in special circumstances, NO MAKE-UP tests will be given. The final will be counted twice if you miss a test.

CURVE:No curve is guaranteed. If I feel a curve is justified at the end of the semester, then one will be applied to your final average. The curve will NEVER be larger than 10 points and is typically a point or two.

PROGRAMS :Programs will be written in Java and run on a Unix or Linux system. The example programs in this booklet were tested in Netbeans, version 5.5. Some have also been tested on Linux. Before the semester ends, I hope to have tested all of them there. You will be given permanent ids to our Linux machines. Seven(7) program

Page 5: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

assignments will be made. Programs will be due approximately every two weeks.

WHAT TO TURN IN:When turning in a program assignment, all I want is a printout. Do not fold the printout. Write your NAME, SECTION, and the PROGRAM NUMBER and PROGRAM NAME on the page in large clear letters. The printout will be a shell script containing:

Source codeCompilation commandInput data file(s) (if present) Run commandProgram executionOutput file(s) (if present)

OTHER DATES OF INTEREST: Oct 2 is the last day to drop or withdraw with a Q/W. (YOUR FIRST TEST WILL BE GIVEN ON OR BEFORE THIS DAY.)

Nov 9 is the last day to drop or withdraw. (YOUR SECOND TEST WILL BE GIVE ON OR BEFORE THIS DAY.)

COLLUSION ON ASSIGNMENTS :

If I feel that two programs are too similar, the grade given both programs will be zero (0). This imposes a burden on you that you not allow others to copy your programs. This may mean not throwing old versions away at school, as students have been known to remove programs from the wastebasket to copy them. Furthermore, you are not to obtain assistance from others on how to write the algorithm for the solution of a program. You may ask for help on removing a syntactic or semantic error from a program already written.

THIS BOOKLET ON DISK: All of these files can be downloaded from cs.lamar.edu. I can also make you a copy of everything on a CD or flash memory.

EMAIL : [email protected]

Page 6: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

CS 1337 Principles of Computer Science II Topic Outline

Course Description -Continuation of COSC 1336. Review of control structures and data types with emphasis on structured data types. Applies the object-oriented programming paradigm, focusing on the definition and use of classes along with the fundamentals of object-oriented design. Includes basic analysis of algorithms, searching and sorting techniques, and an introduction to software engineering.

Java Topics

1. Review of programming topics from 1337 A. Language primitive atomic data types (int, double, char) B. User defined atomic data types (enum) C. Control structures D. Input/output E. Functions F. Classes and objects G. Single and multi-dimensional arrays

2. File I/O using text files

3. Algorithm Analysis A. Worst case analysis (big 0) B. Average case analysis C. Best case analysis

4. Debugging techniques A. Desk checking B. Output statements C. Built in debugging schemes D. Interactive debuggers

5. String processing

6. Sorting A. Bubble sort (Review) B. Insertion sort C. Selection sort D. Quick sort

8. Searching A. Linear search B. Binary search

9. Simple Classes

Page 7: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

A. Operator Overloading B. Copy constructor; shallow and deep copying C. Reference types versus atomic types D. Public versus Private

10. More Advanced Language Topics (as time allows) A. Inheritance B. Exception Handling

11. Linear Data Structures A. Stack B. Queue (in particular, the circular queue)

12. Recursion A. Direct B. Mutual C. Tests for valid recursive algorithm. D. Elimination of tail recursion. E. Alternatives to recursion.

1. Iteration2. User defined stack

UNIX

0. Learn facility1. Brief history in an overview of UNIX, AT&T, System V, BSD.2. Logging in/out, csh, simple commands; intro to the UNIX directory structure; Pine email3. Vi editor, command and text input modes; memory buffer;exit (w or w/o savng)4. More csh commands; script, ls, cd, cp, mv, rm, cat, more, lpr, mkdir, etc, using email5. Using buffers in vi; cut and paste; moving around in vi; exrc6. I/O redirectin; pipes; simple filters (grep and sort); filename substitution7. Find command; head, tail, wc, echo, make, tar, ar, shell and environment vars8. Simple shell programming, cshrc, profile, alias, protection modes using chmod9. Command line parameters in C++, argc, argv, exit10. (Optional) Remote commands telnet, ftp, who, finger

Page 8: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

PROGRAM 1 COSC 1337 single dim array class

This is a review problem. Create an array class and methods listed below that operate on objects of the class:

1. Default constructor creates an array of 5 integers.2. A constructor with a parameter that determines the size of the array.3. A copy constructor.4. toString Outputs the array 5 per line with spacing.5. put(index, value) Store the value at the specified index6. get( index ) Extract and return the value at the specified index7. input inputs the desired number of integers from the terminal8. sum computes and returns the sum of the array9. sumPositive computes and returns the sum of the positive integers10. aveNegative computes and returns the rounded average of the negative integers11. sumInRange( lowest, highest ) computes the sum of the integers in the range12. countInRange( lowest, highest ) counts how may are in the range

Tasks your main should carry out:a. Create an array A using the default constructor.b. Input 4, -7, 23, -4, 1 into A.c. Make a copy of it B using the copy constructor.d. Use get and put to double all the elements in A. B is not changed.e. Output both A and B.f. Use sumPositive on A and aveNegative on B. Output these results.g. Construct another array C holding 20 values. Use put to store the first 20 perfect squares

in it. 1, 4, 9, … , 400h. Use toString to display C.i. Use sumInRange and countInRange to find the average of the numbers in C between 100

and 300.

Output values should be labeled. Check your answers.

EXTRA CREDIT (15 %) Add these methods and this task to your program:

13. isPositive( index ) returns true if the element at index is positive14. isEven( index ) returns true if the element at index is even

j. displayPositiveEven use the two methods above to output those elements of B that are both positive and even

Page 9: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

PROGRAM 2 COSC 1337 POSTFIX EXPRESSION EVALUATOR

In this program our expressions will consist of only the following elements:

(1) operands : a single letter to be converted to an integer(2) operators : addops(plus, minus),multops(asterisk, slash), and

carrot. Most have the usual meaning. (Carrot is exponentiation.)

(3) semicolon : signals the end of the expression

A stack is a useful device for evaluating expressions, particularly expressions written in polish postfix form. This technique is so useful that many modern assembly languages have a set of arithmetic operations that are stack oriented. 'Postfix' form means that the operator always follows the operands upon which it is to operate.

Postfix expressions have three advantages over infix expressions. First, they can always be scanned left to right (no precedence rules). Second, they do not require parentheses. Third, no temporary variables are required (except for the stack itself). Looking at the example and the algorithm below, you will notice that the stack contains only operands; the operators in a postfix expression are evaluated as they are encountered. The algorithm for the evaluation of a postfix expression is:

Initialize stack to empty ( top = -1 )Not_end_of_expression = true

While ( Not_end_of_expression ) Get_next_character Case ( character ) of

Operand => push value of operand on stack Operator => pop appropriate number of operands.

If stack underflow, then error (too few operands) else {Perform operation. Push result on stack.}

End_character => Pop top of stack getting result. If stack_not_empty then Error (too many operands)

Not_end_of_expression = false Otherwise => Error( unexpected character )

Not_end_of_expression = false EsacElihw

Page 10: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

OPERANDS will be CAPITAL LETTERS (A..Z). 'A' will be given the value '1''B' will be given the value '2' . . . And so on up to 'Y' = 25.'Z' will be given the value '0'

This conversion is simple. Capital ‘A’ has ASCII value 65. ‘B’ is 66 and so on. Therefore, you will write a function intValue that is sent a character and returns an integer. For all except ‘Z’, you can merely compute intValue = (int) letter - 64. In the case of ‘Z’, intValue will be 0.

As the values stored are all integers, division (/) will default to integer division. This is correct.

BEA-^; example string evaluated below

CHAR ACTION TOP(before) STACK(before) TOP(after) STACK(after)

B PUSH(2) -1 NULL 0 2 E PUSH(5) 0 2 1 2, 5 A PUSH(1) 1 2, 5 2 2, 5, 1 - POP(1) 2 2, 5, 1 1 2, 5 POP(5) 1 2, 5 0 2 (5 - 1) Computation PUSH(4) 0 2 1 2, 4 ^ POP(4) 1 2, 4 0 2 POP(2) 0 2 -1 NULL (2 ^ 4) Computation PUSH(16) -1 NULL 0 16 ; POP(16) 0 16 -1 NULL OUTPUT RESULT

The input strings are as follows:BC+; => 5EADB/+*; => 15CD*AB-+B/; AB+C^; => 27MBCD^*+; ABC+D^*; CBA-^H^EM/^;GA*B-C/;AZ+SC-*D+;

Write your program so that it will process all the data in one run. You may assume that the expressions are well formed and that no errors will occur.

Page 11: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

I am expecting good program organization and style.

EXTRA CREDIT 10%Rather than inputting from the terminal or using input redirection, use

file input to input the strings from a file. My FileIo example shows exactly how to do this. It is easy.EXTRA CREDIT 10%

In addition to the stack class, add a Postfix class the data of which will be one of the input strings. Such functions as “input”, “output“, evaluate, etc will be needed.

Page 12: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

PROGRAM 3 COSC 1337 String Manipulation

This program will convert text so that it is suitable for transmission by the old style telegram. Such telegrams consisted only of numbers, upper case letters and spaces. In our case the input text may also contain lower case letters and punctuation characters to be converted, and other characters to be removed. You are to make the following conversions on the text:

Upper case letter : do nothingLower case letter : convert to upper casetab, space, digit : do nothing. (tab is ‘\t’). or ! or ? : change to ' STOP', : change to ' COMMA': : change to ' COLON'- or ; : change to ' DASH'all other characters : remove

You will create a Line class.Each Line object of which will contain exactly one line of text.

Primary member methods for Line:Line Constructor for a null string.Line Constructor for a literal.inputLine Inputs one line of text and stores it.outputLine Outputs one line of text.convertLine Calls the other methods to convert a line.

Secondary methods for Line:convertUpper Converts lower case to upper case.removeChar( index) Removes the specified single character. period Converts '.' or '!` or `?` to ' STOP'. comma Converts ',' to ' COMMA' .colon Converts ':' to ' COLON'.dash Converts `-` or ';' to ' DASH'.

Notes: Add any other methods you need. The resulting output will be rather strange. For example, the line starting with “. or ! or ?” will become:

“STOP OR STOP OR STOP COLON CHANGE TO STOP”

The data for the program is the description above below the title and above "Notes". Obviously, I recommend File I/O. I will create a data file for you containing this information. EXTRA CREDIT (10%) Convert each tab (‘\t’) in the text to “ TAB “. That is, SPACE TAB SPACE. I suspect this is easy but have not done it.

Page 13: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

PROGRAM 4 COSC 1337 A Matrix class

Using the Matrix example as a guide, implement your own Matrix class having at least the following characteristics.

Constructors:

Matrix() The data stored in your 2d array can be integer unless you do the extra credit.Matrix( int rows, int columns )Matrix( Matrix theOriginal )

Member Methods:

rows() // Returns the number of rowscols() // Returns the number of colstranspose() // Returns the transpose of the matrix b[i,j] = a[j,i]addToEach( int i ) // Add the given value to each element of the matrix returning a new

// matrix.subtractFromEach( int I ) // Subtract the given value from each element in the matrix

// returning a new matrix.MultiplyTimesEach( int i ) // Multiply each element of the

// matrix times the given value, returning a new matrix toString()input() // input a matrixmultiply( Matrix theMatrix ) // Multiply two matrices returning a third add( Matrix theMatrix ) // Add two matrices by element returning a thirdsubtract( Matrix theMatrix ) // Subtract the second matrix from the first returning a

third. DATA: Use Matrix.txt It shows eight matrices A,B,C,D,E,F,G,H.

Page 14: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

Client Main program:

Input matrices A,B,C,D,E,F,G,H // A = is.input(); assuming input stream is

Compute (or attempt to compute) I = A.multiply( C ); J = A.multiply( B ); K = C.multiply( D );

L = E.add ( F ); M = E.subtract( F ); N = H.addToEach(3); O = G.subtractFromEach(5); P = C.Transpose(); Q = G.multiplyTimesEach(-2);

R = B.add( C ); S = B.subtract( D ); T = I.Transpose();

Output the matrices A ..T

Output the number of rows and columnsin I, J and K using the rows() and cols() methods.

EXTRA CREDIT :

(20%) Also implement a member function inverse() that inverts the given matrix. Test it on the square matrices your program contains. You can find this algorithm in most any numerical analysis text.

Page 15: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

PROGRAM 5 COSC 1337 Segment and Triangle Classes

Use the Point example to assist you in creating Segment and Triangle classes.

A Segment will be defined by two Points. Methods will include but are not limited to:

Segment(Point a, Point b) //constructorSegment(Point theOriginal) // copy constructorlength // see Distance

(Point)slope // see Slope

(Point)equals // true if lengths are same within

errortoString // use the toString in PointisHorizontal // true if within error of

zeroisVertical // true if greater than

1/error

A Triangle may be constructed from three Points or three Segments so these classes must be included and constructors made for them.

Member methods include but are not limited to:

Given three Points; construct a triangle.Given three Segments; construct a triangle.Copy constructortoString() use the toString in PointisEquilateral() (All three sides “equal”)isIsoscles() (Exactly two sides “equal”)isScalene() (No two sides “equal”)isRight() (Contains an angle “equal” to 90

degrees Or one horizontal side and one vertical side)

area() ( Use Heron’s formula that just uses the three sides OR write a method that computes the perpendicular distance from a line to a point not on the line and use the standard formula.)

S = ( A + B + C ) / 2(semi-perimeter)

AREA = SQRT( S * (S-A ) * (S-B) * (S-C) ) (Heron’s formula)

Page 16: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

isCongruentTo( Triangle theTriangle ) (Each corresponding pair of sides “equal”)

Page 17: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

SUGGESTIONS:0. When dealing with real numbers, it is rarely acceptable to test for

equality or inequality. Because of the round-off errors inherent in floating calculations, it is almost always preferable to test within some acceptable error factor. For example if we want to know if A equals B, we would make the following test instead:

if( Math.abs( A - B ) < error )where error is a predefined constant, perhaps 0.000001

1. A right triangle contains a right angle. An angle is right if the slopes of the two segments forming it are negative reciprocals of one another. (Ex. 2 and -1/2). Be sure to check all three angles. An angle is also right if formed by a vertical segment and a horizontal segment.

2. Much of the triangle testing can be simplified, if your constructor stores both points and segments, and sorts them ordered by segment length.

15% Extra credit. Write and test both successfully and unsuccessfully an isSimilarTo method. (Two triangles are similar if they have equal angles, but it is easier to see if sides are proportional)

Input the following Points:

X Y Name-1 2 A-1 0 B 0 0 C 0 -2 D 2 1 E 2 0 F 4 2 G 4 0 H 2 3.464101615 I

Construct and output the following Segments

NameB C mC A nA B oE F pC F qE C r

Page 18: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

Construct and output the following Triangles

Namemno spqr t

BDF uCFE vCHG wACE xCHI yCDF z

In addition to the tasks above, your main program should also compute the following, and output the results clearly.m.isHorizontal(); n.isHorizontal(); p.isHorizontal;m.isVertical(); n.isVertical();

p.isVertical();

x.isEquilateral(); y.isEquilateral();x.isIsoscles(); y.isIsoscles(); z.isIsoscles();s.isRight(); y.isRight(); t.isRight();s.isScalene(); x.isScalene();s.area(); t.area(); u.area(); v.area()s.isCongruentTo(t); u.isCongruentTo(x);

And if you do the extra credit:s.IsSimilarTo(t); u.IsSimilarTo(x); t.IsSimilarTo(w);

Page 19: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

PROGRAM 6 COSC 1337 Stack and Queue Class (Inheritance)Use the Array class we defined as the base class for a Queue class and a Stack class.

Instantiate two Stacks (Input and Temp) and one Queue (Output).

You are to perform a rearrangement of the input string using the three objects above.

Two strings of letters will be input. The first string will have letters in descending order (Initial, or input order). The second string will have the letters rearranged into a word (final or output order). The input string is pushed onto the Input stack letter by letter. (They will be input high to low, thus the lowest will be on top after input.) The rearranged string is the desired output order. This operation is equivalent to rearranging a number of boxcars to some desired order using a spur line.

Once the initial string is input and pushed onto the Input stack, remove the first letter from the Input stack and place it either on the Output queue or the Temp stack. Once a letter is placed on the Output queue it cannot be removed until you are ready to output the desired word. A letter pushed onto the Temp stack may be removed later (pop) and either pushed back on the Input stack or inserted at the rear of the Output queue.

You will move the letters around until they are arranged in the Output queue in the order dictated by the second string. Each movement of a letter should generate an output line indicating which letter was moved and what happened to it.

For example, suppose the original string is ONECA and the desired string is CANOE

Note: The Input and Temp stacks display bottom to top as left to right. For the Output queue, front to back is left to right.

Step Letter From To Input Temp 0utput0 O InputFile Input {O} {} {}1 N InputFile Input {ON} {} {}2 E InputFile Input {ONE} {} {}3 C InputFile Input {ONEC} {} {}4 A InputFile Input {ONECA} {} {}5 A Input Temp {ONEC} {A} {}6 C Input Output {ONE} {A} {C}7 A Temp Output {ONE} {} {CA}8 E Input Temp {ON} {E} {CA}9 N Input Output {O} {E} {CAN}10 O Input Output {} {E} {CANO}11 E Temp Output {} {}{CANOE}

Note: This example does not require it, but in some cases it will be necessary to pop from Temp and push back on Input. (See “CAB”.)

Page 20: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

OUTPUT: Your output should look something like that on the previous page. If the contents of the Output Queue are continuously displayed, then there is no need to output the final word separately in a series of additional steps.

By inputting the letters in a known initial order, it should make it easier for your algorithm to determine whether the next desired letter is on the Input Stack or on the Temp Stack.

EXTRA CREDIT (15% ) Create an ArrayOfObjects class as the base class. The size of the array should be a parameter to your usual constructor. A default constructor should use size 10.

DATA: (All strings are 15 letters or less.)

Input String Final WordCBA CABONECA CANOETSNNIIEE EINSTEINTRPLIECA PARTICLEZONMLHA HOLZMANWTSRRRPOOLIEEDC TRIPLEWORDSCORE

Page 21: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

PROGRAM 7 COSC 1337 A LetterSet class In this program you will input a series of words from a file, store the unique letters in those words in sets implemented as strings. You will also perform set operations on those sets as described below. The data file is emp50.txt, but the only portion you will be using is the name. Each name is exactly 12 letters long and may contain periods and blanks in addition to capital letters. Only the capital letters are to be stored in the LetterSet. Furthermore, the letters are to be stored in the string in alphabetical order, and repeated letters are not stored a second time. The layout of the file is as follows:

3737 LATIOLIS.MON 9 M W 46.03

The LetterSet constructed from this name would be: “AILMNOST”

At a minimum, the following operations are to be implemented within your LetterSet class. The data must, of course, be private. The universal set for these set operations is obviously the set of CAPITAL LETTERS.

LetterSet()LetterSet( a string literal ) // Constructor for a string literalLetterSet( a character ) // Constructor for a single characterLetterSet( LetterSet theOriginal // copy constructor input() // Input one line from the file, extract the 12 letter name, store the

// unique capital letters in alpha order.toString() // used to utput the contents of the set in alpha order.union( LetterSet theRight )// Construct and return the union of two LetterSetsintersection( LetterSet theRight )

// Construct and return the intersection of two LetterSetsdifference( LetterSet theSet )

// Construct and return a LetterSet containing // those elements of left that are not in theRight.

equals( LetterSet theRight )// return true iff two LetterSets hold the same letters. length() // returns the number of characters in the set. May be a freeby.

// Note that these two utility methods can help solve some of the others.

member( char ch ) // Return true iff ch is found in the setadd( char ch ) // If ch is not in set add it at the correct place. o/w nothing.

e.c. complement() // set complement. A variation on set difference.e.c. subset( LetterSet theRight )

Page 22: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

// return true iff left is a subset of right. // Note that “proper subset” is not required.

The CLIENT PROGRAM will contain the following fleshed out as necessary. “e.c.” indicates extra credit.

LetterSet array[50]; // create an array of 50 LetterSets, all containing the empty string

for( int inWord = 0; inWord < 50; inWord++ ){ array[inWord] = is.input(); // I called the input stream is

System.out.println( array[inWord] );} // echo all 50

for( int loc = 0; loc <= 40; loc+=10 ){ // note that 70% of the LetterSets input will be stepped on and discarded array[loc+3] = array[loc].union( array[loc+1] ); array[loc+4] = array[loc].union( array[loc+1].union( array[loc+2] ) ); array[loc+5] = array[loc].intersection( array[loc+1] ); array[loc+6] = array[loc].intersection ( array[loc+1].intersection( array[loc+2] )); array[loc+7] = array[loc].difference( array[loc+1] ); array[loc+8] = array[loc+1].difference( array[loc] ); array[loc+9] = array[loc+5].union( array[loc+7].union( array[loc+8] ) ); if( array[loc+3].equals( array[loc+9] )output the strings with app message; else output the strings with app message;e.c. if( array[loc+9].subset( array[loc+4] ) otswame.c. else otswame.c. if( array[loc+0].subset( array[loc+1] ) otswame.c. else otswam;}// output a label for the arrayfor( int outWord = 0; outWord < 50; outWord++ ) output-> outWord array[outWord] array[outWord].SizeIs();

Create LetterSets -> B, C, D(“JOHN WAYNE YO”), E( “WAYNE JOHN WOW” )B = array[10].intersection( array[30].union( new letterset( ‘C’ ) ) ); // Note the anonymous object on the rightC = array[11].intersection( array[31].union( new LetterSet( ‘R’ ) ) );

// Output B,C, D, and E with labelsif( B.equals( array[10] ) ) output an appropriate message else oaamif( C.equals( array[11] ) ) oaamelse 0aam

Page 23: cs.lamar.educs.lamar.edu/faculty/foreman/Fall07/1337 CS II/1337 General Information...  · Web viewAbsolute Java, 3rd ed., Savitch . COSC 1337 Example Booklet (Kampus Korner Bookstore)

if( D.equals( E ) ) oaamelse oaame.c. D = B.complement();e.c. E = D.complement();e.c. // Output D and E with labelse.c. if( E.equals( B ) ) oaame.c. else oaam

Extra Credit: (10 %) The set complement(^) and subset(<) methods above, as well as the requested operations using these methods, are prefaced by “e.c.” For extra credit, implement and test these two methods as requested.

Extra Credit: (10 %) Rather than using a string, store your letter sets as an ArrayList and implement your methods on this data instead..

/