Module 5, Part 2: Lab Day#

This second part of Module 5 does not introduce new contents: instead, it provides a series of assignments that focus on console I/O, conditionals, loops, and arrays, to improve your Java programming skills. All the following assignments can be solved with the Java programming notions introduced in Modules 2, 3, 4, and 5 (part 1).

Important

05 - Triangles#

Edit the file Triangles.java provided in the handout, and write a program that reads from the console 3 values (of type double) representing the lengths of the 3 sides of a triangle, and then prints which kind of triangle it is, depending on its internal angles.

The program must print either:

  • Degenerate, if at least one of the internal angles is 180 degrees.

  • Obtuse, if at least one internal angle is greater than 90 degrees and smeller than 180 degrees.

  • Acute, if all internal angles are smaller than 90 degrees.

  • Right, if one internal angle is 90 degrees.

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

Hint

  • Before reading values of type double from the console, remember to configure the Scanner with the English localisation.

  • To compute the internal angles of the triangle (in radians), you can refer to this explanation.

  • To perform the computations required by this program, the Java API provides: (try these on the Java shell!)

    • the static method Math.acos(cos), which computes and returns the arccosine of the quantity cos. Both the argument cos and the return value of Math.acos(cos) have type double; note that Math.acos(...) returns an angle in radians;

    • the constant Math.PI, which gives the value of \(\pi\). For example, to represent the expression \(\frac{\pi}{2}\) in Java (which corresponds to an angle of 90 degrees in radians) we can write the expression: Math.PI / 2.

06 - Pyramids#

Edit the file Pyramid.java provided in the handout, and write a program that reads from the console an int value \(n\), and then prints a pyramid inside a rectangular frame of height \(n\), using the symbols:

  • # where there is a pyramid block;

  • . where the rectangular frame is empty.

For example, if the input is 3, the program must print the following pyramid (of height 3):

..#..
.###.
#####

Instead, if the input is 5, the program must print the following pyramid (of height 5):

....#....
...###...
..#####..
.#######.
#########

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

Warning

The automatic grading on DTU Autolab includes some additional secret checks that test your submission with additional 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.

Hint

You may start solving this assessment by writing a program that prints a simplified version of the required output, that looks like the following (when the input is 5):

#........
###......
#####....
#######..
#########

Then, you could revise your program to produce the required output, by printing a certain number of . symbols before the # symbols, depending on the height of the pyramid, and the current line being printed…

07 - Count the Occurrences 2#

This assessment is a variation of 01 - Count the Occurrences. Your task is to edit the file Utils.java provided in the handout. In the class Utils, implement the static method with the following signature:

static int count(String s, String[] strings)

The static method must count how many times the string s occurs in the array strings, and return the result. The comparison must be case-insensitive: e.g., if the array strings is the array {"hello", "world"}, then the static method call count("Hello", strings) must return 1. Note that strings might have 0 elements.

The static method Utils.count(...) is used in the test program contained in the file Occurrences.java, which is also included in the handout: you should read Occurrences.java and try to run it, but you must not modify it.

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

Important

Besides testing your solution by running ./grade, make sure you are also able to manually compile this multi-file Java program (using javac) and then run it (using java), as explained in Splitting Code into Separate Files.

Warning

The automatic grading on DTU Autolab includes some additional secret checks that test your submission with additional 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.

Hint

  • Remember that, to check whether two strings s1 and s2 are equal to each other, you will need to use s1.equals(s2) (see how to compare strings).

  • When comparing the string argument s with the strings contained in the array strings, it may be handy to convert to uppercase or lowercase

08 - Water Temperatures#

(Based on an exam exercise from ITU, January 2000)

Edit the file WaterTemps.java provided in the handout, and write a program that reads from the console a whole line containing one or more numbers separated by “; ” (i.e. a semicolon and a space): each number represents a water temperature measurement. Then, the program must print:

  1. the maximum water temperature, and

  2. the number of temperature drops, i.e. how many times a temperature is immediately followed by a lower temperature.

For example, if the input of the program is:

15; 16.5; 16; 16.2; 16; 15.8; 15.5

then the program must print:

Maximum water temperature: 16.5
Number of temperature drops: 4

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

Warning

The automatic grading on DTU Autolab includes some additional secret checks that test your submission with additional 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.

Hint

  • You can reuse and adapt some code from the file Evens.java provided with 03 - Even Numbers.

  • To obtain a double from a String, you can use Double.parseDouble(...). For instance, Double.parseDouble("3.14") produces the value 3.14 (of type double).

09 - Sub-Array#

Edit the file ArrayUtils.java provided in the handout and implement the following static method (in the class ArrayUtils):

static boolean contains(int[] arr1, int[] arr2)

This static method must return true if arr1 contains all the values that are contained in arr2, in the same order in which they appear in arr2 (although arr1 may have other values before, after, or in between the values of arr2).

For example, the following code snippet should print true (because arr2 requires arr1 to contain the value 1 followed by 3, possibly with other values in between):

var arr1 = new int[] {4, 1, 2, 3};
var arr2 = new int[] {1, 3};

System.out.println(ArrayUtils.contains(arr1, arr2));

Instead, the following code snippet should print false (because arr2 requires arr1 to contain the value 3 followed by 1, possibly with other values in between):

var arr1 = new int[] {4, 1, 2, 3};
var arr2 = new int[] {3, 1};

System.out.println(ArrayUtils.contains(arr1, arr2));

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

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

Warning

The automatic grading on DTU Autolab includes some additional secret checks that test your submission with additional test programs. 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 read again how to split a Java program into separate files.

  • To check whether your solution works with one of the test programs, you will need to compile at least ArrayUtils.java, ArrayTestUtils.java, and that test program. For instance, if you want to try Test01.java, you will need to first run (on the console):

    javac ArrayUtils.java ArrayTestUtils.java Test01.java
    

    and then you can execute the main method contained in the class Test01, by running:

    java Test01
    

10 - Array Rotation#

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

  • static int[] rotateLeft(int[] arr)
    

    This static method takes an array arr as argument, and returns an array containing the same values of arr, rotated to the left by one position, i.e.:

    • the first value in the array becomes the last value, and

    • each one of the other values is shifted to the left by one position.

    For example, if arr is the array:

    {4, 1, 2, 3}
    

    then the static method must return the array:

    {1, 2, 3, 4}
    

    (Notice that the value 4 has been moved from the first to the last position of the array, and the other values have been moved to the left by one position.)

  • static int[] rotateLeft(int[] arr, int n)
    

    This static method takes an array arr and a non-negative integer n as arguments, and returns an array containing the same values of arr, rotated to the left by n positions.

    For example, if arr is the array:

    {5, 6, 7, 1, 2, 3, 4}
    

    then a call to rotateLeft(arr, 3) must return an array containing the same values of arr rotated to the left by 3 positions, i.e.:

    {1, 2, 3, 4, 5, 6, 7}
    

The handout includes some Java files called Test01.java, Test02.java, etc.: they are test programs that use the class ArrayUtils, and they might not compile or work correctly until you complete the implementation of ArrayUtils.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 ArrayUtils.java on DTU Autolab.

Note

  • To compute and return the rotated array, you may either modify the given array arr, or create and return a new array: feel free to choose the technique that you prefer.

  • You must consider the case where the static methods are called with an array of length 0.

  • An array may be rotated by a number of positions that is greater than the length of the array.

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

Warning

The automatic grading on DTU Autolab includes some additional secret checks that test your submission with additional test programs. 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 read again how to split a Java program into separate files.

  • To check whether your solution works with one of the test programs, you will need to compile at least ArrayUtils.java and that test program. For instance, if you want to try Test01.java, you will need to first run (on the console):

    javac ArrayUtils.java Test01.java
    

    and then you can execute the main method contained in the class Test01, by running:

    java Test01
    
  • After you implement one of the static methods above, you may reuse it to simplify the implementation of the other, i.e.:

    • if you choose to implement rotateLeft(arr) first, you may implement rotateLeft(arr, n) by calling n times rotateLeft(arr);

    • if you choose to implement rotateLeft(arr, n) first, you may implement rotateLeft(arr) by calling rotateLeft(arr, 1).