Hy Friends,

Have to submit a small routine program.

So this is how it goes:

There are soaps on the shelves.

One soap costs $1 and the next soap costs $1.2 until no money is left in the pocket

Each soap is 20% more in price than the previous one.

The Customer has $200 in his pocket.

So now im suppose to calculate the total soaps purchased by the customer , the total cost of the soaps and the total change remaining for the customer.

Please Help as i am suppose to submit it by tomorrow.

Thanks.

This is the code i have worked on so far:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Monetary_Calculation;

import java.util.Scanner;

/**
 *
 * @author SaguWesker
 */
public class Foobar_Calculator 
{
    public static void main(String args[])
    {

    Scanner Foobar_Calculator = new Scanner(System.in);

    double pocket_money , percentage_value = 0.2 , total_foobars , foobar_price = 1 , total_cost  = 0;

    System.out.println("Please Enter The Amount You Have " + "\n$ ");

    pocket_money = Foobar_Calculator.nextDouble();

    do

    {
        total_cost = pocket_money - (foobar_price + foobar_price);

        System.out.println("The Total Cost For The Foobars is $" + total_cost);


    }

    while(total_cost < pocket_money);

    {

    }

    }
}

Recommended Answers

All 26 Replies

So far i have achieved this.

STILL NO LUCK

The output is repeating continously:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Monetary_Calculation;

import java.util.Scanner;
import javax.swing.JOptionPane;

/**
 *
 * @author SaguWesker
 */
public class Foobar_Calculator 
{
    public static void main(String args[])
    {

    Scanner Foobar_Calculator = new Scanner(System.in);

    String pocket_money_string;

    double pocket_money;

    double percentage_value = 0.2;

    double total_foobars;

    double foobar_price = 1; 

    double total_cost  = 0;

    double count = 0;

    pocket_money_string = JOptionPane.showInputDialog("Enter Your Amount You Have");

    pocket_money = Double.parseDouble(pocket_money_string);

    //foobar_price = foobar_price + (foobar_price * percentage_value);



    do
    {

    count = pocket_money - (foobar_price + (foobar_price * percentage_value));

    System.out.println("The Total Cost For The Foobars is $" + count);

    }

    while(count < pocket_money);

    {

    }

    }

    //public static void Calculate_Foobar_Price()
    //{
        //poc
    //}


}

Serious suggestion: work a couple of examples on a sheet of paper writing down everything you do. Then write code to do the same thing.

I have totally given up on this programming language.

Just to much of a stress

can i post the question itself?

If you have $200 in your pocket, and you see a shelf with a long row of foobars priced at $1, $1.2,
$1.44, $1.728 and so forth. Every foobar cost 20% more than the previous one. You buy one of each
foobar, starting with the one that costs $1, until you can't afford the next foobar on the shelf (the
foobars are ordered by price). How many foobars can you buy, and how much change will you get?
Your task is to write a small routine to solve this problem,

You need to solve the problem in maths first before you attempt to do the programming. Now, let's look at how math problem is like...

1) What the problem is asking for? If you still don't know, a key word is "sum" of something.
2) How each item price is calculated? If you still don't know, you may think about cumulative interest from a bank (which calculate interest every day).
3) What is the maximum value of the "sum" you can go up to?

After you answer all theses questions, then you should start to work it on as a math problem. The math level of this is arithmatic, I believe.

Once you get an answer by solving its as a math problem, you now can start to do programming. To program something is the interpretation of how you solve a problem. In this case, it is directly from maths.

Hy Taywin.

As per your advice, I solved the problem mathematically.

Well I can buy 20 foobars with a sum of $ 186.6959157 and a change of $ 13.30408428.

I guess u have managed to solve the problem mathematically.

Now i need your help solving the problem through programming.

I am beginning to like your posts Taywin.

Please guide me programmatically.

Thank You

This Is The Code I have written and all that it outputs me is a continous loop with total cost of $ 1.2.

I have to submit this routine tomorrow.

Please Assist

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Monetary_Calculation;

import java.util.Scanner;

/**
 *
 * @author SaguWesker
 */
public class Monetary_Calculation 
{
    public static void main (String args[])
    {
        Scanner Foobar_Calculator = new Scanner(System.in);

        double Pocket_Money;

        System.out.println("Please Enter The Amount You Have " + "\n$ ");

        Pocket_Money = Foobar_Calculator.nextDouble();

        double foobar_starting_price = 1;

        double percentage = 0.2;

        double total_cost = 0;

        double change = 0;

        while(foobar_starting_price < Pocket_Money)
        {
            total_cost = foobar_starting_price + (foobar_starting_price * 0.2);

            System.out.println("The Total Cost Is " + total_cost);
        }

        System.out.println("The Total Cost Is " + total_cost);



    }
}

Ok Im very very close to solving the logic.

Here is my code but the only problem is it increments 1 to the previous value as it is supposed to take the previous value and multiply by 0.2 (20%) and then add the previous value again to get the next number.

Im very very close

PS: All help appreciated

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Monetary_Calculation;

import java.util.Scanner;

/**
 *
 * @author SaguWesker
 */
public class Monetary_Calculation 
{
    public static void main (String args[])
    {
        Scanner Foobar_Calculator = new Scanner(System.in);

        double Pocket_Money;

        System.out.println("Please Enter The Amount You Have " + "\n$ ");

        Pocket_Money = Foobar_Calculator.nextDouble();

        double foobar_starting_price = 1;

        double percentage = 0.2;

        double total_cost = 0;

        double change = 0;

        total_cost = foobar_starting_price + (foobar_starting_price * 0.2);

        while(total_cost < Pocket_Money)
        {
            total_cost++;

            //total_cost = foobar_starting_price + (foobar_starting_price * 0.2);

            System.out.println("The Total Cost Is " + total_cost);
        }

        System.out.println("The Total Cost Is " + total_cost);



    }
}

Ok i have gotten even more closer to solving it.

It is getting the numbers which are supposed to be added to give the total of $186.6959157 as mentioned in the previous post.

Here is the updated code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Monetary_Calculation;

import java.util.Scanner;

/**
 *
 * @author SaguWesker
 */
public class Monetary_Calculation 
{
    public static void main (String args[])
    {
        Scanner Foobar_Calculator = new Scanner(System.in);

        double Pocket_Money;

        System.out.println("Please Enter The Amount You Have " + "\n$ ");

        Pocket_Money = Foobar_Calculator.nextDouble();

        double foobar_starting_price = 1;

        double percentage = 0.2;

        double total_cost = 0;

        double change = 0;

        double final_total = 0;

        total_cost = foobar_starting_price + (foobar_starting_price * 0.2);

        while(total_cost < Pocket_Money)
        {
            total_cost = total_cost +(total_cost * 0.2);

            //final_total = total_cost;

            System.out.println("\n The Total Cost Is " + total_cost);
        }        

    }
}

Here is the Output:

run:
Please Enter The Amount You Have 
$ 
200

 The Total Cost Is 1.44

 The Total Cost Is 1.728

 The Total Cost Is 2.0736

 The Total Cost Is 2.48832

 The Total Cost Is 2.9859839999999997

 The Total Cost Is 3.5831807999999996

 The Total Cost Is 4.299816959999999

 The Total Cost Is 5.159780351999999

 The Total Cost Is 6.191736422399999

 The Total Cost Is 7.430083706879999

 The Total Cost Is 8.916100448256

 The Total Cost Is 10.6993205379072

 The Total Cost Is 12.83918464548864

 The Total Cost Is 15.407021574586368

 The Total Cost Is 18.48842588950364

 The Total Cost Is 22.18611106740437

 The Total Cost Is 26.62333328088524

 The Total Cost Is 31.94799993706229

 The Total Cost Is 38.337599924474745

 The Total Cost Is 46.0051199093697

 The Total Cost Is 55.206143891243634

 The Total Cost Is 66.24737266949236

 The Total Cost Is 79.49684720339084

 The Total Cost Is 95.39621664406901

 The Total Cost Is 114.47545997288282

 The Total Cost Is 137.3705519674594

 The Total Cost Is 164.84466236095128

 The Total Cost Is 197.81359483314154

 The Total Cost Is 237.37631379976983
BUILD SUCCESSFUL (total time: 2 seconds)

Please Assist Friends. I am so gonna be over the floor if i solve this routine and hand it over by tomorrow.

How is the price of the next soap related to the total cost?
How does the price of each soap change?
How does the total cost change?

Hey NormR1

Thanks for sparing ur time.

Well Price of each soap changes by 20% of the previous price.

By adding the price of the soaps it must give a total which is lesser than the input by the user.

Suppose u have $ 200 in ur pocket and u want to buy soaps, 1st soap starts at $1.

Then next soap goes to $1.2 (which is 20% higher than previous one).

It continues until u cnt afford any soap.

I have to get the total,

the number of soaps purchased and

the total change.

What should i do?

If u cn please provide with a code that will enable me to get close to solving the logic

You have the logic, you just need to code it.

What code would do the following:

Price of each soap changes by 20% of the previous price.

Put that code inside of the loop

adding the price of the soaps it must give a total

Put code to do that inside of the loop.

total which is lesser than the input by the user.

Use those values to control how many times the loop goes around.

Put that code inside of the loop

Hy NormR1

Please assist me in which loop i should use im using do while loop

Assist with some sample code which would achieve what you mentioned in your previous post.

Please Help.

Itis still not working :'(

Whyyyyyyyyyyy is the code still not working?

I dont understand. I have applied the logic but dont know why the code does not accept it?

Code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Monetary_Calculation;

import java.util.Scanner;

/**
 *
 * @author SaguWesker
 */
public class Monetary_Calculation 
{
    public static void main (String args[])
    {
        Scanner Foobar_Calculator = new Scanner(System.in);

        double Pocket_Money;

        System.out.println("Please Enter The Amount You Have " + "\n$ ");

        Pocket_Money = Foobar_Calculator.nextDouble();

        double foobar_starting_price = 1;

        double percentage = 0.2;

        double total_cost = 0;

        double change = 0;

        double final_total = 0;

        double final_sum = 0;

        double price_calculator = foobar_starting_price + (foobar_starting_price * percentage);

        total_cost = foobar_starting_price + (foobar_starting_price * 0.2);

        //total_cost = total_cost +(total_cost * 0.2);

        do
        {

            total_cost = total_cost +(total_cost * 0.2);            

            System.out.println("\nThe Total Cost Is " + total_cost);

            //int counter = 0;

            //counter++;

            //System.out.println(counter);

        }


        while(total_cost < Pocket_Money);
        {            

        }

        for(int count = 0; count < total_cost ; count++)

        {
            //System.out.println(count);
        }

    }
}

Result:

run:
Please Enter The Amount You Have 
$ 
200

The Total Cost Is 1.44

The Total Cost Is 1.728

The Total Cost Is 2.0736

The Total Cost Is 2.48832

The Total Cost Is 2.9859839999999997

The Total Cost Is 3.5831807999999996

The Total Cost Is 4.299816959999999

The Total Cost Is 5.159780351999999

The Total Cost Is 6.191736422399999

The Total Cost Is 7.430083706879999

The Total Cost Is 8.916100448256

The Total Cost Is 10.6993205379072

The Total Cost Is 12.83918464548864

The Total Cost Is 15.407021574586368

The Total Cost Is 18.48842588950364

The Total Cost Is 22.18611106740437

The Total Cost Is 26.62333328088524

The Total Cost Is 31.94799993706229

The Total Cost Is 38.337599924474745

The Total Cost Is 46.0051199093697

The Total Cost Is 55.206143891243634

The Total Cost Is 66.24737266949236

The Total Cost Is 79.49684720339084

The Total Cost Is 95.39621664406901

The Total Cost Is 114.47545997288282

The Total Cost Is 137.3705519674594

The Total Cost Is 164.84466236095128

The Total Cost Is 197.81359483314154

The Total Cost Is 237.37631379976983
BUILD SUCCESSFUL (total time: 5 seconds)

It is suppose to calculate up from 1.44 to 31.94799993706229 and then give me the overall total.

The calculate the from 1.44 to 31.94799993706229 and get the total foobars.

After that calculate the change.

Unfortunately that code is a long way from being right. The problem seems to be that you don't really know how the code is supposed to work, so you don't know what code to write.
It's time to take a step back. Forget about Java for the moment and try writing out the logic in some simple english/pseudocode. You can post that here for helpful feedback.

Hy James,

Here is a small pseudocode i have written using my knowledge i gained in High School:

START PROGRAM

User Enters Total Amount

Foobar Price initialized to 1

Incremented price = Foobar Price * Foobar Price * 0.2

while Entered Amount < total

Total = total of incremented foobar price

STOP PROGRAM

OK, good start.
Now run through that (exactly as written) using a simple test case and writing down the values as you go, eg

START PROGRAM
User Enters Total Amount
Foobar Price initialized to 1

user has 200, price = 1

Incremented price = Foobar Price * [assume should be +] Foobar Price * 0.2

price is now 1.2

while Entered Amount < total
Total = total of incremented foobar price

total is now 1.2

while Entered Amount < total
Total = total of incremented foobar price

total is now 2.4
...

Can you see the first problem? You increase the price just once, before entering the loop, so everything you buy just costs 1.20

Why do you change the price outside of the loop?
It should be changed after its value has been added to the total.

Hello James.

Okay This Is How I have worked.

I have not created a Test Case as i am new to Java Desktop and Console Application Programming.

I Have started this from fresh.

As mentioned, I have coded as exactly as shown in the pseudocode in English and as exactly as you have interepreted my pseudo code.

I think i am getting to know my mistakes

Hope i can receive help:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Monetary_Calculation;

import java.util.Scanner;

/**
 *
 * @author SaguWesker
 */
public class Monetary_Calculation 
{
    public static void main (String args[])
    {
        Scanner Foobar_Calculator = new Scanner(System.in);

        double Pocket_Money;

        System.out.println("Please Enter The Amount You Have " + "\n$ ");

        Pocket_Money = Foobar_Calculator.nextDouble();

        double foobar_starting_price = 1;

        double percentage = 0.2;

        double total_cost = 0;                                     

        total_cost = foobar_starting_price + (foobar_starting_price * percentage);

        System.out.println(total_cost);

    }
}

Please Apologize. I have started everything from beginning to get the logic and steps right. Currently the Total Cost is adding up to $ 1.2. So Now i need to get a formula which will continue adding e.g. $1 , $1.2 , $1.44 , $1.728 , $2.0736 , ..................

Until the total amount of the incremented price is so much that the user cannot purchase any more soaps.

So is my First STEP correct?

All Replies appreciated from experts: JamesCherill and normR1

I really think you need to get the logic right in your own head (by using pseudo-code and test data on paper) before messing about with Java code. I'm pretty confident that Norm will agree.
ps: I'm offline now until some time tomorrow - hopefully Norm or someone else will be around for you.

I fully agree with James. Work on the pseudo code until you have the logic to solve the problem, THEN write the code.
You know there must be a loop.
What goes outside the loop
what goes inside the loop
what goes after the loop

Ok Thanks NormR1 and JamesCherill for the help.

I will try to get the logic

and probably post it here.

Hope to get some advice after that on what to do.

Thanks friends

Cheerz :-)

When you are talking about a loop, you may need to know the terminated condition too (for most cases). What you need to do in a loop (programming) is to calculate certain values (variables) at a time, and then check whether those variables are satisfied what you are looking for. If it is not, do the whole calculation, and then check again. Keep calculating and checking until certain conditions are met.

To confuse you even more (hopefully not), the order of "checking" and "calculating" may affect your program. For simpliciy in your case, what you need to do is "calculation" and then "checking" and that's all. Later on, you may learn that there are certain cases that you would need to "check" before "calculate" as well.

How about this... Let's try to get a logic without an iteration (loop) first.
Given a total amount of money in hand as 200, an item foobar cost 1.
Let n <- 200
Let p <- 1

If there is no loop, this mean the left over money after buying a foobar would be...
leftOver <- n - p
leftOver = 200 - 1
leftOver = 199

You should see what the result of the computation without any loop. Now we will try to add an iteration (loop). If one buys another foobar, the price of it will be increased by 20% from its previous price. So the new price will be calculated "before" it is being sold.
p <- p + (p*0.2) // 100% + 20%
p <- 1 + 0.2
p <- 1.2

With the new price, when buy 2 foobar items, the left over money should be...
leftOver <- leftOver - p
leftOver <- 199 - 1.2
leftOver <- 197.8

Next, if one wants to buy one more foobar, the price of it would be 20% more from its previous price.
p <- p + (p*0.2) // 100% + 20%
p <- 1.2 + 0.24
p <- 1.44

With the new price, when buy 3 foobar items, the left over money should be...
leftOver <- leftOver - p
leftOver <- 197.8 - 1.44
leftOver <- 196.36

Do you see a pattern here? Here is an example of how the outcome should be...
-You have $200.0 in your hand
-A foobar price is $1.0
-You bought one foobar, you have $199.0 left
-A foobar price is $1.2
-You bought one foobar, you have $197.8 left
-A foobar price is $1.44
-You bought one foobar, you have $196.36 left
-A foobar price is $1.728
-You bought one foobar, you have $194.632 left
-A foobar price is $2.0736
-You bought one foobar, you have $192.55841 left
-A foobar price is $2.48832
-You bought one foobar, you have $190.07008 left
-A foobar price is $2.985984
-You bought one foobar, you have $187.0841 left
-A foobar price is $3.583181
-You bought one foobar, you have $183.50093 left
-A foobar price is $4.299817
-You bought one foobar, you have $179.20111 left
-A foobar price is $5.1597805
-You bought one foobar, you have $174.04134 left
-A foobar price is $6.1917367
-You bought one foobar, you have $167.8496 left
-A foobar price is $7.430084
-You bought one foobar, you have $160.41951 left
-A foobar price is $8.916101
-You bought one foobar, you have $151.5034 left
-A foobar price is $10.699322
-You bought one foobar, you have $140.80408 left
-A foobar price is $12.839186
-You bought one foobar, you have $127.96489 left
-A foobar price is $15.407022
-You bought one foobar, you have $112.55787 left
-A foobar price is $18.488426
-You bought one foobar, you have $94.06944 left
-A foobar price is $22.186111
-You bought one foobar, you have $71.88333 left
-A foobar price is $26.623333
-You bought one foobar, you have $45.26 left
-A foobar price is $31.948
-You bought one foobar, you have $13.311998 left
-!!You do not have enough money to buy a foobar priced $38.3376 because you only have $13.311998 left!!

You see that you will keep going back to the same equations. These redundant processes can be replaced with a "loop" in computer programming. Remember, a loop should be ended with a certain condition (or you could get into an infinite loop). In your problem, it is when you do not have enough money to buy one more foobar. It can be translated into one condition -- left over money in hand is less than the next foobar price.

This is the Solution of your problem in codes .

// programm to Solve a maths problem as follows .
/*
 * There are soaps on the shelves.

   One soap costs $1 and the next soap costs $1.2 until no money is left in the pocket

   Each soap is 20% more in price than the previous one.

    The Customer has $200 in his pocket.
     calculate the total soaps purchased by the customer , 
     the total cost of the soaps and the total change remaining for the customer
 * 
 */

package test;

public class Maths_prob1 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        float cost=1;
        float pocket_money=200;
        float total_cost=0;
        int total_soap=0;

        do{

            total_soap ++ ;  // one more soap purchased with one increment 

            pocket_money = pocket_money-cost;  // cost of soap deducted from pocket money getting new remaining pocket money

            cost = (float) ((0.2*cost)+ cost);  // new cost calculated for next execution of loop



        }while(pocket_money>cost);

        total_cost = 200-pocket_money;  
        System.out.println("no of soaps purchased = "+total_soap);
        System.out.println("Total cost of soaps = "+total_cost);
        System.out.println("Pocket money remaining = " +pocket_money);

    }

}

Please don't spoonfeed by writing a solution to the OPs question. Help them find the solution.

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.