Question 1:
Write the temperature conversion program. Your GUI application must inherit from the JFrame class. The GUI and event handling setup should be done in the constructor. Do not use any of the GUI editing capabilities of Eclipse for this assignment. The temperature conversion application should have a label and JTextField where the user inputs a value which must appear in the upper part of the frame. There should be a set of three radio buttons which indicate the input scale of the value to be converted.
There should also be a set of three radio buttons which indicate the output scale to be converted to. The three input scale buttons must appear vertically aligned (i.e., use a JPanel) on the left side of the display, and the three output scale buttons must appear vertically aligned (i.e., use another JPanel) and appear on the right side of the display.  Event handling should be setup so that selection of any input or output radio button causes an event which triggers the event handling code to determine which of nine possible conversions is needed. You can display the result in an output text field or in a Jlabel which appears in the bottom part of the display.

Your program should accurately convert from Fahrenheit, Celsius, Kelvin to Fahrenheit, Celsius, Kelvin. NOTE: Only the selected conversion is displayed in the output area!!! When the conversion selection changes, the output area should change to show only the new result. The output should display three digits after the decimal point. HINT: Use the ItemListener interface and use the isSelected method on the radio buttons to learn which buttons are turned on!

Solution 1:

import java.util.Scanner;

public class MortagageCalculator
{

              final static int MONTHS_IN_YEAR=12;
             
              public static void main(String[] args)
              {
                            String strLoanAmount="";
                            double loanAmount=0;
                            int[] years={7,15,30};
                            double[] rates={5.35,5.5,5.75};
                            double[] mortagagePayments=new double[3];
                           
                            Scanner inputLoanAmount=new Scanner(System.in);
                            System.out.println("ENTER THE LOAN AMOUNT: ");
                            try
                            {
                                          if(inputLoanAmount.hasNextLine())
                                                        strLoanAmount=inputLoanAmount.nextLine();
                                                                                                                loanAmount=Double.parseDouble(strLoanAmount.substring(strLoanAmount.indexOf('$')+1));
                            }
                            catch(NumberFormatException nME)
                            {
                                          System.err.println("INPUT NOT A NUMBER");
                                          System.exit(0);
                            }
                           
                            if(loanAmount<=0)
                            {
                                          System.err.println("INVALID LOAN AMOUNT");
                                          System.exit(0);
                            }
                           
                            for(int count=0;count<3;count++)
                            {
                                          double i=(rates[count]/100)/MONTHS_IN_YEAR;
                                          int n=MONTHS_IN_YEAR*years[count];
                                          mortagagePayments[count]=loanAmount*(i*(Math.pow((1+i),n)))/(Math.pow(1+i,n)-1);
                            }
                           
                            System.out.println("Loan Term              "+"Interest Rate              "+"Mortgage Payment");
                            for(int count=0;count<3;count++)
                            {
                                          System.out.println(years[count]+"years                            "+rates[count]+"%                            "+String.format("$%.2f", mortagagePayments[count]));
                            }
              }

}

 

Question 2:
1) Write a client program that populates an array of Account objects, where each element is either a CheckingAccount object or a SavingsAccount object (use several of each object)

2) After the array has been populated, loop through each element and perform the following tasks and call the appropriate member functions:

• Display the initial account balance

• Prompt and enter a withdrawal amount and Debit() the account

• Prompt and enter a deposit amount and Credit() the account

• If the current element is a SavingsAccount object, then calculateInterest() earned and Credit() the account by that amount

• Finally, Print the updated balance by calling getBalance() 3) After the loop has terminated, Print a heading and start another loop and that calls your toString() method to print each element in the array.

Solution 2:

Save the file as Account.java

import java.util.Scanner;

public class Account
{

      /**
* data members
*/
private double balance;
private Scanner keyboardInput;

/**
* one argument constructor
* @param balance
*/
public Account(double balance)
{
this.balance = balance;
keyboardInput=new Scanner(System.in);
}

      /**
* member function to display initial balance
*/
public void displayInitialBalance()
{
System.out.printf("Initial Balance= %.2f\n",balance);
}

/**
* member function to withdraw amount
*/
public void withdrawAmount()
{
double withdrawalAmount;
String strWithdrawalAmount;
System.out.println("ENTER THE WITHDRAWAL AMOUNT: ");
strWithdrawalAmount=keyboardInput.nextLine();
try
{
withdrawalAmount=Double.parseDouble(strWithdrawalAmount);
if(withdrawalAmount>0 && (balance-withdrawalAmount)>=0)
balance-=withdrawalAmount;
else
{
System.out.println("INVALID AMOUNT OR INSUFFICIENT BALANCE");
System.out.println("TRY AGAIN");
withdrawAmount();
}

}
catch(NumberFormatException nME)
{
System.out.println("INPUT NOT A NUMBER");
System.out.println("TRY AGAIN");
withdrawAmount();
}
}

/**
* member function to deposit amount
*/
public void depositAmount()
{
double depositedAmount;
String strDepositedAmount;
System.out.println("ENTER THE DEPOSITED AMOUNT: ");
strDepositedAmount=keyboardInput.nextLine();
try
{
depositedAmount=Double.parseDouble(strDepositedAmount);
if(depositedAmount>0)
balance+=depositedAmount;
else
{
System.out.println("INVALID AMOUNT");
System.out.println("TRY AGAIN");
withdrawAmount();
}
}
catch(NumberFormatException nME)
{
System.out.println("INPUT NOT A NUMBER");
System.out.println("TRY AGAIN");
depositAmount();
}
}

/**
* accessor function for balance
* @return
*/
public double getBalance()
{
return balance;
}

      /**
* @param balance the balance to set
*/
public void setBalance(double balance)
{
this.balance = balance;
}

      @Override
public String toString()
{
return String.format("Balance= %.2f",balance);
}

}

----------------------------------------------END------------------------------------

 

Save the file as SavingsAccount.java

public class SavingsAccount extends Account
{
private static final double INTEREST_RATE_PERCENT=3.5;
double interest;

      /**
* one argument constructor
* @param balance
*/
public SavingsAccount(double balance)
{
super(balance);
}

/**
* member function to calculate interest
*/
public void calculateInterest()
{
//interest calculation for 1 year period
interest=getBalance()*INTEREST_RATE_PERCENT/100;
credit();
}

/**
* member function to add interest to balance
*/
public void credit()
{
setBalance(getBalance()+interest);
}

      @Override
public String toString() {
return String.format("%s\nSavingsAccount interest= %.2f",super.toString(),interest);
}

}

----------------------------------------------------------END------------------------

 

Save the file as CheckingAccount.java

 

public class CheckingAccount extends Account
{
/**
* one argument constructor
* @param balance
*/
public CheckingAccount(double balance)
{
super(balance);
}

}

-----------------------------------------------END-----------------------------------

Save  the file as AccountTest.java

public class AccountTest
{
public static void main(String[] arg)
{
//savings account objects
SavingsAccount sA1=new SavingsAccount(500.00);
SavingsAccount sA2=new SavingsAccount(200.00);
SavingsAccount sA3=new SavingsAccount(100.00);

//checking account objects
CheckingAccount cA1=new CheckingAccount(400.00);
CheckingAccount cA2=new CheckingAccount(450.00);
CheckingAccount cA3=new CheckingAccount(600.00);

//initialize the array with Accounts
Account accounts[]=new Account[6];
accounts[0]=sA1;
accounts[1]=sA2;
accounts[2]=sA3;
accounts[3]=cA1;
accounts[4]=cA2;
accounts[5]=cA3;

System.out.println("Accounts processed polymorphically:\n" );

int counter=1;
// generically process each element in array accounts
for(Account currentAccount : accounts)
{
System.out.println("Account #"+counter++);
currentAccount.displayInitialBalance();
currentAccount.withdrawAmount();
currentAccount.depositAmount();

if(currentAccount instanceof SavingsAccount)
{
((SavingsAccount) currentAccount).calculateInterest();
}

System.out.printf("Updated Balance: %.2f\n\n",currentAccount.getBalance());
}

System.out.println("BALANCE IN EACH ACCOUNT");

for(int i=0;i<accounts.length;i++)
{
System.out.println("Account is a "+accounts[i].getClass().getName());
System.out.printf("%s\n\n",accounts[i]);
}
}
}

 

------------------------------------------END----------------------------------------

 

Question 3:
Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans:

7 years at 5.35%
15 years at 5.5%
30 years at 5.75%

Use an array for the different loans. Display the mortgage payment amount for each loan.

1.    The loan amount is not specified in. The program must add an interface to prompt the user to enter a loan amount.
2.    Error checking should be done on the input loan amount to make sure that it is a positive number and is not zero.

Solution 3:

import java.util.Scanner;

public class MortagageCalculator
{

              final static int MONTHS_IN_YEAR=12;
             
              public static void main(String[] args)
              {
                            String strLoanAmount="";
                            double loanAmount=0;
                            int[] years={7,15,30};
                            double[] rates={5.35,5.5,5.75};
                            double[] mortagagePayments=new double[3];
                           
                            Scanner inputLoanAmount=new Scanner(System.in);
                            System.out.println("ENTER THE LOAN AMOUNT: ");
                            try
                            {
                                          if(inputLoanAmount.hasNextLine())
                                                        strLoanAmount=inputLoanAmount.nextLine();
                                                                                                                loanAmount=Double.parseDouble(strLoanAmount.substring(strLoanAmount.indexOf('$')+1));
                            }
                            catch(NumberFormatException nME)
                            {
                                          System.err.println("INPUT NOT A NUMBER");
                                          System.exit(0);
                            }
                           
                            if(loanAmount<=0)
                            {
                                          System.err.println("INVALID LOAN AMOUNT");
                                          System.exit(0);
                            }
                           
                            for(int count=0;count<3;count++)
                            {
                                          double i=(rates[count]/100)/MONTHS_IN_YEAR;
                                          int n=MONTHS_IN_YEAR*years[count];
                                          mortagagePayments[count]=loanAmount*(i*(Math.pow((1+i),n)))/(Math.pow(1+i,n)-1);
                            }
                           
                            System.out.println("Loan Term"+"Interest Rate"+"Mortgage Payment");
                            for(int count=0;count<3;count++)
                            {
                                          System.out.println(years[count]+"years"+rates[count]+"%"+String.format("$%.2f", mortagagePayments[count]));
                            }
              }

}