New to Java: Problems instantiating an array from class
this is my code
/**
* class with methods for integer.java
*
* @author (Robin)
* @version (1.0)
*/
publicclass arrayManager
{
// instance variables
privateint[] runningArray;
privateint[] savedArray;
privatestaticboolean changed =false;
/**
* Constructor for array of class arrays; initialises array's elements
* from 1 to 10
*/
publicstaticvoid Array()
{
//initialises array with an initialiser list
int[] runningArray ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}
/**
* Constructor for array of class arrays; initialises array's elements
* from 1 to 10
*/
publicstaticvoid DefaultArray()
{
//initialises array with an initialiser list
int[] savedArray ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}
// truncated
/**
* Sorts array from least to greatest
*/
publicvoid leastToLargest(int[] anArray)
{
System.arraycopy(runningArray, 0, savedArray, 0, 10);
int min, temp;
for (int index = 0; index < runningArray.length-1; index++)
{
min = index;
for (int scan = index+1; scan < runningArray.length; scan++)
if (runningArray[scan] < runningArray[min])
min = scan;
// swap values
temp = runningArray[min];
runningArray[min] = runningArray[index];
runningArray[index] = temp;
//
changed =true;
}
}
/**
* reverseArray method reverses the order of numbers of the savedArray,
* the array of elements as inputted by the user
*/
publicvoid reverseArray()
{
int rev = 0;
// need to fix!
if (changed!=true)
leastToLargest(runningArray);
else
for (int scan = savedArray.length-1; scan >= 0; scan--)
{
rev = savedArray[scan];
}
for (int index = 0; index < savedArray.length; index++)
runningArray[index] = rev;
}
//truncated
}
and my test
/**
* Write a description of class integer here.
*
* @author (your name)
* @version (a version number or a date)
*/
publicclass integer
{
publicstaticvoid main (String[] args)
{
Array testArray[] =new Array();
}
}
I thought to instantiate an array through array() and use methods within the arrayManager class that manipulate the runningArray[] and savedArray[], such as leastToGreatest() and reverseArray().
I've been consistently getting a .class error on compile of my main class; any ideas on what I'm doing wrong?
Please post the exception message and stack trace with line numbers.
> Please post the exception message and stack trace
> with line numbers.
I've been attempting to figure out how to utilize the stack trace method to answer your question about my program but have failed to produce any results.
What I can supply is that:
My arrayManager class compiles fine
My main class, integer, cannot use the array() constructor in arrayManager to instantiate a new array() object... that is an array.
/**
* Write a description of class integer here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class integer
{
public static void main (String[] args)
{
Array testArray[] = new Array();
}
}
/**
* class with methods for integer.java
*
* @author (Robin)
* @version (1.0)
*/
public class arrayManager
{
// instance variables
private int[] runningArray;
private int[] savedArray;
private static boolean changed = false;
/**
* Constructor for array of class arrays; initialises array's elements
* from 1 to 10
*/
public static void Array()
{
//initialises array with an initialiser list
int[] runningArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}
/**
* Constructor for array of class arrays; initialises array's elements
* from 1 to 10
*/
public static void DefaultArray()
{
//initialises array with an initialiser list
int[] savedArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}
/**
* Accessor method to get the max value within an array
*/
public int getMax()
{
leastToLargest(runningArray);
int max = runningArray[9];
return max;
}
/**
* Accessor method to get the min value within an array
*/
public int getMin()
{
leastToLargest(runningArray);
int min = runningArray[0];
return min;
}
/**
* Mutator method sets value of an index of runningArray to a user entered number
*/
public void setIndex(int x, int y)
{
runningArray[x] = y;
}
/**
* Sorts array from least to greatest
*/
public void leastToLargest(int[] anArray)
{
System.arraycopy(runningArray, 0, savedArray, 0, 10);
int min, temp;
for (int index = 0; index < runningArray.length-1; index++)
{
min = index;
for (int scan = index+1; scan < runningArray.length; scan++)
if (runningArray[scan] < runningArray[min])
min = scan;
// swap values
temp = runningArray[min];
runningArray[min] = runningArray[index];
runningArray[index] = temp;
//
changed = true;
}
}
/**
* reverseArray method reverses the order of numbers of the savedArray,
* the array of elements as inputted by the user
*/
public void reverseArray()
{
int rev = 0;
// need to fix!
if (changed!=true)
leastToLargest(runningArray);
else
for (int scan = savedArray.length-1; scan >= 0; scan--)
{
rev = savedArray[scan];
}
for (int index = 0; index < savedArray.length; index++)
runningArray[index] = rev;
}
/**
* arraySum method of arrayManager
*/
public int arraySum()
{
int sum = 0;
for (int index = 0; index < runningArray.length; index++)
sum += runningArray[index];
return sum;
}
/**
* arrayAverage method of arrayManager
*/
public int arrayAverage()
{
int sum = 0, quotient = 0;
for (int index = 0; index < runningArray.length; index++)
sum += runningArray[index];
quotient = sum / (runningArray.length);
return quotient;
}
/**
* printArray method of arrayManager that outputs values in each index
*/
public int printArray(int[] anArray)
{
// need to fix
return 000;
}
}
> I've been attempting to figure out how to utilize the
> stack trace method to answer your question about my
> program but have failed to produce any results.
>
> What I can supply is that:
> My arrayManager class compiles fine
> My main class, integer, cannot use the array()
> constructor in arrayManager to instantiate a new
> array() object... that is an array.
Ok, let's try it this way. How do you know that your main class, integer, cannot use the array() constructor? Your first post says, "I've been consistently getting a .class error on compile of my main class..."
What is the .class error? Copy and paste it here. The whole thing.
By the way, you have several lines where you re-use a variable name and I doubt that is what you want. (This won't cure a compile error, but is likely a logic error you need to correct.) For exampleint[] runningArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
I am pretty sure your intent is to assign the instance variable named runningArray to a new array. But the code is declaring a new local variable with the same name and assigning the new array to it, not to the instance variable. You should change this torunningArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
>
> Ok, let's try it this way. How do you know that your
> main class, integer, cannot use the array()
> constructor? Your first post says, "I've been
> consistently getting a .class error on compile of my
> main class..."
>
cannot resolve symbol - class Array
*It might help to note that I'm using BlueJ to write/compile my code.
BTW thanks for the reply
> By the way, you have several lines where you re-use a
> variable name and I doubt that is what you want.
> (This won't cure a compile error, but is likely a
> a logic error you need to correct.) For
> exampleint[] runningArray = {1, 2, 3, 4, 5, 6,
> 7, 8, 9, 10};
I am pretty sure your intent is
> to assign the instance variable named runningArray to
> a new array. But the code is declaring a new local
> variable with the same name and assigning the new
> array to it, not to the instance variable. You
> should change this torunningArray = {1, 2, 3,
> 4, 5, 6, 7, 8, 9, 10};
I tried that but it prohibited me from using the array initializer list. I'm required to initialize the array via a list.
> cannot resolve symbol - class Array
>
You named your class arrayManager, not Array.If your intent is for "public static void Array()" to be an constructor, the name must be the same as the class name and must not include 'static' or any return type - in other words, no 'void'
It is Java convention that class names start with an upper case letter, so you should make it ArrayManager. Following convention will make it easier for you to get help.
> I tried that but it prohibited me from using the
> array initializer list. I'm required to initialize
> the array via a list.
My mistake. It should work usingrunningArray = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
> > cannot resolve symbol - class Array
> >
> You named your class arrayManager, not Array.If
> your intent is for "public static void Array()" to be
> an constructor, the name must be the same as the
> class name and must not include 'static' or any
> return type - in other words, no 'void'
>
> It is Java convention that class names start with an
> upper case letter, so you should make it
> ArrayManager. Following convention will make it
> easier for you to get help.
-slaps forehead-
thanks again.
v/r,
robin