Data Table HELP!

Resetting Variable, formatting table?

Page 1 of 1

7 Replies - 607 Views - Last Post: 04 May 2010 - 04:54 PM Rate Topic: -----

#1 Celesteon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 12-January 10

Data Table HELP!

Posted 04 May 2010 - 07:14 AM

Hey, so I have an assignment in class:

1. Prompt the user for the amount of an initial investment, the number of years, and the range of interest rates. Then print a table of the value of an initial investment over the range of years at the range of interest rates. The table should look similar to the following for an initial investment of $1000, invested for 5 years.

1. Years
a. 1 2 3 4 5
ii. Interest Rate
iii. 5% $1051 $1105 $1161 $1221 $1284
iv. 6% $1061 $1127 $1197 $1271 $1349
v. 7% $1072 $1150 $1233 $1323 $1419
vi. 8% $1083 $1173 $1271 $1377 $1491
vii. 9% $1094 $1197 $1309 $1433 $1568

Remember that you should calculate the value of the investment compounded daily for 1 year. So for 365 days, we will multiply the current value of the investment by the interest rate / 365 and add that back into the value of investment. You will need to use nested loops to build the table.



Here is the code I have so far:

import java.util.Scanner;
public class  Multitable
{
	public static void main (String Args [])
	{
		double r;
		int n;
		int k;
		double rate1;
		double rate2;
		int years;
		double value;
		double newrate;
		double val;
		double compound;
		double sum;


		
		Scanner in = new Scanner (System.in);
		
		System.out.print("Please enter a starting value ($):");
		value=in.nextDouble();
		
		System.out.print("Please enter a starting rate (%):");
		rate1=in.nextDouble();
	
		System.out.print("Please enter an ending rate(%):");
		rate2= in.nextDouble();
		
		System.out.print("Please enter the number of years:");
		years = in.nextInt();
		
		for (int l=1; l<=years; l++)
	        {
		    	System.out.print("\t  "+l+"   ");
		    	value = value;
	        }
	        System.out.println();
	    
	    for (int l=1; l<=years; l++)
	    {
	    	rate1 = rate1;
	    	rate2 = rate2;	
		    for (r=rate1; r<=rate2; r++)
		    {
		    	if (r<10)
		    	System.out.print(" "+r+"%"+" ");
		    	if (r>=10)
		    	System.out.print(r+"%"+" ");
	
		    	newrate = r/100.0/365.0;
		    	
		    	for (k = 1; k<=365; k++)
					{
				    	value = value*newrate+value;	  	
				    }
				System.out.printf("%.0f ", value); 
			   	System.out.println();
		    }
	    	
    	}
	}
}


Basically, it compiles,and it does a calculation - but I am having a couple problems. I have been testing the formatting of the table, and I've been getting this: (when I test the rate of 5%)


--------------------Configuration: Multitable - JDK version 1.6.0_17 <Default> - <Default>--------------------
Please enter a starting value ($):1000
Please enter a starting rate (%):5
Please enter an ending rate(%):5
Please enter the number of years:5
1 2 3 4 5
5.0% 1051
5.0% 1105
5.0% 1162
5.0% 1221
5.0% 1284

Process completed.


The values are right (save the third one,but that's a rounding error?)
But I can't manage to get it to go in a line across the top...and when I do attempt that, I get the line, but everything has 5.0% next to it.

Also, when I test for more rates, the values get messed up. I don't really know how to rest my variable each time I go through....I need to reset value to the user entered value every time the program starts over for a new rate? How do I do that? It's annoying though, because even my first values get messed up (for 5%) when they work on their own.


--------------------Configuration: Multitable - JDK version 1.6.0_17 <Default> - <Default>--------------------
Please enter a starting value ($):1000
Please enter a starting rate (%):5
Please enter an ending rate(%):6
Please enter the number of years:5
1 2 3 4 5
5.0% 1051
6.0% 1116
5.0% 1173
6.0% 1246
5.0% 1310
6.0% 1391
5.0% 1462
6.0% 1553
5.0% 1632
6.0% 1733

Process completed.


Can anyone help me? Please?! :(

Is This A Good Question/Topic? 0
  • +

Replies To: Data Table HELP!

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Data Table HELP!

Posted 04 May 2010 - 07:39 AM

Quote

The values are right (save the third one,but that's a rounding error?)
But I can't manage to get it to go in a line across the top...and when I do attempt that, I get the line, but everything has 5.0% next to it.


Bit confused what you mean by the line across the top, please post the output you are seeking to attain.

Cheers
Was This Post Helpful? 0
  • +
  • -

#3 Celesteon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 12-January 10

Re: Data Table HELP!

Posted 04 May 2010 - 07:42 AM

The output that I want looks like this:


..................1.........2..........3..........4..........5
5%......$1051...$1105...$1161...$1221...$1284
6%......$1061...$1127...$1197...$1271...$1349
7%......$1072...$1150...$1233...$1323...$1419
8%......$1083...$1173...$1271...$1377...$1491
9%......$1094...$1197...$1309...$1433...$1568



The "." are spaces...haha...I just can't figure out how to space it out

This post has been edited by Celesteon: 04 May 2010 - 07:47 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10443
  • View blog
  • Posts: 38,678
  • Joined: 27-December 08

Re: Data Table HELP!

Posted 04 May 2010 - 03:18 PM

You amy find a couple well placed uses of the tab (\t) escape sequence useful. So if I have: System.out.println("Hello\tworld");, that will print something along the lines of the following with the tab in between:
Hello    World


Was This Post Helpful? 0
  • +
  • -

#5 Celesteon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 12-January 10

Re: Data Table HELP!

Posted 04 May 2010 - 04:35 PM

View Postmacosxnerd101, on 04 May 2010 - 02:18 PM, said:

You amy find a couple well placed uses of the tab (\t) escape sequence useful. So if I have: System.out.println("Hello\tworld");, that will print something along the lines of the following with the tab in between:
Hello    World




I don't mean to sound rude, but that's not really the bit I need help with. I really need help with where to put the print for the rates, considering I'm having trouble getting them to print in that table formation.

I also need help figuring out how to reset a variable...do you know how to do that?
Was This Post Helpful? 0
  • +
  • -

#6 Celesteon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 12-January 10

Re: Data Table HELP!

Posted 04 May 2010 - 04:41 PM

Hey, so I have an assignment in class:

1. Prompt the user for the amount of an initial investment, the number of years, and the range of interest rates. Then print a table of the value of an initial investment over the range of years at the range of interest rates. The table should look similar to the following for an initial investment of $1000, invested for 5 years.

1. Years
a. 1 2 3 4 5
ii. Interest Rate
iii. 5% $1051 $1105 $1161 $1221 $1284
iv. 6% $1061 $1127 $1197 $1271 $1349
v. 7% $1072 $1150 $1233 $1323 $1419
vi. 8% $1083 $1173 $1271 $1377 $1491
vii. 9% $1094 $1197 $1309 $1433 $1568

Remember that you should calculate the value of the investment compounded daily for 1 year. So for 365 days, we will multiply the current value of the investment by the interest rate / 365 and add that back into the value of investment. You will need to use nested loops to build the table.



Here is the code I have so far:

import java.util.Scanner;
public class  Multitable
{
	public static void main (String Args [])
	{
		double r;
		int n;
		int k;
		double rate1;
		double rate2;
		int years;
		double value;
		double newrate;
		double val;
		double compound;
		double sum;


		
		Scanner in = new Scanner (System.in);
		
		System.out.print("Please enter a starting value ($):");
		value=in.nextDouble();
		
		System.out.print("Please enter a starting rate (%):");
		rate1=in.nextDouble();
	
		System.out.print("Please enter an ending rate(%):");
		rate2= in.nextDouble();
		
		System.out.print("Please enter the number of years:");
		years = in.nextInt();
		
		for (int l=1; l<=years; l++)
	        {
		    	System.out.print("\t  "+l+"   ");
		    	value = value;
	        }
	        System.out.println();
	    
	    for (int l=1; l<=years; l++)
	    {
	    	rate1 = rate1;
	    	rate2 = rate2;	
		    for (r=rate1; r<=rate2; r++)
		    {
		    	if (r<10)
		    	System.out.print(" "+r+"%"+" ");
		    	if (r>=10)
		    	System.out.print(r+"%"+" ");
	
		    	newrate = r/100.0/365.0;
		    	
		    	for (k = 1; k<=365; k++)
					{
				    	value = value*newrate+value;	  	
				    }
				System.out.printf("%.0f ", value); 
			   	System.out.println();
		    }
	    	
    	}
	}
}


Basically, it compiles,and it does a calculation - but I am having a couple problems. I have been testing the formatting of the table, and I've been getting this: (when I test the rate of 5%)


--------------------Configuration: Multitable - JDK version 1.6.0_17 <Default> - <Default>--------------------
Please enter a starting value ($):1000
Please enter a starting rate (%):5
Please enter an ending rate(%):5
Please enter the number of years:5
1 2 3 4 5
5.0% 1051
5.0% 1105
5.0% 1162
5.0% 1221
5.0% 1284

Process completed.


The values are right (save the third one,but that's a rounding error?)
But I can't manage to get it to go in a line across the top...and when I do attempt that, I get the line, but everything has 5.0% next to it.

Also, when I test for more rates, the values get messed up. I don't really know how to rest my variable each time I go through....I need to reset value to the user entered value every time the program starts over for a new rate? How do I do that? It's annoying though, because even my first values get messed up (for 5%) when they work on their own.


--------------------Configuration: Multitable - JDK version 1.6.0_17 <Default> - <Default>--------------------
Please enter a starting value ($):1000
Please enter a starting rate (%):5
Please enter an ending rate(%):6
Please enter the number of years:5
1 2 3 4 5
5.0% 1051
6.0% 1116
5.0% 1173
6.0% 1246
5.0% 1310
6.0% 1391
5.0% 1462
6.0% 1553
5.0% 1632
6.0% 1733

Process completed.


Can anyone help me? Please?! :(
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10443
  • View blog
  • Posts: 38,678
  • Joined: 27-December 08

Re: Data Table HELP!

Posted 04 May 2010 - 04:45 PM

Have you heard of the Compounding Interest Formula? If not, the formula to determine total payment over a period of time is: Principal * (1 + rate/numTimesCompounded)^(years * numTimesCompounded). I've written a snippet implementing this, which you may find helpful.

As for the implementation of your code, I'd revise it a little bit. Since you want to show a table based on varying rates and years ellapsed, I would use nested loops. So read in your two rates (start and end), then loop from start to end. Inside there, loop from 1 to your lastYear, and plug in data to my snippet and print the result. So something along this setup:
//maybe loop up here to print out years first

for( ; rate1 < rate2; rate1 += .01){ //increase by 1% each iteration

   //print out rate
   
   for(int year = 1; year < maxYear; year++){
         //plug in data to my snippet
         //and print out the total Amount
         //returned
   }
}


Link to my snippet:
http://www.dreaminco...snippet5060.htm
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10443
  • View blog
  • Posts: 38,678
  • Joined: 27-December 08

Re: Data Table HELP!

Posted 04 May 2010 - 04:54 PM

Please respect rule #5 of this forum. May the 4th Be With You sounds like a title in a Lounge thread, not a descriptive title for your problem in the Java forum (or any other non-lounge forum for that matter).

Also, you may want to check out this link for more help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1