Benefit calculator

Assignment 

Programming Assignment

This programming assignment is intended to demonstrate your knowledge of the following:
▪ Write a while loop
▪ Write methods and calling methods

Financial Assistance

A non-governmental organization needs a program to calculate the amount of financial assistance forneedy families.
The formula is as follows:
• If the annual household income is between $30,000 and $40,000 and the household has at least
three children, the amount is $1,000 per child.
• If the annual household income is between $20,000 and $30,000 and the household has at least
two children, the amount is $1,500 per child.
• If the annual household income is less than $20,000, the amount is $2,000 per child.
Write a program that asks for the house-hold income and number of children for each applicant, printingthe amount returned by your method.

Input Method Specs

  1. doublegetIncome()
    This method requests an annual household income from the user and continues to ask for the
    income value unntil the user gets it right. This method should not accept any arguments.
    Intgetnumber()
    This method requests a number of children and continues to ask for the number until the user getsit right. This method should not accept any arguments.
    3. double computeAssistance(double income, int number)
    This method accepts both the annual household income and the number of children as parametersand return the amount of financial assistance.
  2. Input Errors
    Whenever the user makes an input error, keep at them until they get it right. Do not return from an inputmethod until you have acquired a legal value, even if it takes years. 

Solution 

FinancialAssistance.java 

importjava.util.Scanner;

/*

* Class: CS1A

* Description: A non-governmental organization needs a program to calculate the amount of financial assistance for needy families

* Due date:

* Name: (your name)

* File name: FinancialAssistance.java

*/

public class FinancialAssistance {

static Scanner scan = new Scanner(System.in);

/*

* This method requests an annual household income from the user and continues

* to ask for the income value unntil the user gets it right. This method should

* not accept any arguments

*/

public static double getIncome() {

double income = 0;

while (income <= 0) {

System.out.print(“Please enter annual income: “);

income = scan.nextDouble();

if (income <= 0)

System.out.println(“Invalid income. Try again”);

}

return income;

}

/*

* This method requests a number of children and continues to ask for the number

* until the user gets it right. This method should not accept any arguments

*/

public static intgetnumber() {

intchildNum = 0;

while (childNum<= 0) {

System.out.print(“Please enter number of children: “);

childNum = scan.nextInt();

if (childNum<= 0)

System.out.println(“Invalid income. Try again”);

}

returnchildNum;

}

public static double computeAssistance(double income, int number) {

/*

* If the annual household income is between $30,000 and $40,000 and the

* household has at least three children, the amount is $1,000 per child

*/

if (income >= 30000 && income <= 40000) {

if (number >= 3)

return 1000 * number;

}

/*

* If the annual household income is between $20,000 and $30,000 and the

* household has at least two children, the amount is $1,500 per child.

*/

else if (income >= 20000 && income <= 30000) {

if (number >= 2)

return 1500 * number;

}

/*

* If the annual household income is less than $20,000, the amount is $2,000 per

* child.

*/

else if (income < 20000)

return 2000 * number;

/*

* Other case is not quality

*/

return 0;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

for (int i = 0; i < 3; ++i) {

System.out.println(“===================================================”);

System.out.println(“Run at ” + (i + 1) + ” times”);

double income = getIncome();

intchildNum = getnumber();

System.out.println(“Your income: “+ income);

System.out.println(“Your children number: ” + childNum);

System.out.println(“Your financial assistance amount is: ” + computeAssistance(income, childNum));

}

}

}

/*

===================================================

Run at 1 times

Please enter annual income: 21000

Please enter number of children: 2

Your income: 21000.0

Your children number: 2

Your financial assistance amount is: 3000.0

===================================================

Run at 2 times

Please enter annual income: -1000

Invalid income. Try again

Please enter annual income: 10000

Please enter number of children: 5

Your income: 10000.0

Your children number: 5

Your financial assistance amount is: 10000.0

===================================================

Run at 3 times

Please enter annual income: 35000

Please enter number of children: 2

Your income: 35000.0

Your children number: 2

Your financial assistance amount is: 0.0

*/