Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2004

Tip: Looking for answers? Try searching our database.

Arrays

Thread view: 
Cassie - 16 Sep 2004 18:03 GMT
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.
Andrew Thompson - 16 Sep 2004 18:32 GMT
> 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

Signature

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

Thomas G. Marshall - 18 Sep 2004 17:14 GMT
Andrew Thompson coughed up:

...[rip]...

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

I've often thought that the name of that group /really/ should have been

       comp.lang.java.newbie

instead.  It would have been a much better corral for such questions, and
would also be better frequented by those of us wanting to help newbies
specifically.

Signature

"It's easier to be terrified by an enemy you admire."
-Thufir Hawat, Mentat and Master of Assassins to House Atreides

Paul Lutus - 16 Sep 2004 18:35 GMT
> 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?

Signature

Paul Lutus
http://www.arachnoid.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.