4a -- exercises for 10/14
You may have a partner for this lab; but everyone needs to know how to do all these things!
Introduction:
This lab has a series of very simple tasks; do them in order.
- Write a class, called Exercises with an int variable, x, and a String variable name.
- Make the default constructor in Exercises set x to 17 and name to your name.
- Write a class called Driver, to test your Exercises class; in it, instantiate an Exercises,
and send it as a parameter to sout; what does it print?
- Add a method with the signature public String toString(), which returns x and name with informative
labels (so you don't just see 17 Jane, but rather, I am an Exercises, x=17, name=Jane.
- Run the driver again; what does it print now?
- Add a method, named doubleIt, which doubles the value of x. To test it, send it to your
Exercises twice and then sout your Exercises... if it prints 68, then it is working.
- Write a method named, sout, with an int parameter, which will print that parameter
(with a descriptive label).
- Write a method named giveMeX, which will return the value of x. Test it by saying,
sout("here's what giveMeX returns..." + myExercises.giveMeX());
- Write a method named giveMeTwiceX that returns twice the value of x. Test it.
- Write a method named setXTo, which sets x to its parameter. Test it.
- Write a method named zeroX which sets x to 0.
- Write another class, Coyote, which works exactly like Exercises. You will have to include
a constructor with only the line super(); to get the initialization done. Change your variable
(in the driver) to a Coyote, and make sure everything still works.
- Override giveMeX (in Coyote), so that it returns one more than x.
- Override doublit (in Coyote), so it triples x.