In: Computer Science
Do not use import java.util.Scanner;
Working in the Financial Aid Office you have been asked to create a program that calculates the amount of financial aid a family is eligible for, based on the number of dependents in the household. The business rules for determining eligibility are as follows:
You can use the following stub solution. Copy and paste this solution into your IDE. You should complete blocks 1 through 4.
import javax.swing.JOptionPane;
public class FinancialAid {
public static void main(String[] args) {
final int NUM_MIN_DEPENDENTS =
0;
final int NUM_MAX_DEPENDENTS =
9;
/* #1: Create code that will
call the method you created in #3 below to get the number of
dependents,
*
factoring in the minimum and maximum number of dependents
allowed
*/
/* #2: Create code that will
call the method you created in #4 below to calculate the amount of
aid
*
that will be earned based on the number of dependents
gathered.
*/
printFinancialAid(aid);
}
/* #3: Create a method to get and return the
number of dependents, factoring in
* the minimum and
maximum number of dependents allowed
*/
/* #4: Create a method to calculate and return
the amount of financial aid that will be earned
* based
on the number of dependents gathered
*/
public static void printFinancialAid(double aid)
{
JOptionPane.showMessageDialog(null,
"The amount of financial aid your family is eligible for is: $" +
String.format("%.2f", aid));
}
}
JAVA PROGRAM:
import javax.swing.JOptionPane;
public class Test {
public static void main(String[] args) {
final int NUM_MIN_DEPENDENTS = 0;
final int NUM_MAX_DEPENDENTS = 9;
/** #1: Create code that will call the method you created in
* #3 below to get the number of dependents,
* factoring in the minimum and maximum number of dependents allowed
* */
int num = getNumberOfDependents();
/* check enter number of dependents between 0-9*/
while(num<NUM_MIN_DEPENDENTS || num>NUM_MAX_DEPENDENTS){
// if num out range btween 0-9 then ask again
num = getNumberOfDependents();
}
/** #2: Create code that will call the method you created in #4 below to calculate the amount of aid
* that will be earned based on the number of dependents gathered.*/
double aid = calculateAmount(num);
printFinancialAid(aid);
}
/** #3: Create a method to get and return the number of dependents, factoring in
* the minimum and maximum number of dependents allowed */
public static int getNumberOfDependents() {
// show the dialog box for input
String input = JOptionPane.showInputDialog("Enter the number of dependents:");
int num = Integer.parseInt(input);// convert string to int
return num;
}
/** #4: Create a method to calculate and return the amount of financial aid that will be earned
* based on the number of dependents gathered */
public static double calculateAmount(int num) {
// Declare variable amount
double amount = 0;
// If there are 2 dependents in the household the family is eligible for $1,500 per dependent
if(num==2){
amount = num * 1500;
//If there are 3 dependents in the household the family is eligible for $2,500 per dependent
}else if(num==3){
amount = num * 2500;
//If there are 4 dependents in the household the family is eligible for $3,000 per dependent
}else if(num==4){
amount = num * 3000;
// In all other situtations, the family is not eligible for any financial aid
}else{
amount=0;
}
return amount;
}
public static void printFinancialAid(double aid) {
JOptionPane.showMessageDialog(null, "The amount of financial aid your family is eligible for is: $" + String.format("%.2f", aid));
}
}
OUTPUT:
NOTE: If you don't understand any step please let me know at once.