Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 4, 2006, 07:49 AM   #1
prostuff1
macrumors 65816
 
prostuff1's Avatar
 
Join Date: Jul 2005
Location: Don't step into the kawoosh...
Hello again, need some more help with java.

Well, finals week is finally here and I am just trying to finish off my last homework assignment that was given to me friday in my java programming class. Unfortunantly i am having some trouble with this last assignment.

The direction to explain what i need to do are here.

Just in case people dont want to look at that page here is the jist of what the program needs to do.

Quote:
The program will perform the following actions:

1. Ask the user to enter the name of the input file containing the heart rate measurements, and input the file name;

2. Ask the user to enter the name of the output file where the output HTML document will be saved, and input the file name;

3. Open the input file by using a BufferedReader object (see Input file format for a description of the format of the input file);

4. Open (create) the output file by using a PrintWriter object (see Output file format for a description of the format of the output file);

5. Output to the file the appropriate opening HTML tags;

6. Output to the file the header row of the table;

7. For each series of heart rates in the input file, do the following:

1. Input the series of numbers from the input file and store them in an array;

2. Compute the minimum, maximum, and fitness quotient (i.e. twice the smallest heart rate in this series, divided by the sum of the smallest and largest heart rates) for the series;

3. Output one row of the HTML table to the output file, with the fitness quotient, minimum and maximum heart rates, and the list of heart rates;

8. Output to the file the appropriate closing HTML tags;

9. Close the input and output files.

Make sure you handle all possible IOException exceptions generated by the file I/O calls.
We were given a file (sample-input.txt) to work with and make our code work with but it should not be dependent on that file, obviously.

Right now all i have is this:
Code:
import java.util.Scanner;
import java.io.*;
public class Lab8
{
	public static void main(String[] args) throws IOException
	{
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Enter the name of the input file: ");
		String inputFileName = keyboard.nextLine();
		System.out.print("Enter the name of the output file: ");
		String outputFileName = keyboard.nextLine();
		FileReader freader = new FileReader(inputFileName);
		BufferedReader inputfile = new BufferedReader(freader);
		FileWriter fwriter = new FileWriter(outputFileName);
		PrintWriter outputfile = new PrintWriter(fwriter);
		
	}
}
Which is the really easy part. My problems come when i need get stuff from the file. I can create a inputfile.readLine(); and print those just fine but whenever i try to creat an array to store them i have problems (i think mostly cause i dont knwo the length of the file). I am trying to store them so that i can refer to them later.

If anyone can point me in the right direction that would be great.

Thanks
__________________
Vista: It's the blond version of OS's; pretty and fun, just... not functional for everything
prostuff1 is offline   0 Reply With Quote
Old Jun 4, 2006, 09:35 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Don't use a static array. Use an ArrayList or Vector (look them up the Java docs). Both grow as required.
robbieduncan is offline   0 Reply With Quote
Old Jun 4, 2006, 11:13 AM   #3
prostuff1
Thread Starter
macrumors 65816
 
prostuff1's Avatar
 
Join Date: Jul 2005
Location: Don't step into the kawoosh...
Quote:
Originally Posted by robbieduncan
Don't use a static array. Use an ArrayList or Vector (look them up the Java docs). Both grow as required.
OK, thanks for the pointer.

We have not learned that but I dont think she would care if I used that.
__________________
Vista: It's the blond version of OS's; pretty and fun, just... not functional for everything
prostuff1 is offline   0 Reply With Quote
Old Jun 4, 2006, 12:05 PM   #4
prostuff1
Thread Starter
macrumors 65816
 
prostuff1's Avatar
 
Join Date: Jul 2005
Location: Don't step into the kawoosh...
forget that. I forgot one of the <Integer> "tags" so it was giving me crap about that.
__________________
Vista: It's the blond version of OS's; pretty and fun, just... not functional for everything
prostuff1 is offline   0 Reply With Quote
Old Jun 4, 2006, 03:05 PM   #5
prostuff1
Thread Starter
macrumors 65816
 
prostuff1's Avatar
 
Join Date: Jul 2005
Location: Don't step into the kawoosh...
Ok, i am getting there thanks tot he suggestion

Anyway, here is the code i have so far:
Code:
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.text.DecimalFormat;
public class Lab8
{
	public static void main(String[] args) throws IOException
	{
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Enter the name of the input file: ");
		String inputFileName = keyboard.nextLine();
		System.out.print("Enter the name of the output file: ");
		String outputFileName = keyboard.nextLine();
		FileReader freader = new FileReader(inputFileName);
		BufferedReader inputfile = new BufferedReader(freader);
		FileWriter fwriter = new FileWriter(outputFileName);
		PrintWriter outputfile = new PrintWriter(fwriter);
		ArrayList<Integer> numbersInFile = numbersinfile(inputfile);
		int numberOfCases = numbersInFile.get(0);
		int[] numbersPerPerson = numbersperperson(numbersInFile,numberOfCases);
		int[] maxHeartRate = maxheartrate(numbersInFile,numberOfCases,numbersPerPerson);
		int[] minHeartRate = minheartrate(numbersInFile,numberOfCases,numbersPerPerson,maxHeartRate);
		double[] fitnessQuotient = fitnessquotient(numberOfCases,maxHeartRate,minHeartRate);
		for(int index = 0; index < numberOfCases; index++)
		{
			System.out.println(fitnessQuotient[index]);
		}
	}
	private static ArrayList<Integer> numbersinfile(BufferedReader inputfile) throws IOException
	{
		ArrayList<Integer> numbersInFile = new ArrayList<Integer>();
		String line = inputfile.readLine();
		while(line != null)
		{
			numbersInFile.add(Integer.parseInt(line));
			line = inputfile.readLine();
		}
		inputfile.close();
		return numbersInFile;
	}
	private static int[] numbersperperson(ArrayList<Integer> numbersInFile, int numberOfCases)
	{
		int[] numbersPerPerson = new int[numberOfCases];
		int count = 1;
		int index = 0;
		while(index < numberOfCases)
		{
			numbersPerPerson[index] = (numbersInFile.get(count));
			count = (count + numbersInFile.get(count)) + 1;
			index++;
		}
		return numbersPerPerson;
	}
	private static int[] maxheartrate(ArrayList<Integer> numbersInFile,int numberOfCases, int[] numbersPerPerson)
	{
		int[] maxHeartRate = new int[numberOfCases];
		int index = 0;
		int count = 0;
		int max = 0;
		int between = 2;
		while(index < numberOfCases)
		{
			while(count < numbersPerPerson[index] &&  between < numbersInFile.size())
			{
				if(numbersInFile.get(between) > max)
				{
					max = numbersInFile.get(between);
					maxHeartRate[index] = max;
				}
				between++;
				count++;
			}
			max = 0;
			between = between + 1;
			count = 0;
			index++;
		}
		return maxHeartRate;
	}
	private static int[] minheartrate(ArrayList<Integer> numbersInFile,int numberOfCases, int[] numbersPerPerson, int[] maxHeartRate)
	{
		int[] minHeartRate = new int[numberOfCases];
		int index = 0;
		int count = 0;
		int min = maxHeartRate[index];
		int between = 2;
		while(index < numberOfCases)
		{
			while(count < numbersPerPerson[index] &&  between < numbersInFile.size())
			{
				if(numbersInFile.get(between) < maxHeartRate[index])
				{
					min = numbersInFile.get(between);
					maxHeartRate[index] = min;
					minHeartRate[index] = min;
				}
				between++;
				count++;
			}
			min = 0;
			between = between + 1;
			count = 0;
			index++;
		}
		return minHeartRate;
	}
	private static double[] fitnessquotient(int numberOfCases, int[] maxHeartRate,int[] minHeartRate)
	{
		double[] fitnessQuotient = new double[numberOfCases];
		DecimalFormat formatter = new DecimalFormat("0.000");
		for(int index = 0; index < numberOfCases; index++)
		{
			fitnessQuotient[index] = ((2*minHeartRate[index])/(minHeartRate[index] + maxHeartRate[index]));
		}
		return fitnessQuotient;
	}
}
The one thing i can't get quite right is the fitness quotient to display correctly. I need it to be in the format "0.000." I created a decimal format like that but i can't get it to work. This is also the first time I have used deciaml formating. And my book does a piss poor job explaining it.

If someone could exaplin what i need to do that would be great.

Thanks
__________________
Vista: It's the blond version of OS's; pretty and fun, just... not functional for everything
prostuff1 is offline   0 Reply With Quote
Old Jun 4, 2006, 05:34 PM   #6
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by prostuff1
The one thing i can't get quite right is the fitness quotient to display correctly. I need it to be in the format "0.000." I created a decimal format like that but i can't get it to work.
I don't know anything about Java decimal formatters, but it looks like you create the formatter but then never actually use it in any way with your fitness quotient variable. I assume you'd have to connect the formatter to the variable in some way (such as calling a method on the formatter and passing in the number you want formatted)?
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Jun 4, 2006, 07:43 PM   #7
prostuff1
Thread Starter
macrumors 65816
 
prostuff1's Avatar
 
Join Date: Jul 2005
Location: Don't step into the kawoosh...
Thanks everyone for the help. Especially robbieduncan for the suggestion on using an ArrayList. Hopefully the teacher does nto complain to much about that since technically we had not learned it yet.

Anyway, if someone cares here is whhat the final code looks like:
Code:
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.text.DecimalFormat;
public class Lab8
{
	public static void main(String[] args) throws IOException
	{
		Scanner keyboard = new Scanner(System.in);
		int numbers = 2;
		int count2 = 0;
		System.out.print("Enter the name of the input file: ");
		String inputFileName = keyboard.nextLine();
		System.out.println();
		System.out.print("Enter the name of the output file: ");
		String outputFileName = keyboard.nextLine();
		System.out.println();
		FileReader freader = new FileReader(inputFileName);
		BufferedReader inputfile = new BufferedReader(freader);
		FileWriter fwriter = new FileWriter(outputFileName);
		PrintWriter outputfile = new PrintWriter(fwriter);
		ArrayList<Integer> numbersInFile = numbersinfile(inputfile);
		int numberOfCases = numbersInFile.get(0);
		int[] numbersPerPerson = numbersperperson(numbersInFile,numberOfCases);
		int[] maxHeartRate = maxheartrate(numbersInFile,numberOfCases,numbersPerPerson);
		int[] minHeartRate = minheartrate(numbersInFile,numberOfCases,numbersPerPerson);
		double[] fitnessQuotient = fitnessquotient(numberOfCases,maxHeartRate,minHeartRate);
		outputfile.println("<html>");
		outputfile.println("<body>");
		outputfile.println("<table border=1 cellpadding=5");
		outputfile.println("<tr align=center>");
		outputfile.println("<th>Fitness Quotient</th>");
		outputfile.println("<th>Minimum Heart Rate</th>");
		outputfile.println("<th>Maximum Heart Rate</th>");
		outputfile.println("<th>Heart Rate Series</th></tr>");
		for(int index = 0; index < numberOfCases; index++)
		{
			outputfile.print("<tr><td align=center>" + fitnessQuotient[index] + "</td><td align=center>" + minHeartRate[index] + "</td><td align=center>" + maxHeartRate[index] + "</td><td>");
			while(count2 < numbersPerPerson[index] &&  numbers < numbersInFile.size())
			{
				outputfile.print(numbersInFile.get(numbers));
				if(count2 != (numbersPerPerson[index]-1))
				{
					outputfile.print(", ");
				}
				numbers++;
				count2++;
			}
			numbers = numbers + 1;
			count2 = 0;
		}
		outputfile.close();
		outputfile.println("</td></tr></table></body></html>");
		
	}
	private static ArrayList<Integer> numbersinfile(BufferedReader inputfile) throws IOException
	{
		ArrayList<Integer> numbersInFile = new ArrayList<Integer>();
		String line = inputfile.readLine();
		while(line != null)
		{
			numbersInFile.add(Integer.parseInt(line));
			line = inputfile.readLine();
		}
		inputfile.close();
		return numbersInFile;
	}
	private static int[] numbersperperson(ArrayList<Integer> numbersInFile, int numberOfCases)
	{
		int[] numbersPerPerson = new int[numberOfCases];
		int count = 1;
		int index = 0;
		while(index < numberOfCases)
		{
			numbersPerPerson[index] = (numbersInFile.get(count));
			count = (count + numbersInFile.get(count)) + 1;
			index++;
		}
		return numbersPerPerson;
	}
	private static int[] maxheartrate(ArrayList<Integer> numbersInFile,int numberOfCases, int[] numbersPerPerson)
	{
		int[] maxHeartRate = new int[numberOfCases];
		int index = 0;
		int count = 0;
		int max = 0;
		int between = 2;
		while(index < numberOfCases)
		{
			while(count < numbersPerPerson[index] &&  between < numbersInFile.size())
			{
				if(numbersInFile.get(between) > max)
				{
					max = numbersInFile.get(between);
					maxHeartRate[index] = max;
				}
				between++;
				count++;
			}
			max = 0;
			between = between + 1;
			count = 0;
			index++;
		}
		return maxHeartRate;
	}
	private static int[] minheartrate(ArrayList<Integer> numbersInFile,int numberOfCases, int[] numbersPerPerson)
	{
		int[] minHeartRate = new int[numberOfCases];
		int index = 0;
		int count = 0;
		int min = 1000;
		int between = 2;
		while(index < numberOfCases)
		{
			while(count < numbersPerPerson[index] &&  between < numbersInFile.size())
			{
				if(numbersInFile.get(between) < min)
				{
					min = numbersInFile.get(between);
					minHeartRate[index] = min;
				}
				between++;
				count++;
			}
			min = 1000;
			between = between + 1;
			count = 0;
			index++;
		}
		return minHeartRate;
	}
	private static double[] fitnessquotient(int numberOfCases, int[] maxHeartRate,int[] minHeartRate)
	{
		DecimalFormat formatter = new DecimalFormat("0.000");
		double[] fitnessQuotient = new double[numberOfCases];
		for(int index = 0; index < numberOfCases; index++)
		{	
			double fitnessquotient = (double)((double)(2*(double)minHeartRate[index])/(double)((double)minHeartRate[index] + (double)maxHeartRate[index]));
			fitnessQuotient[index] = Double.parseDouble(formatter.format(fitnessquotient));
		}
		return fitnessQuotient;
	}
}
To get this to work you need to download a file from the link in my first post.

Again, thanks for the help.
__________________
Vista: It's the blond version of OS's; pretty and fun, just... not functional for everything
prostuff1 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Some apps prompt to install Java 6 even when Java 7 is installed MacMan988 OS X Mavericks (10.9) 1 Apr 18, 2014 07:56 AM
Java 6 or Java 7 for OSX Mavericks for your Macbook Pro? Starfyre MacBook Pro 3 Dec 22, 2013 03:49 PM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 03:48 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 09:13 AM
Installed Java 7.07 but doesn't show up in Java preferences? -mattias- OS X 10.8 Mountain Lion 21 Aug 31, 2012 04:49 PM

Forum Jump

All times are GMT -5. The time now is 02:26 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC