Reputation: 39
I am new to programming and I would appreciate the help on this problem: A program to calculate the amount of financial assistance. If the annual household income is between $30000 and $40000 and the household has at least three children, the amount is $1000 per child. If the annual household income is between $20000 and $30000 and the household has at least three children, the amount is $1500 per child. If the annual household income is less than $20000, the amount is $2000 per child. I have to implement a method and Use -1 as the sentinel value. Here is my program so far
public static void main(String[] args) {
// **METHOD** //
Scanner in = new Scanner(System.in);
System.out.println("What is the annual household income?");
double income = in.nextDouble();
System.out.println("How many children are in the household?");
int children = in.nextInt();
System.out.println("The financial assistance for this household is: "
+ assistance(income, children));
}
//**TEST PROGRAM**//
public static double assistance(double income, int children)
{
if(income >= 30000 && income < 40000 && children >= 3)
{
assistance = children * 1000; //says that it cannot find the symbol
}
else if(income >= 20000 && income < 30000 && children >= 2)
{
assistance = children * 1500;
}
else if(income < 20000)
{
assistance = children * 2000;
}
else
{
assistance = 0.0;
}
return assistance;
}
}
Upvotes: 1
Views: 618
Reputation: 12339
You may be thinking of another language, such as Basic, where you declare a function and return the value of the function by using the function's name.
Java just has you return the value.
Others have had you declare assistance
. Please consider a variable of a different name, ans
. At the end, you would return ans
instead.
public static double assistance(double income, int children)
{
double ans = 0.0;
if(income >= 30000 && income < 40000 && children >= 3)
{
ans = children * 1000; //says that it cannot find the symbol
}
else if(income >= 20000 && income < 30000 && children >= 2)
{
ans = children * 1500;
}
else if(income < 20000)
{
ans = children * 2000;
}
return ans;
}
}
Upvotes: 1
Reputation: 1734
First off, I'm going to assume you have the code you listed in a class because in Java, everything is a Class. As of right now you haven't defined a variable assistance. so you I would suggest validating your input so you can handle instances where the user doesn't input a numeric value. Another thought would be to make assistance an object since it doesn't have to be static. You could then use that object to call the assistance method. Like this:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// **METHOD** //
Scanner in = new Scanner(System.in);
System.out.println("What is the annual household income?");
double income = in.nextDouble();
System.out.println("How many children are in the household?");
int children = in.nextInt();
System.out.println("The financial assistance for this household is: "
+ new Assistance.getAssistance(income,children));
}
}
public class Assistance {
public double getAssistance(double income, int children) {
/* Declare assistance here */
double assistance;
if (income >= 30000 && income < 40000 && children >= 3) {
assistance = children * 1000; // says that it cannot find the symbol
}
else if (income >= 20000 && income < 30000 && children >= 2) {
assistance = children * 1500;
} else if (income < 20000) {
assistance = children * 2000;
} else {
assistance = 0.0;
}
return assistance;
}
}
Upvotes: 0
Reputation: 346
You need to declare assistance
at the beginning of the function public static double assistance
public static double assistance(double income, int children)
{
double assistance = 0.0;
"Rest of your code here"
}
Upvotes: 0