From d23bc8b371f9e7a65ef7f2e22d12505dca4005ad Mon Sep 17 00:00:00 2001 From: me Date: Fri, 10 Apr 2026 09:16:39 +0300 Subject: [PATCH] update --- .../courses/prog-intro/homeworks/sum/Sum.java | 4 +- .../prog-intro/homeworks/sum/_index.md | 94 ++++++++--- .../courses/prog-intro/homeworks/sum/Sum.java | 4 +- .../prog-intro/homeworks/sum/index.html | 151 ++++++++++++++---- public/en.search-data.json | 2 +- 5 files changed, 191 insertions(+), 64 deletions(-) diff --git a/content/courses/prog-intro/homeworks/sum/Sum.java b/content/courses/prog-intro/homeworks/sum/Sum.java index 9225eda..7830d1b 100644 --- a/content/courses/prog-intro/homeworks/sum/Sum.java +++ b/content/courses/prog-intro/homeworks/sum/Sum.java @@ -14,7 +14,7 @@ public class Sum { // if character is whitespace if (!number.isEmpty()) { // if number is not empty - sum = sum + Integer.parseInt(number.toString()); // add number to sum + sum += Integer.parseInt(number.toString()); // add number to sum } number = new StringBuilder(); // empty the number by creating new empty StringBuilder } @@ -23,7 +23,7 @@ public class Sum { // check for any remaining digits in number if (!number.isEmpty()) { // if number is not empty - sum = sum + Integer.parseInt(number.toString()); // add number to sum + sum += Integer.parseInt(number.toString()); // add number to sum } } diff --git a/content/courses/prog-intro/homeworks/sum/_index.md b/content/courses/prog-intro/homeworks/sum/_index.md index 5b756b1..af7ae24 100644 --- a/content/courses/prog-intro/homeworks/sum/_index.md +++ b/content/courses/prog-intro/homeworks/sum/_index.md @@ -50,14 +50,14 @@ weight: 5 --- -# Solution +## 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. +Now let's start coding. Firstly let's define the structure of our program. I will be putting the name of the file and its path (if it's nessesary) at the first line comment. ```java // Sum.java @@ -69,7 +69,7 @@ public class Sum { } ``` -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`. +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 now 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. @@ -128,7 +128,7 @@ $ java Sum " " > [!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. +Now let's try not only to count our arguments but to list them as well. Let's modify our program a little bit more. ```java // Sum.java @@ -275,25 +275,6 @@ So we figured out that we need to cast `argument` to integer type. Let's change ```java // Sum.java -public class Sum { - public static void main(String[] args) { - System.out.println("number of arguments: " + args.length); - - int sum = 0; - for (String argument : args) { - sum = sum + Ineteger.parseInt(argument); - System.out.println(argument); - System.out.println(sum); - } - - System.out.println(sum); - } -} -``` - -```java -// Sum.java - public class Sum { public static void main(String[] args) { System.out.println("number of arguments: " + args.length); @@ -457,4 +438,69 @@ public class Sum { ``` > [!IMPORTANT] -> Note, that we can't just do `Integer.parseInt()` +> Note, that we can't just do `Integer.parseInt(number)`, because `number` is an instance of `StringBuilder`, not just a regular `String`. In order to get the current state of the string we can use [`StringBuilder.toString()`](https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#toString--) method. + +> [!NOTE] +> Also it's worth mentioning, that we can shorten operations like thse +> ```java +> arg1 = arg1 X arg2 +> ``` +> to +> ```java +> arg1 X= arg2 +> ``` +> where `X` is some operation like `+`, `-`, `*`, `/` and so on. + +Let's make this change to our code + +```java +// Sum.java + +public class Sum { + + public static void main(String[] args) { + int sum = 0; + for (String argument : args) { + StringBuilder number = new StringBuilder(); // we will form numbers here using StringBuilder + for (char c : argument.toCharArray()) { + if (!Character.isWhitespace(c)) { + // if character is not whitespace + number.append(c); // concatenate new digit + } else { + // if character is whitespace + if (!number.isEmpty()) { + // if number is not empty + sum += Integer.parseInt(number.toString()); // add number to sum + } + number = new StringBuilder(); // empty the number by creating new empty StringBuilder + } + } + + // check for any remaining digits in number + if (!number.isEmpty()) { + // if number is not empty + sum += Integer.parseInt(number.toString()); // add number to sum + } + } + + System.out.println(sum); + } +} +``` + +This is it! The code is ready. + +--- + +Below are some modifications to the code for deeper understanding of basic language constructions primarily data types. It is always advised to try completing them by yourself before reading the solution. Modifications progress in difficulty from easy to hard. + +# SumDouble + +- Input data is 64-bytes floating point numbers. +- Class should be named `SumDouble`. + +## Solution + +This modification is fairly easy. All we need to do is to replace all integers with floating point numbers. + +--- diff --git a/public/courses/prog-intro/homeworks/sum/Sum.java b/public/courses/prog-intro/homeworks/sum/Sum.java index 9225eda..7830d1b 100644 --- a/public/courses/prog-intro/homeworks/sum/Sum.java +++ b/public/courses/prog-intro/homeworks/sum/Sum.java @@ -14,7 +14,7 @@ public class Sum { // if character is whitespace if (!number.isEmpty()) { // if number is not empty - sum = sum + Integer.parseInt(number.toString()); // add number to sum + sum += Integer.parseInt(number.toString()); // add number to sum } number = new StringBuilder(); // empty the number by creating new empty StringBuilder } @@ -23,7 +23,7 @@ public class Sum { // check for any remaining digits in number if (!number.isEmpty()) { // if number is not empty - sum = sum + Integer.parseInt(number.toString()); // add number to sum + sum += Integer.parseInt(number.toString()); // add number to sum } } diff --git a/public/courses/prog-intro/homeworks/sum/index.html b/public/courses/prog-intro/homeworks/sum/index.html index bc2e315..6ffa722 100644 --- a/public/courses/prog-intro/homeworks/sum/index.html +++ b/public/courses/prog-intro/homeworks/sum/index.html @@ -35,7 +35,7 @@ java Sum 1 2 -3 Expected output: 0." /> - + >Sum - + @@ -355,7 +367,15 @@ java Sum 1 2 -3 Expected output: 0.">