Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Maleficum

macrumors member
Original poster
Jun 29, 2006
31
0
In a Chair
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.
 
Code:
public void setArray(double[] nums)
{  
  for(int i=0; i<nums.length; i++) 
  { 
     numbers[i] = nums[i];
  }
}

should fill the array.

Using setArray within itself is an unrestricted recursive loop. I'm sure you'll learn about using recursion correctly sometime in the future. It's extremely powerful, even when done wrong. ;)
 
You can count up the values in an array using the following
Code:
public static double arrayCounter(double[] theArray){[INDENT]double tot=0.0;
for(int i=0;i<theArray.length;i++){
[INDENT]tot=tot+theArray[i][/INDENT]
}
theArray=null;//to save on system resources.
return tot
[/INDENT]}

This should work perfectly, you can feed it all your arrays and add up those totals if needed.
 
Hey
you're +1 out of your array in getTotal()

Indeed!
Code:
 public double getTotal()
 {
  double total=0;
  for(int i=0; i<numbers.length; i++) 
  { 
          total=total+numbers[i]; //Otherwise you will get an IndexOutOfBounds 
          System.out.println(total);
  }
             
		return total;
	}
 
I assume you were going more for something like:
Code:
public class ArrayTotal
{
	private double[] numbers;

	public ArrayTotal(double[] nums)
	{
		setArray(nums);
	}

	public void setArray(double[] nums)
	{
		numbers = new double[nums.length];
		for(int i=0; i<numbers.length;i++)
		{
			numbers[i]=nums[i];
		}
	}
     
	public double getTotal()
	{
		double total=0.0;
		for(int i=0; i<numbers.length; i++) 
		{ 
			total += numbers[i];
		}
		System.out.println(total);
		return total;
	}

	public String toString( )
	{
		StringBuffer output = new StringBuffer();
		output.append('\n');
		for (int h = 0; h<numbers.length; h++)
		{
			output.append("number ");
			output.append( Integer.toString(h) );
			output.append(" :: ");
			output.append( String.format("%.2f",numbers[h]) );
			output.append('\n');
		}
		return output.toString();
	}
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.