Due. In lecture on Thursday, November 30, 2000. You may turn it in to a
consultant before that date in the consulting room in Carpenter. Do not turn it
in at Carpenter on the due date. Programs will not be accepted late.
Note. Always
write your name, Cornell ID#, and the day/time/instructor for your section in
the first comment of any program you hand in for credit; otherwise it will not
be graded. This cannot be written in
by hand. If you wrote the program with a partner, turn in only one printout with your partner's name
and ID# in the comment, as well as your own. The comment must also include the
section day/time/instructor for partner; the program will be returned to the
first person listed. Sign your name(s) by the comment. Please staple the pages
of your assignment together.
Grading. This assignment will be given two grades: the
first based on correctness, the second on program organization and style. Each
grade will be a 0, 4, or 5. Not only should your program work, but it should
contain adequate comments to guide the reader who is interested in
understanding it. The declaration of every significant variable should include
a comment describing that variable. There should be appropriate comments in the
code so the reader can see the structure of the program, but not so many that
the program text is hard to read.
Goals. This assignment has two parts. In part A), you
will practice the use of inheritance and related objected oriented concepts. In
part B), you will practice programming in Matlab.
Your goal is to extend Program #3, the Family database, by creating a
subclass of Person. This subclass will be called "Royalty" and will
extend the functionality of the Person class, using inheritance and method
overriding.
Given that your program should be an extension of Program #3, the program should accept the same input commands as the original program. Your program should also accept the following two new commands:
king name
Creates a male royal named name.
queen name
Creates a female royal named name.
The behavior of the s(on) and d(aughter) commands should be extended as follows:
s name1 name2
Create
a s(on) , as in Program 3. The s(on)
should be royal if and only if both parents are royal. A royal son is a Prince.
d name1 name2
Create
a d(aughter) , as in Program 3. The
d(aughter) should be royal if and only if both parents are royal. A royal
daughter is a Princess.
The behavior of the query commands "? name" and "p" should be extended so that royalty are referred to by their proper titles. In particular, if e.g. John is a king, his name should be printed as "King John". Similarly, if x is a queen, her name should be printed as "Queen x". A prince x and princess x should be printed as "Prince x" and "Princess x", respectively. This is illustrated with the following sample output:
> king tom
> queen sue
> m tom sue
> s tom paul
> d sue jolene
> b kirk
> g chihua
> m kirk chihua
> s chihua dave
> d kirk sarah
> m paul sarah
> m jolene dave
> s paul radu
> d jolene pat
> p
name: King tom
sex:male other: Queen sue kids:
Princess jolene Prince paul
name: Queen sue sex:female other: King tom
kids:
Princess jolene Prince paul
name: Prince paul sex:male mom: Queen sue
dad: King
tom other:sarah kids: radu
name: Princess jolene sex:female mom: Queen
sue dad:
King tom other:dave kids: pat
name:kirk sex:male other:chihua kids: sarah
dave
name:chihua sex:female other:kirk kids:
sarah dave
name:dave sex:male mom:chihua dad:kirk
other: Princess
jolene kids: pat
name:sarah sex:female mom:chihua dad:kirk
other:
Prince paul kids: radu
name:radu sex:male mom:sarah dad: Prince
paul kids:
none
name:pat sex:female mom: Princess jolene
dad:dave
kids:
none
>
The key idea behind this part of the assignment is for you to learn how Java's inheritance features let you obtain the required additional functionality with relatively few changes to existing code. To enable you to do so, we have created a slightly modified version of the original solution to assignment #3. This version is written in a better object-oriented style than our original solution. You should download the classes Program6.java and Person.java from the assignment section of the course web site and minimally extend the existing code.
First, make sure you can run Program6 and obtain the original functionality as described in assignment #3.
Then, extend the program by creating a subclass of Person called Royalty. This class should be stored in a file named Royalty.java. Royalty should have its own constructor, and its own definitions of print_name and child.
The child method of the Royalty class should construct a child of the appropriate class. To check the classes of a proposed child's parents, you should use two special instance methods, called getClass() and getName(). These methods are part of the object reflection facility in Java. To access them, you will need the line
import java.lang.reflect.*;
at the top of your Royalty.java file. Given an object x,the expression
x.getClass().getName()
returns a string equal to the class name of object x, e.g., if x is of class Royalty, then the given expression will be equal to the string "Royalty".
Finally, you should also modify the Program6.java file to incorporate the additional king and queen commands, and achieve the new functionality for the s(on) and d(aughter) commands described above.
Note: You'll have to write the Royalty.java class from scratch and you'll have to modify Program6.java. But, Person.java should not be modified.
A) A printout of your Royalty.java and Program6.java.
B) Output from a sample run, demonstrating the functioning of the program.
Please remember to
staple all material together.
Setting.
The ratio of the circumference of a circle to its diameter is pi (π). What is the value of π?
Your goal is to estimate the value of π in two different ways.
Each method has an associated parameter N. In general, the bigger the value of N the better the approximation. For each method, write a function with one integer argument N that returns a 1-by-N matrix containing the successive approximations to π. Name the functions SeriesA and SeriesB.
Series A. Leibniz (1646 - 1716) gave
the following infinite series for π.
π / 4 = 1/1 - 1/3 + 1/5 - 1/7 + 1/9 ...
Here, N is simply the number of terms in the approximation. So, for N = 3, the approximation is 4*(1/1 - 1/3 + 1/5).
Series B.
1/ (3^3 * 7) + 1/ (3^4 * 9) - ... ]
N is again the number of terms.
What to turn
in.
A printout of each of the two functions.
A printout of a main procedure that produces a plot of SeriesA vs. N and SeriesB vs. N (on the same printout) and a hardcopy plot of the result of this comparison.
Develop and test each function, one at a time, using small values of N. For your printout, use whatever value of N you consider appropriate.
Restrictions. The
main point of this part of the assignment is to learn how to use MatLab's
uniform operations on matrices. In other words, your functions should not have
loops that iterate N times.