Results 1 to 7 of 7

Thread: Elements third java assignment!

  1. #1
    Join Date
    Jun 2007
    Posts
    532
    Mentioned
    1 Post(s)
    Quoted
    68 Post(s)

    Default Elements third java assignment!

    Hello guys! I am going to keep doing this lol.


    Assignment!

    You are planning a pizza party want to order exactly the number of slices of pizzas and drinks. Each pizza has 10 slices. A pizza costs $10.50 per pizza or $1.25 a slice and a drink costs $.90 per drink. After a survey, you determined that each attendee will eat 4 slices of pizza and drink 2 drinks. There will be a tax of 9.5% added to your total. Write a program that will calculate the total cost of your pizza party. The program will perform the following tasks:
    •Ask and accept the user’s number of attendees.
    •Calculate the number of whole pizzas and extra slices of pizzas as well as the number of drinks.
    •Calculate the cost of the pizzas (whole pizza plus extra slices), drinks, salestax, and total due.
    •Display to the monitor the number of attendees, number of whole pizzas, additional slices, cost of the pizzas, the number of drinks and cost of drinks,tax due, and total due.
    •Use integer data types for the number of attendees, number of pizzas, number of slices, and number of drinks.
    •Use double data types for cost of pizzas, cost of drinks, tax, total due.
    •Use constant double for the price of the whole pizzas, price per slice, price of drinks, and tax rate.
    •Please note that output of all costs will not be formatted. For example, 20.50 may be displayed as 20.5 or a tax due may be 1.9474 is also acceptable.
    Assignment 4 will require output formatting.


    I am not asking someone to do this for me! I am asking for help and as I post my code give me tips and pointers and let me know if you see anything wrong. Thanks for everything guys!
    Code:
    import java.util.Scanner;
    
    /*Andrew Varnick 
      9/12/2013 
      CSCI 1015 
      Programming Assignment 3
    */
    
    public class VarnickPass3
    {
       public static void main(String[] args)
       {
          Scanner keyboard = new Scanner(System.in); 
          final double CostWholePizza, CostPizzaSlice, CostDrink, TaxRate;
          double WholePizza, Drinks, Tax, Total;
          int NumberAttendees, NumberOfWholePizza, NumberOfPizzaSlice, NumberOfDrinks;
          int x, y;
          String Number_Attendees;
          
          //Constants
          System.out.println("Loading Constants....");
          CostWholePizza = 10.50;
          CostPizzaSlice = 1.25;
          CostDrink = .90;
          TaxRate = 0.095;
           
          //Gets the number of people attending
          System.out.println("Done loading constans!");
          System.out.println("Please enter the number of attendees");
          Number_Attendees =
             keyboard.nextLine();
          
          //Converts number of people from a string to an int
          NumberAttendees = Integer.parseInt(Number_Attendees);
            
          //Math
          x = NumberAttendees * 4; 
          System.out.print("Total number of whole pizzas is:" + x);
          
          System.exit(0); 
       }
    }
    Finished B.S. Program in Radiology!!

    Projects: A big one! Total secret! hehe

  2. #2
    Join Date
    Feb 2012
    Location
    Wonderland
    Posts
    1,988
    Mentioned
    41 Post(s)
    Quoted
    272 Post(s)

    Default

    constants are, I suppose in this case, like: "static final double price1 = 1.00;" and usually reside outside the main method as far as I've been told in relation to java coding convention.

    use nextInt on the scanner obj instead of nextline and then parsing the int? perhaps I misread something that needs the way you typed

    x and y variables only seem to clutter the work, no? you should only need variables along the lines of; attendees, pizzas, slices, drinks, pizzasPrice, drinksPrice, tax, total. at least those are the variables I would choose to complete the assignment. also of course the scanner obj

    I do not see a reason to print the loading of constants? (could just code comment this)


    keep up the work, code is fun :>
    cheers
    Lj

  3. #3
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Naming convention: Objects should be UpperCamelCase.
    Declaring and initializing of variables can be done on the same statement for convenience: (rather than doing it the PascalScript way)
    eg. double CostOfPizza = 1.25;

    Feel free to ask if u need further help on the assignment :)

  4. #4
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    You could essentially finish all the math in that math method you made, easy calling from there on in. Also the I agree with Jingle, should have constants outside of mainloop (just good practice) I personally do this for things I would use many times (I also make all my methods first, then put my mainloop on the bottom but that could be just me) also the "loading constants/finished loading" thing is redundant unless you are using it for debugging purposes.




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  5. #5
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    A constant should be initialized upon variable declaration.

    Consider:

    Java Code:
    final double Cost;
    Cost = 0.5;


    Now consider:

    Java Code:
    final double Cost = 0.5;
    Last edited by Brandon; 09-13-2013 at 02:24 PM.
    I am Ggzz..
    Hackintosher

  6. #6
    Join Date
    Jun 2007
    Posts
    532
    Mentioned
    1 Post(s)
    Quoted
    68 Post(s)

    Default

    First off I want to say thank you guys for all of your help!!!!!!
    I tried to take everything you guys said and apply it but I may have missed a few things. I also am having a hard time of getting out of coding like I am still doing Pascal.

    Anyway here is my what I have now after reading your guys post. I think it is done, well the math is done maybe I will need to clean it up a little. I also used the loading constants as a debugger type thing. I removed it.


    Code:
    import java.util.Scanner;
    
    /*Andrew Varnick 
      9/12/2013 
      CSCI 1015 
      Programming Assignment 3
    */
    
    public class VarnickPass3
    {
    //Constants
        static final double CostWholePizza = 10.50;
        static final double CostPizzaSlice = 1.25;
        static final double CostDrink = .90;
        static final double TaxRate = 0.095;
    
       public static void main(String[] args)
       {
          Scanner keyboard = new Scanner(System.in); 
          double PizzaPrice, DrinkPrice, TaxDue, Total;
          int NumberOfWholePizza, NumberOfExtraPizzaSlice, NumberOfDrinks;
          int NumberAttendees, NumberOfSlices;
          
           
          //Gets the number of people attending
          System.out.println("Please enter the number of attendees");
          NumberAttendees =
             keyboard.nextInt();
             System.out.println("The number attending is:" + NumberAttendees);
            
          //Math
          NumberOfSlices = NumberAttendees * 4;
          
          NumberOfWholePizza = NumberOfSlices / 10;
          System.out.println("Total number of whole pizzas is:" + NumberOfWholePizza);
          
          NumberOfExtraPizzaSlice = NumberOfSlices % 10;
          System.out.println("Total number of extra pizza slices is:" + NumberOfExtraPizzaSlice);
        
          NumberOfDrinks = NumberAttendees * 2;
          System.out.println("Total number of drinks is:" + NumberOfDrinks);
          
          PizzaPrice = ((NumberOfWholePizza * CostWholePizza) + (NumberOfExtraPizzaSlice * CostPizzaSlice));
          System.out.println("Total cost for pizza is:" + PizzaPrice);
          
          DrinkPrice = (NumberOfDrinks * CostDrink);
          System.out.println("Total cost for drinks is:" + DrinkPrice);
          
          TaxDue = ((PizzaPrice + DrinkPrice) * TaxRate);
          System.out.println("Total tax due:" + TaxDue);
          
          Total = (PizzaPrice + DrinkPrice + TaxDue);
          System.out.println("Total due at the end is:" + Total);
          
          System.exit(0); 
       }
    }
    Finished B.S. Program in Radiology!!

    Projects: A big one! Total secret! hehe

  7. #7
    Join Date
    Feb 2012
    Location
    Wonderland
    Posts
    1,988
    Mentioned
    41 Post(s)
    Quoted
    272 Post(s)

    Default

    appears your work looks substantial (matched the 3 trials I ran against my quick-code)

    Java Code:
    public class pizzaParty {
        private static final double pizzaPrice = 10.50;
        private static final double slicePrice = 1.25;
        private static final double drinkPrice = 0.90;
        private static final double taxPercent = 0.095;
        public static void main(String[] args) throws java.util.InputMismatchException, java.util.NoSuchElementException {
            System.out.println("Enter the number of attendees: ");
            java.util.Scanner scanner = new java.util.Scanner(System.in);
            int attendees = scanner.nextInt();
            scanner.close();
            double pizzasPrice = (attendees * 4 / 10) * pizzaPrice + (attendees * 4 % 10) * slicePrice;
            double drinksPrice = (attendees * 2) * drinkPrice;
            double tax = (pizzasPrice + drinksPrice) * taxPercent;
            double total = pizzasPrice + drinksPrice + tax;
            System.out.printf("Attendees = %d\npizzas = %d\nslices = %d\npizzas cost = %.2f\n"
                            + "drinks = %d\ndrinks cost = %.2f\ntax due = %.2f \ntotal due = %.2f",
                            attendees, (attendees * 4 / 10), (attendees * 4 % 10),
                            pizzasPrice, (attendees * 2), drinksPrice, tax, total);
        }
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •