Lesson 6
Extra practice: Extend Customer Info
- Modify lab 6.1 to ask the user to enter the attributes of the customer
- Use java.util.Scanner to read the attributes
- 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:- Use a java.util.Scanner to read the time of the day from the user input.
- Allow the user to use the format hh:mm:
Extra practice: Seasons
- Create a class Meteo with a method public String getSeason(String month)
- Implement the method and let it return the meteorological season for the month
- (e.g. for december to february return winter)
- Create a class MeteoTest
- Write a main method that
- Creates a Meteo object
- Asks the user for the name of a month
- Uses the Meteo object to get the corresponding season
- Prints out the season
- Run the test program and correct errors until it succeeds
- Modify the program to make it work no matter which case the user uses when entering the month.
- Run the test program and correct errors until it succeeds
Lesson 8
Extra practice: Deck
- Create a class Deck
- Initialise a cardNames array with the value of all cards (ace, two, three..., jack, queen, king)
- Initilise a cardSuites array with the suites of all cards (hearts, spades, clubs, diamonds)
- Create a method called printSize, which
- prints the number of cardValues
- prints the number of cardSuites
- prints the number of cards
- Create a class DeckTest
- Write a main method that
- Creates a Deck object
- Calls printSize on it
- Run the test program and correct errors until it succeeds
- Write a method drawCard, which prints the name of a random card (e.g. jack of spades)
- Hint: use Math.random()
- Draw two cards from the main method
- Run the test program and correct errors until it succeeds
Lesson 9
Extra practice: Dice
- Create a class DiceStat with a method public void printStat ()
- The method should roll 2 dice 10.000 times (Hint: use Math.random()
- Print the number of times each number was rolled
- Create a class DiceTest
- Write a main method that
- Creates a DiceStat object
- Calls printStat
- Run the test program and correct errors until it succeeds
- For each result, express as a percentage, how often it is thrown
- 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
- Create a class Quiz with keywords and definitions.You can find the keywords and definitions here.
- Create a method with signature public boolean askQuestion()
- The method asks the user to give the keyword that corresponds to a randomly chosen definition
- The method compares the answer with the keyword
- The method informs the user about the correctness of the result.
- If the answer was wrong, it shows the correct keyword
- The method returns if the respons is correct
- Create a class QuizTest
- Write a main method that
- Creates a Quiz object
- Calls askQuestion
- Run the test program and correct errors until it succeeds
- Add a method with this signature public int askQuestions(int number)
- The method asks multiple questions
- The method returns the number of correct answers
- Call the method from the main class
- Run the test program and correct errors until it succeeds
- Add a method with this signature
- The method prints out your score percentage.
- The method prints out whether you passed the certification or not.
- The method returns a boolean with the pass result.
- Call the certify method from the main class
- Run the test program and correct errors until it succeeds
-
public boolean certify (int numberOfQuestions, int passPercentage)
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
- Modify the Rectangle class to assign a unique ID to each object that is created.
- Start the ID's at 1 and increment for each new object
- Print the rectangle ID in the creation message from the constructor
- Run the test program and correct errors until it succeeds
Extra practice: Deck
- Add a Card class to the Deck project
- Add a suit and value attribute and methods to retrieve them
- Add a Constructor
- Add a method that returns the card name (e.g. queen of hearts) with signature
- Add a constructor to the Deck class that generates a Deck of 52 cards.
- Modify the drawCard method to return a Card
- After calling drawCard, print out the card name from the main method.
Extra practice: Dice constructor
- 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.
- Make the Deck class generic to work with these variables.
- Run the test program and correct errors until it succeeds
Lesson 12
Extra practice: Rectangle class hierarchy
- Make a class Square, that is a subclass of the Rectangle class.
- Add a Constructor that takes the side as an aargument
- Draw a square from the RetcangleTest main method
- Run the test program and correct errors until it succeeds
- Make rectangle a subclass of Shape. Make a class Shape with an abstract draw method.
- In Rectangle you can also use Refactor => extract interface
- Make a Triangle class that extends Shape (In the interface, you can press ALT+ENTER to generate a class that implements the interface)
- This class is a right angled triangle, with equal width and height. Example: *
- Implement the necessary methods
- Draw a triangle from the RetcangleTest main method
- Run the test program and correct errors until it succeeds
- Make a Canvas class, with a method with signature public void draw (List shapes)
- In the draw method loop over the list
- Assign the objects from the list to a Shape variable
- Draw each Shape
- In the main method of TestRectangle, create a Canvas
- Add a number of rectangles, Squares and triangles to an Arraylist.
- Call the draw method on the canvas, and pass the arraylist to it.
- Run the test program and correct errors until it succeeds
- Make a Parallellogram class that extends Shape (you can make a copy of the Rectangle class and name it Parallellogram to start)
- This class draws a parallellogram with a slope that indents one character: ****
- Implement the necessary methods
- In the main method of TestRectangle, add some paralellograms to the ArrayList
- Run the test program and correct errors until it succeeds
**
***
****
****
****
No comments:
Post a Comment