Jump to content

Java Question: Need Help

ExDreamer

Here is the question

 

Write a program in a class CountPoor that counts the number of families that are considered poor. Write and use a class Family that has private instance variables

  • income – a double value that is the income for the family
  • size – the number of people in the family

And the following methods:

  • Family(income, size) – a constructor that sets the instance variables
  • Accessor method for each of the instance variables (getIncome(), getSize())
  • isPoor(housingCost, foodCost)  - a method that returns true if housingCost + foodCost * size is greater than half the family income (foodCost is the average food cost for an individual, while housingCost is for the family)
  • toString – a method that returns a string containing the information about the family

The program should read an integer k from the keyboard and then create an array of size k whose base type is Family. It should then create k objects of type Family and put them in the array, reading the income and size for each family from the keyboard. After reading an average housing cost and average food cost from the keyboard, it should display the families that are poor.

 

My code

 



public class Family {public double income;public int size;public String info;boolean isPoor;public Family(){}public Family(double Income, int Size){income = Income;size = Size;}public boolean isPoor(double housingCost, double foodCost){if ((housingCost + (foodCost * size)) > (income/2)){isPoor = true;}else if ((housingCost + (foodCost * size)) < (income/2)){isPoor = false;}return isPoor;}public void toString(Family[] family, double housingCost, double foodCost){for(int i=0; i<family.length; i++){if (isPoor = true){info = "\nFamily " + (i+1) + " income: RM" + family[i].income + "0\nFamily " + (i+1) + " size: " + family[i].size + "\nFinancial Standing: Poor";}else{info = "\nFamily " + (i+1) + " income: RM" + family[i].income + "0\nFamily " + (i+1) + " size: " + family[i].size + "\nFinancial Standing: Not poor";}System.out.println(info);}}}
import java.util.Scanner;public class CountPoor{public static void main(String[] args){double housingCost;double foodCost;System.out.println("How many families would you like to evaluate?");Scanner keyboard = new Scanner(System.in);int K = keyboard.nextInt();Family temp = new Family();Family[] family = new Family[K];System.out.println("Please enter the housing cost: RM");housingCost = keyboard.nextInt();System.out.println("Please enter the food cost: RM");foodCost = keyboard.nextInt();for(int i=1; i <= K; i++){System.out.println("\nPlease enter the household income for family " + i + " :RM ");double Income = keyboard.nextDouble();System.out.println("Please enter the number of household members in family " + i + ".");int Size = keyboard.nextInt();family[i-1] = new Family(Income, Size);}temp.toString(family, housingCost, foodCost);}}

My problem is i will get "Financial standing:poor "not matter how much family income i put. it should be (housingCost + (foodCost * size) > (income/2) = poor and vice versa. If i change if (isPoor = true) to if (isPoor == true) i would get "Financial standing: not poor" even though that family should be poor.

Any help would be appreciated.Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

Please use the 

code tag 

Also, cleaning up your code might help (or make it more easy to find the error)

public boolean isPoor(double housingCost, double foodCost){   return ((housingCost + (foodCost * size)) > (income / 2));}

Also to find the error, using:

System.out.println(someValue);

helps alot.
 
The problem with your define boolean isPoor; but also it in a method (public boolean isPoor()), simply call isPoor(par1, par2) when you want the result.

Best regards Zahlio,
Unity asset developer - Game developer (http://playsurvive.com) - Computer Science student

Link to comment
Share on other sites

Link to post
Share on other sites

Really? No one spotted this?

 

In 

public void toString(...if (isPoor = true)

That's an assignment. Every time you call toString, you are setting isPoor = true. You need to use two equals back to back, ie ==.

 

Also, the result of the function isPoor() is undefined if income/2 is exactly equal to homecost + foodcost. Fix this by either removing the elseif (and using else instead) or setting one expression to >= / <=

Link to comment
Share on other sites

Link to post
Share on other sites

You also want to make your instance variables private, looks like the assignment says at the very least income and size.

 

private double income;
private int size;

 

Then just write getter methods.

 

public double getIncome() { return income; }

public int getSize() { return size; }

SSD Firmware Engineer

 

| Dual Boot Linux Mint and W8.1 Pro x64 with rEFInd Boot Manager | Intel Core i7-4770k | Corsair H100i | ASRock Z87 Extreme4 | 32 GB (4x8gb) 1600MHz CL8 | EVGA GTX970 FTW+ | EVGA SuperNOVA 1000 P2 | 500GB Samsung 850 Evo |250GB Samsung 840 Evo | 3x1Tb HDD | 4 LG UH12NS30 BD Drives | LSI HBA | Corsair Carbide 500R Case | Das Keyboard 4 Ultimate | Logitech M510 Mouse | Corsair Vengeance 2100 Wireless Headset | 4 Monoprice Displays - 3x27"4k bottom, 27" 1440p top | Logitech Z-2300 Speakers |

Link to comment
Share on other sites

Link to post
Share on other sites

Well zahlio's "cleaned up" code actually fixes another issue that you *might* eventually have run into...but I will still mention it anyways because it is good to see other mistakes that might not be present immediately.

public boolean isPoor(double housingCost, double foodCost){    if ((housingCost + (foodCost * size)) > (income/2))    {        isPoor = true;    }    else if ((housingCost + (foodCost * size)) < (income/2)) //So you have a problem here, it should be <= not <, otherwise isPoor might never be set, or//you could end up with code that sometimes is poor and sometimes isn't, depending on the last inputs     {        isPoor = false;    }    return isPoor;}

asquirrel is right though the = instead of == is the real culprit of your problems.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Instead of having a isPoor flag, you should hold state for housingCost and foodCost to determine what isPoor function will do.

 

Btw, your toString method is horribly wrong, as it does not return a string and it does not perform the task you specified.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×