Version 13 (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
. These should be private
.
- 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
- All animals can
speak
, but they all speak differently - 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.
- Ex.
- Then,
Cloud
willeat
0.2
,move
0.8
, andspeak
. Spot
will theneat
0.4
,move
0.6
, andspeak
.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
Animal Farm step 2: Constructor Overloading
- All animals can also be created with only a
mass
. - If an animal is created without a
name
, it's name is set toSteve
by default. - All animals now also have a
setName
method which sets theirname
. - All animals can
printInformation
, which prints information on the specific animal.- Ex.
Spot is a Dog with a mass of 15.4
- Ex.
Create a class called Hummingbird
- All
Hummingbird
have awingspan
. - Hummingbirds must be created with a
mass
,wingspan
, andname
. - When a Hummingbird
speak
, it printsTweet!
- Everybody can use
getWingspan
to get thewingspan
of a Hummingbird.
Farm
should:
- Create an array of 5
Animal
calledanimals
that contains:- 1
Dog
withmass
10
- 1
Dog
withmass
15
andname
Fido
- 1
Cat
withmass
13
andname
Cloud
- 1
Hummingbird
withmass
1.1
,name
Birdie
, andwingspan
2.2
- 1
Cat
withmass
5
- 1
printInformation
on allAnimal
in the array
Questions to think about:
animals[4]
is aHummingbird
, but doesanimals[4].getWingspan()
work?- How can you get the wingspan of animals[4]?
Animal Farm step 3: Method Overloading and Overriding
- All animals can also
eat
otherAnimal
, which increases theirmass
by themass
of theAnimal
they ate, and sets themass
of theAnimal
they ate to0
.
Hummingbirds are very small, so they will only eat
a maximum of their own mass
.
- If a Hummingbird with
mass
1
eats5
, it will end with a mass of2
- If a Hummingbird with
mass
2
eats aCat
withmass
5
, it will end with a mass of4
, and theCat
will end with amass
of0
mass
andname
should stayprivate
. This may be a bit challenging at first, but be creative!- Hint:
super.method()
will runmethod()
of the superclass