Hello, I have the following java code that's been giving me trouble to figure out. I'm in my second semester of java, so I should figure this out by now. But, it's been a little tough for me. Before I get to the code, we just started learning about objects. I do not fully understand the concept of this. Can someone explain it in really simple language. I know there are three different kinds of objects, and and you can have a class within the main class. But the second class has to be static?
For example, if I have a main class name "First", a.k.a. "public class First {} ", but I want a second class, the second class to create an object, the second class has to be "static class Second {}"? Ok, in this program, the second class creates an object, of which I can call it in the main program in the main class.
Here is the assignment:
Data Structure A data structure is a class that holds fields of various type as a whole structure. Then this class can be use as a single entity, i.e. in ArrayList. There are different methods to create data structure, but the one that we prefer here has this properties: All fields should be final value of the fields should be pass/set using one of the constructors no set method This is an example: public final class MyPoint { public final double x, y; public MyPoint (final int p_x, final int p_y) { x = p_x; y = p_y; } public MyPoint (final int p_x) { this(p_x, 0); } } Task Your task is exercise 3.2.31 of the text book. You need to read data file (DJIA.csv) and store each of the records into a data type called Entry. To store all records in memory, use an ArrayList of your entries. The task is to find the average of all quantities (Open, High, Low, Close, Volume, Adj. Close) over various periods of time. The first entry on each record is the date. Read two dates from the standard input stream, compute the average of all the data between the two dates, and print them to the standard output stream. Repeat this task for every pair of dates in the input. To create a new inner class use this template: public final class StockPrice { static class Entry { public final ...; ... public Entry (...) { ... } } public static void main (String[] args) { ... } } You need to read dates from the input stream and store them in a Date object. Use SimpleDateFormat to parse a string of date. Because all of the dates are in the past, we set the 2 digit year start to 100 years before the current time. This is a simple code to do so: final Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, -100); final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy"); ... df.set2DigitYearStart(cal.getTime()); ... final Date d = df.parse( your_string ); ... The only difference of this task with your previous tasks is reading data from a file. To do so, simply create a Scanner object by passing a File object instead of system.in. You need to take care of the IOException that can be occur during creating of Scanner object with a File object. This is a sample code to do so: public static void main(final String[] args) throws IOException, ParseException { final Scanner input = new Scanner( new File ("Filename") ); ... } The argument of the program should be the data filename. On standard input, each pair of dates should use for computing the averages. be aware that first seven tokens in the data file are the name of the column and you should skip them in your programs (read them, but do not store them). please take a look at sample input and output and write your program with the same format of IO. Sample Input: command: java StockPrice DJIA.csv standard input: 16-Mar-06 17-Mar-06 15-Mar-06 17-Mar-06 12-Mar-28 15-Mar-28 29-Feb-44 12-May-71 Sample Output: 11252.96,11309.87,11214.65,11266.45,2420899968.00,11266.45 11218.56,11292.67,11175.51,11247.55,2378266624.00,11247.55 No Data 511.32,515.08,507.81,511.42,4215199.18,511.42 Notes: use BigDecimal for all numbers round numbers to two digits after decimal points (use scale 2 and ROUND_HALF_UP, see BigDecimal documentation of divide command) Dates may not be in order in DJIA.cvs but on the input first date will be always before second date use instance method compareTo of the Date class to compare dates. see API documentation. if no data avalable for specified range or the range is wrong, write "No Data" on the output. use static inner classes (for Entry) to construct your data type Notes on Static vs Instance.
and here is my code that I wrote in eclipse. I am not getting the lines to print out individually, and I don't think I am using proper objects to create this program. Remember, you can write this program using normal return methods, but the instructor wants us to use as much objects as possible. Also, I couldn't get the toString() object to print out my statements in the way I want it to print out. Here is the code:
import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.Scanner; /* * Author: * Course: * Project: StockPrice.java */ public class StockPrice { static String s = ""; /** * No java docs */ private static final int NUMBER_NEGATIVE_100 = -100; /** * No java docs */ private static final int NUMBER_6 = 6; /** * No java docs */ private static final int NUMBER_5 = 5; /** * No java docs */ private static final int NUMBER_4 = 4; /** * No java docs */ private static final int NUMBER_3 = 3; /** * No java docs */ private static final int NUMBER_2 = 2; /** * No java docs */ private static final int NUMBER_1 = 1; /** * No java docs */ private static final int NUMBER_0 = 0; static class Entry { public final ArrayList<Date> date = new ArrayList<Date>(); public final ArrayList<BigDecimal> open = new ArrayList<BigDecimal>(); public final ArrayList<BigDecimal> high = new ArrayList<BigDecimal>(); public final ArrayList<BigDecimal> low = new ArrayList<BigDecimal>(); public final ArrayList<BigDecimal> close = new ArrayList<BigDecimal>(); public final ArrayList<BigInteger> volume = new ArrayList<BigInteger>(); public final ArrayList<BigDecimal> adjclose = new ArrayList<BigDecimal>(); public void addEntry(final Date nDate, final BigDecimal nOpen, final BigDecimal nHigh, final BigDecimal nLow, final BigDecimal nClose, final BigInteger nVolume, final BigDecimal nAdjclose) { Date dateVariable; BigDecimal openVariable; BigDecimal highVariable; BigDecimal lowVariable; BigDecimal closeVariable; BigInteger volumeVariable; BigDecimal adjcloseVariable; dateVariable = nDate; openVariable = nOpen; highVariable = nHigh; lowVariable = nLow; closeVariable = nClose; volumeVariable = nVolume; adjcloseVariable = nAdjclose; date.add(dateVariable); open.add(openVariable); high.add(highVariable); low.add(lowVariable); close.add(closeVariable); volume.add(volumeVariable); adjclose.add(adjcloseVariable); } public boolean getTest(final String n) throws ParseException { final int length1 = this.date.size(); boolean truefalse = false; for (int a = NUMBER_0; a < length1; a++) { final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy"); final Date dateString = df.parse(n); if ((dateString.compareTo(date.get(a))) == NUMBER_0) { truefalse = true; break; } } return truefalse; } public int position(final String n) throws ParseException { final int length1 = this.date.size(); int index = NUMBER_0; for (int a = NUMBER_0; a < length1; a++) { final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy"); final Date dateString = df.parse(n); if ((dateString.compareTo(date.get(a))) == NUMBER_0) { index = a; break; } } return index; } public String average(final int a, final int b) { final int total = (a - b) + NUMBER_1; final Double overavgDouble = 0.0; final Double highavgDouble = 0.0; final Double lowavgDouble = 0.0; final Double closeavgDouble = 0.0; final int volumeavgInteger = NUMBER_0; final Double adjcloseavgDouble = 0.0; BigDecimal openavg = new BigDecimal(overavgDouble); BigDecimal highavg = new BigDecimal(highavgDouble); BigDecimal lowavg = new BigDecimal(lowavgDouble); BigDecimal closeavg = new BigDecimal(closeavgDouble); BigInteger volumeavg = BigInteger.valueOf(volumeavgInteger); BigDecimal adjcloseavg = new BigDecimal(adjcloseavgDouble); for (int c = b; c <= a; c++) { openavg = openavg.add(this.open.get(c)); highavg = highavg.add(this.high.get(c)); lowavg = lowavg.add(this.low.get(c)); closeavg = closeavg.add(this.close.get(c)); volumeavg = volumeavg.add(this.volume.get(c)); adjcloseavg = adjcloseavg.add(this.adjclose.get(c)); } final BigDecimal total1 = new BigDecimal(total); final BigInteger total2 = BigInteger.valueOf(total); openavg = openavg.divide(total1, NUMBER_2, RoundingMode.HALF_UP); highavg = highavg.divide(total1, NUMBER_2, RoundingMode.HALF_UP); lowavg = lowavg.divide(total1, NUMBER_2, RoundingMode.HALF_UP); closeavg = closeavg.divide(total1, NUMBER_2, RoundingMode.HALF_UP); volumeavg = volumeavg.divide(total2); adjcloseavg = adjcloseavg.divide(total1, NUMBER_2, RoundingMode.HALF_UP); final String average = openavg.toString() + "," + highavg.toString() + "," + lowavg.toString() + "," + closeavg.toString() + "," + volumeavg.toString() + "," + adjcloseavg.toString(); return average; } public String sString(String s) { String tempString = ""; tempString = tempString + "\n" + s; return tempString; } } public static void main(final String[] args) throws IOException, ParseException { String s1 = ""; final Scanner input = new Scanner(new File("DJIA.csv")); Date d; BigDecimal open1; BigDecimal high1; BigDecimal low1; BigDecimal close1; BigInteger volume1; BigDecimal adjclose1; final Entry entry = new Entry(); input.nextLine(); while (input.hasNext()) { final String first = input.nextLine(); final String[] split = first.split(","); final Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, NUMBER_NEGATIVE_100); final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy"); df.set2DigitYearStart(cal.getTime()); d = df.parse(split[NUMBER_0]); open1 = new BigDecimal(split[NUMBER_1]); high1 = new BigDecimal(split[NUMBER_2]); low1 = new BigDecimal(split[NUMBER_3]); close1 = new BigDecimal(split[NUMBER_4]); volume1 = new BigInteger(split[NUMBER_5]); adjclose1 = new BigDecimal(split[NUMBER_6]); entry.addEntry(d, open1, high1, low1, close1, volume1, adjclose1); } final Scanner stdInput = new Scanner(System.in); int a = NUMBER_0; int b = NUMBER_0; while (stdInput.hasNext ()) { String getLine = stdInput.nextLine(); final String[] divide = getLine.split(" "); final String first = divide[NUMBER_0].toString(); final String second = divide[NUMBER_1].toString(); boolean temp1 = false; boolean temp2 = false; temp1 = entry.getTest(first); temp2 = entry.getTest(second); if (temp1 && temp2) { a = entry.position(first); b = entry.position(second); final String output = entry.average(a, b); s = entry.sString(output); } else { s1 = "No Data"; s = entry.sString(s1); } } System.out.print(s); } }
--- Update ---
and, the DJIA.csv file that I am using is this:
I am trying to make the program in a way that the user can type in five different inputs in the command prompt, each input on a new line. And then when he ends the program by pressing "Ctrl c", the program displays the five different results, each on a new line. This is similar to what the assignment asked of
--- Update ---
and, the DJIA.csv file that I am using is this:
I am trying to make the program in a way that the user can type in five different inputs in the command prompt, each input on a new line. And then when he ends the program by pressing "Ctrl c", the program displays the five different results, each on a new line. This is similar to what the assignment asked of