10-06-2004, 12:53 PM | #1 (permalink) |
Buffering.........
Location: Wisconsin...
|
[Java] assignment 2 compute compound investment growth.
Thanks for all your help guys with my first assignment I really learned alot, I really appreciated it...well here is my next assignment...I just some help not much.....
This is the assignment In this assignment, set up a Java application that will compute compound investment growth. If you invest P dollars at R percent interest rate compounded annually, in N years, your investment will grow to Write a Java program that accepts P, R, and N as input and computes (then outputs) the value of the investment after N years. (The interest rate, R, must be input—and used in the formula above—as the percent, not as the decimal equivalent. For example, if the interest rate is 7.5%, use 7.5 for R, not .075.) Use standard input and standard output for input and output. Your program must prompt the user for each piece of input (P, R, and N). The output should be in the form of a complete sentence, with the result appearing as a monetary figure (i.e., with a dollar sign and with two places displayed after the decimal point). the only real problem I can see is how to I make that formula into one that java understands...I know I have to use floating point numbers...Also I don't know how to input the values..
__________________
Donate now! Ask me How! Please use the search function it is your friend. Look at my mustang please feel free to comment! http://www.tfproject.org/tfp/showthread.php?t=26985 Last edited by merkerguitars; 10-06-2004 at 01:20 PM.. |
10-06-2004, 01:59 PM | #2 (permalink) |
I am Winter Born
Location: Alexandria, VA
|
Well, as far as converting the formula to one that Java understands:
denominator = ( 1 - R/100); numerator = P * (1- ( power( R/100, n+1) ) ); total = numerator / denominator; That assumes that you've got a function that'll do powers - but that's pretty simple to make (for positive powers only, especially). As for reading the input, my memory of Java is kind of fuzzy, so unfortunately I can't help you with that part - but the formula itself doesn't seem like it'll be that tricky. I apologize if anything above is wrong, due to my bad memory. It's been a few years since I've messed with Java. |
10-06-2004, 02:57 PM | #3 (permalink) |
<Insert wise statement here>
Location: Hell if I know
|
Well you can alway look in the Java API:
Java API but the general method of getting input is: import java.util.Scanner; (class) { (main method) { Scanner scan* = new Scanner(System.in); (prompt for input) (object) = scan.next(); //Look in the API for other methods in Scanner class there is also nextInt, nextDouble, etc; You should now have an object with the value of the input, just input the object into the formula. repeat the prompt and scan.next() or whichever Method you use for each variable *You can also name the Scanner object anything you want it doesn't have to be "scan" i hope this helps, if it just confuses you then ignore it.
__________________
Apathy: The best outlook this side of I don't give a damn. Last edited by MageB420666; 10-06-2004 at 03:05 PM.. |
10-06-2004, 03:03 PM | #4 (permalink) |
<Insert wise statement here>
Location: Hell if I know
|
oh and as for the formula he got it a little wrong,
numerator = p*(1-(math.pow(R/100, n+1))) You don't have to import the class since it's part of the java.lang package.
__________________
Apathy: The best outlook this side of I don't give a damn. |
10-06-2004, 07:53 PM | #5 (permalink) |
Buffering.........
Location: Wisconsin...
|
I don't think I can use the scanner class.....we haven't covered that in school yet...this is what I have for code so far
Code:
package assignment2; /** * <p>Title: Assignment 2</p> * <p>Description: CS 161 Assignment 2 </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: UW-River Falls</p> * @author Eric Merker * @email ericmerker@centurytel.net * @date October 4th 2004 * @team prometheusfree.org crew and the tfproject.org crew * @version 1.0 */ import java.math.*; import java.text.*; import java.util.*; public class Assignment2 { public Assignment2() { } public static void main(String[] args) { Assignment2 assignment21 = new Assignment2(); denominator = ( 1 - R/100); numerator = p*(1-(math.pow(R/100, n+1))) total = numerator / denominator; } } I'm getting closer....damn this is a tough one...
__________________
Donate now! Ask me How! Please use the search function it is your friend. Look at my mustang please feel free to comment! http://www.tfproject.org/tfp/showthread.php?t=26985 |
10-06-2004, 09:00 PM | #6 (permalink) |
<Insert wise statement here>
Location: Hell if I know
|
you forgot the ; on the end of the numerator statement, and unless you just haven't written the code yet, you don't need to import the java.math class, the math.pow your using is part of the java.lang package, did your teacher tell you to use it?
I don't know of any way to get input from the user other than the Scanner class, which if your import the java.util package you may as well use. the only other option I know of is to assign the variables a value yourself, and if your doing that you may as well just enter the numbers. You also haven't declared any of the objects, that will give you a compile error.
__________________
Apathy: The best outlook this side of I don't give a damn. |
10-06-2004, 09:00 PM | #7 (permalink) |
Über-Rookie
Location: No longer, D.C
|
numerator = p*(1-(math.pow(R/100, n+1)))
that is the line that needs a ; after it as far as the math.pow.. You almost have it. math is a class, therefore needs to be capitalized. Math.pow(R/100, n+1) edit: i didn't mention another way to get input.. You should be able to get input through the use of InputStreamReader and BufferedReader // Set up input stream for console use InputStreamReader iStream = new InputStreamReader(System.in); BufferedReader buffer = new BufferedReader(iStream); System.out.println("What do you want to enter?" String userInput = buffer.readLine(); you can also just use System.in.read(); which should read a character at a time (as integers). There are lots of ways to do user input. (The bufferedReader requires you to import java.io.*; Last edited by oblar; 10-06-2004 at 09:08 PM.. |
10-06-2004, 09:01 PM | #8 (permalink) | |
<Insert wise statement here>
Location: Hell if I know
|
Quote:
it should still work with math lowercase.
__________________
Apathy: The best outlook this side of I don't give a damn. |
|
10-06-2004, 09:12 PM | #9 (permalink) | |
Über-Rookie
Location: No longer, D.C
|
Quote:
this is just curiousity now, I find a lot of the things I had taken as strict truths can sometimes be bent, so I try to find where they are so I am not anal about them to others *grin* |
|
10-06-2004, 11:26 PM | #10 (permalink) |
Upright
|
since Math is a class and your not instaciating it you need to use the capitol Math when referring to it =p as for using a BufferedReader to accept the numbers you should try looking at Float.parseFloat() in the java api
http://java.sun.com/j2se/1.4.2/docs/api/index.html as for the Scanner class... O_o amazing, last java i wrote in was 1.4.2 not 1.5 @_@ and Scanner is new to 1.5 (or 5.0 as sun wants to call it ~_~) merkerguitars: what version of java are you programming with so that people can help you better because if your using 1.4.2 Scanner will be useless to you oh btw first post ^_^ |
10-07-2004, 02:14 PM | #11 (permalink) |
Upright
Location: Brisbane
|
merkerguitars: wtf (I have the following error also ';' expected at line 27 27:41).. if you cant debug this problem after doing your first assignment then you need to do some serious catch up. All this information will be in your final exam and if you dont learn it now (which is what it is intended for) then your not going to pass.
Maybe you should actually put some thought into your assignment and not just get everyone here to do it? I'm more then happy to help you with problems that may exceed what you have currently learned, but seriously dude, put the effort in first, then come and ask us. |
10-07-2004, 07:39 PM | #12 (permalink) |
Buffering.........
Location: Wisconsin...
|
Sorry for asking so many question....the reason I ask for so much help is i'm pretty new with this and my teacher doesn't offer a whole ton of help and it works out better learn it from you people....and the error I got i did put the ";" in there and the program I used gave an error, I'll guess i'll fiddle around again...I have been putting in effort, but like I said my teacher is vague. The program I use is Jbuilder X foundation, which can be a pain in the ass (i have talked to other people that program and they said it doesn't work the greatest, but it's what I have to use. The way I have to punch in the details is i have to use the System.out.println to ask for the input of all the variables so it shows up in the message window....too bad I couldn't do it with Joption windows otherwise it would be a little easier. I know my last line will be System.out.println("Your investment will grow to $" + D (or whatever the hell how I input the value) ) Using D as my value of final dollars.
Oh yeah too..i'll figure out which version of java i'm using..probably make things a little more simple.
__________________
Donate now! Ask me How! Please use the search function it is your friend. Look at my mustang please feel free to comment! http://www.tfproject.org/tfp/showthread.php?t=26985 Last edited by merkerguitars; 10-07-2004 at 07:55 PM.. |
10-08-2004, 03:36 PM | #14 (permalink) |
Über-Rookie
Location: No longer, D.C
|
merkerguitars, I cannot be 100% sure, but most places really don't care what you write your code in. The reason they tell you to use program X to write your code is because then if you have a problem the instructor (or TA) only needs to know the one program.. Every other program is use at your own risk.
If they just ask for the *.java files at the end, then it shouldn't make one iota of a difference what program you use. Heck, you could use a plain text editor (pico, nano, notepad, etc) and write everything that way. |
10-08-2004, 11:05 PM | #15 (permalink) |
Buffering.........
Location: Wisconsin...
|
Well i got it working today..tell me what you guyz think
Code:
package assignment2; /** * <p>Title: Assignment 2</p> * <p>Description: CS 161 Assignment 2 </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: UW-River Falls</p> * @author Eric Merker * @email ericmerker@centurytel.net * @date October 4th 2004 * @team prometheusfree.org crew visit us at irc.shadowfire.org at #prometheus and the tfproject.org crew at #tfp * @version 1.0 */ //This package is for the Decimal Format String import java.text.*; // This package is used for the IOException class and the Buffered Reader import java.io.*; import java.text.DecimalFormat; public class Assignment2 { public Assignment2() { } //The IOException makes it so it ignore Input Output errors public static void main(String[] args)throws IOException { Assignment2 assignment21 = new Assignment2(); //These three lines are the values that need to be input and tells what type of variables they are String inputStr; int N; double P, R; //The BufferedReader is used to help input the variables BufferedReader bufReader; bufReader = new BufferedReader( new InputStreamReader ( System.in ) ); //this takes input from the users keyboard and places into the formula for each of the variables System.out.print(" Input Percent Interest Compounded Anually: "); inputStr = bufReader.readLine(); R = Double.parseDouble(inputStr); System.out.print(" Number of Years: "); inputStr = bufReader.readLine(); //This is an Int instead of a double is why the the following code is written N = Integer.parseInt(inputStr); System.out.print(" Input Number of dollars you wish to invest: "); inputStr = bufReader.readLine(); P = Double.parseDouble(inputStr); // This is the actual math formula, the word double is in front to tell the program what the value actually are double denominator = ( 1 - R/100); double numerator = P*(1-(Math.pow(R/100, N+1))); double total = numerator / denominator; //This makes it so any double number will only be out the 0.00 spot. DecimalFormat df = new DecimalFormat ( "0.00" ); //This will say the final value as an result of the formula above the df.format makes the out only say the value to the 0.00 spot System.out.println( "Your money will accrue to $" + df.format(total) ); } }
__________________
Donate now! Ask me How! Please use the search function it is your friend. Look at my mustang please feel free to comment! http://www.tfproject.org/tfp/showthread.php?t=26985 |
10-11-2004, 10:04 AM | #16 (permalink) |
Whatever
Location: Littleton, CO
|
If I was doing a code review for one of my programmers, I would make these comments:
Comments are good. Use descriptive variable names - P, N and R tell me nothing. Get rid of 'Assignment2 assignment21 = new Assignment2();' its not needed. Fix your indentation of comments and code. Catch the NumberFormatException thrown from parseInt and parseDouble and do something useful like retrying the input. |
10-11-2004, 02:52 PM | #17 (permalink) |
<Insert wise statement here>
Location: Hell if I know
|
up at the top
import java.text.* and import java.text.DecimalFormat; are redundant. the import java.text.* imports everything in the java.text package
__________________
Apathy: The best outlook this side of I don't give a damn. |
Tags |
assignment, compound, compute, growth, investment, java |
|
|