Lab Notes Ten: Help with Homework Four
Let's write a program that reads a `String` and reports it.

And in the process puts parens around the characters.

```class One {
public static void main(String[] args) {
System.out.print("Enter: ");
System.out.println(line);
for (int i = 0; i < line.length(); i++) {
char c;
c = line.charAt(i);
System.out.print("(" + c + ")");
}
System.out.println();
}
}```
Here's a run with it.
```frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
(a)(d)(r)(i)(a)(n)
frilled.cs.indiana.edu%```
Now let's write a program that sums up the digits in such a `String`.

```class Two {
public static void main(String[] args) {
System.out.print("Enter: ");
System.out.println(line);
int sum = 0;
for (int i = 0; i < line.length(); i++) {
sum = sum + line.charAt(i) - '0';
}
System.out.println("The sum is: " + sum);
}
}```
Of course, we need to assume that the characters are all digits.

```frilled.cs.indiana.edu%java Two
Enter: 123
123
The sum is: 6
frilled.cs.indiana.edu%java Two
Enter: 666
666
The sum is: 18
frilled.cs.indiana.edu%java Two
Enter: 123456789
123456789
The sum is: 45
frilled.cs.indiana.edu%java Two
Enter: AB
AB
The sum is: 35
frilled.cs.indiana.edu%```
Otherwise, even if it computes, it doesn't make much sense.

So the user has to enter only digits.

Now let's set up two arrays and sum them up element by element.

```class Three {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] b = { 9, 8, 7, 6, 5, 4, 3, 2, 1};

for (int i = 0; i < a.length; i++) {
System.out.println(a[i] + " + " + b[i] + " = " + (a[i] + b[i]));
}

}
}```
Here's what "element by element" means.

```frilled.cs.indiana.edu%java Three
1 + 9 = 10
2 + 8 = 10
3 + 7 = 10
4 + 6 = 10
5 + 5 = 10
6 + 4 = 10
7 + 3 = 10
8 + 2 = 10
9 + 1 = 10
frilled.cs.indiana.edu%```
Let's store these values in a third array.

```class Four {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] b = { 9, 8, 7, 6, 5, 4, 3, 2, 1};

int[] c = new int[a.length];

for (int i = 0; i < a.length; i++) {
c[i] = a[i] + b[i];
}

}
}```
But this program doesn't print anything.

So let's print the resulting array with parens, as in the beginning.

```class Four {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] b = { 9, 8, 7, 6, 5, 4, 3, 2, 1};

int[] c = new int[a.length];

for (int i = 0; i < a.length; i++) {
c[i] = a[i] + b[i];
}

for (int i = 0; i < c.length; i++) {
System.out.print("(" + c[i] + ")");
}

System.out.println();

}
}```
Here's a run with it.

```frilled.cs.indiana.edu%java Four
(10)(10)(10)(10)(10)(10)(10)(10)(10)
frilled.cs.indiana.edu%```
Now let's read a `String` of digits, and turn that into an array of `int`s.

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

System.out.print("Enter: ");

int[] a = new int[line.length()];

for (int i = 0; i < line.length(); i++) {
a[i] = line.charAt(i) - '0';
}

for (int i = 0; i < a.length; i++) {
System.out.print("(" + a[i] + ")");
}

System.out.println();

}
}```
Note that if the assumption is broken our calculations are no longer meaningful.

```frilled.cs.indiana.edu%java Five
Enter: 123
(1)(2)(3)
frilled.cs.indiana.edu%java Five
Enter: 67890
(6)(7)(8)(9)(0)
frilled.cs.indiana.edu%java Five
Enter: abc
(49)(50)(51)
frilled.cs.indiana.edu%```
Now, let's
• read in two `String`s of the same length,
• turn them into arrays
• add them up element by element and store the results in a third array
• then print the result
Here's the code, not much different from what we've done before.

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

System.out.print("Enter: ");

int[] a = new int[line.length()];

for (int i = 0; i < line.length(); i++) {
a[i] = line.charAt(i) - '0';
}

System.out.print("Enter: ");

int[] b = new int[line.length()];

for (int i = 0; i < line.length(); i++) {
b[i] = line.charAt(i) - '0';
}

int[] c = new int[a.length];

for (int i = 0; i < c.length; i++) {
c[i] = a[i] + b[i];
}

for (int i = 0; i < c.length; i++) {
System.out.print("(" + c[i] + ")");
}

System.out.println();

}
}```
Once again the assumptions are important, because we don't enforce or double-check for anything.

```frilled.cs.indiana.edu%java Six
Enter: 12345
Enter: 12345
(2)(4)(6)(8)(10)
frilled.cs.indiana.edu%java Six
Enter: 6666
Enter: 6666
(12)(12)(12)(12)
frilled.cs.indiana.edu%java Six
Enter: 123
Enter: 1234
(2)(4)(6)
frilled.cs.indiana.edu%java Six
Enter: 1234
Enter: 123
at Six.main(Six.java:27)
frilled.cs.indiana.edu%```
Now suppose we have an array with overflow, which needs to be propagated forward.

(In lecture today we used back propagation).

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

int[] c = { 12, 13, 14, 15 };

for (int i = 0; i < c.length - 1; i++) {

if (c[i] >= 10) {

c[i + 1] = c[i + 1] + (c[i] / 10);

c[i] = c[i] % 10;

}

}

Seven.show(c);

}

static void show(int[] c) {

for (int i = 0; i < c.length; i++) {
System.out.print("(" + c[i] + ")");
}

System.out.println();

}

}```
Here's how it runs.

```frilled.cs.indiana.edu%javac Seven.java
frilled.cs.indiana.edu%java Seven
(2)(4)(5)(16)
frilled.cs.indiana.edu%```
Here's a program that reads a `String` and reports it backwards.

And in the process puts parens around the characters.

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

System.out.print("Enter: ");

System.out.println(line);

for (int i = line.length() - 1; i >= 0; i--) {

System.out.print("(" + line.charAt(i) + ")");

}

System.out.println();
}
}```
Here's how it works.

```frilled.cs.indiana.edu%javac Eight.java
frilled.cs.indiana.edu%java Eight
(n)(a)(i)(r)(d)(a)
frilled.cs.indiana.edu%java Eight
Enter: nairda
nairda
(a)(d)(r)(i)(a)(n)
frilled.cs.indiana.edu%```
Why do you think this is important?

Let's

• read two numbers of same length (as `String`s)
• store them backwards in two arrays
• sum them up, and store the result in a third array
• clean it up by propagating any overflow in the result
• print the result backwards
Here's the code.
```class Nine {
public static void main(String[] args) {

System.out.print("Enter: ");

int[] a = new int[line.length()];

for (int i = a.length - 1, j = 0; i >= 0; i--, j++) {
a[j] = line.charAt(i) - '0';
}

System.out.print("Enter: ");

int[] b = new int[line.length()];

for (int i = a.length - 1, j = 0; i >= 0; i--, j++) {
b[j] = line.charAt(i) - '0';
}

int[] c = new int[a.length];

for (int i = 0; i < a.length; i++) {
c[i] = a[i] + b[i];
}

Nine.clean(c);

for (int i = c.length - 1; i >= 0; i--)
System.out.print("(" + c[i] + ")");

System.out.println();

}

static void clean(int[] c) {
for (int i = 0; i < c.length - 1; i++) {
if (c[i] >= 10) {
c[i + 1] = c[i + 1] + (c[i] / 10);
c[i] = c[i] % 10;
}
}
}
}```
Here's how it runs.

```frilled.cs.indiana.edu%javac Nine.java
frilled.cs.indiana.edu%java Nine
Enter: 1
Enter: 1
(2)
frilled.cs.indiana.edu%java Nine
Enter: 10
Enter: 12
(2)(2)
frilled.cs.indiana.edu%java Nine
Enter: 01
Enter: 16
(1)(7)
frilled.cs.indiana.edu%java Nine
Enter: 99
Enter: 99
(19)(8)
frilled.cs.indiana.edu%```
To reach this stage is the goal of this lab.

So here's

A201/A597 LAB ASSIGNMENT TEN

You have to turn in on Monday a program that behaves like this.

```frilled.cs.indiana.edu%java Ten
Enter: 1
Enter: 1
Result 2
frilled.cs.indiana.edu%java Ten
Enter: 123
Enter: 001
Result 124
frilled.cs.indiana.edu%java Ten
Enter: 001
Enter: 999
Result 1000
frilled.cs.indiana.edu%java Ten
Enter: 999
Enter: 999
Result 1998
frilled.cs.indiana.edu%```
The program
• expects numbers of the same length, and
If you have numbers of not the same length use leading 0's (zeros).

So `100 + 1` would be written `100 + 001`.

Here's also how you can fix the alignment problem:

```	Nine.clean(c);

System.out.print("Result");

if (c[c.length - 1] < 10)

System.out.print(" ");

for (int i = c.length - 1; i >= 0; i--)
System.out.print(c[i]);

System.out.println();

}
```
So now my program works like this (and yours should too).

```frilled.cs.indiana.edu%java Ten
Enter: 123
Enter: 123
Result 246
frilled.cs.indiana.edu%java Ten
Enter: 999
Enter: 001
Result1000
frilled.cs.indiana.edu%java Ten
Enter: 100
Enter: 001
Result 101
frilled.cs.indiana.edu%java Ten
Enter: 999
Enter: 999
Result1998
frilled.cs.indiana.edu%```
The program is due at the beginning of lab on Monday.

The homework is due on the same day at 11:59pm.

For the homework assignment you just need to remove the need to add leading 0's (zeros).

Last updated: Jul 19, 2001 by Adrian German for A201