Tuesday July 25, 2000
There is one point that needs to be made about type conversion.
Recall that Java will perform automatic type conversion if it can be done
safely. This means if we don't lose any bits.
Another aspect of type conversion is that when we perform operations on
integers the result is an integer as well. That is, if we perform the
operation 1/3, then the result is a 0.
Consider the following code.
public class Test {
public static void main(String[] args) {
int a = 1/3;
double b = 1/3;
System.out.println("a is " + a);
System.out.println("b is " + b);
}
}
The output of this program is
a is 0
b is 0.0
So this means that it is not necessary to place the casting operator in
front of the quotient. We used the casting operator earlier in the
semester when we did these operations to avoid any confusion. That is to
make sure the result is an integer, we used the casting operator. Although
it was not necessary to do so, it is still good style to make explicit
casts in certain cases.
Be careful about these types of operations in your programming assignment.
Note that just because you make a division between two numbers, the result
will not necessarily be a real number.
Let us continue our discussion of arrays.
Let us recall a few important points about arrays.
- An array is an object.
- However, an array is not a class. It is not defined in a class like
other objects. It is built into the language.
- There are no methods associated with an array. We use the symbols []
to retrieve elements from an array.
- Arrays can hold primitive data types as well as object references.
- The types of the elements in an array must be the same.
- Arrays cannot grow. Once we declare the size of array, it can't
change.
One other aspect of an array is that there is a field or property
associated with an array called length. This tells us how many elements
are in an array. You might ask "Why do we need such a field since we must
specify the size of an array when we declare it? This seems to be
overkill."
The reason this fields is important can be seen when we use an array as a
parameter to a method.
Arrays as Parameters
Arrays can be used as parameters to methods just as other objects can.
Recall that since an array is an object, the arrayname is an object
reference. So that when we send it as a parameter to a method, we are
sending it by reference. Therefore we can have the method change the
contents of the array.
Consider the following code.
public class Test2 {
public static void sort(int[] array) {
for (int counter=0;counter<array.length-1;counter++)
for (int counter1=counter+1;counter1<array.length;counter1++)
if (array[counter1] < array[counter]) {
int temp = array[counter1];
array[counter1] = array[counter];
array[counter] = temp;
}
}
public static void display_contents(int[] array,String arrayname) {
for (int counter=0;counter<array.length;counter++)
System.out.println("arrayname[" + counter + "] = " + array[counter] + ".");
}
public static void main(String[] args) {
int[] numbers = {5,6,2,3,4};
System.out.println("Original\n");
Test2.display_contents(numbers,"numbers");
Test2.sort(numbers);
System.out.println("\nSorted\n");
Test2.display_contents(numbers,"numbers");
}
}
This code produces the following output.
Original
arrayname[0] = 5.
arrayname[1] = 6.
arrayname[2] = 2.
arrayname[3] = 3.
arrayname[4] = 4.
Sorted
arrayname[0] = 2.
arrayname[1] = 3.
arrayname[2] = 4.
arrayname[3] = 5.
arrayname[4] = 6.
Note that as with any pass by reference, the method cannot change where
the reference points.
Consider the following modification.
public class Test3 {
public static void sort(int[] array) {
for (int counter=0;counter<array.length-1;counter++)
for (int counter1=counter+1;counter1<array.length;counter1++)
if (array[counter1] < array[counter]) {
int temp = array[counter1];
array[counter1] = array[counter];
array[counter] = temp;
}
array = new int[17];
}
public static void display_contents(int[] array,String arrayname) {
for (int counter=0;counter<array.length;counter++)
System.out.println("arrayname[" + counter + "] = " + array[counter] + ".");
}
public static void main(String[] args) {
int[] numbers = {5,6,2,3,4};
System.out.println("Original\n");
Test3.display_contents(numbers,"numbers");
Test3.sort(numbers);
int a = numbers.length;
System.out.println("\n" + a);
System.out.println("\nSorted\n");
Test3.display_contents(numbers,"numbers");
}
}
The output of this code is
Original
arrayname[0] = 5.
arrayname[1] = 6.
arrayname[2] = 2.
arrayname[3] = 3.
arrayname[4] = 4.
5
Sorted
arrayname[0] = 2.
arrayname[1] = 3.
arrayname[2] = 4.
arrayname[3] = 5.
arrayname[4] = 6.
Arrays as return types
Arrays can also be used as return types. Consider the following
modification.
public class Test4 {
public static int[] sort(int[] array) {
for (int counter=0;counter<array.length-1;counter++)
for (int counter1=counter+1;counter1<array.length;counter1++)
if (array[counter1] < array[counter]) {
int temp = array[counter1];
array[counter1] = array[counter];
array[counter] = temp;
}
return(array);
}
public static void display_contents(int[] array,String arrayname) {
for (int counter=0;counter<array.length;counter++)
System.out.println("arrayname[" + counter + "] = " + array[counter] + ".");
}
public static void main(String[] args) {
int[] numbers = {5,6,2,3,4};
System.out.println("Original\n");
Test4.display_contents(numbers,"numbers");
numbers = Test4.sort(numbers);
System.out.println("\nSorted\n");
Test4.display_contents(numbers,"numbers");
}
}
The output of this code is
Original
arrayname[0] = 5.
arrayname[1] = 6.
arrayname[2] = 2.
arrayname[3] = 3.
arrayname[4] = 4.
Sorted
arrayname[0] = 2.
arrayname[1] = 3.
arrayname[2] = 4.
arrayname[3] = 5.
arrayname[4] = 6.
Multidimensional arrays can be used just as single dimensional arrays.
Recall the syntax for creating a two-dimensional array.
data-type[][] arrayname = new data-type[rows][columns];
We can consider a two-dimensional array as a rectangular grid of rows and
columns. The rows are numbered from 0 to rows-1, and the columns are
numbered from 0 to columns-1. Each entry in the array can be accessed by
the array name, the row number, and the column number.
Consider the following code.
public class Test5 {
public static void main(String[] args) {
int[][] board = new int[3][3];
for (int counter=0;counter< 3;counter++) {
String line = "";
for (int counter1=0;counter1< 3;counter1++)
line += board[counter][counter1] + " ";
System.out.println(line);
}
}
}
The output of this code is
0 0 0
0 0 0
0 0 0
Consider this modification
public class Test6 {
public static void main(String[] args) {
String[][] board = new String[3][3];
for (int counter=0;counter< 3;counter++) {
String line = "";
for (int counter1=0;counter1< 3;counter1++)
line += board[counter][counter1] + " ";
System.out.println(line);
}
}
}
The output of this code is
null null null
null null null
null null null
Consider the following modification.
public class Test7 {
public static void main(String[] args) {
String[][] board = new String[3][3];
for (int counter=0;counter<3;counter++) {
String line = "";
for (int counter1=0;counter1<3;counter1++)
line = line.concat(board[counter][counter1]);
System.out.println(line);
}
}
}
What is wrong?
Recall that searching is a fundamental operation related to arrays.
Consider the following problem.
Given the function f(x)=x2-2, find a point, x, where f(x) = 0.
Recall your programming assignment.
Your final program will involve two parts. You will prompt the user for which part they want to
execute.
The first option that you will give the user is to have you sort an array of randomly generated
ints between 0 and a certain number. They will input how many numbers you should generate and
what the maximum number to be generated is. For example, they might ask you to produce 20 random
ints between 0 and 500. You will sort the array and determine the following information.
The minimum number generated.
The maximum number generated.
The number of prime numbers generated.
The number of even numbers generated.
The number of odd numbers generated.
The minimum prime number generated.
The maximum prime number generated.
The sum of all prime numbers generated.
You may produce random numbers using the Math.random() method. It returns a double between 0.0 and
1.0. So to produce a random number between 0 and a certain number, you would multiply the result
of
Math.random() by that number and cast it as an int.
The second option that you will present to the user will be for you to produce a table of
logarithms for them. The table will be stored in a two-dimensional array.
The rows in the array will represent tenths of numbers, that is, the first row will represent 1.0,
the second row will represent 1.1, the third row will represent 1.2, etc. The last row in the
array will represent 10.0.
The columns in the array will represent hundreths of numbers up to 0.09. That is the columns will
represent the numbers 0.00, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, and 0.09.
The user will input what base to use, and each entry in the array will be the logarithm to that
base of the sum of the numbers represented by the row and column.
For example, the first entry in the table will the logarithm to the base of the input number of
1.0 + 0.00.
The logarithms can be computed using the Math.log() method and the change of base formula
When the logarithms are produced, truncate them to 3 decimal places.
Then prompt the user for a filename to use and write the table to that file.