PDA

Click to See Complete Forum and Search --> : array and methods ??


lindsey
Dec 7th, 2002, 11:10 AM
:( hi i am learning java, so forgive me. i am trying to do a sum requiring 2 arrays. the sum involves the average, of arrays. i need also to use a method for calculating the array by the power of 2 for each array ect. But i will try and work them out myself if any one can confirm or put me on the right track. As i don't know how to call these methods in my answer.

here is my code, if i declared it wrong please advise

import java.io.*;

public class LinearLeast
{ // start of new class

public static void main(String[]args)
{ //start of main method


//values for array x
double [] arrX ={-4.91,-3.84,-2.41,-2.62,-3.78,-0.52,-1.83,-2.01,0.28,1.08,
-0.94,0.59,0.69,3.04,1.01,3.60,4.53,5.13,4.43,4.12};

// values for array y
double [] arrY ={-8.18,-7.49,-7.11,-6.15,-5.62,-3.30,-2.05,-2.83,-1.16,0.52,
0.21,1.73,3.96,4.26,5.75,6.67,7.70,7.31,9.05,10.05};


//defines the avarage method
public double average (double arr[])
{
double sumA =0;
double AverageB =0;
if (arr.lenght >=1)
{
for(int i=0; i< arr.lenght; i++)
{
sumA +=arr[i];
}
AverageB = sumA / arr.lenght;
}


}
System.out.println("array x "+arrX+"\n array y"+arrY);
System.out.println(" average of array X "+AverageB);
System.out.println(" average of array Y "+AverageB);

} //end of main method
} // end of class



thanks if you can help

HairyDave
Dec 8th, 2002, 01:07 PM
If I am getting you right, you need to declare your class properly.

A class can have a number of methods and variables (you probably know this but its easier to explain this way). These methods and variables can be accessed by external entities if they are public access methods/variables - they are declared as 'public double myFunction()' or something like that.

To run a java program, you need an entry point. This is usually the 'main' method:


public static void main(String[] args)


So, a java program that requires no objects etc could just be:


public class MyClass
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}


This is simple. However, you can add functionality to the class by adding methods and variables etc.


public class MyClass
{
public int myVariable;

public myClass(int initNum)
{
myVariable = initNum;
}

public void Add(int addNumber)
{
myVariable = myVariable + addNumber;
}

public static void main(String[] args)
{
MyClass mCl = new MyClass(8)

mCl.Add(7);

System.out.println("Adding 7 to MyClass: " + mCl.myVariable);
}
}


This program starts up by creating a new instance of the class MyClass, then calling the MyClass member method 'Add()'. It then accesses the variable myVariable to print out the answer.

To call member methods you need an instance of the class - unless the member is static, in which case it can be used MyClass.StaticMethod() - but we wont get into that now.

Beyond the simple example above is the use of private members - which cannot be accessed from outside of the class itself. For instance, in the case of the above class, you could make the myVariable variable private. You couldn't access the variable like in the above - mCl.myVariable - you would have to write a Get method or something similar.

Does this quick guide help at all? I dont want to go too far as you said you wanted to work most of it out yourself.

HD

CornedBee
Dec 8th, 2002, 01:21 PM
A tip: put your code into tags, it is easier to read this way.