[RESOLVED] If you've got a free minute, I could use some Java help

Started by WhiteRose, October 09, 2010, 10:42:12 pm

Previous topic - Next topic

WhiteRose

I'm trying to write this program for my computer science course, but for some reason it's not working at all as it should be. If anyone who is familiar with Java has a free minute, I would really appreciate it if you went over my code and helped me out. Yes, I'm aware it's very messy and probably not the best way to do things, but it should at least work. However, it doesn't, so obviously something is wrong. It's not a very complex program at all; just reading in text from a file and analyzing it, basically. Let me know if you'd like to help me out, and I'll send you a copy of my code and describe the assignment and requirements. Thanks a ton!

blackfox1250


Aqua


Diokatsu



WhiteRose

October 10, 2010, 12:27:55 am #5 Last Edit: October 10, 2010, 01:48:48 am by WhiteRose
<3 you both. Here's a copy the assignment, so that you'll have something to reference:

Spoiler: ShowHide

You will write a program that computes the total value of a portfolio for a pretend customer at a bank.

Your code will pull data for the portfolio from a local data file. You will prompt the user for the needed file name.

There may be three kinds of assets in the customers account:

   Checking accounts each with a name and an associated balance
   Bonds each with a name, year of issue, initial value, and a rate of return
   Stocks each with a name and the number of shares

This will look something like this:

stock stock1 200
checking myaccount 100.00
bond mybond1 1997 100 0.07
bond mybond2 2010 1000 0.01
stock stock2 10
junk kjsdfd jkdsfj 121


"stock stock1 200" means that the pretend customer holds 200 shares of a stock called "stock1".

"checking myaccount 100.00" means that the customer has a checking account called "myaccount" containing $100.00.

"bond mybond1 1997 100 0.07" means that the customer holds a bond called "mybond1" purchased in 1997 for $100.00 that earns 7% interest per year.

Note that, as shown, the customer may have multiple (many) assets of each type. Any line that starts with something other than "stock", "checking", or "bond", including blank lines, should be ignored, for example the "junk" line shown above. Lines that start right, but lack the needed information should be ignored!.

Your program should compute the total value of the portfolio.

The value of checking accounts is just the balance given.

The value of bonds is computed assuming interest compounded annually. See pages 137 and 138 for an example. (don't include 2010)

The value of stocks is more complex to compute. For stocks you will look up the value at a web site ( http://dna.cs.byu.edu/cs142/stocks.txt ) which gives the name and share price for each stock. This "stock price data" looks like:

stock1 234.22
stock2 123.89

To compute the value of each stock you must scan through this web site ( http://dna.cs.byu.edu/cs142/stocks.txt ) , find the price and then multiply the price by the number of shares held. The TAs will check that your code looks at the website to get this information. Look at the examples for tips on how to read web pages and files. If the customer holds several stocks, you may have to scan through this file multiple times. Students that already know about arrays or other data structures will not be penalized for using them here, but do not get overly fancy.

As you did in the last labs, write some test cases. These should start very small and simple but progressively become more complex. You will use these to test that your program does what it is supposed to.


Sorry for the wall of text. Anyway, here's my code. I've uploaded it to Mediafire; it's just a .java file, so if you import it into Eclipse or whatever you should be able to take a look at it. Don't make fun of my bad coding - I'm new at this.

http://www.mediafire.com/?935woww6okg4a4v

If possible, I don't really want you to code this for me. Rather, I'd like to see what could be done to make my code work properly. So, if it's so bad that you'd rather just write something of your own instead of fix it, if you could add some comments so I understand what you've written, that would be great. As for the text cases it has to handle, make sure that it can deal with files that have things such as starting a line correctly, but then just junk. The TAs try pretty hard to break the code, so it's got to be pretty much foolproof. Thanks again for your help. Let me know if you have any further questions, or if there's anything I can do for you.

winkio

I would offer to help, but it seems like Aqua/Dio should have this covered.

Zeriab

Hi WhiteRose.

It's fun to see problem structured that input-output way spreading.
I was attending a programming competition a year ago which used the same structure.
To help train I developed a small gui to simplify running problem solutions with certain inputs.
You can find it here: http://paste-bin.com/view/97c1cb8f (Yes, ugly code >_>)

Instructions spoilered.
Spoiler: ShowHide
If you want to try it out I suggest creating a new project. Create separate folders for source and class files. (Must be src and bin for my code to work. Yes... not very dynamic  :<_<: )
Create a gui package folder (in the src folder) and put the code there.
To run it you can create a Driver class like this: (Also in the gui package)
package gui;

public class Driver {
    public static void main(String[] args) {
        new SelectDialog(null).setVisible(true);
    }
}


Create a packed called problems. The code looks through this package and all subpackages (recursively) for files named filename.in and classes.
In the gui you can select a class and an input file (*.in) and click run. It will then attempt to run the main method of the selected class where you can read the contents of the chosen input file from System.in

You don't have to use this. It's just something which may help decrease your development time. I am assuming that you should read the contents from the system input stream since that's what the automated judge typically does.



I see that you try to calculate while you are parsing the input. I suggest that you keep it separate.
You can for example structure your main like this:
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            // Read all data from br and store it appropriately

            // Read stocks info from website and store it
           
            // Do your calculations
           
            // Print out total value to System.out
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

This will probably simplify your code.
Spoiler: ShowHide
Be aware that this approach does not work if you to select what info to read based on ongoing calculations.
It is NOT the case here :3


The input has a very rigid structure so I suggest using br.readLine() for reading the lines and use a StringTokenizer or simply string.split(" ") to split the line into the space separated tokens.

I'll keep quiet for now. Just keep on trying :3

@winkio: Just help anyway ;)

*hugs*

Edit:
I just saw that you have to read the System.in for a filename. That kinda sucks. You don't have a system where you can submit java code for automatic test and check?
Anyway, you can still use my tool. You just have to have 2 files for each test. One which is the actual test file and another which simply contains the name of the actual test file.

WhiteRose

October 10, 2010, 08:03:30 pm #8 Last Edit: October 11, 2010, 11:18:54 pm by WhiteRose
Quote from: winkio on October 10, 2010, 03:24:44 am
I would offer to help, but it seems like Aqua/Dio should have this covered.


Yeah, so it seems. Thanks anyway, though!

@Zeriab: Most of that went a bit over my head, but I'll probably understand once I reread it a few times. Like I've mentioned, I'm pretty new at coding. From what I can understand, that seems like it would be very useful. I'll be sure to take a look at it. Thanks for your help!

EDIT: Dear friends, if you could somehow accomplish this sometime soon, that would be grand.

Diokatsu

Quote from: WhiteRose on October 10, 2010, 08:03:30 pm
Quote from: winkio on October 10, 2010, 03:24:44 am
I would offer to help, but it seems like Aqua/Dio should have this covered.


Yeah, so it seems. Thanks anyway, though!

@Zeriab: Most of that went a bit over my head, but I'll probably understand once I reread it a few times. Like I've mentioned, I'm pretty new at coding. From what I can understand, that seems like it would be very useful. I'll be sure to take a look at it. Thanks for your help!

EDIT: Dear friends, if you could somehow accomplish this sometime soon, that would be grand.


TBH I'm not going to be able to help any more than Zeriab was. Most likely less. Maybe you can post what you have so far down and we can kind of help and move it around so that it makes sense. Short of writing the program for you of course.

WhiteRose

Quote from: Diokatsu on October 12, 2010, 06:13:51 pm
Quote from: WhiteRose on October 10, 2010, 08:03:30 pm
Quote from: winkio on October 10, 2010, 03:24:44 am
I would offer to help, but it seems like Aqua/Dio should have this covered.


Yeah, so it seems. Thanks anyway, though!

@Zeriab: Most of that went a bit over my head, but I'll probably understand once I reread it a few times. Like I've mentioned, I'm pretty new at coding. From what I can understand, that seems like it would be very useful. I'll be sure to take a look at it. Thanks for your help!

EDIT: Dear friends, if you could somehow accomplish this sometime soon, that would be grand.


TBH I'm not going to be able to help any more than Zeriab was. Most likely less. Maybe you can post what you have so far down and we can kind of help and move it around so that it makes sense. Short of writing the program for you of course.


I have posted what I've done so far; it was in my reply to your post, right after the description of the assignment detailing what it was supposed to do. I thought my program would work, but it isn't working. That's why I need help; I wouldn't just start a topic and ask people to do my homework for me. XD I've given it my best shot, but it's not working for some reason. No worries if you don't have the time to help anymore, though. I'll probably figure it out on my own sooner or later.

WhiteRose

*legally doubleposts*

I finally figured this out, so I'm going to mark it as resolved. Thanks everyone for your offers of help. :)

winkio

I actually tried to look at this earlier today, but I don't have java installed anymore.  The coding looked fairly decent, but I couldn't test it, so I couldn't easily figure out the problem.  What was it?