I need help finishing this java code This is what i have so far import java.text
ID: 640401 • Letter: I
Question
I need help finishing this java code
This is what i have so far
import java.text.DecimalFormat;
public class assigment {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DecimalFormat df = new DecimalFormat("#.##");
double interest = calculateInterest(1500.0,0.043,4.0,6.0);
System.out.println(df.format(interest));
}
private static double calculateInterest(double p, double j, double n, double t ) {
return Math.pow( (1+(j/n)),n*t)*p;
}
public static String returninterestresults(double p, double j, double n, double t ){
for(double interest= 0.01; interest >= 0.10; interest +=0.5) {
}
return "";
}
}
Explanation / Answer
1)
Program Code:
import java.text.DecimalFormat;
public class Assignment
{
public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat("#.##");
//calculate the final balance by calling the calculateInterest
double interest = calculateInterest(1500.0,0.043,4.0,6.0);
//
System.out.println("The compound interest is: "+df.format(interest));
}
private static double calculateInterest(double p, double j, double n, double t )
{
double Balance=0;
double interest=0;
interest=Math.pow( (1+(j/n)),n*t);
Balance=interest*p;
return Balance;
}
}
Sample output:
The compound interest is: 1938.84
2)
import java.text.DecimalFormat;
public class Assignment
{
public static void main(String[] args)
{
//calculate the final balance by calling the calculateInterest
calculateInterest(1500.0,4.0,6.0);
}
private static void calculateInterest(double p, double n, double t )
{
//create an object to the DecimalFormat
DecimalFormat df = new DecimalFormat("#.##");
double Balance=0;
double interest=0;
//compute the Balance for the respective annual interest rates.
//Since the rate of interest is not specified whether it is
//a monthly interest or the percent so
//the code added is directly considering the each loop value as annual
//interest.
for(double i=1;i<=10;i=i+0.5)
{
//computing the power function
interest=Math.pow( (1+(i/n)),n*t);
//computing the balance
Balance=interest*p;
//Print the balance for the respective interest rates
System.out.println("The balance for interest "+i+" is = "+df.format(Balance));
}
}
}
Sample Output:
The balance for interest 1.0 is = 317637.36
The balance for interest 1.5 is = 3128643.04
The balance for interest 2.0 is = 25251168.29
The balance for interest 2.5 is = 172413801.11
The balance for interest 3.0 is = 1020949891.99
The balance for interest 3.5 is = 5347142875.43
The balance for interest 4.0 is = 25165824000
The balance for interest 4.5 is = 107821578189.44
The balance for interest 5.0 is = 425081000142.7
The balance for interest 5.5 is = 1556030101241.96
The balance for interest 6.0 is = 5329070518200.75
The balance for interest 6.5 is = 17186785028295.58
The balance for interest 7.0 is = 52489920014804.94
The balance for interest 7.5 is = 152545582518407.44
The balance for interest 8.0 is = 423644304721500
The balance for interest 8.5 is = 1128474576789396
The balance for interest 9.0 is = 2892623582658713.5
The balance for interest 9.5 is = 7155861248802100
The balance for interest 10.0 is = 17128696863105870
3)
import java.util.*;
import java.io.*;
//WritingDataToFile class contains a function that
//takes parameters as filename, n, t, p values
//and write the data to the file
public class WritingDataToFile
{
//main function
public static void main(String args[] ) throws Exception
{
//create a file object
File in=new File("datafile.txt");
int p=1000;
int n=4;
int t=10;
//call the function writeData to write the
//data into the file
writeData(in, p, n, t);
}
//method writeData checks whether the file exist or not
//if exist it creates a PrintWriter object and writes
//the data into the file using the println function.
//Since, we are dealing with the files either write a throws clause
//or put the code inside the try..catch block
public static void writeData(File fname, int p, int n, int t)
{
try
{
if(fname.exists())
{
PrintWriter in=new PrintWriter(fname);
in.println("Interest Amounts");
in.println("p = "+p);
in.println("n = "+n);
in.println("t = "+t);
in.close();
}
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
}
Sample Output Code:
Initial output text file: datafile.txt
{empty}
After executing the code:
Interest Amounts
p = 1000
n = 4
t = 10
4)
import java.text.DecimalFormat;
import java.util.*;
import java.io.*;
//WritingDataToFile class contains a function that
//takes parameters as filename, n, t, p values
//and write the data to the file
public class WritingDataToFile
{
//main function
public static void main(String args[] ) throws Exception
{
//create a file object
File in=new File("datafile.txt");
int p=1000;
int n=4;
int t=10;
//call the function writeData to write the
//data into the file
writeData(in, p, n, t);
writeDataFile(in, p, n, t);
}
//method writeData checks whether the file exist or not
//if exist it creates a PrintWriter object and writes
//the data into the file using the println function.
//Since, we are dealing with the files either write a throws clause
//or put the code inside the try..catch block
public static void writeData(File fname, int p, int n, int t)
{
try
{
if(fname.exists())
{
PrintWriter in=new PrintWriter(fname);
in.println("Interest Amounts");
in.println("p = "+p);
in.println("n = "+n);
in.println("t = "+t);
in.close();
}
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
public static void writeDataFile(File fname, int p, int n , int t)
{
try
{
if(fname.exists())
{
PrintWriter in=new PrintWriter(new FileOutputStream(fname, true));
DecimalFormat df = new DecimalFormat("#.##");
double Balance=0;
double interest=0;
for(double i=1;i<=10;i=i+0.5)
{
//computing the power function
interest=Math.pow( (1+(i/n)),n*t);
//computing the balance
Balance=interest*p;
in.append("The balance for interest "+i+" is = "+df.format(Balance)+" ");
}
in.close();
}
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
private static void calculateInterest(double p, double n, double t )
{
//create an object to the DecimalFormat
DecimalFormat df = new DecimalFormat("#.##");
double Balance=0;
double interest=0;
//compute the Balance for the respective annual interest rates.
//Since the rate of interest is not specified whether it is
//a monthly interest or the percent so
//the code added is directly considering the each loop value as annual
//interest.
for(double i=1;i<=10;i=i+0.5)
{
//computing the power function
interest=Math.pow( (1+(i/n)),n*t);
//computing the balance
Balance=interest*p;
//Print the balance for the respective interest rates
System.out.println("The balance for interest "+i+" is = "+df.format(Balance));
}
}
}
Sample Output file: datafile.txt
Interest Amounts
p = 1000
n = 4
t = 10
The balance for interest 1.0 is = 7523163.85
The balance for interest 1.5 is = 340492795.15
The balance for interest 2.0 is = 11057332320.94
The balance for interest 2.5 is = 271728137858.88
The balance for interest 3.0 is = 5266498289314.88
The balance for interest 3.5 is = 83186122741949.94
The balance for interest 4.0 is = 1099511627776000
The balance for interest 4.5 is = 12426744681243760
The balance for interest 5.0 is = 122264598055704640
The balance for interest 5.5 is = 1063027643880327940
The balance for interest 6.0 is = 8271806125530276900
The balance for interest 6.5 is = 58233421752616660000
The balance for interest 7.0 is = 374375787445778100000
The balance for interest 7.5 is = 2215715322550481600000
The balance for interest 8.0 is = 12157665459056929000000
The balance for interest 8.5 is = 62230152778611420000000
The balance for interest 9.0 is = 298768247169762650000000
The balance for interest 9.5 is = 1351920291788082300000000
The balance for interest 10.0 is = 5790576106764119000000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.