CS100 Spring 1998

Assignment 3

"Things That Go Bump in the Night"

Due: at the end of lecture Tuesday February 17

Assignments will not be accepted late

The assignment can be handed in before the due date at Carpenter. Type your name and section day/time/instructor in a comment at the top of your assignment and sign the front page. It is also helpful if you highlight your name and the name of your partner with a highlighting pen.

Table of Contents

• Working with others

• Grading of the assignment

• The goals of this assignment

• The program that you will modify

• What to do for this assignment, Parts A, B, and C

• What to hand in

• Grading guidelines

1. Working with others: For this assignment, you may work with one other person. If you do, then you must actually do the work together; sit at a computer together, and make changes together. It is not proper for one person to do the work and then to add someone else's name to it. If you work with someone else, then hand in only one assignment; put both person's name, id and section information on it.

2. Grading of the assignment: There are three parts to this assignment. Each part will be given a grade of 0 to 2 for correctness and 0 to 2 for style and structure, for a total of a possible 6 points. In calculating your final grade this assignment will be worth the same as assignment 2 (and also assignment 1). That is, points will be scaled to make these assignments equal in weight.

3. The goals of this assignment:

• Give you practice writing a program from scratch.

• Give you more practice using classes and methods.

• To give you experience working with (a) constructors, (b) access modifier private,

(c) extensions of classes, (d) access modifier protected, and (e) writing and using a

function (i.e. a method that returns a value).

4. The program that you will modify: You will write the program almost from scratch. When you use the CUCSApplication stationery,  it will create a default file CUCSApplication.java. Modify this file as necessary, but be sure to delete ALL extraneous code AND comments except those needed for PCs, as described below. (CUCSGraphicsApplication can also be used but is unnecessary because this assignment has no graphics.) Important: If you are working on a PC, do not remove the statements

SystemInput sysIn = new SystemInput();

TokenReader in = new TokenReader(System.in);

that you will find at the top of the main method. The rest of the code in that method should be removed. Also do not remove the import statement from the top of the file.

These two statements will open an input window which you do not use in doing this assignment. However, they are needed when working on a PC in order to force the output and error message windows to stay open. They are not needed for a Mac, but you can still leave them there.

 

5. What to do for this assignment:

Overview. This program concerns classes that "model" monsters. You will write one general class that gives a few properties of all monsters and then two subclasses for particular kinds of monsters. We strongly suggest that you read the entire assignment before starting to work on part A. You will define:

1.) A class called Monster that has three fields (described later in this handout) and

a method for changing the number of legs on the monster.

2.) Two subclasses of Monster for those with wings and those with fins. Each

subclass has additional fields and methods

 

Part A

 

1) Start a new project in CodeWarrior, using stationery CUCSApplication. Erase all the statements in the body of method main, except those we mentioned for PC users on the previous page.

2) Create a new file, save it as Monsters.java, and add it to the project

(using, say, menu item Project | Add Window).

In Monsters.java, write a public class Monster with the following components:

(a) The class should have three fields. For now make all of them public. You will

change this label later in the assignment. The three fields should be name of type

String, poisonous of type boolean, and numLegs of type int.

(b) A constructor that has a String parameter named n and a boolean parameter

named p. The constructor should initialize name to the value of n and poisonous

to the value of p. It should also initialize numLegs to 0.

(c) A constructor having no parameters that just sets the fields to their default values.

(You can do this easily by just giving the constructor an empty body, i.e.

curly braces with no statements between them.)

(d) A public method called changeLegs having a parameter num of type int.

Method changeLegs should change the value of the field numLegs to the

value of num.

3) Add statements to the body of the main method that do the following:

(a) Create two monsters (i.e. objects in the class monster). Use the constructor

with two parameters to give them names (just make up your favorite ones) and to initialize the field poisonous. Make the first one of them poisonous and the other one not.

(b) Use method changeLegs to give the first monster 5 legs and the second one 20

legs.

(c) Use the method System.out.println to print a complete description of each

monster (i.e. giving the name, number of legs and stating whether or not it is

poisonous). Print a description that is complete and readable. For instance, don't

just print the word false or true to say whether or not it is poisonous. (Hint: You

will probably want to use an if statement to print a message such as "He is

poisonous" or "He is not poisonous".

4) Run the program to make sure it does what you want it to. Print a copy of the program and the output produced.

 

 

Part B

For this part you should start with the code you already have and modify it as described.

1) Change the prefixes on the fields (i.e. the instance variables) of class monster from public to private. Make sure that you leave your constructors and the method changeLegs as public. The label beside the word class also needs to remain public. It is only the fields that are changed to being private. Run the program again. Describe the error message you get and explain why your program is no longer correct.

2) You should have found that an error was caused by your code from part A (3c). That is, when you tried to print the values of the fields of the monsters from the main method it is no longer legal. Fix this problem by adding a new public method to the class Monster that prints out the description for you. Specifically, add the following method:

// Print a description of the monster

public void monsterTalks( ) {

System.out.print("I am a monster. My name is " + name + ". I have " +

numLegs + " legs" );

if (poisonous)

System.out.println(" and I am poisonous.");

else

System.out.println(" and I am not poisonous.");

System.out.println("I look very scary");

}

3) Change your main method so that instead of trying to print a description of each monster directly in the main method, it calls method monsterTalks for each of the two monsters.

4) Run the program again. For output you should now have a description of each monster produced by the calls to monsterTalks. Print a copy of the program and the output produced.

 

 

Part C

For this part start with the program you have completed for Part B.

1) Create two subclasses of classMonster. The first class should be named Winged. Class Winged will inherit all of the fields of class Monster as well as the method changeLegs. Do not redefine these in the subclass.

Class Winged should have one additional field, numWings of type int. It should also have a constructor with header

// Initialize name to nm and poisonous to b

public Winged (String nm, boolean b)

This constructor initializes a winged monster exactly the same way we initialized a normal monster and in addition sets the number of wings to 0. Complete the body of this constructor, using the command super appropriately.

Next add to class Winged a public method named changeWings having a parameter num of type int. Method changeWings should assign the field numWings the value num.

Last of all add to class Winged a public method monsterTalks(). This method should print a complete description of an object from class Winged. The method should be similar to the method monsterTalks() defined for class Monster, but it should print that it is a winged monster (instead of just any old monster) and print the number of wings. Notice that we are giving this method the same name as the method already defined in class Monster . This will not cause any problems. When we call the method monsterTalks for an object belonging to class Monster the original method will be used. When we call the method for an object belonging to class Winged this new one you wrote will be used.

The second subclass of class Monster should be named Finned. It will also inherit the fields of class Monster and the method changeLegs. Class Finned should have an additional field called numFins of type int. Again, define a constructor for class Finned that initializes the fields name and poisonous and sets the number of legs to 0 (by default) using the command super, just as you did in class Winged. In addition it should set the number of fins to 0.

Add to class Finned a public method called changeFins with parameter num of type int that assigns numFins the value contained in num

Now add to class Finned a public method monsterTalks(). This method should print a complete description of an object from class Finned. Have the method print that this is a finned monster in addition to giving the name, number legs, etc.

2) Add some statements to your main method that do the following. (Place it after the code you already had there.)

(a) Create one winged monster and one finned monster. Give them any names you want. Make one of them poisonous and one of them not. This should be done using the constructors you defined.

(b) Give the first one 4 legs and 8 wings. The second one should be left with 0 legs (no change is needed to do that) and 2 fins. The legs, wings, and fins should

be assigned by using calls to the methods changeLegs, changeWings, and changeFins.

(c) Add appropriate calls to method monsterTalks to print out the information on each monster.

3) Before you run the program you need to make one more modification or you will get an error message. (Make sure you understand why.) Change the prefixes on all of your fields in your classes from private to protected.

4) Run the program and make sure the output is what you expected.

5) Create one more monster of your own by adding more code to the main method. Make it either Winged or Finned. Give it appropriate features and use a call to method monsterTalks to print out a description of the monster.

6) Now add a function (i.e. a method that has a return value) to the class Winged. Specifically, add the following method to that class

// Return the number of extremities a winged monster has

public int howMany ( ) {

return (numLegs + numWings);

}

Add appropriate calls to this function inside the main method and print out the number of extremities one of your winged monsters has. Make sure the output is clearly labeled.

7) Run the program. Print a copy of your final version of the program and the output it produced.

 

6. What to hand it: Turn in a copy of the code and the output for each of the three parts of the assignment. In addition, write (this can be by hand) your answer to Part B (1) at the bottom of your code for part B, or on a page inserted immediately after that.

7. A few general guidelines: Remember to place meaningful comments in your code and to use indentation appropriately. Every method should have a comment over it describing what it does in terms of its parameters. Make sure your comments are informative but short and concise.