I'm trying to take user input in the form of myMonthlyPayment, myAnnualInterestRate, and myPrincipal in order to calculate the number of months needed to pay off debt by using The formula I've attached to this post. What I have in eclipse for the formula right now is:
monthsNeeded = ((Math.log(myMonthlyPayment) - Math.log(myMonthlyPayment)
- ((myAnnualInterestRate / 1200.0) * myPrincipal))
/ ((Math.log(myAnnualInterestRate) / 1200.0) + 1.0));
I should be getting an output of 79 months with the inputs I'm using but instead I'm getting -62. I know the formula is correct, I'm almost positive I've made a mistake somewhere in the translation of it into Java. If someone could point it out that would be greatly appreciated!
So I've fixed it, with a sample input and output.
I didn't put much effort into making this code beautiful but you can see that even separating it into 3 parts using method extraction (although I didn't know how to name them, lacking the domain knowledge) made the code easier to understand.
public class Example {
public static void main(String[] args) {
double myMonthlyPayment = 2000;
double myAnnualInterestRate = 5;
double myPrincipal = 200000;
System.out.println(a(myMonthlyPayment));
System.out.println(b(myPrincipal, myAnnualInterestRate, myMonthlyPayment));
System.out.println(c(myAnnualInterestRate));
double monthsNeeded = (a(myMonthlyPayment) - b(myPrincipal, myAnnualInterestRate, myMonthlyPayment))
/ c(myAnnualInterestRate);
System.out.println(monthsNeeded);
}
private static double c(double myAnnualInterestRate) {
return Math.log((myAnnualInterestRate / 1200.0) + 1);
}
private static double b(double myPrinicipal, double myAnnualInterestRate, double myMonthlyPayment) {
return Math.log(myMonthlyPayment - (myAnnualInterestRate / 1200.0) * myPrinicipal);
}
private static double a(double myMonthlyPayment) {
return Math.log(myMonthlyPayment);
}
}
I think this is what you're looking for:
monthsNeeded = (Math.log(myMonthlyPayment) - Math.log(myMonthlyPayment - myAnnualInterestRate / 1200d * myPrincipal)) / Math.log(myAnnualInterestRate / 1200d + 1);
It seems that, in your solution, you weren't calculating your myAnnualInterestRate/1200*myPrincipal inside your second Math.log(...). You had also left some calculations outside of Math.log(...) in the bottom half of your equation.
If you have an equation that does an operation inside a natural log, when you convert that equation to Java code, the operation needs to still be done, inside the natural log:
ln(someNumber + 10)
would be converted to:
Math.log(someNumber + 10),
NOT:
Math.log(someNumber) + 10
Hope this helps and good luck. :)
In my app I use the method "DistanceTo()" to get the distance between to places, but don't works correctly!! This is my code for this distance:
double latitudine;
double longitudine;
latitudine = 40.17;
longitudine = 24.9;
Location qui = new Location("Corrente");
qui.setLatitude(latitudine);
qui.setLatitude(longitudine);
double latitudine2;
double longitudine2;
latitudine2 = 40.16;
longitudine2 = 25;
Location due = new Location("Corrente2");
due.setLatitude(latitudine2);
due.setLatitude(longitudine2);
float b = qui.distanceTo(due);
Toast.makeText(getBaseContext(), "DESTINAZIONE KILOMETRI b:" + b, Toast.LENGTH_SHORT).show();
The toast show to me 11077.14 meters, but the distance should be 8.5 km!! as shown in these sites: http://www.movable-type.co.uk/scripts/latlong.html http://www.mapdevelopers.com/distance_from_to.php
In this case the difference is for about 3km, but sometimes the difference is sometimes the difference is even more!! Please help me! Why this code doesn't work correctly? Thanks
you are not calling setLongitude on both Locations, you need:
qui.setLongitude(longitudine);
due.setLongitude(longitudine2);
Might want to take a look at Point2D.Distance instead. It returns
the distance between the two sets of specified coordinates.
I wanted to get regression parameters by using Apache's Commons.Math3 library and the OLSMultipleLinearRegression.
The regression should be polynomial with a power of 2.
It worked fine with test data but when I use this experimental data the methode gives me an absolutely wrong regression.
public static void poly (){
OLSMultipleLinearRegression quadRegression = new OLSMultipleLinearRegression();
double [] y = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,
51,52,53,54,55,56,57,58,59};
double [][] x = {{1.00,1.00},{1.00,1.00},{1.00,1.00},{1.00,1.00},{1.00,1.00},{1.00,1.00},{1.00,1.00},{1.00,1.00},{1.00,1.00},{0.95,0.90},{0.96,0.91},{0.96,0.92},{0.96,0.92},{0.96,0.92},{0.92,0.84},{0.92,0.85},
{0.92,0.86},{0.93,0.86},{0.93,0.87},{0.89,0.80},{0.90,0.81},{0.90,0.81},{0.90,0.82},{0.89,0.80},{0.90,0.81},{0.90,0.82},{0.91,0.82},{0.91,0.83},{0.90,0.80},{0.90,0.80},{0.90,0.81},{0.91,0.82},
{0.89,0.79},{0.89,0.80},{0.90,0.80},{0.90,0.81},{0.88,0.77},{0.88,0.77},{0.88,0.78},{0.88,0.78},{0.86,0.73},{0.86,0.74},{0.86,0.74},{0.86,0.74},{0.84,0.71},{0.85,0.72},{0.85,0.72},{0.85,0.73},
{0.84,0.71},{0.84,0.71},{0.84,0.71},{0.84,0.71},{0.83,0.69},{0.83,0.69},{0.83,0.69},{0.82,0.68},{0.82,0.68},{0.82,0.68},{0.82,0.68}};
quadRegression.newSampleData(y, x);
quadRegression.setNoIntercept(false);
double [] results = quadRegression.estimateRegressionParameters();}
For this input data I get the equation y=117.54x²-504.83x+389.088 which would result in a y-value of 379.760.85 for x=59 - way beyond my input value.
So I either handled the class absolutly wrong or I got stuck in a mathematical pitfall.
If someone please could explain me what I did wrong or misinterpreted - this problem drives me insane.
I'm trying to calculate the distance for a third grader's geometry game app I'm developing. The basic idea is that the user inputs the directions using directions and how long he wants to go in each direction.( eg input: Right (radians) , travel a metres, left (radians), travel b metres).
So, now my app will find out if the user can get back to the starting point and how far he is away from home.
So far, I have been able to proceed with this logic here. I would like to know if there are other ways to do this? thanks!
You can do it using third grade geometry fittingly.
When you use the commands you listed, you are creating a vector. If you store dir, x, and y you can use simple sin and cosin to figure out your ending location.
dir+=rad
x+=cosin(dir)
y+=sin(dir)
If you have saved your start location, you can use Pythagorean Theorem to figure out the displacement from home!
public double distance (int x, int y){
int xdist = x-startx;
int ydist = y-starty;
int squarex = xdist*xdist
int squarey = ydist*ydist
return Math.sqrt((double)(squarex+squarey));
}
I have this simple problem I'm trying to do. I'm trying to write a program that doubles these five numbers. Then I want to compute the average of these numbers and print them out. The code runs with no errors, but it will not print my answer for some reason. How can I get it to print the output of the problem or simply print something? I am using Netbeans.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package marina;
/**
*
* #author bax
*/
public class Precedence {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
double grade1 = 100;
double grade2 = 75;
double grade3 = 88;
double grade4 = 65;
double grade5 = 99;
int x = (int) (grade1+grade2+grade3+grade4+grade5/5.0);
System.out.println(x);
}
}
Output:
run:
BUILD SUCCESSFUL (total time: 0 seconds)
(Like I said in the comments:)
Make sure you have your file open and press Shift + F6, which runs the current file. Notice that if you press just F6 then the main project will be run, not necessarily this project. Also, make sure you hit the Clean and build option, to clean up cache and so on.
group your additions in brackets(elm1+elm2)/5 and then divide it by 5
int x = (int) ((grade1+grade2+grade3+grade4+grade5)/5);
System.out.println(x);
System.out.println() will print to the console, so make sure you run it there.
Further, you are not calculating an average. Division has higher precedence than addition. Rewrite like:
int x = (int) ((grade1+grade2+grade3+grade4+grade5)/5.0);
If you're using Eclipse or another IDE to run your program, there should be a "console" window somewhere in the IDE, that will display the console output.
To run your program without an IDE, run the following in a console (cmd on windows, terminal/bash on linux, ...):
javac MyFile.java
java MyFile
If that doesn't work, you will have to add the JDK binaries to your PATH.
It should print the result but you have also a mistake with the calculation.
You only divide grade5 through 5.0.
This is what you want to do I think:
int x = (int) ((grade1+grade2+grade3+grade4+grade5) / 5.0);
Try also:
System.out.println("Result: " + x);
int x = (int) (grade1+grade2+grade3+grade4+grade5/5.0);
(1+1+1+1+5/5)=5
(1+1+1+2+5)/5 = 2