wiki:ControlSystems/SoftwareTeam/Training/GettingStarted/JavaExercises

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 a Garden without looking back at the exercise or your Animal Farm code.

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 a name when they are created
  • All animals can eat an amount, which increases their mass by amount
  • All animals can also move a distance, which decreases their mass by distance
  • 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 prints Meow!

Create a class called Dog that extends Animal

  • When a dog speak, it prints Woof!

Create a class called Farm

  • The Farm has a main method
  • In the main method, the Farm creates a Cat with a mass of 12.2 named Cloud
  • In the main method, the Farm creates a Dog with a mass of 15.4 named Spot

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

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 to Steve by default.
  • All animals now also have a setName method which sets their name.
  • All animals can printInformation, which prints information on the specific animal.
    • Ex. Spot is a Dog with a mass of 15.4

Create a class called Hummingbird

  • All Hummingbird have a wingspan.
  • Hummingbirds must be created with a mass, wingspan, and name.
  • When a Hummingbird speak, it prints Tweet!
  • Everybody can use getWingspan to get the wingspan of a Hummingbird.

Farm should:

  • Create an array of 5 Animal called animals that contains:
    • 1 Dog with mass 10
    • 1 Dog with mass 15 and name Fido
    • 1 Cat with mass 13 and name Cloud
    • 1 Hummingbird with mass 1.1, name Birdie, and wingspan 2.2
    • 1 Cat with mass 5
  • printInformation on all Animal in the array

Questions to think about:

  • animals[4] is a Hummingbird, but does animals[4].getWingspan() work?
  • How can you get the wingspan of animals[4]?

Step 3: Method Overloading and Overriding

  • All animals can also eat other Animal, which increases their mass by the mass of the Animal they ate, and sets the mass of the Animal they ate to 0.

Hummingbirds are very small, so they will only eat a maximum of their own mass.

  • If a Hummingbird with mass 1 eats 5, it will end with a mass of 2
  • If a Hummingbird with mass 2 eats a Cat with mass 5, it will end with a mass of 4, and the Cat will end with a mass of 0
  • mass and name should stay private. This may be a bit challenging at first, but be creative!
  • Hint: super.method() will run method() 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 called State, which can be TAMED, PARTIALLY_TAMED, or WILD
  • Cat and Dog all have a State. Hummingbirds do not
  • It is possible to getState of both Cat and Dog
  • Cat and Dog start with a default State of WILD
  • It is possible to setState of Cat
  • You can tame a Dog, which makes them more tamed (WILD -> PARTIALLY_TAMED, PARTIALLY_TAMED -> TAMED, TAMED -> TAMED)

Step 5: Static Variables and Methods

  • Animal has a static int called population, which starts at 0 and increases by 1 every time an animal is created
  • Animal has a static 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 a static method in Animal

Step 6: Interfaces

  • Create an interface called Building
  • A building must be able to collapse
  • A building must be able to store an Object, which return true if it was successful and false otherwise.

Step 7: Lists, Generics

  • A Farm implements Building
  • A Farm has a List of Animal
    • Research Java Generics to find out how to create a List of Animal, and to find out what generics do
    • Use either an ArrayList or LinkedList, but keep the type generalized as List
    • When a Farm store an Object, it add the object to the end of the List Animal and return true if the object is an Animal. Otherwise, it does nothing and return false.
  • A Farm has an address
  • A Farm has a Color that can be RED, BROWN, WHITE, BLUE, or GREEN
  • When a Farm collapse, it's List of Animal 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.

Last modified 4 years ago Last modified on Dec 2, 2016, 9:41:13 PM