Lab Exercise for Assignment 2

COMP112 2020 Tri 1: Lab Exercise 2

Goals

After completing this assignment, you will be able to write Java programs that:
  • use conditionals (if statements)
  • create new objects and call methods on them

Preparation

Download the zip file for lab exercise 2 and extract it to the COMP112-2020T1-Lab2 folder in your home folder. It should contain templates for the exercises program. Read through the exercises to see what you need to do. You can run the demo programs on the ECS lab computers (they are in the COMP112 folder on your desktop). Note that the demo programs cannot be run outside of the labs (since that would make the answers available), but you can view the demo videos.

Look at the model answers to assignment 1 and make sure you understand all the components of the programs. If you wrote your programs a little differently from the model solution, that's OK as long as your program works but still make sure you understand how the model solution code works.

Summary

Conditionals exercise

The ConditionalsExercise class contains four methods that each involves asking for some information from the user and then performing different actions depending on the answers. Run the ConditionalsExercise demo to see what your methods should do.

Complete the following methods in the ConditionalsExercise.java file. You should complete the methods, until you have written at least two by yourself. (It may be useful practice to do all of them!)

  • validHour() should ask the user for a number. If the number is out of range (< 0 or > 12), then it reports "that number is not a valid time". Otherwise, it prints the number in the for "The time is 6 o'clock" (if they entered 6).

  • wordGame() should ask the user to enter a word. It then prints "Yes, that fits" if the word starts with "p" and is 7 characters long, otherwise it prints "Sorry, that word won't work".
    Hint: You can call the length() method on a String to find out how many characters long it is, and you can call the startsWith("xyz") method on a String to determine whether the start of the String matches "xyz". (Look up the documentation of those methods in the online java documentation for the String class).

  • drawAFlag() should ask the user to enter the name of a country, and then draws the appropriate flag by calling one of the drawJapanFlag, drawIndonesiaFlag, drawAustriaFlag and drawBangladeshFlag methods which are provided. If the country is not one of Japan, Indonesia, Austria, or Bangladesh, the program prints "sorry, I don't know about that country".
    Note: all the flag drawing methods are included in the file for you - you don't have to write them.

  • longestWord() should ask the user to enter three words and print out the longest one. If two of the words are equally long, it doesn't matter which it prints. You can call the length() method on a string to find out how long it is.

New Objects exercise

The file NewObjectsExercise.java contains three methods that create new objects and then call a sequence of methods on them. You need to complete these methods. Each method creates objects from a different class. The other "object classes" (Flower , Balloon, and Car ) are all provided and you do not need to modify them.

  • First, run the NewObjectExercise demo to see what your methods should do. Note that your methods should do similar things, but don't have to be identical.

The file NewObjectsExercise.java contains the following methods to complete. (Remember, you do not need to complete all of them - you may stop when you have completed one of them by yourself.)

  • growFlowers() should create two or three Flower objects and make them grow and bloom.
  • blowUpBalloons() should create two Balloon objects and make them expand several times and pop.
  • driveCars() should create two Car objects and make them drive towards each other then turn to the side and drive some more.

When you write the statements to create new objects and call methods on them, you will need to provide the correct arguments to the constructor and to the methods. To see the details of what those arguments are, you should open up the Flower , Balloon and Car classes in BlueJ and select the Documentation option (instead of Source Code ) at the top right. You will then see, for example, that the Flower constructor requires two double arguments - the position of the Flower.