Hi, i'm new to java, can someone help to solve this question:

In Wonderland a man distribute his monthly salary among his family as follows:
• Half the salary goes on general expenditure in the house
• one third of the remainder is spent on his car
• Out of the rest, one third is saved while the remaining part is shared among members of the family so that parents get twice as much as the children.
Write a program that allows you to input the salary of a person and the number of children he has and output the pocket money obtained by one of the parents and one child.

Recommended Answers

All 12 Replies

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

    double salary;
    String children;

    Scanner in = new Scanner(System.in);

    System.out.println("Enter Salary:");
    salary = in.nextInt();

    System.out.println("Enter number of children:");
    children = in.nextLine();

    How should I process with the calculation??

You are missing so much that I don't knw where to start...
Re-visit your first "hello world" program and see what pieces you are missing.
Do you really not know how to claculate a variable as one half of another variable?

This is really not a Java question. It is an algebra question, and we are being asked to represent the answer with Java code.

I recommend we start by forgetting about Java and just try to understand the question. If we can't solve it, how can we teach the computer to solve it?

We are being asked to:

Write a program that allows you to input the salary of a person and the number of children he has and output the pocket money obtained by one of the parents and one child.

To prove toourselves that we really understand the task, let's say we have a family of 5: mother, father, and three kids. The father make $1500 per week. How much money pocket money does the wife and each kid get?
If we can answer that, then try to abstract it:

  • What if the father makes $1600 instead?
  • What if the father makes $1200?
  • What if the father makes x dollars per week? Can we express the answers in terms of x?

Once we are able to express the answer in terms of x, we are close to being able to write a program to do this calculation for us. Of course, the number of children also needs to be a variable.

commented: "If we can't solve it, how can we teach the computer to solve it?" - Brilliant +15

Is this good and how should I calculate the pocket money for the children?

public static void main(String[] args)
{
    double salary;
    Scanner in = new Scanner(System.in);

    System.out.println("Enter Salary:");
    salary = in.nextDouble();

    salary = (salary/2);
    salary = (salary *1/3);
    salary = (salary *1/3);
    salary = (salary *1/2);

    System.out.println("Pocket money obtain by one parent: " + salary);
    salary = in.nextDouble();
    }

does it work? Does it do what you want it to do? If not, fix it so it does do what it's supposed to do.

It works, but how should I proceed to calculate the pocket money obtain by the children??

re-read your original post (which contains the assignmet, and thus the steps to take) and write the code.

To say that an aglorithm works means that we tested it on some input values and verified that it gives us the right answer.
What input did you check the math with? How many kids were you assuming?

From my earlier post:

To prove toourselves that we really understand the task, let's say we have a family of 5: mother, father, and three kids. The father make $1500 per week. How much money pocket money does the wife and each kid get?

So, let's walk through this example with your code:

//salary input 1500.
salary = (salary/2);  //now salary is 750, half went to house.
salary = (salary *1/3); //now salary is 250. This is how much we are saving.
salary = (salary *1/3); Now salary is 83.3333 Why?
salary = (salary *1/2);  Now salary is 41.67. No, wrong answer.
System.out.println("Pocket money obtain by one parent: " + salary);

Now, lets forget the code, like I suggested, and do the problem.
The whole salary is 1500.
Half goes to the housr, so 750 is left.
One third gets saved, so 750 - 250 = 500 remains to dived as spending money.
At this point we realize that you were not dividing up the problem correctly. The first part of the problem is to determine how much spending money is left for the whole family. We just did that part now and you never did that in your program.
Now, how does the family divide the spending money?
In the example we are working out, we have two parents and three kids.
Since parents get a doulbe share, we have to divide the money by 7. Each share is 500/7 = 71.43
The parents each get 142.86 and the kids get 71.43

I'm not sure why you would think that 41.67 is the right answer here.

Now, here are the steps you need to follow:
1. Make up your own example and work it out the way I demonstrated. Do Not write code yet. First convince yourself that you understand the problem because all along you have been too quick to throw some code on the screen and wonder if it works.
2. Once you have your own example, then write the code.
3. Test the code on my example and your example. Make sure it gives you the correct answer.

Thank u 4 ur help man

how to connect database using java .please help me

don't hijack threads. create your own.
and re-think whether you really want to ask questions that a ten-second-googling session will answer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.