Help with unhappy beginner code...

Hello Java gurus,
I'm learning java and one of my class assignments is the old "mortgage calculator"... I made code up, compiled okay, but it won't execute. Gives me all sorts of errors. I THINK it's to do with the double vs int vs float... Please help!!! I've been playing with this and can't figure whats erroring. I know its prob something simple so please explain when you write back what I'm not understanding... Thank you! -nika
Assignment:
The monthly payment on mortgage loan of L dollars, at a rate of interest r is given by
Monthly payment = []
Where N is the number of years of mortgage. Write a program to find the monthly
payment for 30 years, for principals from $100,000 through $200,000(with increments
of $20,000) and interest rates of 6% through 10% (with increments of 0.5%). Print
monthly payment as a whole number (rounded off).
The output must be a neat table, similar to the following (your answers may be slightly
different because of rounding off).
Principal 6% 6.5% 7% 7.5% 8% 8.5% 9% 9.5% 10%
100000 600 632 665 699 733 768 804 840 877
120000 719 758 798 839 880 922 965 1009 1053
140000 � � � � � � � � �
160000 � � � � � � � � �
180000 � � � � � � � � �
200000 � � � � � � � � �
Hint: Use the following idea to compute (1 + r/12)12N
double temp = 1+r/12;
double temp1 = 1;
for (int i = 1; i<=12*N;i++)
temp1 = temp1*temp;
The final formula now becomes:
double mp = (L*(r/12)*temp1)/(temp1-1);
MY CODE:
import java.util.*;
class MortgagePmt {
public static void main(String args[])     {
int n = 30;     
System.out.println("Mortgage Monthly Payment");
System.out.println("Principal 6.0% 6.5% 7.0% 7.5% 8.0% 8.5% 9.0% 9.5% 10.0% ");
for (int l = 100000; l<=200000; l=l+20000){
System.out.printf("%9d ",l);
for(double r = 6.0; r<=10.0; r=r+0.5){
double temp = 1+r%12;
double temp1 = 1;
for (int i = 1; i<=12*n;i++)
temp1 = temp1*temp;
double mp = (l*(r%12)*(temp1))/(temp1 - 1);
System.out.printf("%5.0f", mp);
System.out.println("");
}

You really need to do an analysis on your algo... there is NO WAY that it is correct.
MY CODE:
import java.util.*;
class MortgagePmt {
public static void main(String args[])     {
int n = 30;     
System.out.println("Mortgage Monthly Payment");
System.out.println("Principal 6.0% 6.5% 7.0% 7.5% 8.0% 8.5% 9.0% 9.5% 10.0% ");
for (int l = 100000; l<=200000; l=l+20000){
System.out.printf("%9d ",l);
for(double r = 6.0; r<=10.0; r=r+0.5){
double temp = 1+r%12;
double temp1 = 1;What is this following little section for? temp1 comes out of there at 1.7x10^304... err, no way that is right. I do not believe there is that much anything in the world, let alone money.
for (int i = 1; i<=12*n;i++)
temp1 = temp1*temp;
double mp = (l*(r%12)*(temp1))/(temp1 - 1);
System.out.printf("%5.0f", mp);
System.out.println("");
}One of the best things you can do at this point, if it's not thee very best thing, is to get your debugger out and find out how to use it. Your code ran as you posted it, but your algorithm is not right and numbers you are calculating do not even make sense--all of them output as NaN (not a number). Take each feature of your assignment and code it 1 piece at a time tracing each seeing that the numbers you calculate make sense, and as you get that piece to work, move on to the next feature of your assignment. This way you don't write out a bunch of none tested code that you don't know what it is doing.
Throwing out code that you think is a solution that you do not know what it does and have no hope of fixing is no better than no solution at all--IMO it is far worse: you'll work attempting to fix it, not knowing what it is doing and all the time all you are building is your frustration.
(BTW: don't think I'm singling you out and speaking harshly to you--I've been there too. Sometimes it's better to just start over. Get a handle on what needs to be done. Get a working pencil and paper model, then describe the process in an algorithm, then code from that algorithm. Implement in steps and make sure each step works before moving on to the next. There is no magic in the computer--it won't make a bad process good--on the contrary it usually makes bad things worse, you have to design a good process first to implement a good process.)

Similar Messages

  • Help with some beginner code

    Hello, I am new to java and I need a bit of help with some code that I'm writing. here is the code:
    import javax.swing.*;
    public class Test{
         public static void main(String[] args){
         JOptionPane.showMessageDialog(null,"We will now build a block with *'s","Block",1);
         String input=JOptionPane.showInputDialog(null,"Type a number: ","Number",3);
         int number=Integer.parseInt(input);
         int count=0; int count2=0;
         for(count2=0; count2<number; count2++){
              for(count=0; count<number; count++){
              System.out.print("* ");
    System.exit(0);
    }Now, all I need is to build a block of *'s with the number that the user inputs. With the code that I wrote I get the correct number of *'s but not in the form of a block. They just print out in a straight line. I know this is a very simple task but could someone please help me out? What do I need to modify in my code so that the *'s print out arranged as a block like so:
    **********

    Your code only uses the print method which prints without a carriage return/line feed. So you need to add a line of code to print a carriage return/line feed. Where? well that is your task to work out.

  • Need help with a activation code for Adobe Acrobat X Standard for my PC,, Don't have older version serial numbers,  threw programs away,  only have Adobe Acrobat X Standard,  need a code to unlock program?

    Need help with a activation code for Adobe Acrobat X Standard for my PC, Don't have older Version of Adobe Acrobat 9, 8 or 7. 

    You don't need to install the older version, you only need the serial number from your original purchase. If you don't have them to hand, did you register? If so, they should be in your Adobe account. If not you really need to contact Adobe, though it isn't clear they will be able to do anything without some proof of purchase etc.

  • HT3209 Purchased DVD in US for Cdn viewing. Digital download will not work in Cda or US? please help with new Digital code that will work

    Purchased DVD in US for Cdn viewing. Digital download will not work in Cda or US? please help with new Digital code that will work

    You will need to contact the movie studio that produced the DVD and ask if they can issue you a new code valid for Canada. Apple cannot help you, and everyone here in these forums is just a fellow user.
    Regards.

  • Need help with adjusting Javascript code to work in Adobe Edge (Countdown)

    Hello
    Im a newbie when it comes to working with Javascript and Adobe Edge and need a bit of help with adjusting some javascript code to work with Adobe Edge. A friend of mine helped me with making this javascript code: Edit fiddle - JSFiddle
    Its a simple countdown which counts down to a certain time at a certain date. What I aim to do is to add this code as a trigger on a text-element called "countdown" (within a symbol called "count").
    I have tried to do this as the code is, but it does not work. Anyone have any suggestions?
    Thanks!
    Mvh,
    Øyvind Hermans

    Hello again
    I have stumbled upon a problem with these animations; They crash the browser after viewing them a little while, usually less than 30 seconds in.
    Is this problem also occuring when you watch the animations?
    Is the countdown-code to much for the browsers to handle?
    Thanks in advance for your answers.
    Sincerely,
    Øyvind Hermans

  • Help with HTML form code

    I need help on some HTML code if at all possible.
    What I am trying to do is set up a page that someone can
    enter their name address and email into a form and when they hit
    the submit button it automatically sends them a premade email of my
    choosing that I make prior and somehow maybe embeded into the html
    code to the address that they entered? I dont know if it is
    possible but I am sure it can be.
    Thank you in advance

    Actually, you could be subject to "abuse complaints", not
    "abuse".
    Murray --- ICQ 71997575
    Adobe Community Expert
    (If you *MUST* email me, don't LAUGH when you do so!)
    ==================
    http://www.dreamweavermx-templates.com
    - Template Triage!
    http://www.projectseven.com/go
    - DW FAQs, Tutorials & Resources
    http://www.dwfaq.com - DW FAQs,
    Tutorials & Resources
    http://www.macromedia.com/support/search/
    - Macromedia (MM) Technotes
    ==================
    "bregent" <[email protected]> wrote in
    message
    news:fb547q$b49$[email protected]..
    > >What I am trying to do is set up a page that someone
    can enter their name
    > address
    >
    > Sure, you can do it fairly easily with any scripting
    language. You need to
    > be
    > cautious about sending automated emails to anyone
    without first validating
    > that
    > they are the actual owners of the email address,
    otherwise you open your
    > site
    > up to abuse.
    >
    > >I dont know if it is possible but I am sure it can
    be.
    >
    > Huh?
    >

  • Need Help With Simple ABAP Code

    Hello,
    I'm loading data from a DSO (ZDTBMAJ) to an Infocube (ZCBRAD06). I need help with ABAP code to some of the logic in Start Routine. DSO has 2 fields: ZOCTDLINX & ZOCBRDMAJ.
    1. Need to populate ZOCPRODCD & ZOCREFNUM fields in Infocube:
        Logic:-
        Lookup /BI0/PMATERIAL, if /BIC/ZOCBRDMAJ = /BIC/OIZOCBRDMAJ
        then /BIC/ZOCPRODCD = ZOCPRODCD in Infocube
               /BIC/ZOCREFNUM = ZOCREFNUM in Infocube         
    2. Need to populate 0G_CWWTER field in Infocube:
        Logic:
        Lookup /BIC/PZOCTDLINX, if /BIC/ZOCTDLINX = BIC/OIZOCTDLINX
        then G_CWWTER = 0G_CWWTER in Infocube.
    I would need to read single row at a time.
    Thanks!

    I resolved it.

  • Would like help with treeset ,the codes that using for sorting, please ?

    hi every body. i faced a problem while writing this program, everything worked properly except this one. The program is related to Jframe and it asks the user to add three labels , three text field , one text area and three Jbuttons which are add sort and exit. i could deal with add and exit but sort, i couldn't do it properly because i don't know the codes that should be added in actionperfomed?
    These codes are :
    package gui;
    import javax.swing.SwingUtilities;
    import java.awt.BorderLayout;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.Rectangle;
    import java.util.ArrayList;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JTextArea;
    import sun.misc.Sort;
    import domain.Student;
    public class StudentFrame extends JFrame {
          * This method initializes btnAdd     
          * @return javax.swing.JButton     
         private JButton getBtnAdd() {
              if (btnAdd == null) {
                   btnAdd = new JButton();
                   btnAdd.setBounds(new Rectangle(13, 135, 59, 27));
                   btnAdd.setText("Add");
                   btnAdd.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent e) {
                        btnAddClicked(); // TODO Auto-generated Event stub actionPerformed()
              return btnAdd;
         protected void btnAddClicked() {
              // TODO Auto-generated method stub
         String name = txtName.getText();
         String id = txtID.getText();
         String age = txtAge.getText();
         Student s = new Student(name , id , age);
         txtArea.setText(String.valueOf(s ));
          * This method initializes btnSort     
          * @return javax.swing.JButton     
         private JButton getBtnSort() {
              if (btnSort == null) {
                   btnSort = new JButton();
                   btnSort.setBounds(new Rectangle(95, 136, 62, 25));
                   btnSort.setText("Sort");
                   btnSort.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent e) {
                             btnSortClicked(); // TODO Auto-generated Event stub actionPerformed()
              return btnSort;
         protected void btnSortClicked() {
              // TODO Auto-generated method stub
          * This method initializes btnExit     
          * @return javax.swing.JButton     
         private JButton getBtnExit() {
              if (btnExit == null) {
                   btnExit = new JButton();
                   btnExit.setBounds(new Rectangle(173, 134, 61, 23));
                   btnExit.setText("Exit");
                   btnExit.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent e) {
                             System.exit(0); // TODO Auto-generated Event stub actionPerformed()
              return btnExit;
          * This method initializes txtName     
          * @return javax.swing.JTextField     
         private JTextField getTxtName() {
              if (txtName == null) {
                   txtName = new JTextField();
                   txtName.setBounds(new Rectangle(79, 11, 60, 23));
              return txtName;
          * This method initializes txtID     
          * @return javax.swing.JTextField     
         private JTextField getTxtID() {
              if (txtID == null) {
                   txtID = new JTextField();
                   txtID.setBounds(new Rectangle(73, 55, 65, 26));
              return txtID;
          * This method initializes txtAge     
          * @return javax.swing.JTextField     
         private JTextField getTxtAge() {
              if (txtAge == null) {
                   txtAge = new JTextField();
                   txtAge.setBounds(new Rectangle(74, 96, 61, 24));
              return txtAge;
          * This method initializes txtArea     
          * @return javax.swing.JTextArea     
         private JTextArea getTxtArea() {
              if (txtArea == null) {
                   txtArea = new JTextArea();
                   txtArea.setBounds(new Rectangle(138, 5, 154, 128));
              return txtArea;
          * @param args
         public static void main(String[] args) {
              // TODO Auto-generated method stub
              SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        StudentFrame thisClass = new StudentFrame();
                        thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        thisClass.setVisible(true);
          * This is the default constructor
         public StudentFrame() {
              super();
              initialize();
          * This method initializes this
          * @return void
         private void initialize() {
              this.setSize(300, 200);
              this.setContentPane(getJContentPane());
              this.setTitle("JFrame");
              return jContentPane;
    }Edited by: hypocrisy on Apr 26, 2009 6:51 PM

    There are three actionPerformed in that program.One for add Jbutton , Sort , and Exit
    the codes that i have added for add button after action performed are
    String name = txtName.getText();
    String id = txtID.getText();
    String age = txtAge.getText();
    Student s = new Student ( name , id , age);
    System.out.println(s);
    the codes for Exit button to make it works properly was :
    System.out.println(0);
    But when i came to Sort Button and i tried to enter many codes but it didn't sort properly so would you like to help me with the right codes :)

  • Help with basic ABAP code (merge internal tables, sort of...)

    Hello,
    Can someone please help write some basic code for a Basis guy with limited ABAP knowledge?
    Should be some easy points for an experienced ABAPer!
    I have identicaly structured internal tables I_A and I_B and I_C which have already been filled by function models I called.
    How will I code the following?:
    I want to read all the data of I_A into a new internal table I_MASTER (structured the same as I_A,I_B and I_C).
    Then I want to read I_B and:
    1)Update I_MASTER with NEW records
    2)Update existing records if the value of field MYFIELD in I_B is smaller than the value of MYFIELD in I_MASTER.
    Then I want to read I_C and:
    1)Update I_MASTER with NEW records
    2)Update existing records if the value of field MYFIELD in I_C is smaller than the value of MYFIELD in I_MASTER.
    Let me know if I can provide anymore information.
    Thanks in advance for you help!
    Adriaan
    Message was edited by: Adriaan
    Message was edited by: Adriaan

    Hi Adriaan ,
    I want to read all the data of I_A into a new internal table I_MASTER (structured the same as I_A,I_B and I_C).
    <b>i_master[] = i_a[] .</b>
    loop at i_b .
    read table i_master with key myfiled < i_b-myfield .
    if sy-subrc = 0 .
    append i_master from i_b .
    endif.
    endloop.
    loop at i_c .
    read table i_master with key myfiled < i_c-myfield .
    if sy-subrc = 0 .
    append i_master from i_c .
    endif.
    endloop.
    Let me know if this helped .
    Regards,
    Varun .
    Message was edited by: varun sonu

  • Help with free unlock code

    I just purchased a 7290 that was supposed to be unlocked (apparently it is refurbished). It came from cingular originally. My carrier can't unlock it to work with their system. Can anyone help me with free unlock codes?

    Only your carrier can provide free unlock codes.
    If you contact Horizon below in my signature, they do so cheaply and reliably.
    1. If any post helps you please click the below the post(s) that helped you.
    2. Please resolve your thread by marking the post "Solution?" which solved it for you!
    3. Install free BlackBerry Protect today for backups of contacts and data.
    4. Guide to Unlocking your BlackBerry & Unlock Codes
    Join our BBM Channels (Beta)
    BlackBerry Support Forums Channel
    PIN: C0001B7B4   Display/Scan Bar Code
    Knowledge Base Updates
    PIN: C0005A9AA   Display/Scan Bar Code

  • Help with new error code (-1)!

    Hi I am trying to update my iphone 4 to ios5. When it comes to the last stage, it shows this error of -1. I am unable to find any info related to this error code. Any help with this is highly appreciated! Many Thanks. Now the phone is unusable. Previously it showed several errors including 1600 series and they are all resolved. This is the last hudrle. Please Help!
    Thanks

    Like many I experienced some issues when updating to iOS 5 (on launch day). I finally gave up on the Update option and chose the Restore option.
    iOS 5 downloaded and installed without issue. I restored from Back Up and everything was in place. No errors.
    If you are updating, try using Restore instead.

  • Hi there can you help with this error code Oxc5d1281

    I refilled the HP cartridges in my Photosmart C 7250 All-in-One and put them in and got this error.  I have D/C power,etc and it still comes up with this error code.  Now what?
    Cheers,
    British_eh

    Hi british_eh
    You could try resetting the printer, details here
    Note though that the reliability of refills tends not be as good as originals, links below FYI.....
    http://h10025.www1.hp.com/ewfrf/wc/document?docname=c00853819&cc=us&dlc=en&lc=en&jumpid=reg_R1002_US...
    http://www.qualitylogic.com/2009HPinktest.pdf
    Kind Regards, Ciara
    Although I am an HP employee, I am speaking for myself and not for HP.
    Twitter: @Ciara_B_HP

  • Can anyone help with an Error Code 4450

    I keep trying to burn cd's and everytime that I do, itunes cancels the disk burn and gives me the error code 4450. can anyone help out with this? thanks..

    I am having the exact same problem! and now i can't access my pending downloads!! if anybody has any answers, i am begging you, please help me. if i can't fix this then i have wasted $40., so please, help me!

  • Help with some html code for flash site!

    Got this site>
    http:/www.thedesignport.com
    Site works great! everything is uploaded! What I cant seem to
    get workning is the html code to add a description under the title
    of the website in a google search? Goto google enter "topanga
    mountain school" I'm top spot on the second page but with NO
    description?? have a look at the html code for
    http://www.thedesignport.com
    Have I gone wrong somewhere?? But I've found if you put
    "topanga mountain school pdf" into google ( I have three pdf's on
    the site) My site is Second on the search with a DESCRIPTION of the
    pdf's under the site name?? I dont get it?? Any help would be
    great!

    You're not getting any help here because this is the forum for discussions of the Community Help Client application and Help system in general. For CSS questions, you should probably try something like the Dreamweaver forum.

  • Help With Error in Code Generated by WSDL Wizard

    I generated code using the webservice wizard, and then wrote
    a simple mxml file to test the code. I get this error:
    1061: Call to a possibly undefined method getClass through a
    reference with static type Class.
    It occurs in the following code:
    * Internal event handler to process a successful operation
    call from the server
    * The result is decoded using the schema and operation
    settings and then the
    * events get passed on to the actual facade that the user
    employs in the application
    * @private
    private function
    processResult(result:Object,wrappedData:Object):void
    var token:AsyncToken = wrappedData.returnToken;
    var currentOperation:WSDLOperation = wrappedData.operation;
    var decoder:SOAPDecoder = new SOAPDecoder();
    decoder.resultFormat="object";
    decoder.ignoreWhitespace = true;
    decoder.makeObjectsBindable=true;
    decoder.wsdlOperation = currentOperation;
    decoder.schemaManager = currentOperation.schemaManager;
    var body:Object = result.message.body;
    var stringResult:String = String(body);
    if (stringResult == null || stringResult == "")
    return;
    var soapResult:SOAPResult =
    decoder.decodeResponse(result.message.body);
    if (soapResult.isFault)
    var faults:Array = soapResult.result as Array;
    for each (var soapFault:Fault in faults)
    var soapFaultEvent:FaultEvent =
    FaultEvent.createEvent(soapFault,token,null);
    token.dispatchEvent(soapFaultEvent);
    } else {
    result = decoder.decodeResponse(result.message.body).result;
    if (result is ArrayCollection)
    //shoud upcast to specific type here
    var arrayTypedClass:Class =
    SchemaTypeRegistry.getClass(currentOperation.outputMessage.parts[0].type);
    result = new arrayTypedClass(result.source);
    var event:ResultEvent =
    ResultEvent.createEvent(result,token,null);
    token.dispatchEvent(event);
    The statement where the error occurs is in
    bold above. The comment above that line says I should upcast
    to a specific type here, but I don't know what that means. Which
    specific type? How do I determine what type to upcast to?
    Any help would be appreciated.
    TIA,
    Randy

    Hi Martin,
    From the error you showed it seems you have upgraded your SDK
    version, but not the builder. The problem you are experiencing
    comes from the fact that the one of the SDK classes (the
    SchemaTypeRegistry) has been updated from a static to a dynamic
    class. Therefore, using the getClass() call on it will not work any
    more. Also, the lines that register type mappings won't work.
    We've updated the generated code, but it seems you are
    running on an older version. Now, to fix your code you have to:
    1. Locate all occurences where the SchemaTypeRegistry class
    is called as a static class.
    2. Replace the SchemaTypeRegistry part from the method calls
    with SchemaTypeRegistry.getInstance(), like this:
    SchemaTypeRegistry.registerClass(params) becomes
    SchemaTypeRegistry.getInstance().registerClass(params)
    The problem is that when you re-generate the code you will
    have to re-do all these changes again.
    Hope this helps,
    Cristian

Maybe you are looking for