Pull data from file into constructors and return low,high, ave, total

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 3479 Views - Last Post: 07 December 2011 - 06:00 AM Rate Topic: -----

#1 jmtravismr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-December 11

Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 08:26 PM

here is what I have so far, the assignment is
:Write a class with a constructor that accepts a file name as its argument. assume the file contains a series of numbers, each written on a sepeate line. the class should read the contents of the file into an array, and then displays the followin data: lowest number in the array, highest number in the array, total of the numbers, average of the numbers. When I leave getHighestNumber(), getLowestNumber(), etc blank my numbers data isnt transfered to the other methods, but when I apply (double numbers) I get an error that double cannot be dereferenced, any help?

public class NumberAnalysis
{
public static void main(String[] args)
{
double[] numbers;

numbers = getArray();
 
}
/**
getArray method
@return A reference to an array of doubles read f.
*/

public static double[] getArray()
{
final int SIZE = 12;
double[] numbers = new double[SIZE];
int index = 0;

File file = new File("Numbers.txt");
Scanner inputFile = new Scanner(file);

while (inputFile.hasNext() && index < numbers.length)
{
numbers[index] = inputFile.nextInt();
index++;
}

inputFile.close(); 

return numbers;

}
/**
getLowest method
@return the lowest value in the array
*/
public double getLowestNumber(double numbers)
{
double lowest;
lowest = numbers[0];

for (int index = 1; index < numbers.length; index++)
{
if (numbers[index] < lowest)
lowest = numbers[index];
}
return lowest;
}
/**
getHighest method 
@return The highest value stored in the array
*/
public double getHighestNumber(double numbers)
{
double highest;
highest = numbers[0];

for (int index = 1; index < numbers.length; index++)
{
if (sales[index] > highest)
highest = numbers[index];
}
return highest;
}
/**
getTotal
@return the total of all the elements in the stored array
*/
public double getTotal(double numbers)
{
double total = 0.0;

for (int index = 0; index < numbers.length; index++)
total += numbers[index];

return total;
}
/**
getAverage
@return the average of all the elements in the stored array
*/
public double getAverage(double numbers)
{
return getTotal() / numbers.length;
}
}

This post has been edited by smohd: 06 December 2011 - 08:32 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: Pull data from file into constructors and return low,high, ave, total

#2 jmtravismr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-December 11

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 08:36 PM

Oops, i forgot code tags, thanks Smohd for adding them
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 09:48 PM

And indent your code properly, it is a nightmare to read

public class NumberAnalysis
{
	public static void main(String[] args)
	{
		double[] numbers;

		numbers = getArray();

	}
	/**
getArray method
@return A reference to an array of doubles read f.
	 */

	public static double[] getArray()
	{
		final int SIZE = 12;
		double[] numbers = new double[SIZE];
		int index = 0;

		File file = new File("Numbers.txt");
		Scanner inputFile = new Scanner(file);

		while (inputFile.hasNext() && index < numbers.length)
		{
			numbers[index] = inputFile.nextInt();
			index++;
		}

		inputFile.close(); 

		return numbers;

	}
	/**
getLowest method
@return the lowest value in the array
	 */
	public double getLowestNumber(double numbers)
	{
		double lowest;
		lowest = numbers[0];

		for (int index = 1; index < numbers.length; index++)
		{
			if (numbers[index] < lowest)
				lowest = numbers[index];
		}
		return lowest;
	}
	/**
getHighest method 
@return The highest value stored in the array
	 */
	public double getHighestNumber(double numbers)
	{
		double highest;
		highest = numbers[0];

		for (int index = 1; index < numbers.length; index++)
		{
			if (sales[index] > highest)
				highest = numbers[index];
		}
		return highest;
	}
	/**
getTotal
@return the total of all the elements in the stored array
	 */
	public double getTotal(double numbers)
	{
		double total = 0.0;

		for (int index = 0; index < numbers.length; index++)
			total += numbers[index];

		return total;
	}
	/**
getAverage
@return the average of all the elements in the stored array
	 */
	public double getAverage(double numbers)
	{
		return getTotal() / numbers.length;
	}
}



Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 09:55 PM

First get ride of all your static methods (but main()). Java is an OO language, you should create a NumberAnalysis object,to which you can pass the array as parameter to the constructor, and then call this object methods.

To read the file, do it in the main() method, the NumberAnalysis class should be independant from where the data is coming from: user input, database or a flat file as in your case

Happy OO coding :^:
Was This Post Helpful? 0
  • +
  • -

#5 jmtravismr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-December 11

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 09:57 PM

View Postpbl, on 06 December 2011 - 09:48 PM, said:

And indent your code properly, it is a nightmare to read


I apologize, I copied it to yahoo, and then onto here, it lost its indents.

I have made a ton of changes and am now only getting issues when I try to call my secondary methods into my main method to display information.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
Author: Travis
Date: 12/06/11
This program takes values from another file and displays the lowest, highest, average, and total of these values.
*/



public class DataConstructor
{

	public static void main(String[] args)
	{
	
		double[] data;

		data = getArray();
		
		getAverage();
		getTotal();
		getLowest();
		getHighest();

	
	}
/**
getArray method
@return A reference to an array of doubles read f.
*/

	public static double[] getArray()
	{
		final int SIZE = 12;
		double[] data = new double[SIZE];
		int index = 0;

		File file = new File("numbers.txt");
		Scanner inputFile = new Scanner(file);

		while (inputFile.hasNext() && index < data.length)
		{
		data[index] = inputFile.nextInt();
		index++;
		}

	inputFile.close(); 

	return data;
	}
	
	public double getTotal(double[] data)
	{
		double total = 0.0; //accumulator
	
		for (int index = 0; index < data.length; index++)
		total += data[index];
		System.out.println("Total: " + total);
		return total;
	}
	
	public double getAverage(double[] data,double total)
	{
		double average = total / data.length;
		System.out.println("Average: " + average);
		return average;
	}
	
	public double getHighest(double[] data)
	{
		double highest = data[0];
		for (int index = 1; index < data.length; index++)
		{
			if (data[index] > highest)
				highest= data[index];
		}
		System.out.println("Highest: " + highest);
		return highest;
	}
	
	public double getLowest(double[] data)
	{
		double lowest = data[0];
		
		for (int index = 1; index < data.length; index++)
		{
			if (data[index] < lowest)
				lowest = data[index];
		}
		System.out.println("Lowest: " + lowest);
		return lowest;
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 10:11 PM

So, you cut & paste code from the net, code that does not work, and now you ask us to fix it ?

:v:

public class DataConstructor
{

	public static void main(String[] args)
	{
	
		double[] data;

		data = getArray();
		
		getAverage();  // this does not make sense
		getTotal();    // "
		getLowest();   // "
		getHighest();  // "

	
	}



Where is the constructor of DataConstructor ?
We will be glad to help you with the code you wrote
Was This Post Helpful? 0
  • +
  • -

#7 jmtravismr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-December 11

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 10:13 PM

View Postpbl, on 06 December 2011 - 10:11 PM, said:

So, you cut & paste code from the net, code that does not work, and now you ask us to fix it ?

:v:

public class DataConstructor
{

	public static void main(String[] args)
	{
	
		double[] data;

		data = getArray();
		
		getAverage();  // this does not make sense
		getTotal();    // "
		getLowest();   // "
		getHighest();  // "

	
	}



Where is the constructor of DataConstructor ?
We will be glad to help you with the code you wrote


what makes this code from the net? i challenge you to find it sir.

The parts you labeled as not making sense are from my book, it says to post them that way to make the system output message from my other methods display.
Was This Post Helpful? 0
  • +
  • -

#8 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 10:17 PM

Let's give him the benefit of the doubt here. I think he DID write it, but he tried to get help on yahoo first, and then came here to dream-in-code. So, Yahoo answers has no code tags and deletes your indentations after you post it. So, he lazily copied from yahoo back to here instead of from his editor. Am I right?

Either way you should probably have 2 classes. 1 as pbl suggests should handle the array regardless of input, and the other should take a filename in the constructor as your homework assignment asks you to do and use the other class to get the results.

Edit: fixed some minor spelling errors.

This post has been edited by Sheph: 06 December 2011 - 10:17 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 10:18 PM

Sorry I misinterpret that

Quote

I apologize, I copied it to yahoo, and then onto here, it lost its indents.


OK give me 5 minutes
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10477
  • View blog
  • Posts: 38,835
  • Joined: 27-December 08

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 10:20 PM

@pbl: I think he is saying he cross-posted, not copy/pasted.

Quote

The parts you labeled as not making sense are from my book, it says to post them that way to make the system output message from my other methods display.

Your methods return values, not print them. So you don't assign the returned values to variables, use them in calculations, print them, etc. Therefore, having those calls doesn't make sense b/c you aren't using the values.
Was This Post Helpful? 0
  • +
  • -

#11 jmtravismr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-December 11

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 10:23 PM

View Postmacosxnerd101, on 06 December 2011 - 10:20 PM, said:

@pbl: I think he is saying he cross-posted, not copy/pasted.

Quote

The parts you labeled as not making sense are from my book, it says to post them that way to make the system output message from my other methods display.

Your methods return values, not print them. So you don't assign the returned values to variables, use them in calculations, print them, etc. Therefore, having those calls doesn't make sense b/c you aren't using the values.


I think I understand, how to I make the system out commands display then, if they are shown in methods outside of main?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10477
  • View blog
  • Posts: 38,835
  • Joined: 27-December 08

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 10:25 PM

Pass the values returned from the methods to the System.out.println() calls.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 10:27 PM

That should put you on the right track
import java.io.*;
import java.util.Scanner;


public class DataConstructor
{
	double[] data;

	DataConstructor(double[] data) {
		this.data = data;
	}
	public static void main(String[] args)
	{
		double[] data = getArray();
		DataConstructor dc = new DataConstructor(data);

		System.out.println("Average: " + dc.getAverage());
		System.out.println("  Total: " + dc.getTotal());
		System.out.println("    Min: " + dc.getLowest());
		System.out.println("    Max: " + dc.getHighest());


	}
	/**
getArray method
@return A reference to an array of doubles read f.
	 */

	public static double[] getArray()
	{
		final int SIZE = 12;
		double[] data = new double[SIZE];
		int index = 0;

		File file = new File("numbers.txt");
		try {
			Scanner inputFile = new Scanner(file);

			while (inputFile.hasNextDouble() && index < data.length)
			{
				data[index] = inputFile.nextDouble();
				index++;
			}

			inputFile.close();
		}
		catch(IOException e) {
			return new double[0];
		}

		return data;
	}

	public double getTotal()
	{
		double total = 0.0; //accumulator

		for (int index = 0; index < data.length; index++)
			total += data[index];
		System.out.println("Total: " + total);
		return total;
	}

	public double getAverage()
	{
		double average = getTotal() / data.length;
		System.out.println("Average: " + average);
		return average;
	}

	public double getHighest()
	{
		double highest = data[0];
		for (int index = 1; index < data.length; index++)
		{
			if (data[index] > highest)
				highest= data[index];
		}
		System.out.println("Highest: " + highest);
		return highest;
	}

	public double getLowest()
	{
		double lowest = data[0];

		for (int index = 1; index < data.length; index++)
		{
			if (data[index] < lowest)
				lowest = data[index];
		}
		System.out.println("Lowest: " + lowest);
		return lowest;
	}
}



Was This Post Helpful? 1
  • +
  • -

#14 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 10:30 PM

But his assignment still asks for a class with a constructor that takes a filename. Meaning, that you should find a way to change your static method getArray() into your constructor.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Pull data from file into constructors and return low,high, ave, total

Posted 06 December 2011 - 10:36 PM

I am not the one who invented the getArray() static method
How about that ?

import java.io.*;
import java.util.Scanner;


public class DataConstructor
{
	final int SIZE = 12;
	double[] data;

	DataConstructor(String filename) {
		
		data = new double[SIZE];
		int index = 0;

		File file = new File("numbers.txt");
		try {
			Scanner inputFile = new Scanner(file);

			while (inputFile.hasNextDouble() && index < data.length)
			{
				data[index] = inputFile.nextDouble();
				index++;
			}

			inputFile.close();
		}
		catch(IOException e) {
			data = new double[0];
		}

	}
	public static void main(String[] args)
	{
		DataConstructor dc = new DataConstructor("filename.txt");

		System.out.println("Average: " + dc.getAverage());
		System.out.println("  Total: " + dc.getTotal());
		System.out.println("    Min: " + dc.getLowest());
		System.out.println("    Max: " + dc.getHighest());


	}

	public double getTotal()
	{
		double total = 0.0; //accumulator

		for (int index = 0; index < data.length; index++)
			total += data[index];
		System.out.println("Total: " + total);
		return total;
	}

	public double getAverage()
	{
		double average = getTotal() / data.length;
		System.out.println("Average: " + average);
		return average;
	}

	public double getHighest()
	{
		double highest = data[0];
		for (int index = 1; index < data.length; index++)
		{
			if (data[index] > highest)
				highest= data[index];
		}
		System.out.println("Highest: " + highest);
		return highest;
	}

	public double getLowest()
	{
		double lowest = data[0];

		for (int index = 1; index < data.length; index++)
		{
			if (data[index] < lowest)
				lowest = data[index];
		}
		System.out.println("Lowest: " + lowest);
		return lowest;
	}
}



Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3