diff --git a/content/courses/prog-intro/homeworks/sum/_index.md b/content/courses/prog-intro/homeworks/sum/_index.md new file mode 100644 index 0000000..7a72868 --- /dev/null +++ b/content/courses/prog-intro/homeworks/sum/_index.md @@ -0,0 +1,190 @@ +--- +title: Sum +weight: 5 +--- + +# Task + +1. You need to create a `Sum` class which will sum integers from command line arguments and output the sum to console. +2. Examples: + + ```sh + java Sum 1 2 3 + ``` + + Expected output: `6`. + + ```sh + java Sum 1 2 -3 + ``` + + Expected output: `0`. + + + ```sh + java Sum "1 2 3" + ``` + + Expected output: `6`. + + ```sh + java Sum "1 2" " 3" + ``` + + Expected output: `6`. + + ```sh + java Sum " " + ``` + + Expected output: `0`. + +3. Arguments can be: + - digits, + - signes `+` and `-`, + - [space symbols](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Character.html#isWhitespace(char)) + +4. You can assume that `int` type is sufficient for in-between calculations and result. +5. Before doing the task make sure to read docs for classes [`String`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/String.html) and [`Integer`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Integer.html). +6. For debugging use [`System.err`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/System.html#err), because it will be ingnored by the testing program. + +--- + +# Solution + +After reading about [`String`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/String.html), [`Integer`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Integer.html), [`System.err`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/System.html#err) we now know about some usefull methods: + +- [`Integer.parseInt(String s, int radix)`](https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#parseInt-java.lang.String-) which parses the string argument as a signed integer in the radix specified by the second argument. So it basically converts a number from the `String` data type to `int`. +- [`Character.isWhitespace(char ch)`](https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isWhitespace-char-) which checks if a character `ch` is a space symbol or not. + +Now let's start coding. Firstly let's define the structure of our program. I will be putting the name of the file at the first line comment in a file and its path if its nessesary. + +```java +// Sum.java + +public class Sum { + public static void main(String[] args) { + // ... + } +} +``` + +Okay that's done. What do we do next? Let's look at `String[] args` argument to our `main` method. It represents an array of command line arguments which we need to sum. So know we made our task a little bit easier. Now we can just say that we need to find sum of elements of array `args`. + +How are we going to do it though? First let's understand what we can do with this array. Let's modify our class a little bit. + +```java +// Sum.java + +public class Sum { + public static void main(String[] args) { + System.out.println(args.length); + } +} +``` + +We've added `System.out.println(args.length)` which takes the field `length` from our `args` object and prints it to the console. + +Let's try it. First compile our class using + +```sh +$ javac Sum.java +``` + +And then we can do some manual testing. + +```sh +$ java Sum 1 2 3 +3 +``` + +We got `3` as an output as expected. We gave our program 3 command line arguments: `1`, `2` and `3`. + +Here are all of the examples + +```sh +$ java Sum 1 2 -3 +3 +``` + +```sh +$ java Sum "1 2 3" +1 +``` + +> [!NOTE] +> Notice, that we got 1 instead of 3. That's because we put our arguments in `""` so this becomes a single string argument. + +```sh +$ java Sum "1 2" " 3" +2 +``` + +```sh +$ java Sum " " +1 +``` + +> [!NOTE] +> Here program gives us 1 instead of 0, because despite not having any numbers in the arguments a single whitespace is still an argument. + +Now let's try not obly to count our arguments but to list them as well. Let's modify our program a little bit more. + +```java +// Sum.java + +public class Sum { + public static void main(String[] args) { + System.out.println("number of arguments: " + args.length); + + for (String argument : args) { + System.out.println(argument); + } + } +} +``` + +Here I used `for` loop to do printing ***for*** every `String` element in `args`. + +Let's try this with our examples. And don't forget to recompile using `javac Sum.java`. + +```sh +$ java Sum 1 2 3 +number of arguments: 3 +1 +2 +3 +``` + +```sh +$ java Sum 1 2 -3 +number of arguments: 3 +1 +2 +-3 +``` + +```sh +$ java Sum "1 2 3" +number of arguments: 1 +1 2 3 +``` + +> [!NOTE] +> Again, notice only ***one*** string argument. + + +```sh +$ java Sum "1 2" " 3" +number of arguments: 2 +1 2 + 3 +``` + +```sh +$ java Sum " " +number of arguments: 1 + +``` + +Okay, now that we know how to ***iterate*** (or do something for every element), we can diff --git a/public/courses/index.html b/public/courses/index.html index 399d8ff..e9736fb 100644 --- a/public/courses/index.html +++ b/public/courses/index.html @@ -178,8 +178,19 @@ href="/courses/prog-intro/homeworks/" >Homeworks + + +
+ +
  • Homeworks + + +
    + +
  • Homeworks + +
    + +
  • Homeworks + + +
    + +
  • + + + + + + + + + +Sum – CodeJava + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +
    + +
    + + + + + +
    +
    + +
    +

    Sum

    +

    Task

      +
    1. +

      You need to create a Sum class which will sum integers from command line arguments and output the sum to console.

      +
    2. +
    3. +

      Examples:

      +
      + +
      java Sum 1 2 3
      + +
      +
      +

      Expected output: 6.

      +
      + +
      java Sum 1 2 -3
      + +
      +
      +

      Expected output: 0.

      +
      + +
      java Sum "1 2 3"
      + +
      +
      +

      Expected output: 6.

      +
      + +
      java Sum "1 2" " 3"
      + +
      +
      +

      Expected output: 6.

      +
      + +
      java Sum " "
      + +
      +
      +

      Expected output: 0.

      +
    4. +
    5. +

      Arguments can be:

      + +
    6. +
    7. +

      You can assume that int type is sufficient for in-between calculations and result.

      +
    8. +
    9. +

      Before doing the task make sure to read docs for classes String and Integer.

      +
    10. +
    11. +

      For debugging use System.err, because it will be ingnored by the testing program.

      +
    12. +
    +
    +

    Solution

    After reading about String, Integer, System.err we now know about some usefull methods:

    + +

    Now let’s start coding. Firstly let’s define the structure of our program. I will be putting the name of the file at the first line comment in a file and its path if its nessesary.

    +
    + +
    // Sum.java
    +
    +public class Sum {
    +    public static void main(String[] args) {
    +        // ...
    +    }
    +}
    + +
    +
    +

    Okay that’s done. What do we do next? Let’s look at String[] args argument to our main method. It represents an array of command line arguments which we need to sum. So know we made our task a little bit easier. Now we can just say that we need to find sum of elements of array args.

    +

    How are we going to do it though? First let’s understand what we can do with this array. Let’s modify our class a little bit.

    +
    + +
    // Sum.java
    +
    +public class Sum {
    +    public static void main(String[] args) {
    +        System.out.println(args.length);
    +    }
    +}
    + +
    +
    +

    We’ve added System.out.println(args.length) which takes the field length from our args object and prints it to the console.

    +

    Let’s try it. First compile our class using

    +
    + +
    $ javac Sum.java
    + +
    +
    +

    And then we can do some manual testing.

    +
    + +
    $ java Sum 1 2 3
    +3
    + +
    +
    +

    We got 3 as an output as expected. We gave our program 3 command line arguments: 1, 2 and 3.

    +

    Here are all of the examples

    +
    + +
    $ java Sum 1 2 -3
    +3
    + +
    +
    +
    + +
    $ java Sum "1 2 3"
    +1
    + +
    +
    +
    +

    Note

    + +
    +

    Notice, that we got 1 instead of 3. That’s because we put our arguments in "" so this becomes a single string argument.

    +
    +
    +
    + +
    $ java Sum "1 2" " 3"
    +2
    + +
    +
    +
    + +
    $ java Sum " "
    +1
    + +
    +
    +
    +

    Note

    + +
    +

    Here program gives us 1 instead of 0, because despite not having any numbers in the arguments a single whitespace is still an argument.

    +
    +
    +

    Now let’s try not obly to count our arguments but to list them as well. Let’s modify our program a little bit more.

    +
    + +
    // Sum.java
    +
    +public class Sum {
    +    public static void main(String[] args) {
    +        System.out.println("number of arguments: " + args.length);
    +        
    +        for (String argument : args) {
    +            System.out.println(argument);
    +        }
    +    }
    +}
    + +
    +
    +

    Here I used for loop to do printing for every String element in args.

    +

    Let’s try this with our examples. And don’t forget to recompile using javac Sum.java.

    +
    + +
    $ java Sum 1 2 3
    +number of arguments: 3
    +1
    +2
    +3
    + +
    +
    +
    + +
    $ java Sum 1 2 -3
    +number of arguments: 3
    +1
    +2
    +-3
    + +
    +
    +
    + +
    $ java Sum "1 2 3"
    +number of arguments: 1
    +1 2 3
    + +
    +
    +
    +

    Note

    + +
    +

    Again, notice only one string argument.

    +
    +
    +
    + +
    $ java Sum "1 2" " 3"
    +number of arguments: 2
    +1 2
    + 3
    + +
    +
    +
    + +
    $ java Sum " "
    +number of arguments: 1
    + 
    + +
    +
    +

    Okay, now that we know how to iterate (or do something for every element), we can

    + +
    +
    +
    + +
    +
    +
    + + + + + + + + diff --git a/public/courses/prog-intro/index.html b/public/courses/prog-intro/index.html index 02f8a13..728e078 100644 --- a/public/courses/prog-intro/index.html +++ b/public/courses/prog-intro/index.html @@ -178,8 +178,19 @@ href="/courses/prog-intro/homeworks/" >Homeworks + + +
    + +
  • Homeworks + + +
    + +
  • - + @@ -178,8 +178,19 @@ href="/courses/prog-intro/homeworks/" >Homeworks + + +
    + +