I can't seem to find out why it doesnt work with average or smallest but largest and sum does work.

//constructor

public class DataSet
{


    private int sum = 0;
    private int count = 0;
    private int Smallest;
    private int largest;
    public DataSet()
    {

    }


    public void addValue(int a)
    {
        sum = sum + a;
        count++;
        Smallest = Math.min(Smallest,a);
        largest = Math.max(largest, a);
    }

    public int getSum()
    {
        return sum;
    }


    double getAverage()
    {
        double avg;
        avg = sum / count;
        return avg;
    }

    public int getSmallest()
    {
        return Math.min(Smallest, largest);
    }

    public int getLargest()
    {
        return Math.max(Smallest,largest);

         }

}



//tester

import java.util.Scanner;

class DataSetTester
{
public static void main(String[] args)
{
    Scanner in = new Scanner (System.in);

    System.out.println("Enter first number");
    int w = in.nextInt();

    System.out.println("Enter second number");
    int x = in.nextInt();

    System.out.println("Enter third number");
    int y = in.nextInt();

    System.out.println("Enter fourth number");
    int z = in.nextInt();

    DataSet set1 = new DataSet();

    set1.addValue(w);
    set1.addValue(x);
    set1.addValue(y);
    set1.addValue(z);

    System.out.println("The sum is:" + set1.getSum());
    System.out.println("The avgerage is:" + set1.getAverage());
    System.out.println("The smallest is:" + set1.getSmallest());
    System.out.println("The largest is:" + set1.getLargest());

    System.exit(0);
}
}

First of all, please wrap your code with

tags for more convenient reading. I have placed your main method inside the DataSet tester for now. This is the code again for clearer reading for everyone:

import java.util.Scanner;
public class DataSet {

	private int sum = 0;
	private int count = 0;
	private int Smallest;
	private int largest;

	public DataSet()
	{

	}

	public void addValue(int a)
	{
		sum = sum + a;
		count++;
		Smallest = Math.min(Smallest,a);
		largest = Math.max(largest, a);
	}
	public int getSum()
	{
		return sum;
	}


	double getAverage()
	{
		double avg;
		avg = sum / count;
		return avg;
	}

	public int getSmallest()
	{
		return Math.min(Smallest, largest);
	}

	public int getLargest()
	{
		return Math.max(Smallest,largest);

	}
	
	public static void main(String[] args)
	{
		Scanner in = new Scanner (System.in);

		System.out.println("Enter first number");
		int w = in.nextInt();

		System.out.println("Enter second number");
		int x = in.nextInt();

		System.out.println("Enter third number");
		int y = in.nextInt();

		System.out.println("Enter fourth number");
		int z = in.nextInt();

		DataSet set1 = new DataSet();

		set1.addValue(w);
		set1.addValue(x);
		set1.addValue(y);
		set1.addValue(z);

		System.out.println("The sum is:" + set1.getSum());
		System.out.println("The avgerage is:" + set1.getAverage());
		System.out.println("The smallest is:" + set1.getSmallest());
		System.out.println("The largest is:" + set1.getLargest());

		System.exit(0);
	}

}

Now, regarding your problems - First, you do not initialize smallest and largest, and by Java default those values get initialized to zero. If then you enter 4 numbers that are larger than 0, 0 will still be the smallest. You need to initialize those values to the first integer you get from the user, and on the other values run min/max with this first value. The same problem will happen to you with largest if you insert all the 3 variables to be smaller than zero.
The second problem with the average - the average is causing you trouble because even though it is defined as a double, when dividing two integers, the result will be an integer. What you need to do is to cast one of them to a double, and then the division will result in a double.

double getAverage()
{
   double avg;
   avg = ((double)sum / count);
   return avg;
}

You don't initialise Smallest, so it starts as 0, so if all your values are >0, smallest will always be zero. Best to initialise it to the largest valid int value.

double avg;
avg = sum / count;
Although you have declared avg as double, the calc of sum/count is a calc of two ints, so it's done in integer arithmetic (result is truncated to int). The result is then converted to double, but the decimal places have already been thrown away. You can force the calc to be done in floating point by something as simple as
avg = (sum*1.0)/count;

ps Simultaneous double post with apines - but same answer.

Still confused on how to get small is this what you mean?

public class DataSet
{
	
	
	
	private int sum = 0;
	private int count = 0;
	private int Smallest=-99999;
	private int largest=-99999;

Smallest = Math.min(Smallest,a);

If Smallest is -99999 then for any value of a >= 99999 this will return -99999. Not quite what you need.
Like I said before: Best to initialise it to the largest valid int value.

To elaborate on what JamesCherrill is suggesting - take a look at the Integer class, it has two constant in it - MAX_VALUE and MIN_VALUE.
I agree with him - it is an elegant solution.

Thanks alot finally got it.

No problem :) Glad to help - please mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.