Write a method that creates and initializes a two-dimensional array of double. The size of the array is determined by the arguments of the method, and the initialization values are a range determined by beginning and ending values that are also arguments of the method. Create a second method that will print the array generated by the first method. In main( ) test the methods by creating and printing several different sizes of arrays.
My Code:
import java.util.*;
class ArrayClass{
//creates a static 2-dimensional array of double
static double[][] banana;
void initAr(int f1, int f2, double upper, double lower){
//initializes the array to values f1 and f2
banana = new double[f1][f2];
//creates values to use in the loops
int i, j;
double before,after;
//first iteration goes through one dimension of the array
for(i=0;i<f1;i++){
//second iteration goes through 2nd dimension of the array
for(j=0;j<f2;j++){
//gives before a random integer value
before = new int[rand.nextInt((int) upper))];
//gives after a random integer value
after = new int(rand.nextInt(99));
//makes after a value between 0.01 and 0.99
after = (after/100);
//puts the new after value after the decimal place of before
before = before + after;
//if the new value is smaller than the minimum value, set before to
//equal the minimum value.
if(before < lower){
before = lower;
}
//sets the element of the array to value before.
banana[i][j] = before;
}
}
}
//prints the array. 'nuff said.
void printAr(){
int i,j;
System.out.println("And here is the Matrix Printed:");
for(i=0;i<banana.length;i++){
for(j=0;j<banana[i].length;j++){
System.out.print(banana.[i][j] + " ")}
System.out.println();
}
System.out.println("And .... Done!");
}
}
public class Prob19{
//creates a new object of type ArrayClass and tests the methods in the class with
//values of int: 4, 5 and doubles 100.00 and 50.00
public static void main(String[] args){
ArrayClass test = new ArrayClass();
test.initAr(4,5,100.00,50.00);
test.printAr();
}
}
//end of java code
output from "javac Prob19.java"
[cripps@localhost Chapter 4]$ javac Prob19.java
Prob19.java:12: ']' expected
before = new int[rand.nextInt((int) upper))];
^
Prob19.java:13: '[' expected
after = new int(rand.nextInt(99));
^
Prob19.java:13: illegal start of expression
after = new int(rand.nextInt(99));
^
Prob19.java:30: <identifier> expected
System.out.print(banana.[i][j] + " ")}
^
Prob19.java:30: ')' expected
System.out.print(banana.[i][j] + " ")}
^
5 errors