The University of Western Australia CITS2200 Data Structures and Algorithms : School of Computer Science and Software Engineering : The University of Western Australia

Data Structures and Algorithms Lab Exercises 3

Aims

The aims of these exercises are to:

  1. Practice writing logical tests in Java.

  2. Practice using arrays in Java.

Students should read Weiss Chapter 2 Section 2.4 in preparation for these lab exercises.

Students are not expected to complete every exercise in these lab exercises. Select about 5 examples from each group.

Programming Exercises

Useful array methods

Write a method public static void printArray( int [ ] arr ) to print out the elements of an array in order. You can use any formatting you like to make your printed list clear.

Write a method public static int countZeros( int [ ] arr ) to print out the number of elements in an array (if any) that are equal to 0.

Write a method public static int countNs( int [ ] arr, int n ) to print out the number of elements in an array (if any) that are equal to the parameter n. Change your countZeros method from the last question so that it simply calls your new countNs method. This approach is called using a helper method.

Write a method public static int isIncreasing( int [ ] arr ) which is true only if the elements of the array are increasing. That is, following elements are greater than or equal to previous elements.

CodingBat Exercises

You need to sign up for accounts in CodingBat to access these questions. Once you create an account, you can come back to the server another time and your code, your checkmarks, etc, will be saved. You can do everything on the server without an account, but then your work will be forgotten when your session ends after two hours without activity. Visit this web sites to set up your accounts:

Note that CodingBat has several help videos available.

If you are not on-line for this lab, then answer the questions given below in your own Java file instead. A sample java file is given here: lab03answers.java. It is recommended that you save your answers in this Java file anyway, so you can use them later for revision.

CodingBat Logic Exercises

Logic Exercises

  1. sleepIn The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return true if we sleep in.
    sleepIn(false, false) returns true
    sleepIn(true, false) returns false
    sleepIn(false, true) returns true
  2. monkeyTrouble We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble.
    monkeyTrouble(true, true) returns true
    monkeyTrouble(false, false) returns true
    monkeyTrouble(true, false) returns false
  3. parrotTrouble
  4. makes10
  5. near100 Given an int n, return true if it is within 10 of 100 or 200. Note: Math.abs(num) computes the absolute value of a number. or35
  6. hasTeen
  7. lastDigit
  8. dateFashion (Hint: needs if statements - see CodingBat hints) You and your date are trying to get a table at a restaurant. The parameter "you" is the stylishness of your clothes, in the range 0..10, and "date" is the stylishness of your date's clothes. The result getting the table is encoded as an int value with 0=no, 1=maybe, 2=yes. If either of you is very stylish, 8 or more, then the result is 2 (yes). With the exception that if either of you has style of 2 or less, then the result is 0 (no). Otherwise the result is 1 (maybe).
    dateFashion(5, 10) returns 2
    dateFashion(5, 2) returns 0
    dateFashion(5, 5) returns 1
  9. caughtSpeeding
  10. alarmClock
  11. old35
  12. answerCell (Challenge: try doing this with a single return statement)
  13. lastDigit
  14. intMax Given three int values, a b c, return the largest.
    intMax(1, 2, 3) returns 3
    intMax(1, 3, 2) returns 3
    intMax(3, 2, 1) returns 3
  15. sumDouble

CodingBat Array Exercises

  1. firstLast6

    Given an array of ints, return true if 6 appears as either the first or last element in the array. The array will be length 1 or more.
    firstLast6({1, 2, 6}) returns true
    firstLast6({6, 1, 2, 3}) returns true
    firstLast6({13, 6, 1, 2, 3}) returns false

  2. unlucky1

    We'll say that a 1 immediately followed by a 3 in an array is an "unlucky" 1. Return true if the given array contains an unlucky 1 in the first 2 or last 2 positions in the array.
    unlucky1({1, 3, 4, 5}) returns true
    unlucky1({2, 1, 3, 4, 5}) returns true
    unlucky1({1, 1, 1}) returns false

  3. makeEnds

    Given an array of ints, return a new array length 2 containing the first and last elements from the original array. The original array will be length 1 or more. The method signature will be public int[] makeEnds(int[] nums).
    makeEnds({1, 2, 3}) returns {1, 3}
    makeEnds({1, 2, 3, 4}) returns {1, 4}
    makeEnds({7, 4, 6, 2}) returns {7, 2}

  4. rotateLeft3

    Given an array of ints length 3, write a method public int[] rotateLeft3(int[] nums) to return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.
    rotateLeft3({1, 2, 3}) returns {2, 3, 1}
    rotateLeft3({5, 11, 9}) returns {11, 9, 5}
    rotateLeft3({7, 0, 0}) returns {0, 0, 7}

  5. front11

    Given 2 int arrays, a and b, of any length, write a method public int[] front11(int[] a, int[] b) to return a new array with the first element of each array. If either array is length 0, ignore that array.
    front11({1, 2, 3}, {7, 9, 8}) returns {1, 7}
    front11({1}, {2}) returns {1, 2}
    front11({1, 7}, {}) returns {1}

End of lab exercises 3