Version 5 (modified by 9 years ago) (diff) | ,
---|
http://www.ntu.edu.sg/home/ehchua/programming/java/j2a_basicsexercises.html Recommended exercises: SumAndAverage? CheckerBoard? fibonacci Bin2Dec GradeStatistics?
Another challenge: Write a high-low guessing game; have a person think of the number, and have the computer try to guess it After each guess, the human will tell the computer whether it's higher or lower
Another challenge: Animal Farm
Create an abstract class
called Animal
that has a mass
and a name
- All animals should have a constructor which gives them a
mass
and aname
when they are created - All animals can
eat
anamount
, which increases theirmass
byamount
- All animals can also
move
adistance
, which decreases theirmass
bydistance
- Everybody can use
getName
to get the name of an animal - Everybody can use
getMass
to get the mass of an animal
Create a class called Cat
that extends Animal
- When a cat
speak
, it printsMeow!
Create a class called Dog
that extends Animal
- When a dog
speak
, it printsWoof!
Create a class called Farm
- The
Farm
has amain
method - In the
main
method, theFarm
creates aCat
with amass
of12.2
namedCloud
- In the
main
method, theFarm
creates aDog
with amass
of15.4
namedSpot
In the main
method, the Farm
will print the names of both animals and their masses.
- Ex.
Spot is a Dog with a mass of 15.4.
Then, Cloud
will eat
0.2
, move
0.8
, and speak
.
Spot
will then eat
0.4
, move
0.6
, and speak
.
Farm
will print the names of both animals and their masses once again.
You will probably get weird numbers like 15.200000000001. This is imprecision with floating point numbers, and is normal