Arrays

C

Cassie

Arrays
I am a student and am learning Java. I am not allowed to use graphical
user interface, at this point. I MUST use an array to display 3
mortgages;
1.) 7 years, 5.35% interest, $200,000 principle
2.) 15 years, 5.5% interest, $200,000 principle
3.) 30 years at 5.75% interest, $200,000 principle
I have accomplished this successfully BUT I also have to "list the
loan balance and interest paid for each payment over the term of the
loan". I am trying to call a payment and amortization method from 3
separate programs (7year.java, 15year.java & 30year.java).

Is this possible? Do the programs need to be merged into one (this is
possible)?
If this is not possible what is while using the array?

This is my program

//importing classes
import java.lang.Math;
import java.text.NumberFormat;

//identifying program
public class Array2exp
{
//calling Currency formatting
static NumberFormat nf = NumberFormat.getCurrencyInstance();

public static void main (String [] args)
{
// Array declration
double mortgages [];
double interest [];
int term [];
double amort [];
double pay [];

//Array creation
mortgages = new double[3];
interest = new double [3];
term = new int [3];
amort = new double [3];
pay = new double [3]

// Array initialization
mortgages [0] = (200000*5.35*(Math.pow((1+5.35/1200),84)))/(1200*(Math.pow((1+5.35/1200),84)-1));
mortgages [1] = (200000*5.5*(Math.pow((1+5.5/1200),180)))/(1200*(Math.pow((1+5.5/1200),180)-1));
mortgages [2] = (200000*5.75*(Math.pow((1+5.75/1200),360)))/(1200*(Math.pow((1+5.75/1200),360)-1));
interest [0] = 5.35;
interest [1] = 5.5;
interest [2] = 5.75;
term [0] = 7;
term [1] = 15;
term [2] = 30;
amort [0] = 7year.amortization();
amort [1] = 15year.amortization();
amort [2] = 30year.amortization();
pay [0] = 7year.payment();
pay [1] = 15year.payment();
pay [2] = 30year.payment();

//for loop, (initialization, expression, update)
for (int MonthlyPayment=0; MonthlyPayment<=2; MonthlyPayment++)
{
//print statement
System.out.print("$200,000 principle, ");
System.out.print(interest[MonthlyPayment]);
System.out.print("% interest, ");
System.out.print(term[MonthlyPayment]);
System.out.print(" years, ");
System.out.println("Payment ");
System.out.print(nf.format(mortgages[MonthlyPayment]));
System.out.println("");
System.out.println(pay[MonthlyPayment]);
System.out.println(amort[MonthlyPayment]);
}
}
}
I'd be grateful for any help. Thank you.
 
A

Andrew Thompson

I am a student and am learning Java.

You seem to be swimming Cassie.

By the looks of this problem, your instructor expects
you to understand method calls, loops, conditionals..
Your code displays little, if any of that understanding.

One answer to your question is displayed in the
following variant of your code. It displays
defining and calling a method, and a switch
statement (conditional).

But we cannot run an entire Java course
through usenet though, so you are going
to need to hit the books and figure out
what these changes mean.

<sscce>
//importing classes
import java.lang.Math;
import java.text.NumberFormat;

// naming this class
public class Array2exp
{
//calling Currency formatting
static NumberFormat nf = NumberFormat.getCurrencyInstance();

public static double amortization(int years) {
// do the amortization calcualations..
// .. blah, blah
double amount;
switch(years) {
case 7:
amount = 0.4;
break;
case 15:
amount = 0.6;
break;
case 30:
amount = 0.8;
break;
default:
amount = -1.0;
System.out.println("!! Logic Error !!");
}

return amount;
}

public static double payment(int years) {
// do the amortization calcualations..
// .. blah, blah
return 1.0;
}

public static void main (String [] args)
{
// Array declration
double mortgages [];
double interest [];
int term [];
double amort [];
double pay [];

//Array creation
mortgages = new double[3];
interest = new double [3];
term = new int [3];
amort = new double [3];
pay = new double [3];

// Array initialization
mortgages [0] =
(200000*5.35*(Math.pow((1+5.35/1200),84)))/(1200*(Math.pow((1+5.35/1200),84)-1));
mortgages [1] =
(200000*5.5*(Math.pow((1+5.5/1200),180)))/(1200*(Math.pow((1+5.5/1200),180)-1));
mortgages [2] =
(200000*5.75*(Math.pow((1+5.75/1200),360)))/(1200*(Math.pow((1+5.75/1200),360)-1));
interest [0] = 5.35;
interest [1] = 5.5;
interest [2] = 5.75;
term [0] = 7;
term [1] = 15;
term [2] = 30;
amort [0] = amortization(7);
amort [1] = amortization(15);
amort [2] = amortization(30);
pay [0] = payment(7);
pay [1] = payment(15);
pay [2] = payment(30);

//for loop, (initialization, expression, update)
for (int MonthlyPayment=0; MonthlyPayment<=2; MonthlyPayment++)
{
//print statement
System.out.print("$200,000 principle, ");
System.out.print(interest[MonthlyPayment]);
System.out.print("% interest, ");
System.out.print(term[MonthlyPayment]);
System.out.print(" years, ");
System.out.println("Payment ");
System.out.print(nf.format(mortgages[MonthlyPayment]));
System.out.println("");
System.out.println(pay[MonthlyPayment]);
System.out.println(amort[MonthlyPayment]);
}
}
}
</sscce>

BTW, please do not cross-post.
<http://www.physci.org/codes/javafaq.jsp#xpost>
Since these sort of questions are best
dealt with on c.l.j.help, I will set
follow-ups to ge to that group alone.

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 
P

Paul Lutus

Cassie said:
Arrays
I am a student and am learning Java. I am not allowed to use graphical
user interface, at this point.

This is a Good Thing(tm). Using a GUI very often obscures the things you are
trying to learn.
I MUST use an array to display 3
mortgages;
1.) 7 years, 5.35% interest, $200,000 principle
2.) 15 years, 5.5% interest, $200,000 principle
3.) 30 years at 5.75% interest, $200,000 principle

This suggestion *will* help your grade: the word is spelled "principal".
I have accomplished this successfully BUT I also have to "list the
loan balance and interest paid for each payment over the term of the
loan". I am trying to call a payment and amortization method from 3
separate programs (7year.java, 15year.java & 30year.java).

Do you need to do it this way? Are you allowed to include the computations
for these different scenarios in your program, or is the above a course
requirement?

About your program. You have all your code in a single method: main(). Try
breaking your code up into individual methods, this will greatly ease the
task of writing this or nearly any other program.
Is this possible? Do the programs need to be merged into one (this is
possible)?

The question is whether you are permitted include the programs you describe.
If so, then yes, this is a better approach.

By the way, is using arrays a requirement? The problem you posed doesn't
rewquire them. If the lesson is about using arrays, go ahead as you are
doing, but if not, you may want to reconsider your approach.
(200000*5.35*(Math.pow((1+5.35/1200),84)))/(1200*(Math.pow((1+5.35/1200),84)-1));
mortgages [1] =
(200000*5.5*(Math.pow((1+5.5/1200),180)))/(1200*(Math.pow((1+5.5/1200),180)-1));
mortgages [2] =
(200000*5.75*(Math.pow((1+5.75/1200),360)))/(1200*(Math.pow((1+5.75/1200),360)-1));
interest [0] = 5.35;

Wow. Consider using program constants to describe the various parametes for
your computation, and I certainly advise that you write at least one method
to set up the mortgage initialization that you are now doing by copying a
code line and changing some numbers, as above.
If this is not possible what is while using the array?

Say again?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top