Control - IF


Note

The file CIS214.java contains three methods, getInput(), getIntInput(), and getDoubleInput(). Each takes as argument a string to use as a prompt, and returns the user's response (as a String, int, or double). These are static methods, which means we don't need to use new to create an instance. They are used like this:

double userCost = CIS214.getDoubleInput("What's your cost?");
String userName = CIS214.getInput("What's your name?");
int userAge = CIS 214.getIntInput("What's your age?");


Classwork -- Must be done in pairs, due in class!


Part A

At a certain amusement park, children under 5 get in for $4, children up to (inclusive) 12 get in for $10, teens up to 19 get in for $12, adults up to 59 for $15, seniors up to 79 for $10, and seniors 80 and over for $5.

Write an java program with a main which asks the user for hir age and using a single compound if-else statement, print out the appropriate price, given the age. That is, use else, else if, nesting, etc, but not individual, unrelated if statements


Part B

Ask the user for three ints.

Using conditionals to determine the order, print the three ints back out in non-decreasing order. Do not use special math methods or other structures.


Part C

Assuming we have these variables:
ints: x, y, z, w
booleans: isGreen, isBlue, isRed, happy, dead
give boolean expressions for the following:

Note that I'm only asking for the expression, not an if statement using it.

Homework -- May be done in pairs, due in one week!

Points will be deducted from here on for not following formatting requirements, including comments.


Part A

Create a new animal class, representing a specific type of animal, and a test harness for it. You can choose any animal you want, however silly, other than Cat, Dog, or Bunny.

Your class should have at least two appropriate instance variables of different types, and at least one method for an appropriate behavior.

The behavior method should use a conditional statement to determine how the animal performs the behavior (you need only print a message to report the action). The condition for this if should involve the values of two instance variables, and use a logical operator. For instance, if the cat is hungry and weighs less than 7 pounds, it mews piteously, if it is hungry but larger, it demands food, scratching and clawing, and if it is not hungry it purrs.

Write accessors and mutators for the instance variables. At least one of the mutators should limit the values the variable can be set to.

Write a default constructor and a parameterized constructor.

Write a test harness which uses all accessors and mutators as well as both constructors and the behavior. Make sure to test setting the instance variables to values that will trigger different versions of the behavior. Turn in both the class and the harness.


Part B

Create a class Point2D representing a point in 2 dimensions (x and y coordinates). Give your class accessors and mutators, and a default and a parameterized constructor.

Test your Point2D class in a test harness.


Part C

To your animal class from part A, add an instance variable location, of type Point2D. Your animal should always start at (0, 0). You need not allow the location to be passed into the parameterized constructor.

Also add to your animal class a method that prints out a message showing the animal's position. Don't add an accessor or mutator for the location variable.

Also add to your animal class methods north(), south(), east(), and west(), each of which moves the animal by 1 in the appropriate direction (east is positive on the x axis, north is positive on the y axis). So if you first create your animal and then move it east east south west south, it would end up at (1, -2).

Hint: do not add x and y variables to your animal class. Use the ones in the location Point2D variable. In your earlier test harness, test that you can move your animal in all directions and see the report of its location change.



How to turn in work for a single classwork or homework:

Short answer questions can be answered with sentences, phrases, bulleted lists, pictures, and any combination that gets across your understanding. Pictures may be hand-drawn and hand-labeled (legibly!) but accompanying text should still be typed.

When example code is given on the website, you may use it as a model when doing assignments, unless I say otherwise.

You should only use constructs and concepts we have covered so far in the class.