.



Members Panel
Go Back   Support Forums : OneNetwork.Net > OneNetwork.Net General Forums > Technical Discussion
 

Reply
  #1  
Old 12-20-2010, 05:12 AM
gannon55 gannon55 is offline
Junior Member
 
Join Date: Dec 2010
Posts: 4
Default java coding help needed?

Write a program that reads a sequence of integers into an array and that computes the alternating sum of all elements in the array. For example, if the program is executed with the input data

1 4 9 16 9 7 4 9 11
then it computes

1 - 4 + 9 - 16 + 9 - 7 + 4 - 9 + 11 = - 2

Complete the following class in your solution:

import java.util.Arrays;

/**
This class computes the alternating sum
of a set of data values.
*/
public class DataSet
{
private double[] data;
private int dataSize;

/**
Constructs an empty data set.
*/
public DataSet()
{
final int DATA_LENGTH = 100;
data = new double[DATA_LENGTH];
dataSize = 0;
}

/**
Adds a data value to the data set.
@param x a data value
*/
public void add(double x)
{
if (dataSize == data.length)
data = Arrays.copyOf(data, 2 * data.length);
data[dataSize] = x;
dataSize++;
}

/**
Gets the alternating sum of the added data.
@return sum the sum of the alternating data or 0 if no data has been added
*/
public double alternatingSum()
{
. . .
}
}

Use the following class as your tester class:

/**
This program calculates an alternating sum.
*/
public class AlternatingSumTester
{
public static void main(String[] args)
{
DataSet data = new DataSet();

data.add(1);
data.add(4);
data.add(9);
data.add(16);
data.add(9);
data.add(7);
data.add(4);
data.add(9);
data.add(11);

double sum = data.alternatingSum();
System.out.println("Alternating Sum: " + sum);
System.out.println("Expected: -2.0");
}
}

i've been working on a bunch of codes, and i just cant seem to figure the rest of this one out, help please
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:19 AM.
Copyright ©2000 - 2014, OneNetwork.Net. All Rights Reserved.