Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Simple looping statement

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Simple looping statement

    So I have a homework assignment where a program needs to be designed to allow the user to input the Number of bags sold (of coffee) as well as the Weight per bag and output the information in a table as I've shown in my code and as I've tried to outline below:

    Coffee Table
    Number of bags sold Weight per Bag Price per pound Total Price Price with Tax


    You have to calculate the total price using Total price= (unit weight) * num of units sold * 5.99

    You calculate price with tax using = (total price) + (total price) * 0.0725

    The only problem I'm having is that she's given use a table of values for Number of bags sold and weight per bag (that the user should be able to enter) and I know I have to incorporate a loop so that the values successfully output into the table. Here's my code:

    main method
    import java.util.*;
    public class CalcMain
    {
        public static void main(String[] args){
            Scanner scanner = new Scanner(System.in);
            double weight, units, totalPrice, taxPrice, pricePerPound;
            System.out.println("\t\t\t\t" + "Coffee Table");
     
            System.out.println("Enter Weight of Bag: ");
            weight = scanner.nextDouble();
            System.out.println("Enter number of units: ");
            units = scanner.nextDouble();
     
            Calculator calculator = new Calculator();
            totalPrice = calculator.getTotalPrice(weight, units);
            pricePerPound = totalPrice/weight;
     
            taxPrice = calculator.getTaxPrice(totalPrice);
     
            System.out.println("Number of bags sold" + "\t" + "Weight per bag" +
                               "\t" + "Price per pound" + "\t" + "Total Price" + "\t" + "Price with tax");
            System.out.println(units + "\t\t\t" + weight + "\t\t" + "$5.99" + "\t\t" +
                               totalPrice + "\t\t" + taxPrice);
            }
    }

    calculator method
    public class Calculator
    {
        //set global variables
        public static final double PRICE = 5.99;
        public static final double TAX = 0.0725;
     
        // instance variables - replace the example below with your own
        private double totalPrice;
        private double taxPrice;
     
        /**
         * Constructor for the calculator setting total/tax prices at 0.
         */
        public Calculator()
        {
            // initialise instance variables
            totalPrice = 0.0;
            taxPrice = 0.0;
        }
     
        /**
         * getTotalPrice and getTaxPrice methods
         */
        public double getTotalPrice(double weight, double units){
            totalPrice = weight * units * PRICE;
            return totalPrice;
        }
     
        public double getTaxPrice(double totalPrice){
            taxPrice = totalPrice + (totalPrice * TAX);
            return taxPrice;
        }
    }

    I'm not sure in the least bit, how to incorporate a loop to allow multiple inputs of # of bags sold and Weight per bag and have it output as I've designated in the code. Could anyone possibly clue me in? I understand basic for-and-while loops.

  2. The Following User Says Thank You to gta1 For This Useful Post:

    depsai (May 24th, 2013)


  3. #2
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Simple looping statement

    Update:

    So I think I made progress but I'm not completely there. This is my new code and the resultant output:

    main method
    import java.util.*;
    public class CalcMain2
    {
        public static void main(String[] args){
            Scanner scanner = new Scanner(System.in);
            double weight, units, totalPrice, taxPrice, pricePerPound;
            System.out.println("\t\t\t\t" + "Coffee Table");
     
            int counter = 0;
            while (counter < 10){
            System.out.println("Enter Weight of Bag: ");
            weight = scanner.nextDouble();
            System.out.println("Enter number of units: ");
            units = scanner.nextDouble();
            counter++;
     
     
            Calculator calculator = new Calculator();
            totalPrice = calculator.getTotalPrice(weight, units);
            pricePerPound = totalPrice/weight;
     
            taxPrice = calculator.getTaxPrice(totalPrice);
     
            System.out.println("Number of bags sold" + "\t" + "Weight per bag" +
                               "\t" + "Price per pound" + "\t" + "Total Price" + "\t" + "Price with tax");
            System.out.println(units + "\t\t\t" + weight + "\t\t" + "$5.99" + "\t\t" +
                               totalPrice + "\t\t" + taxPrice);
                            }
            }
    }


    calculator class
    public class Calculator
    {
        //set global variables
        public static final double PRICE = 5.99;
        public static final double TAX = 0.0725;
     
        // instance variables - replace the example below with your own
        private double totalPrice;
        private double taxPrice;
     
        /**
         * Constructor for the calculator setting total/tax prices at 0.
         */
        public Calculator()
        {
            // initialise instance variables
            totalPrice = 0.0;
            taxPrice = 0.0;
        }
     
        /**
         * getTotalPrice and getTaxPrice methods
         */
        public double getTotalPrice(double weight, double units){
            totalPrice = weight * units * PRICE;
            return totalPrice;
        }
     
        public double getTaxPrice(double totalPrice){
            taxPrice = totalPrice + (totalPrice * TAX);
            return taxPrice;
        }
    }

    Now given this table by my professor:
     
    Number of bags sold       Weight per bag
    24                                4
    13                                12
    72                                2
    2                                  6
    .... (and so on)               .... (and so on)


    I get this output:

    				Coffee Table
    Enter Weight of Bag: 
    4
    Enter number of units: 
    24
    Number of bags sold	Weight per bag	Price per pound	Total Price	Price with tax
    24.0			4.0		$5.99		575.04		616.7303999999999
    Enter Weight of Bag: 
    12
    Enter number of units: 
    13
    Number of bags sold	Weight per bag	Price per pound	Total Price	Price with tax
    13.0			12.0		$5.99		934.44		1002.1869
    Enter Weight of Bag: 
    2
    Enter number of units: 
    72
    Number of bags sold	Weight per bag	Price per pound	Total Price	Price with tax
    72.0			2.0		$5.99		862.5600000000001



    Now the only thing I can't determine is how to design it so that the outputs accumulate in a single table rather than separately. Or someone puts in all the inputs and the entire output table is created (sorry for putting the table and output in code but I couldn't maintain formatting otherwise).

  4. #3
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: Simple looping statement

    There are lots of ways to do things, but I will give you some example pseudo code that I would write. But you would either need to add fields to CalcMain2 and use it as the FooObject, or create another class that holds the fields.

    ArrayList<FooObject> collector = new ArrayList<FooObject>

    while(there is input){

    sys.out Enter weight/units
    (new instance) FooObject.weight = scanner.next
    (same instance) FooObject.units = scanner.next

    collector.add(FooObject(instance))

    } //***** End of while loop

    for (int i = 0; i < collector.size(); i++){

    collector.get(i).printTableFormat();

    } //***** End of for loop

    } //***** End of main

    That way you could loop through the input, and then at the end, print out the tables for each object you added to the collection.
    Last edited by theoriginalanomaly; May 6th, 2013 at 11:56 AM. Reason: clarity

  5. The Following User Says Thank You to theoriginalanomaly For This Useful Post:

    gta1 (May 8th, 2013)

  6. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Simple looping statement

    I appreciate your reply . I've since found out you weren't kidding when you said there were a number of ways to do this, however, I certainly like the way you've laid it out.

Similar Threads

  1. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  2. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  3. Need help with simple repetition statement
    By amgleason83 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 24th, 2012, 07:44 PM
  4. Looping Over and Over Again?
    By avalanche72 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 1st, 2012, 05:11 AM
  5. [SOLVED] (Simple) How to use an if statement to respond to random integers
    By DusteroftheCentury in forum Loops & Control Statements
    Replies: 9
    Last Post: January 27th, 2012, 09:15 PM