You are on page 1of 2

public void exercise4_22()

{
Scanner s = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
System.out.print("Loan amount: ");
double balance = s.nextDouble();
System.out.print("Number of Years: ");
int numberOfYears = s.nextInt();
System.out.print("Annual Interest Rate: ");
double annualInterestRate = s.nextDouble();
double monthlyInterestRate = annualInterestRate / 1200.0D;
double monthlyPayment = balance * monthlyInterestRate / (1.0D - 1.0D /
Math.pow(1.0D + monthlyInterestRate, numberOfYears * 12));
System.out.println("\nMonthly Payment: " + df.format(monthlyPayment));
System.out.println("Total Payment: " + df
.format(monthlyPayment * 12.0D * numberOfYears));
System.out.println("\nPayment#\tInterest\tPrinciple\tBalance");
for (int i = 1; i < numberOfYears * 12 + 1; i++)
{
double interest = monthlyInterestRate * balance;
double principle = monthlyPayment - interest;
balance -= principle;
System.out.println(i + "\t\t" + df.format(interest) + "\t\t" + df
.format(principle) +
"\t\t" + df
.format(balance));
}
}
public static void exercise8_14()
{
StopWatch newTime = new StopWatch();
int[] list = new int[100000];
for (int i = 0; i < list.length; i++) {
list[i] = i;
}
newTime.start();
for (int i = 0; i < list.length - 1; i++) {
int currentMax = list[i];
int currentMaxIndex = i;
for (int j = i + 1; j < list.length; j++)
{
if (currentMax < list[j]) {
currentMax = list[j];
currentMaxIndex = j;
}
}
if (currentMaxIndex != i) {
list[currentMaxIndex] = list[i];
list[i] = currentMax;
}
}
newTime.stop();
System.out.println("Time taken to sort numbers in descending order: " + newT
ime
.getElapsedTime() + " milliseconds");
}
public void exercise11_8()
{
Account george = new Account("George", 1122, 1000.0D, 1.5D);
Transaction d30 = new Transaction(george, 'D', 30.0D, "Deposit");
Transaction d40 = new Transaction(george, 'D', 40.0D, "Deposit");
Transaction d50 = new Transaction(george, 'D', 50.0D, "Deposit");
Transaction w5 = new Transaction(george, 'W', 5.0D, "Withdrawal");
Transaction w4 = new Transaction(george, 'W', 4.0D, "Withdrawal");
Transaction w2 = new Transaction(george, 'W', 2.0D, "Withdrawal");
System.out.println(george.toString());
}

You might also like