Welcome to Dream.In.Code
Become a Java Expert!

Join 150,356 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,883 people online right now. Registration is fast and FREE... Join Now!




Sooo Lost

2 Pages V  1 2 >  
Reply to this topicStart new topic

Sooo Lost, Mortgage Calculator

iNaStY v3
4 Feb, 2008 - 09:13 AM
Post #1

D.I.C Head
Group Icon

Joined: 3 Feb, 2008
Posts: 63


Dream Kudos: 50
My Contributions
Alright I just started this whole Java thing, and my book likes to say "don't worry about this now, we'll cover it later" and it's making me go crosseyed.
I saw the topics about the Mortgage problem, but they use the javax.swing to get the input info, and we haven't learned that yet and I'm pretty sure my Prof would wonder how I know it lol, so here's my problem:

User needs to input principle amount, interest rate, and period of loan. Output should include monthly payment, total interest due, and total loan amount to be paid. So far I'm just working on getting the monthly payment:

CODE

import java.util.Scanner;

public class SerafinMortgage
{
    public static void main(String[] args)
    {
    final int MONTH_IN_YEAR = 12;
    
    double principle = 100000;
    double interestRate = 6;
    double monthlyPayment;
    int loanPeriod = 30;

    String principle;
    String monthlyPayment;
    String finalPayment;
    String finalInterest;
    String loanPeriod;
    Scanner in;

    // Get Input values
    in = new Scanner(System.in);
    System.out.println("Initial Loan Amount: ");
    principle = in.nextDouble();
    System.out.println("Interest Rate: ");
    finalInterest = in.nextDouble();
    System.out.println("Loan Period: ");
    loanPeriod = in.nextInt();

    // Compute Monthly Payment
    principle = principle;
    finalInterest = finalInterest/12/1;
    monthlyPayment = (principle * (finalInterest/12) * (1+finalInterest/12) * (12*30)/
    ((1+finalInterest/12)*12*30-1));

    // Display the Result
    System.out.println("Loan Amount is $" + principle);
    System.out.println("Monthly Interest Rate is " + finalInterest + "%");
    System.out.println("Loan Period (years) is " + loanPeriod);
    
    System.out.println("\n");
    System.out.println("Monthly Payment is $ " + monthlyPayment);
    }
}


As I compile it I get about 13 error messages all of which are similar: incompatible types, operator cannot be applied...

I don't understand why I put the values I did double principle = 100000, double interestRate = 6...and so on (it's what my book and the other forum discussing this did).

I'm so lost and my professor is a dousche and I can't get ahold of him! I'm not looking for someone to just post the answer with the correct code, I'm trying to figure out what to do mad.gif

Help? //please

This post has been edited by iNaStY v3: 4 Feb, 2008 - 09:34 AM
User is offlineProfile CardPM
+Quote Post

DillonSalsman
RE: Sooo Lost
4 Feb, 2008 - 09:19 AM
Post #2

D.I.C Head
Group Icon

Joined: 30 Oct, 2007
Posts: 72


Dream Kudos: 50
My Contributions
QUOTE(iNaStY v3 @ 4 Feb, 2008 - 10:13 AM) *

Alright I just started this whole Java thing, and my book likes to say "don't worry about this now, we'll cover it later" and it's making me go crosseyed.
I saw the topics about the Mortgage problem, but they use the javax.swing to get the input info, and we haven't learned that yet and I'm pretty sure my Prof would wonder how I know it lol, so here's my problem:

User needs to input principle amount, interest rate, and period of loan. Output should include monthly payment, total interest due, and total loan amount to be paid. So far I'm just working on getting the monthly payment:

CODE

import java.util.Scanner;

public class Mortgage
{
    public static void main(String[] args)
    {
    final int MONTH_IN_YEAR = 12;
    
    double principle = 100000;
    double interestRate = 6;
    double monthlyPayment;
    int loanPeriod = 30

    String principle;
    String monthlyPayment;
    String finalPayment;
    String finalInterest;
    String loanPeriod;

    // Get Input values
    System.out.println("Initial Loan Amount: ");
    principle = in.nextLine();
    System.out.println("Interest Rate: ");
    finalInterest = in.nextLine();
    System.out.println("Loan Period: ");
    loanPeriod = in.nextLine();

    // Compute Monthly Payment
    principle = principle;
    finalInterest = finalInterest/12/1;
    monthlyPayment = (principle * (finalInterest/12) * (1+finalInterest/12) * (12*30)/
    ((1+finalInterest/12)*12*30-1));

    // Display the Result
    System.out.println("Loan Amount is $" + principle);
    System.out.println("Monthly Interest Rate is " + finalInterest + "%");
    System.out.println("Loan Period (years) is " + loanPeriod);
    
    System.out.println("\n");
    System.out.println("Monthly Payment is $ " + monthlyPayment);
    }
}


As I compile it I get about 13 error messages all of which are similar: incompatible types, operator cannot be applied...

I don't understand why I put the values I did double principle = 100000, double interestRate = 6...and so on (it's what my book and the other forum discussing this did).

I'm so lost and my professor is a dousche and I can't get ahold of him! I'm not looking for someone to just post the answer with the correct code, I'm trying to figure out what to do mad.gif

Help? //please



I am pretty new to Java however I believe that you are only allowed to have one variable with the name principle. wink2.gif
A neat little trick I learned from LibertyBasic is that you can have a variable called "word" as an int or something and then "word$" as another variable type, prefferably String (in Liberty the $ is the only way to use strings wink2.gif)

However I don't think that is very professional but I'm 15 and doing this for fun so I don't have to be professional biggrin.gif

This post has been edited by DillonSalsman: 4 Feb, 2008 - 09:20 AM
User is offlineProfile CardPM
+Quote Post

iNaStY v3
RE: Sooo Lost
4 Feb, 2008 - 09:30 AM
Post #3

D.I.C Head
Group Icon

Joined: 3 Feb, 2008
Posts: 63


Dream Kudos: 50
My Contributions
Dillon - So you're saying that once I define something with a Floating-Point type I shouldn't define it with String?

eg.
CODE

double principle = 100000;
String principle;


that is wrong?

This post has been edited by iNaStY v3: 4 Feb, 2008 - 09:31 AM
User is offlineProfile CardPM
+Quote Post

capty99
RE: Sooo Lost
4 Feb, 2008 - 09:33 AM
Post #4

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,257



Thanked: 16 times
Dream Kudos: 550
My Contributions
no need to quote someone for the first reply in a thread. smile.gif

but yes, just come up with a different name.

note: since your doing it for school adding a symbol won't get you the grade, the profs are sometimes real particular about having a name that is descriptive so that a) it can be graded and cool.gif you follow habits that will pay off the in future
User is offlineProfile CardPM
+Quote Post

iNaStY v3
RE: Sooo Lost
4 Feb, 2008 - 09:38 AM
Post #5

D.I.C Head
Group Icon

Joined: 3 Feb, 2008
Posts: 63


Dream Kudos: 50
My Contributions
wait...give what a different name?

lol and what symbol did I add? The name of the project SerafinMortgage, Serafin is my last name (he told us to do this)

here maybe this will help you help me, these are my error messages:

operator / cannot be applied to java.lang.String, int
operator + cannot be applied to int, <any>
operator * cannot be applied to double, <nulltype>
incomptible types found : int require: java.lang.String



12 errors total

This post has been edited by iNaStY v3: 4 Feb, 2008 - 09:42 AM
User is offlineProfile CardPM
+Quote Post

capty99
RE: Sooo Lost
4 Feb, 2008 - 10:00 AM
Post #6

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,257



Thanked: 16 times
Dream Kudos: 550
My Contributions
in your variable initiations,

you have double principle = 100000;

AND

String principle;

change one of those names cause you can't have principle be a String and a Double
User is offlineProfile CardPM
+Quote Post

iNaStY v3
RE: Sooo Lost
4 Feb, 2008 - 11:00 AM
Post #7

D.I.C Head
Group Icon

Joined: 3 Feb, 2008
Posts: 63


Dream Kudos: 50
My Contributions
Well I guess I'm confused as to whether or not I even need the

String principle

if I change the name or not. I know I need the

double principle

because it defines the values that can be stored inside the variable principle. So I'm unsure about that, and why I'm putting 100000 as the value, as well as
double interestRate = 6; and
int loanPeriod = 30;

I don't understand why I'm using those values.

Also, when I remove my String values that I have already named as Double or int it removes two error messages, but I'm still getting all of the operator error messages and the incompatible types error messages

thanks for being patient with me lol
User is offlineProfile CardPM
+Quote Post

capty99
RE: Sooo Lost
4 Feb, 2008 - 11:15 AM
Post #8

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,257



Thanked: 16 times
Dream Kudos: 550
My Contributions
oh okay i thought you had written this out ...


principle is defined as 100000, but as soon as the program starts thats over-written with a user input. normally you just want to declare to zero, or without a number so you don't get confused... like you just did. smile.gif

also you declare a String finalInterest, but then the second thing pulled from user input is a double, so you need to match those up. Declare finalInterest as a double earlier in your code and remove the string crap.

I'm at the FAC for another 1/2 hour if your close I could show you... otherwise I can get home and test it later tonight and get you a complete answer.

would you mind posting the whole assignment though....

that way i can get a real understanding , not sure why the loan period and interest are hardcoded and the rest is pulled from input.
User is offlineProfile CardPM
+Quote Post

iNaStY v3
RE: Sooo Lost
4 Feb, 2008 - 12:38 PM
Post #9

D.I.C Head
Group Icon

Joined: 3 Feb, 2008
Posts: 63


Dream Kudos: 50
My Contributions
capty thank you SO much that cleared things up very nicely for me!

here is the original problem:

Lab 1 (Due 2/5):
Write a program to calculate a mortgage's monthly payment with the end user interactively entering the loan’s a fixed interest rate, number of years, amount borrowed, and estimated escrow amount. The program will also display the payment value. You will first need to find the appropriate formula. Warning – translating the formula into code is not a simple task. Output the monthly payment amount, the total amount paid over the life of the loan, and the total amount of interest paid over the life of the loan.


here is my new (WORKING biggrin.gif ) code:

CODE

import java.util.Scanner;

public class SerafinMortgage
{
    public static void main(String[] args)
    {
    final int MONTH_IN_YEAR = 12;
    
    double principle = 0;
    double interestRate = 0;
    double monthlyPayment;
    double monthlyInterest = 0;
    double finalPayment = 0;
    double finalInterest = 0;
    int loanPeriod = 0;

    Scanner in;

    // Get Input values
    in = new Scanner(System.in);
    System.out.print("Initial Loan Amount: ");
    principle = in.nextDouble();
    System.out.print("Interest Rate: ");
    monthlyInterest = in.nextDouble();
    System.out.print("Loan Period: ");
    loanPeriod = in.nextInt();

    // Compute Monthly Payment
    principle = principle;
    monthlyInterest = monthlyInterest/12/1;
    monthlyPayment = (principle * (monthlyInterest/12) * (1+monthlyInterest/12) * (12*30)/
    ((1+finalInterest/12)*12*30-1));
    finalPayment = monthlyPayment * 12 * loanPeriod;
    finalInterest = finalPayment - principle;

    // Display the Result
    System.out.println("\n");
    System.out.println("Monthly Payment is $ " + monthlyPayment);
    System.out.println("Total Amount Due is $ " + finalPayment);
    System.out.println("Total Interest Due is $ " + finalInterest);
    }
}


my only question now is how the hell do I get rid of the 13 decimal places in the results? lol that looks ridiculous!!

Also, just for fun, if I wanted to jazz it up a lil with GUI, I know i would use this:

CODE

import javax.swing.*;
.
.
.
//Results
JOptionPane.showMessageDialog(null, "Monthly Payment is $ " + monthlyPayment);


but I can't figure out how to all three of my outputs (monthly payment, total payment, and total interest) in the same dialog box on seperate lines. All I can do is put them in three different dialog boxes or all on one line.

That's just curiosity lol I'm prolly getting wayyy ahead of myself. And if anyone wouldn't mind critiquing my style that would be awesome! I just want to make sure I'm writing things correctly and not over-complicating things. ANY advice/opinions/tips would be greatly appreciated!

But again, thanks a ton for clearing some stuff up for me, cause like you said, I confused the hell out of myself biggrin.gif

// God this site rules

This post has been edited by iNaStY v3: 4 Feb, 2008 - 12:40 PM
User is offlineProfile CardPM
+Quote Post

iNaStY v3
RE: Sooo Lost
4 Feb, 2008 - 01:42 PM
Post #10

D.I.C Head
Group Icon

Joined: 3 Feb, 2008
Posts: 63


Dream Kudos: 50
My Contributions
EDIT:

the program works just fine as it is, but my calculation is wrong:

CODE

monthlyPayment = (principle * (monthlyInterest/12) * (1+monthlyInterest/12) * (12*30)/
    ((1+finalInterest/12)*12*30-1));


^^that's what I'm using right now^^

The only reason I'm using this formula is because I don't know how to write exponents. I thought that it was just (1523) ** 5, 5 being the exponent, but I get error messages.

The formula I want to use (because it's right) is this:

CODE

amoritizedLoan = loanPeriod * 12;
monthlyPayment = (principle * (monthlyInterest/(1 - (1 + monthlyInterest)**-amoritizedLoan);


but I can't figure out how to write an exponent blink.gif shit i'm going crosseyed again

This post has been edited by iNaStY v3: 4 Feb, 2008 - 01:43 PM
User is offlineProfile CardPM
+Quote Post

capty99
RE: Sooo Lost
4 Feb, 2008 - 03:10 PM
Post #11

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,257



Thanked: 16 times
Dream Kudos: 550
My Contributions
http://www.cafeaulait.org/course/week4/40.html

scroll down to the pow

its java.lang.Math
User is offlineProfile CardPM
+Quote Post

iNaStY v3
RE: Sooo Lost
4 Feb, 2008 - 05:38 PM
Post #12

D.I.C Head
Group Icon

Joined: 3 Feb, 2008
Posts: 63


Dream Kudos: 50
My Contributions
Awesome Awesome Awesome! Here is my code (working) one last time, with the correct math equation (for anyone else in the future biggrin.gif )

CODE

import java.util.Scanner;
import javax.swing.*;

public class SerafinMortgage
{
    public static void main(String[] args)
    {
    final int MONTH_IN_YEAR = 12;
    
    double principle = 0;
    double interestRate = 0;
    double monthlyPayment;
    double monthlyInterest = 0;
    double finalPayment = 0;
    double finalInterest = 0;
    double months = 0;
    int loanPeriod = 0;
    
        Scanner in;

    // Get Input values
    in = new Scanner(System.in);
    JOptionPane.showMessageDialog(null, "Start the Mortgage Calculator?");
    System.out.print("Initial Loan Amount: ");
    principle = in.nextDouble();
    System.out.print("Annual Interest Rate: ");
    monthlyInterest = in.nextDouble();
    System.out.print("Loan Period (years): ");
    loanPeriod = in.nextInt();

    // Compute Monthly Payment
    principle = principle;
    monthlyInterest = monthlyInterest/(12 * 100);
    months = loanPeriod * 12;
    monthlyPayment = principle * monthlyInterest/
             (1 - Math.pow( (1 + monthlyInterest), -months) );
    finalPayment = monthlyPayment * 12 * loanPeriod;
    finalInterest = finalPayment - principle;

    // Display the Result
    System.out.println("\n");
    System.out.println("Monthly Payment is $ " + monthlyPayment);
    System.out.println("Total Amount Due is $ " + finalPayment);
    System.out.println("Amount Owed in Interest is $ " + finalInterest);
    }
}


Now will someone PLEASE lol help me so that there isn't 13 decimal places after my output? 2 would be nice lol

I think it has something to do with 'truncate'? What I found on Google looked like this:

CODE

0.499159 * 100.0                      // Result is 49.9159
(int) (0.499159 * 100.0)            // Result is 49


but first I can't figure out how to put this in my code without getting a million error messages, and secondly I still want 2 decimal places.....

This post has been edited by iNaStY v3: 4 Feb, 2008 - 05:51 PM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month