This is a list of exercises that will start simple and begin to get more and more advanced. As the exercises get more advanced, the amount of hints and information given will decrease, allowing for more freedom and independence while programming.
- As the exercises get harder you will most likely have to start searching up information you don't know
- Searching up information you don't know is just a part of programming
- If you still don't understand after searching something up, feel free to ask one of the leads or deputies, or even the people around you. We're always glad to help.
- Please do not feel pressured to complete all these exercises
- Focus on fully understanding all the exercises you complete instead of trying to cover all the information
- Feel free to mess around with the code to see what changes
- If you still feel shaky about a topic after completing the exercise, try coming up with your own similar exercise and completing it without hints
- Ex. If you feel shaky on
Animal Farm
, try creating aGarden
without looking back at the exercise or yourAnimal Farm
code.
- Ex. If you feel shaky on
Basic exercises:
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
Step 1: Abstract Classes
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
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]?
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
From here on out, run whatever code you need in Farm
to check to make sure your code works the way it is expected to.
Step 4: Enums
- Create an
enum
calledState
, which can beTAMED
,PARTIALLY_TAMED
, orWILD
Cat
andDog
all have aState
. Hummingbirds do not- It is possible to
getState
of bothCat
andDog
Cat
andDog
start with a defaultState
ofWILD
- It is possible to
setState
ofCat
- You can
tame
aDog
, which makes them more tamed (WILD
->PARTIALLY_TAMED
,PARTIALLY_TAMED
->TAMED
,TAMED
->TAMED
)
Step 5: Static Variables and Methods
Animal
has astatic int
calledpopulation
, which starts at0
and increases by1
every time an animal is createdAnimal
has astatic getPopulation
method which returns the total number of animals created- When two Animals
meet
, the more massive one eats the one with less mass. This should be astatic
method inAnimal
Step 6: Interfaces
- Create an
interface
calledBuilding
- A building must be able to
collapse
- A building must be able to
store
anObject
, whichreturn
true
if it was successful andfalse
otherwise.
Step 7: Lists, Generics
- A
Farm implements Building
- A
Farm
has aList
ofAnimal
- Research
Java Generics
to find out how to create aList
ofAnimal
, and to find out what generics do - Use either an
ArrayList
orLinkedList
, but keep the type generalized asList
- When a
Farm
store
anObject
, itadd
the object to the end of theList Animal
andreturn true
if the object is anAnimal
. Otherwise, it does nothing andreturn false
.
- Research
- A
Farm
has anaddress
- A
Farm
has aColor
that can beRED
,BROWN
,WHITE
,BLUE
, orGREEN
- When a
Farm
collapse
, it'sList
ofAnimal
is cleared.- Warning: Clearing a List is not the same as setting it to null.
Final Step: Double Check
Double check all your code to make sure it makes sense. Edit anything you need. For example...
- Animals can eat themselves
- Animals that have been eaten still count as part of the population
Review: Deck of Cards (Enums, Lists)
Create a Deck of Cards. Each Card has a value Ace through King, and a suit. You can draw cards from the top of the deck, do a perfect riffle shuffle (split the deck in half and alternate cards) of the deck, and print out all the cards in the deck. Overriding the toString
method will be very helpful.
Review: Creatures (Inheritance, Static)
Create a Garden of Plants. Plants and Animals are both Creatures. Make up some methods that you think are suitable for each, and create them (ex. What do all creatures do the same? What do all creatures do differently? What do only specific creatures do?). Make sure you include static variables and methods (ex. list of all creatures ever created).
Deck of Cards step 2:
Program a card game (war, go fish, etc.) using your deck of cards. Create any methods you will need for your game, and see how much you can optimize your game.
Creatures step 2:
Program a game using all your creatures classes (ex. text based agar.io). Create whatever methods and/or classes you will need, and try to keep your code as simple as possible.