Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [Java] assignment 2 compute compound investment growth. (https://thetfp.com/tfp/tilted-technology/71655-java-assignment-2-compute-compound-investment-growth.html)

merkerguitars 10-06-2004 12:53 PM

[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
http://www.airstreamcomm.net/~southpark/java.jpg
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..

Pragma 10-06-2004 01:59 PM

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.

MageB420666 10-06-2004 02:57 PM

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. :thumbsup:

MageB420666 10-06-2004 03:03 PM

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.

merkerguitars 10-06-2004 07:53 PM

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 have the following error also ';' expected at line 27 27:41

I'm getting closer....damn this is a tough one...

MageB420666 10-06-2004 09:00 PM

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.

oblar 10-06-2004 09:00 PM

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.*;

MageB420666 10-06-2004 09:01 PM

Quote:

Originally Posted by oblar
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)


it should still work with math lowercase.

oblar 10-06-2004 09:12 PM

Quote:

./Test.java:9: cannot resolve symbol
symbol : variable math
location: class Test
value = math.sqrt(x*x);
^
./Test.java:13: cannot resolve symbol
symbol : variable math
location: class Test
System.out.println(math.pow(y,x) + " " + Math.pow(y,x));
^
2 errors
just tried it on my system with 2 instances of math.* and two instances of Math.* and it only yelled about the lower case.. Perhaps it is specific to the java compiler for the platform I am running. I am using the Java SDK for Linux, how about yourself?

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*

chasis 10-06-2004 11:26 PM

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 ^_^

duxx0r 10-07-2004 02:14 PM

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.

merkerguitars 10-07-2004 07:39 PM

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.

Pragma 10-07-2004 07:55 PM

I'd recommend using Eclipse - it's a much nicer platform (from my experiences) for Java development, it should make your life easier.

oblar 10-08-2004 03:36 PM

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.

merkerguitars 10-08-2004 11:05 PM

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) );
   


  }

}


avsdude 10-11-2004 10:04 AM

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.

MageB420666 10-11-2004 02:52 PM

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

MageB420666 10-11-2004 03:50 PM

And as far as what SDK version, I use 1.5 on window XP pro.


All times are GMT -8. The time now is 03:04 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360