25 September 2013

Java fundamentals: extra labs

Lesson 6

Extra practice: Extend Customer Info

  1. Modify lab 6.1 to ask the user to enter the attributes of the customer
  2. Use java.util.Scanner to read the attributes
    1. Take care: after reading an int, you also need to read the return (newline) to advance to the next line of input

Lesson 7

Practice 7.1:

Modify the exercise to:
  1.     Use a java.util.Scanner to read the time of the day from the user input.
  2.     Allow the user to use the format hh:mm:

Extra practice: Seasons

  1.  Create a class Meteo with a method
  2.     public String getSeason(String month) 
  3.  Implement the method and let it return the meteorological season for the month 
    1. (e.g. for december to february return winter)
  4. Create a class MeteoTest
  5. Write a main method that
    1. Creates a Meteo object
    2. Asks the user for the name of a month
    3. Uses the Meteo object to get the corresponding season
    4. Prints out the season
  6. Run the test program and correct errors until it succeeds
  7. Modify the program to make it work no matter which case the user uses when entering the month.
  8. Run the test program and correct errors until it succeeds

Lesson 8

Extra practice: Deck

  1. Create a class Deck  
  2. Initialise a cardNames array with the value of all cards (ace, two, three..., jack, queen, king)
  3. Initilise a cardSuites array with the suites of all cards (hearts, spades, clubs, diamonds)
  4. Create a method called printSize, which
    1. prints the number of cardValues
    2. prints the number of cardSuites
    3. prints the number of cards
  5.  Create a class DeckTest
  6. Write a main method that
    1. Creates a Deck object
    2. Calls printSize on it
    3. Run the test program and correct errors until it succeeds
  7. Write a method drawCard, which prints the name of a random card (e.g. jack of spades)
    1. Hint: use Math.random()
  8. Draw two cards from the main method
  9. Run the test program and correct errors until it succeeds

Lesson 9

Extra practice: Dice

  1.  Create a class DiceStat with a method
  2.     public void printStat () 
  3. The method should roll 2 dice 10.000 times (Hint: use Math.random()
  4. Print the number of times each number was rolled
  5.  Create a class DiceTest
  6. Write a main method that
    1. Creates a DiceStat object
    2. Calls printStat
  7. Run the test program and correct errors until it succeeds
  8. For each result, express as a percentage, how often it is thrown
  9.  Run the test program and correct errors until it succeeds

Extra practice: Factorials

A factorial (n!) is the product of all numbers less than or equal to n. Example:

3!= 3*2*1

Create an application, called factor that will print the factorial of the number given as an argument to the application:

$ java FactorTest 3
3! = 6

Lesson10

Extra practice: Quiz

  1.  Create a class Quiz with keywords and definitions.You can find the keywords and definitions here.
  2. Create a method with signature
  3.     public boolean askQuestion()
    1. The method asks the user to give the keyword that corresponds to a randomly chosen definition
    2. The method compares the answer with the  keyword
    3. The method informs the user about the correctness of the result.
    4. If the answer was wrong, it shows the correct keyword
    5. The method returns if the respons is correct
  4. Create a class QuizTest
  5. Write a main method that
    1. Creates a Quiz object
    2. Calls askQuestion
  6. Run the test program and correct errors until it succeeds
  7. Add a method with this signature
  8.       public int askQuestions(int number) 
    1. The method asks multiple questions
    2. The method returns the number of correct answers
  9. Call the method from the main class
  10. Run the test program and correct errors until it succeeds
  11. Add a method with this signature
    1.  public boolean certify (int numberOfQuestions, int passPercentage)
    2. The method prints out your score percentage.
    3. The method prints out whether you passed the certification or not.
    4. The method returns a boolean with the pass result.
  12. Call the certify method from the main class
  13. Run the test program and correct errors until it succeeds

Extra practice: Hypothenuse

The hypotenuse is the longest side in a right angled triangle. Pythagore's theorem states that the length of the hyothenuse (h) relates to the two other sides (x and y)  as:

Write a program, using that calculates the hypothenus, using java's Math class:

$ java HypothenuseTest 2 3
Hypothenuse = 3.6

Lesson 11

Extra practice: Unique object ID's

  1.  Modify the Rectangle class to assign a unique ID to each object that is created.
    1. Start the ID's at 1 and increment for each new object
    2. Print the rectangle ID in the creation message from the constructor
  2.  Run the test program and correct errors until it succeeds

Extra practice: Deck

  1.  Add a Card class to the Deck project
    1. Add a suit and value attribute and methods to retrieve them
    2. Add a Constructor 
    3. Add a method that returns the card name (e.g. queen of hearts) with signature
         public String toString(); 
  2. Add a constructor to the Deck class that generates a Deck of 52 cards.
  3. Modify the drawCard method to return a Card
  4. After calling drawCard, print out the card name from the main method.

Extra practice: Dice constructor

  1. Add a constructor to the DiceStat class that accepts the number of dies, the number of sides each dice has and the number of throws.
  2. Make the Deck class generic to work with these variables.
  3. Run the test program and correct errors until it succeeds

Lesson 12

Extra practice: Rectangle class hierarchy

  1. Make a class Square, that is a subclass of the Rectangle class. 
    1. Add a Constructor that takes the side as an aargument
  2. Draw a square from the RetcangleTest main method
  3. Run the test program and correct errors until it succeeds
  4. Make rectangle a subclass of Shape. Make a class Shape with an abstract draw method.
    1. In Rectangle you can also use Refactor => extract interface
  5. Make a Triangle class that extends Shape (In the interface, you can press ALT+ENTER to generate a class that implements the interface)
    1. This class is a right angled triangle, with equal width and height. Example:
    2. *
      **
      ***
      ****
    3. Implement the necessary methods
  6. Draw a triangle from the RetcangleTest main method
  7. Run the test program and correct errors until it succeeds
  8.  Make a Canvas class, with a method with signature
  9. public void draw (List shapes)
    1. In the draw method  loop over the list
    2. Assign the objects from the list to a Shape variable
    3. Draw each Shape
  10. In the main method of TestRectangle, create a Canvas
  11. Add a number of rectangles, Squares and triangles to an Arraylist.
  12. Call the draw method on the canvas, and pass the arraylist to it.
  13. Run the test program and correct errors until it succeeds
  14. Make a Parallellogram class that extends Shape (you can make a copy of the Rectangle class and name it Parallellogram to start)
    1. This class draws a parallellogram with a slope that indents one character:
    2. ****
       ****
        ****
    3.  Implement the necessary methods
  15. In the main method of TestRectangle, add some paralellograms to the ArrayList
  16. Run the test program and correct errors until it succeeds