d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Need A Java Programmer > Very Basic
Add Reply New Topic New Poll
Member
Posts: 10,660
Joined: Oct 9 2010
Gold: 373.00
Sep 20 2013 11:48am
I will not be able to get to my computer much this weekend and I need a very basic program to be written.
You should have an understanding of arrays and file input/output like I said it is very basic and shouldn't take very long.
If you can help PM me and I'll give you further instruction and we can work out a payment.
Banned
Posts: 4,377
Joined: Dec 25 2012
Gold: 13,953.01
Warn: 10%
Sep 20 2013 12:04pm
sure, post details
Member
Posts: 10,660
Joined: Oct 9 2010
Gold: 373.00
Sep 20 2013 01:41pm
being helped

thanks
Banned
Posts: 4,377
Joined: Dec 25 2012
Gold: 13,953.01
Warn: 10%
Sep 21 2013 04:05am
your inbox is full. I optimized the code a bit (I'm a bit rusty on Java, so its kinda sloppy, but whatever). Here's the new stuff.

It isn't as readable as it could be, but I couldn't stand writing 4 methods that did almost the same thing. I started doing that and changed my mind mid-way; hence, the odd variable names (isHigh, etc.)

Code
import java.util.*;
import java.io.*;

public class TempExercise
{
private static final int NUMMONTHS = 12;
private static final int MAXDAYS = 31;
private static final String DELIMS = "[ ]";
private static final String FILENAME = "Input.txt";


private static boolean compareTemps(double currentTemp, double high, boolean isHigh)
{
return ((currentTemp > high) && isHigh) || ((currentTemp < high) && !isHigh);
}

private static double averageComp(String[][] dataArray, boolean isHigh)
{
double sumHigh = 0;
double resetHigh = Double.POSITIVE_INFINITY;
if (isHigh) resetHigh *= -1.0;
double currentTemp;
double monthHigh;

for (int i = 0; i < NUMMONTHS; i++)
{
monthHigh = resetHigh;
for (int j = 0; j < dataArray.length; j++)
{
if (dataArray[j][i] == null) break;
if (dataArray[j][i].equals("")) continue;
currentTemp = Double.parseDouble(dataArray[j][i]);
if (compareTemps(currentTemp,monthHigh,isHigh)) monthHigh = currentTemp;
}
sumHigh += monthHigh;
}
return sumHigh/NUMMONTHS;
}

private static double indexComp(String[][] dataArray, boolean isHigh)
{
double high = Double.POSITIVE_INFINITY;
if (isHigh) high *= -1.0;
double currentTemp;

for (int i = 0; i < NUMMONTHS; i++)
{
for (int j = 0; j < dataArray.length; j++)
{
if (dataArray[j][i] == null) break;
if (dataArray[j][i].equals("")) continue;
currentTemp = Double.parseDouble(dataArray[j][i]);
if (compareTemps(currentTemp,high,isHigh)) high = currentTemp;
}
}
return high;
}

private static double averageHigh(String[][] dataArray)
{
return averageComp(dataArray, true);
}
private static double averageLow(String[][] dataArray)
{
return averageComp(dataArray, false);
}
private static double indexHigh(String[][] dataArray)
{
return indexComp(dataArray, true);
}
private static double indexLow(String[][] dataArray)
{
return indexComp(dataArray, false);
}


private static boolean getData(String[][] dataArray) throws FileNotFoundException
{
File file = new File(FILENAME);
if (!file.exists())
{
System.out.println("Invalid file name");
return false;
}

Scanner inFile = new Scanner( file );

if (!inFile.hasNext())
{
System.out.println("File is empty");
return false;
}
inFile.nextLine();

String line;
int lineNum = 0;
String lineArray[];
while (inFile.hasNext())
{
if (lineNum >= MAXDAYS)
{
System.out.println("Invalid number of months");
return false;
}
line = inFile.nextLine(); // read the next line
lineArray = line.split(DELIMS);

int lineIndex = 0;
for (int i = 0; i < NUMMONTHS; i++)
{
dataArray[lineNum][i] = lineArray[lineIndex];

if (!lineArray[lineIndex].equals(""))
{
lineIndex++;
continue;
}
while (lineArray[lineIndex].equals(""))
lineIndex++;
}

lineNum++;
}
return true;
}


public static void main ( String [] args ) throws FileNotFoundException
{
String[][] dataArray = new String[MAXDAYS][NUMMONTHS];
if (!getData(dataArray))
{
System.out.println("File reading failed");
return;
}
System.out.println("Average High");
System.out.println(averageHigh(dataArray));
System.out.println("Average Low");
System.out.println(averageLow(dataArray));
System.out.println("Index High");
System.out.println(indexHigh(dataArray));
System.out.println("Index Low");
System.out.println(indexLow(dataArray));

}

}


This post was edited by zackill4 on Sep 21 2013 04:14am
Go Back To Homework Help Topic List
Add Reply New Topic New Poll