Question

In: Computer Science

The goal of Java program(s) implemented as part of this assignment is: 1. To use for,...

The goal of Java program(s) implemented as part of this assignment is:
1. To use for, while, do-while loops to solve the given problem(s)
2. Write custom method(s)
Note:
Be sure to document your code thoroughly. In other words, you must comment
each pertinent line of code to briefly state what it is intending to do.

Requirement-1

In the Requirement-1 you will create a program named
‘ComputeCompoundInterest, that will compute compound interest using the
formula as follows:
amount = principal * ( 1 + rate )numYears
(a) Ask the user to enter the following 3 data elements:
(i) The ‘Principal’
(ii) The ‘Interest Rate’
(iii) The ‘Number of Years’
(b) You will write the following 3 custom methods, each using a different loop
type to compute and print compound interest using the above mentioned
formula:
(i) “computeWhile”
(ii) “computeDoWhile”
(iii) “computeFor”
March 2, 2019
page 3
(c) Each method will take 3 parameters, as mentioned in (a) above, with the
right type, and will not be returning anything. So be sure the method
signature is defined the right way. Hint: return value should be ‘void’
(d) Each of the 3 methods should be called/invoked from the main() method of
your class
(e) The output from each of the 3 methods should look like the following. The
number of rows of output will depend on the number of years entered by
the user input. Sample output should like the following:
Output from ‘while’ loop:
Year 1 Amount: $xxx
Year 2 Amount: $yyy
…..
…..
Output from ‘do-while’ loop:
Year 1 Amount: $xxx
Year 2 Amount: $yyy
…..
…..
Output from ‘for’ loop:
Year 1 Amount: $xxx
Year 2 Amount: $yyy
…..
…..
Note:
• Please add a blank line after the output from each method is completed

Solutions

Expert Solution

Java Code:

Output Snapshot:

Text Code:

import java.io.*;
import java.util.Scanner; // Importing Scanner class for user input

public class Main
{
/* Main */
   public static void main(String[] args) {
   /* Creating scanner class object to take input from user */
   Scanner userInput = new Scanner(System.in);
  
/* Prompting user to input Principal Amount */
System.out.print("\nEnter Principal Amount :$");
double principal = userInput.nextDouble(); // Storing Double value from user
/* Prompting user to input Interest Rate */
System.out.print("\nEnter Interest Rate (0.00 - 1.00) :");
double interestRate = userInput.nextDouble(); // Storing Double value from user
/* Prompting user to input Number of Years */
System.out.print("\nEnter Number of Years :");
int numOfYear = userInput.nextInt(); // Storing Integer value from user
/* Calling methods to calculate compound interest */
       computeWhile(principal, interestRate, numOfYear);
       computeDoWhile(principal, interestRate, numOfYear);
       computeFor(principal, interestRate, numOfYear);
   }
  
   /*
   * Method Name : computeWhile
   * Input Arguments : principal : principal amount to calculate compound interest
   interestRate : interest rate on the principal amount
   numOfYear : number of years to calculate compound interest
   * Output Argument : void
   * Description : To calculate compound interest using while loop and display Output
   on output screen
   */
   public static void computeWhile(double principal, double interestRate, int numOfYear){
   System.out.println("\nOutput from ‘while’ loop:");
   int i=1; // loop counter initialisation
   while(i<=numOfYear){ // when counter value is less than or equal to numOfYear
   /* Calculating amount using compound interest formula */
   double amount = principal*Math.pow((1+interestRate),i);
   /* Displaying the output for each year */
   System.out.println("\t Year " + i + " Amount: $" + amount);
   /* Incrementing loop counter */
   i++;
   }
   }
   /*
   * Method Name : computeDoWhile
   * Input Arguments : principal : principal amount to calculate compound interest
   interestRate : interest rate on the principal amount
   numOfYear : number of years to calculate compound interest
   * Output Argument : void
   * Description : To calculate compound interest using do-while loop and display Output
   on output screen
   */
   public static void computeDoWhile(double principal, double interestRate, int numOfYear){
   System.out.println("\nOutput from ‘do-while’ loop:");
   /* Calculating amount using compound interest formula */
   int i=1;
   do{
   /* Calculating amount using compound interest formula */
   double amount = principal*Math.pow((1+interestRate),i);
   /* Displaying the output for each year */
   System.out.println("\t Year " + i + " Amount: $" + amount);
   /* Incrementing loop counter */
   i++;
   }while(i<=numOfYear);

   }
   /*
   * Method Name : computeFor
   * Input Arguments : principal : principal amount to calculate compound interest
   interestRate : interest rate on the principal amount
   numOfYear : number of years to calculate compound interest
   * Output Argument : void
   * Description : To calculate compound interest using for loop and display Output
   on output screen
   */
   public static void computeFor(double principal, double interestRate, int numOfYear){
   System.out.println("\nOutput from ‘for’ loop:");   
   for(int i=1; i<=numOfYear; i++){
   /* Calculating amount using compound interest formula */
   double amount = principal*Math.pow((1+interestRate),i);
   /* Displaying the output for each year */
   System.out.println("\t Year " + i + " Amount: $" + amount);
   }
   }
  
}

Explanation:

  • The formula used for calculating the compound interest amount is as following:
  • Please note that, the interest rate range is 0.00 (for 0 % interest rate) to 1.00 (for 100% interest rate). For 10% interest rate, input must be 0.10.
  • Please refer code comments for the understanding purpose.

Related Solutions

The goal of Java program implemented as part of this assignment is to build an Inventory...
The goal of Java program implemented as part of this assignment is to build an Inventory Management System, from here on referred to as IMS, for a bookstore. You will need to do the following tasks: Requirement-1 – Create 3 different Arrays (15 pts) (a) Create an array named items that will hold the following item names (strings) - Pen - Pencil - Eraser - Marker - Notepad (b) Create a second array named quantity, that will hold corresponding quantities...
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program...
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program that uses repetition (i.e. “loops”) and decision structures to solve a problem. 2. PROBLEM DEFINITION  Write a Python program that performs simple math operations. It will present the user with a menu and prompt the user for an option to be selected, such as: (1) addition (2) subtraction (3) multiplication (4) division (5) quit Please select an option (1 – 5) from the...
Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods...
Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht can olny sepll a wrod one way.”...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create a java class InventoryItem which has a String description a double price an int howMany Provide a copy constructor in addition to other constructors. The copy constructor should copy description and price but not howMany, which defaults to 1 instead. In all inheriting classes, also provide copy constructors which chain to this one. Write a clone method that uses the copy constructor to create...
Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on this assignment. 1. A structure named student which contains:   a. int ID; student id; b. last name; // as either array of char or a string c. double GPA;    2. An input file containing the information of at least 10 students. 3. An array of struct: read the student information from the input file into the array. 4. A stack: you can use the...
For Part 2 of this assignment, you will use the “Assignment 1 – Linear Kinematics Data”...
For Part 2 of this assignment, you will use the “Assignment 1 – Linear Kinematics Data” excel file. In the data set you are provided with vertical position and time data for a person’s vertical center of mass motion for an unspecified movement task. You will utilize excel in all (well, some…) of its glory to calculate the vertical velocity and vertical acceleration data from the position and time data provided in the excel file. Again you will use the...
Suppose the interface and the class of stack already implemented, Write application program to ( java)...
Suppose the interface and the class of stack already implemented, Write application program to ( java) 1- insert 100 numbers to the stack                         2- Print the even numbers 3- Print the summation of the odd numbers
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
1.Which of the following is not a goal of the internal controls implemented by owners and...
1.Which of the following is not a goal of the internal controls implemented by owners and managers? Multiple Choice to safeguard assets to ensure reliability of accounting data to promote compliance with management policies and applicable laws to reduce expenses through the use of efficient processes 2.The three major legal forms of business entity are the sole proprietorship, the partnership, and the __________. Multiple Choice merchandiser corporation service business small business 3.The financial statements submitted to the SEC by a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT