关于JAVA的练习

来源:百度知道 编辑:UC知道 时间:2024/09/22 13:42:21
Write a method that creates, initializes, and returns an array of type double. The method will have 3 arguments: the size of the array, the starting value, and the ending value. Initialize each value in the array to use the specified range.
Write a second method that accepts a double[] and prints it.
Exercise the methods from main.
Split the previous into 2 classes, one of which has the main method, the other of which has all the others
In the non-main class, have everything NOT static
Have the main method instantiate the other class and exercise its methods using the object reference。

第一个程序是:array.java
在同一个类里调用方法
public class array
{
public double[] createArray(int size,double startValue,double endValue)
{
double[] array=new double[size];
array[0]=startValue;
for(int i=1;i<size-1;i++)
{
array[i]=array[i-1]+1;
}
array[size-1]=endValue;
return array;
}

public void printArray(double[] array)
{
for(int i=0;i<array.length;i++)
{
System.out.println(array[i]);
}
}
public static void main(String[] args)
{
array a=new array();
double[] test=a.createArray(3, 1, 3);
a.printArray(test);
}
}

第二个程序是:array1.java
在不同类之间用对象声明调用方法
class array0
{
public double[] createArray(int size,double startValue,double endValue)
{
double[] array=new double[size];
array[0]=startValue;
for(int i=1;i<size-1;i++)
{
array[i]=array[