Understanding static public int main(String[] args)

Figure 440. public static void main(String[] args) Slide presentation
package myapp;
public class Cmd {
  public static void main(String[] args) {
    for (int i = 0; i < args.length; i++) {
      System.out.println("Parameter " + (i + 1) + ": " + args[i]);
    }
  }
}
java myapp.Cmd 21 334 -13
Parameter 1: 21
Parameter 2: 334
Parameter 3: -13

Figure 441. Intellij IDEA run configuration Slide presentation
Intellij IDEA run configuration

Entry class myapp.Cmd containing ... main(String[] args).

Parameters 21, 334 and -13 being passed to String[] args.


Figure 442. Intellij IDEA run configuration Slide presentation
Intellij IDEA run configuration

Figure 443. Creating executable jar Slide presentation
<artifactId>maven-shade-plugin</artifactId>
...
<transformer ...>
  <manifestEntries>
    <Main-Class>myapp.Cmd</Main-Class>
  </manifestEntries>...
unzip ww-1.0-SNAPSHOT.jar
cat  tmp/META-INF/MANIFEST.MF
...
Created-By: Apache Maven 3.5.0
Build-Jdk: 1.8.0_151
Main-Class: myapp.Cmd
mvn package ...
java -jar target/ww-1.0-SNAPSHOT.jar 21 334 -13
Parameter 1: 21
Parameter 2: 334
Parameter 3: -13

exercise No. 154

Reading console input

Q:

Consider the following code example computing the sum of an arbitrary number of integer values:

import java.util.Scanner;

public class Driver {
  public static void main(String[] args) {

    final Scanner scanner = new Scanner(System.in);

    System.out.print("How many values do you want to summ up? ");
    final int numValues = scanner.nextInt();

    int sum = 0;
    for (int i = 0; i < numValues; i++) {
      System.out.print("Enter value #" + (i + 1) + " of " + numValues + ": ");
      sum += scanner.nextInt();
    }
    System.out.println("Sum=" + sum);
    scanner.close();
  }
}

This program:

  1. Asks a user how many integer numbers shall be processed. A user may enter e.g. 4 indicating his intention to subsequently enter 4 values.

  2. Actually reads e.g. 4 input numbers and calculates their sum.

The result might look like:

How many values do you want to summ up? 4
Enter value #1 of 4: 11
Enter value #2 of 4: 22
Enter value #3 of 4: -33
Enter value #4 of 4: 1
Sum=1

Modify this program to output not just the desired sum but the complete arithmetic expression:

How many values do you want to summ up? 4
Enter value #1 of 4: 1
...
1+2+3-5 = 1

Hints:

  1. Use an integer array to store the entered input values.

  2. Mind the input values' signs: While positive or zero values are not being represented with a leading + sign negative values already do have a preceding - sign. With respect to the above example output representations like 22+-33 are discouraged.

A:

exercise No. 155

Prettifying output representation

Q:

The output representation of Reading console input shall be altered by the following rules:

  1. A empty set of input values shall produce just 0 (zero) avoiding the = sign:

    {} → 0.

  2. A list containing just one input value shall be represented by just this value avoiding the = sign:

    {-4} → -4.

  3. Zero values shall be excluded completely:

    • {0,1,0,4} → 1+4=5.

    • {0,0,0} → 0. (By virtue of rule 1)

    • {0,1,0} → 1. (By virtue of rule 2)

  4. If possible a sum shall not start with a negative value. The list {-1, -2, 5} for example will generate -1-2+5=2. Instead the first negative value shall be swapped with the first positive value (if existent). The current example will thus result in 5-2-1=2.

Write your application in a unit testable fashion.

Tip

  • Apply these rules in order 3-4-2/1

  • The following Maven project skeleton contains both a yet not implemented class method de.hdm_stuttgart.mi.sda1.console_sum.App.prettifyOutput(int[] values) and a corresponding Junit test suite being defined in de.hdm_stuttgart.mi.sda1.console_sum.AppTest:

    All tests would currently fail. Therefore the test class de.hdm_stuttgart.mi.sda1.console_sum.AppTest is being masked by an @Disabled annotation:

    ...
    @Disabled
    public class AppTest {
       /**
        * empty list
        */
       @Test
       public void testEmptyList() {
           Assert.assertEquals("0", App.prettifyOutput(new int[0]));
       } ...

    Import this project into your IDE and complete its implementation of de.hdm_stuttgart.mi.sda1.console_sum.App.prettifyOutput(int[] values) until all Junit tests do succeed. Removing the @Disabled line will allow for testing your implementation.

A: