Module 7, Part 1: Lab Day#

This first part of Module 7 does not introduce new contents: instead, it provides a series of assessments to improve your Java programming skills. All the following assignments can be solved with the Java programming notions introduced in Modules 2, 3, 4, 5, and 6.

01 - Array Equality#

Edit the file ArrayUtils.java provided in the handout, and implement the following static methods:

  • static boolean areEqual(int[] arr1, int[] arr2)
    

    which returns true if the given arrays arr1 and arr2 contain the same number of int values, and all the int values in corresponding positions are equal. Otherwise, this static method must return false.

  • static boolean areEqual(int[][] arrArr1, int[][] arrArr2)
    

    which returns true if the given arrays of arrays arrArr1 and arrArr2:

    • contain the same number of arrays, and

    • all their arrays in corresponding positions contain the same number of int values, and all the int values in corresponding positions are equal.

    Otherwise, this static method must return false.

The handout includes some Java files called Test01.java, Test02.java, etc.: they are test programs that use the code you should write in ArrayUtils.java, and they might not compile or work correctly until you complete your work. You should read those test programs, try to run the tests, and also run ./grade to see their expected outputs — but you must not modify those files.

When you are done, submit the modified file ArrayUtils.java on DTU Autolab.

Hint

After you implement the first static method above, you might reuse it to implement the second.

Note

  • This method must also support jagged arrays.

  • You might notice that we are defining two (static) methods that have the same name (areEqual) but take a different number and/or types of arguments: technically, this is called overloading.

02 - Cow Say#

Your task is to edit the file CowSay.java and implement a version of the program “cowsay”. The program must read one line of text (as a String) from the terminal; then, the program must display on the terminal a cow with a speech balloon containing that text.

For instance, suppose that a user runs the program CowSay.java and writes the following input line on the console:

How does a computer eat pizza? One byte at a time.

Then, the program CowSay.java must display:

 _________________________________________
| How does a computer eat pizza? One byte |
| at a time.                              |
 -----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

The rules for formatting the text in the speech balloon are the following:

  • The speech balloon must have a fixed width of 43 characters, as shown in the example above.

  • The number of text characters displayed on each line of the speech balloon must be at most 39:

    • if the input text does not fit in 39 characters, it must be printed on multiple lines, showing as many words as possible on each line;

    • each line must be enclosed with the symbol | to the left and to the right (as shown above).

When you are done, submit the modified file CowSay.java on DTU Autolab.

Hint

  • You may want to split the input line into an array of words. To achieve this, you may have a look at Example 35 again…

  • If you want to include a line separator in a string, you should use System.lineSeparator(), as explained in the hints of 06 - Square 2.

  • Remember that in order to include the character \ in a string, you will need to escape that character by adding another \ in front. For instance, the following Java statement:

    System.out.println("Here is a single backslash: \\");
    

    produces the output: (try on the Java shell!)

    Here is a single backslash: \
    

Warning

The automatic grading on DTU Autolab includes some additional secret checks that test your submission with more inputs. After you submit, double-check your grading result on DTU Autolab: if the secret checks fail, then your solution is not correct, and you should fix it and resubmit.

03 - Palindromes#

Edit the file Palindromes.java provided in the handout, and write a program that prints whether each command-line argument is a palindrome, i.e. a word that reads the same way from left to right, and from right to left.

For example, if the program is executed from a terminal as:

java Palindromes.java foo racecar

then the program must print:

foo is not a palindrome
racecar is a palindrome

When you are done, submit the modified file Palindromes.java on DTU Autolab.

Hint

  • Example 41 shows how a Java program can examine its command-line arguments.

  • Remember that if str is a String, then the method str.charAt(n) returns the character at position n of str.

Warning

The automatic grading on DTU Autolab includes some additional secret checks that test your submission with more words that may or may not be palindromes. After you submit, double-check your grading result on DTU Autolab: if the secret checks fail, then your solution is not correct, and you should fix it and resubmit.

04 - Video Game Characters#

Your task is to edit the file Character.java provided in the handout, and implement a class named Character representing a video game character. Each object of the class Character must contain a name, and values for speed and strength: when defining the class, you need to decide the names and types of its fields. The requirements for the class Character are the listed below.

  • The class Character must have the following constructor:

    Character(String name, int speed, int strength)
    

    which allows us to create a new Character object by writing e.g.:

    var c = new Character("Lara Croft", 17, 13);
    
  • Character objects must have the following method:

    String description()
    

    which returns a representation of this object as a String. For instance, when this method is called on c (from the point above) it must return the string:

    Lara Croft (speed = 17, strength = 13)
    
  • Character objects must have the following method:

    void powerUp(int amount)
    

    which increases the speed and strength of this object by the given amount. For instance, when this method is called on c (from the point above) as c.powerUp(3), it must bring c’s speed to 20 and strength to 16. (Remember that void means that this method does not return any value.)

  • Character objects must have the following method:

    void nerf(int amount)
    

    which reduces the speed and strength of this object by the given amount. For instance, when this method is called on c (from the point above) as c.nerf(3), it must bring c’s speed to 14 and strength to 10. (Remember that void means that this method does not return any value.)

  • Character objects must have the following method:

    boolean isBetterThan(Character other)
    

    which must return true if both the speed and the strength of this object are greater than those of the given other object. Otherwise, the method must return false.

  • The class Character must have the following static method:

    static void printBest(Character[] characters)
    

    This static method must print on the terminal which character object in the array characters is better than all the other characters in the array. Characters must be compared using the method isBetterThan (see above). The static method printBest must print either:

    • The array is empty! if the array characters has length zero; or

    • There is no best character in the array! if none of the Character objects in the array characters is better than all the others; or

    • Best character: ... with the description of the best Character object in the array (formatted according to the method description, see above). For instance, if the array characters contains the object Lara Croft defined above, and that character is better than any other character in the array, then this static method must print:

      Best character: Lara Croft (speed = 17, strength = 13)
      

    Tip

    • To find the best character in the given array (if there is one), you might consider using nested loops…

    • Remember that void means that this method does not return any value. If you want to end the method and return to the caller, you can use the return statement (without any argument).

When you are done, submit the modified file Character.java on DTU Autolab.

Note

This exercise requires writing a Java class that contains both static methods and non-static methods (a.k.a. object methods, or just methods): this is perfectly valid, but the two different kinds of methods must not be confused when reasoning about our Java code. Remember that an object method can only be called on an object, which must have been created beforehand!

Warning

The automatic grading on DTU Autolab includes some additional secret checks. After you submit, double-check your grading result on DTU Autolab: if the secret checks fail, then your solution is not correct, and you should fix it and resubmit.

05 - Train Departures#

Edit the file Departure.java provided in the handout, and implement a class named Departure representing the time when a train departs from a train station, as follows.

  • Objects of the class Departure must have three fields called respectively:

    • hour (of type int) and minutes (of type int), which represent the time the train leaves the station;

    • destination (of type String), which is the destination of the train;

    Therefore, if d is an object of type Departure, we can write e.g.:

    d.hour = 10;
    d.minutes = 25;
    d.destination = "Aarhus";
    System.out.println("The train to " + d.destination + " leaves at "
                        + d.hour + ":" + d.minutes);
    
  • The class Departure must have the following constructor:

    Departure(int hour, int minutes, String destination)
    

    which allows us to create a new Departure object by writing e.g.:

    var d = new Departure(10, 25, "Aarhus");
    
  • Departure objects must have the following method:

    String description()
    

    which returns a representation of this object as a String. When this method is called on d (from the point above) it must return the string:

    10:25 to Aarhus
    
  • Departure objects must have the following method:

    boolean leavesBefore(Departure other)
    

    When called, the method returns true if the time of this departure comes before the time of the other departure. Otherwise, the method returns false.

  • Departure objects must have the following method:

    void delay(int minutes)
    

    When called, the method modifies the time of this departure by delaying it by the given amount of minutes. The method does not return anything (void).

The handout includes some Java files called Test01.java, Test02.java, etc.: they are test programs that use the class Departure, and they might not compile or work correctly until you complete the implementation of class Departure in the file Departure.java. You should read those test programs and try to run them, but you must not modify them.

When you are done, submit the modified file Departure.java on DTU Autolab.

06 - Dates#

Edit the file Date.java provided in the handout, and implement a class named Date which represent a date on the calendar. The class must have fields to store int values for the year, month, and day. Your task is to implement the class Date as follows.

  • The class Date must have the following constructor:

    Date(int y, int m, int d)
    

    This constructor allows us to create a new object of the class Date by simply writing, e.g.:

    var d = new Date(2023, 9, 29);
    

    The arguments passed to the constructor must be stored in the class fields.

  • Date objects must have the following method:

    String toISO()
    

    This method returns a string representation of this object, according to the standard ISO format YYYY-MM-DD. For example, the following code snippet:

    var d = new Date(2023, 9, 29);
    System.out.println(d.toISO());
    

    should print on the console:

    2023-09-29
    

    (Note that single-digit months and days are represented with a 0 in front, e.g. 2023-02-03)

  • Date objects must have the following method:

    boolean isValid()
    

    This method returns true if this date is valid, and false otherwise. For example, the following code snippet should print true:

    var d = new Date(2023, 9, 29);
    System.out.println(d.isValid());
    

    Instead, the following code snippet should print false (there is no 13th month):

    var d2 = new Date(2023, 13, 29);
    System.out.println(d2.isValid());
    

    Also, the following code snippet should print false (September only has 30 days):

    var d3 = new Date(2023, 9, 31);
    System.out.println(d3.isValid());
    

    Also, the following code snippet should print false (because February 2023 only has 28 days, since 2023 is not a leap year):

    var d4 = new Date(2023, 2, 29);
    System.out.println(d4.isValid());
    
  • Date objects must have the following method:

    int daysUntil(Date target)
    

    This method computes and returns the number of days between this date and the target date. For example, the following code snippet should print 1:

    var d1 = new Date(2023, 9, 29);
    var d2 = new Date(2023, 9, 30);
    System.out.println(d1.daysUntil(d2));
    

    Instead, the following code snippet should print 10:

    var d1 = new Date(2023, 9, 29);
    var d2 = new Date(2023, 10, 9);
    System.out.println(d1.daysUntil(d2));
    

The handout includes some Java files called Test01.java, Test02.java, etc.: they are test programs that use the class Date, and they might not compile or work correctly until you complete the implementation of Date.java. You should read those test programs and try to run them, but you must not modify them.

When you are done, submit the modified file Date.java on DTU Autolab.

Warning

The automatic grading on DTU Autolab includes some additional secret checks that test your submission with more dates (either valid or invalid). After you submit, double-check your grading result on DTU Autolab: if the secret checks fail, then your solution is not correct, and you should fix it and resubmit.

Hint

  • You may want to write a utility static method to compute the length of a given month in a given year (taking into account leap years): this can be useful to implement both methods isValid() and daysUntil(...).

  • You might reuse some code from the solutions to 03 - Leap Year and 04 - Days in a Month