Friday, January 26, 2007

My most recent beginning code: PizzaCalculations

import java.util.Scanner;
/** @author Don Lam
* pizza parlor calculations
* */

public class PizzaCalculations {
public static void main(String []args) {

double initialBase= .05; // cost of pizza/square inch
double radius= 1; //radius of pizza
double costPizza= 1; //cost is Area*.05 or (input radius/2)^2 * pi
double area= Math.PI*Math.pow(radius/2,2);

System.out.print("Enter pizza size in inches: ");
Scanner input= new Scanner (System.in);

radius=input.nextDouble();
area= Math.PI*Math.pow(radius/2,2);
System.out.println("Your pizza size is: "+(int)radius);
System.out.format("Your area of pizza is: " + "%.2f%n",area);

costPizza=area*initialBase;
System.out.format("The cost of your pizza is $"+"%.2f%n",costPizza);

// Second calculation finding the cost for toppings

double toppingBase= .01;
double toppingCost= 1;
double amountTopping=1;

System.out.print("Enter the number of toppings");
amountTopping= input.nextDouble();
System.out.print("Enter topping size(pizza size) in inches: ");

radius=input.nextDouble();
System.out.println("Your topping size is "+(int)radius+" inches");
area=Math.PI*Math.pow(radius/2,2);
toppingCost=toppingBase*area*amountTopping;

System.out.format("Cost of that size with "+(int)amountTopping + " topping is $" + "%.2f%n",toppingCost);

//Finding the delivery cost

double miles=1;
double fuelCost= 1.89; //cost per one mile
double carTank= 54; //fuel efficiency
double t=2; //for round trip
double delivery= miles/carTank*fuelCost*2; //cost for round trip delivery with x number of miles
System.out.println("Enter number of miles to destination: ");
miles=input.nextDouble();
delivery= miles/carTank*fuelCost*2;
System.out.format("Cost for a round trip delivery for "+ miles +" miles" + " is $" + "%.2f%n", delivery);

//Calculating profit margin

double productCost=1;
double sellingPrice=1;
double profitMargin=(sellingPrice-productCost)/sellingPrice;
System.out.println("Enter cost of production: ");
productCost=input.nextDouble();
System.out.println("Enter selling cost: ");
sellingPrice=input.nextDouble();
profitMargin=(sellingPrice-productCost)/sellingPrice*100;
System.out.print("Your profit margin for sell at $"+ sellingPrice +" with production cost of $" + productCost + " is " + (int)profitMargin);
System.out.print("%");

//This is pizza Calculation Lab

}
}

This program is my third dealing with Java. This code introduces to taking user imports package, the import java.util.Scanner; and some Java arithmetic using Math.pow, and Math.pi. The if you compile the program it is suppose to be able to help you figure out how much your pizza would cost with a certain diameter, how much a delivery cost for the pizza would be based on 54miles per gallon efficiency. All the comments are provided in the code so it is very intuitive you're trying to figure it Java out for the first time. If you wish you can change the code around and make edits to it to find the cost of the pizza from wherever you want buy from.

I'm open to suggestions of making the code more efficient or complicated, as I say, this is only my third program.

2 comments:

CDon said...

In the process of coding this I discover a new way to output numbers without having them trail forever.

Using System.out.format("%.2f%n",xid) whatever value within in xid will be display with only 2 decimal format. To change how many decimal places just change the value after the period.

Yeah yea, I know its simple stuff, but to someone who's just knew to this, this is awesome.

Anonymous said...

Keep up the good work.

NCSU CSC News Channel

The Weather Channel: Your Local Weather Outlook--Raleigh, NC (27608)