Code:
class ArrayTotal
{
private double[] numbers;
public ArrayTotal(double[] nums)
{
numbers = new double[nums.length];
for(int i=0; i<numbers.length;i++)
{
numbers[i]=nums[i];
}
}
[b] public void setArray(double[] nums)
{
setArray(numbers);
}
[/b]
public double getTotal()
{
double total=0;
for(int i=0; i<numbers.length; i++)
{
total=numbers[i]+numbers[i+1];
System.out.println(total);
}
return total;
}
public String toString( )
{
String output="";
for (int h = 0; h<numbers.length; h++)
{
output+="number "+h + " :: " +String.format("%.2f",numbers[h])+"\n";
}
return "\n"+output;
}
}
public class Lab14a
{
public static void main( String args[] )
{
ArrayTotal test = new ArrayTotal(new double[]{93.4,100.0,90.0});
System.out.println(test);
System.out.println("total = "+String.format("%.2f",test.getTotal())+"\n\n");
test.setArray(new double[]{50.0,100.0,80.0});
System.out.println(test);
System.out.println("total = "+String.format("%.2f",test.getTotal())+"\n\n");
test.setArray(new double[]{93.4,-90.0,90.0});
System.out.println(test);
System.out.println("total = "+String.format("%.2f",test.getTotal())+"\n\n");
test.setArray(new double[]{1,2,3,4,5,6,7,8,9});
System.out.println(test);
System.out.println("total = "+String.format("%.2f",test.getTotal())+"\n\n");
}
}
The point of this lab is to pull the numbers from the double[] arrays at the bottom of the code and total them up. I need help with what exactly to put in the bolded area (setArray). I tried what I have, but it pulled an infinite loop. I know my code in getTotal() is in correct, but I'll be fixing that later.