Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Write a method to calculate the monthly payments you would have to make over a given number of years to pay off a loan at a given interest rate compounded continuously. The method takes the number of years t, the principal P, and the annual interest rate r as arguments. The desired value is given by the formula (P ert/ 12 t). Use Math.exp(). The signature of the method should be: public static double monthlyPayment(double years, double principal, double rate) Use the method to write a program Payments that generates a set of the monthly payments one would have to make for a $20,000 loan over 5 years at interest rates varying from 5% to 8% in 0.25% increments.
public class Payments {
public static void main(String[] args) {
for(double r=0.05; r<=0.08; r+=0.0025) {
System.out.println(monthlyPayment(5,20000,r));
}
}
public static double monthlyPayment(double years,double principal,double rate) {
return ((principal*(Math.exp(years*rate)))/(12*years));
}
}
This is what I wrote so far and I'm not sure what to do actually, I don't understand by what the mean by varying from 5% to 8% in 0.25% increment ! Can anybody explain please ?
The question is telling you to create a table for
5.00%
5.25%
...
...
7.75%
8.00
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I am trying to calculate the average of values given via repeated function calls.
Actually Bluej allows me to put the number to rate but if i am trying to put again it replaces the previous one, i want to be stored and after that an average to be shown.
I can't figure out how to do it
This is the part of code:
public int getRate() // here i put an int number
{
return this.rate;
}
public void setRate(int rate) // here i change it but i think i don't need it
{
this.rate = rate;
}
I can't use strange or complex commands because i am allowed to only use this type of commands like get/set and arraylists.
It is a school assignment.
Thanks
An easy way to keep an average of inputs is to keep track of:
The sum of all inputs received so far.
The number of inputs you have received.
Every time you call setRate to update the rate, you add to the sum and increment the count. You also need a special case for when no rates have been added yet, to avoid division by zero:
private int ratesSum = 0;
private int rateCount = 0;
public int getRate()
{
return this.rate;
}
public void setRate(int rate)
{
this.rate = rate;
this.ratesSum += rate;
this.rateCount++;
}
// Gets the average of all rates so far, or returns zero if no rates
// have been set yet.
public float getAverageRate()
{
// Do not divide by zero
if (rateCount == 0) return 0;
return ((float)ratesSum) / ((float)rateCount);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I am aware about what is factorial. eg 5! = 5x4x3x2x1
Can anyone guide about the code to calculate the following for a number through recursion.
eg: for 5, how to calculate 5x4x3x2x1x2x3x4x5 through recursion
This is the code to solve your problem. In the last line, I am multiplying the n with another n since I need to multiply it twice, but I am keeping that multiplication in stack and pending it as I am calling the recursion. Hence at the last I am getting the result as 5*4*3*2*1*2*3*4*5=14400.
public class Test{
public static void main(String... args){
int f=5;
System.out.println(fact(f));
}
public static int fact(int n){
if(n==1) return n;
return n*fact(n-1)*n;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I done a coding to get the output 20,40,60,80 and 100.
Multiple.java:
public class Multiple {
static int n=20;
static int m;
public static void main(String[] args) {
for(int i=1;i<=5;i++){
m=n*i;
System.out.println(m);
}
}
}
Output:
20
40
60
80
100
I didn't have any issue in this coding.I got an output successfully.But loop running 5 times.Let me know is there any other way to get these output(20,40,60,80 and 100) in simplest way.
very interesting. if you know the output result (it's fixed), why don't you write them out directly:
System.out.println(20);
System.out.println(40);
System.out.println(60);
System.out.println(80);
System.out.println(100);`
oh i see, you think that's too long. Then probably you can use the below:
int a = 0;
while(a<100){ a += 20; System.out.println(a);}
Enjoy it!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a list of Portfolio objects in java collection. This Portfolio object has a property called scoringRule. I want to calculate how much percentage of Portfolio objects are present in the list for each type of scoringRule. Actually, I have only 2 different types of scoring rules. The scoringRUle property can be either "single" or "double". How can I do this?
Class Portfolio{
private String name;
private String type;
private String scoringRule;
}
List<Portfolio> portfolios = new ArrayList<Portfolio>();
This is your solution.
double single = 0;
double doublee = 0;
for (Portfolio pobj : portfolios) {
if(pobj.scoringRule.equals("single"))
single++;
else
doublee++;
}
System.out.println("single Percentage : " +(single / list.size() )*100);
System.out.println("doublee Percentage : " + (doublee / list.size())*100);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
1) Change the program so that the user has to enter the initial balance and the interest rate. Assume that the user will enter interest rates in whole numbers ("5" would represent an interest rate of 5%, for example). Assume that the user will enter initial balances that are numeric only - no commas. 2) Change the code to display how many years it takes an investment to triple.
I have entered my scanner so the user can input their balance and interest. no matter what user enters it outputs 2000.
import java.util.Scanner;
public class InvestmentRunner
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Please Enter Initial Balance:");
String Balance = in.next();
System.out.print("Please Enter Interest Rate:");
String Interest = in.next();
final double INITIAL_BALANCE = 10000;
final double RATE = 5;
Investment invest = new Investment(INITIAL_BALANCE, RATE);
invest.waitForBalance(2 * INITIAL_BALANCE);
int years = invest.getYears();
System.out.println("The investment doubled after "
+ years + " years");
}
}
Per the question askers comment: well no matter what the user inputs the output stays at 2000
These are the lines in which you're constructing an Investment object, calling methods on this object, and then printing the value.
Investment invest = new Investment(INITIAL_BALANCE, RATE);
invest.waitForBalance(2 * INITIAL_BALANCE);
int years = invest.getYears();
System.out.println("The investment doubled after " + years + " years");
You haven't posted the Investment class, so I just have to assume these methods are fine. The PROBLEM is here:
final double INITIAL_BALANCE = 10000;
final double RATE = 5;
These are the variables you're sending to the Investment constructor. Constants you've hardcoded in. Meanwhile, this is where you take user input:
System.out.print("Please Enter Initial Balance:");
String Balance = in.next();
System.out.print("Please Enter Interest Rate:");
String Interest = in.next();
You take the Balance (should be balance), then take Interest (should be interest), then you do nothing with these values. You need to parse a double out of these Strings and then send them as the arguments to your constructor.