Second Summer 2002


Lecture Notes Twenty: 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) {
	ConsoleReader console = new ConsoleReader(System.in); 
	System.out.print("Enter: "); 
	String line = console.readLine(); 
	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
Enter: adrian
adrian
(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) {
	ConsoleReader console = new ConsoleReader(System.in); 
	System.out.print("Enter: "); 
	String line = console.readLine(); 
	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 ints.

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

	ConsoleReader c = new ConsoleReader(System.in); 
	System.out.print("Enter: "); 
	String line = c.readLine(); 

	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 Here's the code, not much different from what we've done before.

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

	ConsoleReader console = new ConsoleReader(System.in); 

	System.out.print("Enter: "); 
	String line = console.readLine(); 

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

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

	System.out.print("Enter: "); 
	line = console.readLine(); 

	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
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
	at Six.main(Six.java:27)
frilled.cs.indiana.edu%
Now suppose we have an array with overflow, which needs to be propagated forward.

(Forward means from left to right, here.)

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 now is 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) {

	ConsoleReader console = new ConsoleReader(System.in); 

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

	String line = console.readLine(); 

	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
Enter: adrian
adrian
(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

Here's the code.
class Nine {
    public static void main(String[] args) {

	ConsoleReader console = new ConsoleReader(System.in); 

	System.out.print("Enter: "); 
	String line = console.readLine(); 

	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: "); 
	line = console.readLine(); 

	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 set of notes.

So here's

YOUR NEXT CHALLENGE

Change what we have above into something that works 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%
This program 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%
For the homework assignment you just need to remove the need to add leading 0's (zeros).


Last updated: Jun 16, 2002 by Adrian German for A201