SRL Archive - Java Help and Tutorials

Elements third java assignment!


Element17

2013-09-12 22:39:02

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!
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);
}
}

Le Jingle

2013-09-13 01:26:16

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

riwu

2013-09-13 06:37:07

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 :)

[XoL]

2013-09-13 09:20:25

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.

Brandon

2013-09-13 10:20:31

A constant should be initialized upon variable declaration.

Consider:


final double Cost;
Cost = 0.5;


Now consider:


final double Cost = 0.5;

Element17

2013-09-13 11:47:11

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.


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);
}
}

Le Jingle

2013-09-13 13:16:46

appears your work looks substantial (matched the 3 trials I ran against my quick-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);
}
}