Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can I get some help with this java looping assignment? ASSIGNMENT Loop Processin

ID: 3681453 • Letter: C

Question

Can I get some help with this java looping assignment?

ASSIGNMENT Loop Processing I. Define a Java class as described below. All variables should be declared as private and all methods as public. Save the class definition in a file with an appropriate filename. Class Name Invest Symbolic Constants: MIN_INVEST- the minimum amount of initial investment accepted without penalty: this should be initialized to $1500.00 FEE- the surcharge subtracted from the initial investment if it is less than the min amount (above): this should be initialized to $100.00 Class variable defaultRate the default interest rate to be used if the user does not specify one: this should be initialized to 5%. This variable should be declared public Instance Variables: intRate-double (the interest rate as a % value) (Fields) investAmount double (the initial invested amount) investBalance -double (the current value of the investment) Instance Methods: A constructor that accepts one argument representing the initial invested amount to be assigned to the investAmount field. The investBalance is determined as follows: If the initial investment is less than the MIN_INVEST the FEE is deducted otherwise the value in this field is the same as the input value. The intRate field is set to the value of the default interest rate class variable. A constructor that accepts two arguments, an interest rate and an initial invested amount that are assigned to their respective class fields. The investment balance is to be determined as above A default constructor that does nothing (e.g.: a null constructor).

Explanation / Answer

######## Invest.java ################

public class Invest {

   public static final double MIN_INVEST = 150.00;
   public static final double FEE = 100.00;
  
   public static double defaultRate = 5;
  
   private double intRate;
   private double investAmount;
   private double investBalance;
  
   public Invest(double investAmount) {
       this.investAmount = investAmount;
       if(investAmount < MIN_INVEST){
           investBalance = investAmount - FEE;
       }else
           investBalance = investAmount;
       intRate = defaultRate;
   }
  
   public Invest(double intRate, double investAmount) {
       this.investAmount = investAmount;
       if(investAmount < MIN_INVEST){
           investBalance = investAmount - FEE;
       }else
           investBalance = investAmount;
       this.intRate = intRate;
   }
  
   public Invest() {
      
   }

   public double getIntRate() {
       return intRate;
   }

   public double getInvestAmount() {
       return investAmount;
   }

   public double getInvestBalance() {
       return investBalance;
   }

   public double calcEarning(){
       double totalInterest = 0;
       for(int i=1; i<=12; i++){
           double interest = (investBalance*(intRate/100.0))/12.0;
          
           totalInterest = totalInterest+interest;
       }
       investBalance = investBalance+totalInterest;
   return totalInterest;
   }
  
  
  
}

################TestInvest.java ########################

import java.util.Scanner;

public class TestInvest {

   // method to return invest Object
   public Invest getInstance(){
       Scanner sc = new Scanner(System.in);
       Invest invest = null;
       System.out.println("Enter invested amount: ");
       double amount = sc.nextDouble();
       char c = 'n';
       System.out.println("Do you want to enter interest rate ? (y/n)");
       c = sc.next().charAt(0);
       if(c=='y'){
           System.out.println("Enter interest rate: ");
           double rate = sc.nextDouble();
           invest = new Invest(rate, amount);
       }else
           invest = new Invest(amount);
       return invest;
   }
   // calculate earning method
   public void calcEarning(int year, Invest invest){
      
       double yearlyInterest = 0;
       for(int i=1; i<=year; i++){
           yearlyInterest = invest.calcEarning();
           System.out.println("Total earned interest at end of year "+i+" :"+String.format("%.2f", yearlyInterest));
           System.out.println("Current balance at end of year "+i+" :"+String.format("%.2f", invest.getInvestBalance()));
       }
      
   }
   public static void main(String[] args) {
       int choice = 0;
       Scanner sc = new Scanner(System.in);
       Invest invest = null;
       int year = 0;
       boolean flag = false;
       TestInvest test = new TestInvest();
       while(choice !=5){
           System.out.println("1. New Investment");
           System.out.println("2. Calculate Earnings");
           System.out.println("3. Summary Report");
           System.out.println("4. Change Int. Rate");
           System.out.println("5. Quit");
          
           choice = sc.nextInt();
          
           switch(choice){
          
           case 1:
                   System.out.println("Enter number of years: ");
                   year = sc.nextInt();
                   invest = test.getInstance();
                   break;
           case 2:
                   if(flag){
                       System.out.println("You have already called this method.");
                   }else{
                       flag = true;
                       test.calcEarning(year, invest);
                   }
                   break;
           case 3:
                   System.out.println("Int. Rate: "+String.format("%.2f", invest.getIntRate()));
                   System.out.println("Initial Amount: "+String.format("%.2f", invest.getInvestAmount()));
                   System.out.println("Current balance: "+String.format("%.2f", invest.getInvestBalance()));
                   break;
           case 4:
                   System.out.println("Enter new default interest rate: ");
                   Invest.defaultRate = sc.nextDouble();
                   break;
           case 5:
                   choice = 5; // loop exist condition
           }
           System.out.println();
       }
   }
}

/*

Output:

1. New Investment
2. Calculate Earnings
3. Summary Report
4. Change Int. Rate
5. Quit
1
Enter number of years:
3
Enter invested amount:
400
Do you want to enter interest rate ? (y/n)
n

1. New Investment
2. Calculate Earnings
3. Summary Report
4. Change Int. Rate
5. Quit
3
Int. Rate: 5.00
Initial Amount: 400.00
Current balance: 400.00

1. New Investment
2. Calculate Earnings
3. Summary Report
4. Change Int. Rate
5. Quit
2
Total earned interest at end of year 1 :20.00
Current balance at end of year 1 :420.00
Total earned interest at end of year 2 :21.00
Current balance at end of year 2 :441.00
Total earned interest at end of year 3 :22.05
Current balance at end of year 3 :463.05

1. New Investment
2. Calculate Earnings
3. Summary Report
4. Change Int. Rate
5. Quit
5

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote