Having issues with rounding numbers - java

import java.util.Scanner;
public class clockwise {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the coordinates in a clowise order");
System.out.println("Enter the GPS coordinates for the 1st city: ");
double coordinateX1 = input.nextDouble();
double coordinateY1 = input.nextDouble();
System.out.println("Enter the GPS coordinates for the 2nd city: ");
double coordinateX2 = input.nextDouble();
double coordinateY2 = input.nextDouble();
System.out.println("Enter the GPS coordinates for the 3rd city: ");
double coordinateX3 = input.nextDouble();
double coordinateY3 = input.nextDouble();
System.out.println("Enter the GPS coordinates for the 4th city: ");
double coordinateX4 = input.nextDouble();
double coordinateY4 = input.nextDouble();
//
double earthRadius = 6371.01;
// Get distance
// distance=(radius)arccos(sin(x1)sin(x2)+cos(x1)cos(x2)cos( y1−y2))
// ****************************************************************************1
// 1 35.2270869 -80.8431267
double distance1 = (earthRadius)
* Math.acos(Math.sin(coordinateX1) * Math.sin(coordinateX2))
+ Math.cos(coordinateX1) * Math.cos(coordinateX2)
* Math.cos(coordinateY1 - coordinateY2);
System.out.println("distance1: "+distance1);
// 2 35.2270869 -80.8431267
double distance2 =
(earthRadius)
* Math.acos(Math.sin(coordinateX2) * Math.sin(coordinateX4))
+ Math.cos(coordinateX2) * Math.cos(coordinateX4)
* Math.cos(coordinateY2 - coordinateY4);
System.out.println("distance2: "+distance2);
// 3 28.5383355 -81.3792365
double distance3 = (earthRadius)
* Math.acos(Math.sin(coordinateX4) * Math.sin(coordinateX1))
+ Math.cos(coordinateX4) * Math.cos(coordinateX1)
* Math.cos(coordinateY4 - coordinateY1);
System.out.println("distance3: "+distance3);
// ******************************************************************************2
// 4 33.7489954 -84.3879824
// 1 35.2270869 -80.8431267
double distance01 = (earthRadius)
* Math.acos(Math.sin(coordinateX2) * Math.sin(coordinateX3))
+ Math.cos(coordinateX2) * Math.cos(coordinateX3)
* Math.cos(coordinateY2 - coordinateY3);
// System.out.println("distance: "+distance01);
// 2 32.0835407 -81.0998342
double distance02 = (earthRadius)
* Math.acos(Math.sin(coordinateX3) * Math.sin(coordinateX4))
+ Math.cos(coordinateX3) * Math.cos(coordinateX4)
* Math.cos(coordinateY3 - coordinateY4);
//System.out.println("distance: "+distance02);
// 3 28.5383355 -81.3792365
double distance03 =
(earthRadius)
* Math.acos(Math.sin(coordinateX4) * Math.sin(coordinateX2))
+ Math.cos(coordinateX4) * Math.cos(coordinateX2)
* Math.cos(coordinateY4 - coordinateY2);
// System.out.println("distance: "+distance03);
double rodistance1 = Math.ceil(distance1);
double rodistance2 = Math.ceil(distance1);
double rodistance3 = Math.ceil(distance3);
double rodistance01 = Math.ceil(distance01);
double rodistance02 = Math.ceil(distance02);
double rodistance03 = Math.ceil(distance03);
double s1 = (rodistance1 + rodistance2 + rodistance3) / 2;
double s2 = (rodistance01 + rodistance02 + rodistance03) / 2;
double area1 = Math.sqrt(s1 * (s1 - rodistance1) * (s1 - rodistance2)
* (s1 - rodistance3));
double area2 = Math.sqrt(s2 * (s2 - rodistance01) * (s2 - rodistance02)
* (s2 - rodistance03));
double totalArea = Math.ceil(area1 + area2);
System.out.println("The area is: " + totalArea);
}
}
//SAMPLE
//Please enter the coordinates in a clockwise order.
//Enter the GPS coordinates for the 1st city: 35.2270869 -80.8431267
//Enter the GPS coordinates for the 2nd city: 32.0835407 -81.0998342
//Enter the GPS coordinates for the 3rd city: 28.5383355 -81.3792365
//Enter the GPS coordinates for the 4th city: 33.7489954 -84.3879824
//The area is: 117863.342
I am getting 1.06664794E8 What can I do to get the answer as the sample? Unless I am having errors with my formulas, I should be getting a ok answer. I am using Math.ceil() it might not be what I really wanted I needed to round to 3 decimals .................................................................

Use BigDecimal and setScale()
BigDecimal bg1 = new BigDecimal("1.06664794E8");
// set scale of bg1 to 3 and using CEILING as rounding mode
bg1 = bg1.setScale(3, RoundingMode.CEILING);
System.out.println("After changing the scale to 3 and rounding is "+bg1);
Output:After changing the scale to 3 and rounding is 106664794.000

You can use BigDecimal here
double val = 1.06664794E8;
BigDecimal bigDecimal = new BigDecimal(val);
System.out.println(bigDecimal);
Out put:
106664794

Related

Finding The Area Of A Triangle

I have a question that I would like to ask you. In my book for java programming, it asks me to write a program that finds the area of a triangle given 3 points. I tried many ways but I could never get the right answer. Can you please give me a solution to this problem. Thanks! Here is the question:
Here is my code:
import java.util.Scanner;
public class shw2point15 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter three points for a triangle:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double x3 = input.nextDouble();
double y3 = input.nextDouble();
double s = ((x1 + y1) + (x2 + y2) + (x3 + y3)) / 2;
double area = Math.sqrt(s * (s - (x1 - y1)) * (s - (x2 - y2)) * (s - (x3 - y3)));
System.out.println("The area of the triangle is " + area);
}
}
The reason you're not getting a correct answer is because you are not finding the sides correctly. However, after finding the side length you can get the answer. Here is what I did:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter three points for a triangle:");
//Store the values in an array.
double[] xCoordinates = new double[3];
double[] yCoordinates = new double[3];
double[] sides = new double[3];
// Input the values into the array
xCoordinates[0] = input.nextDouble();
yCoordinates[0] = input.nextDouble();
xCoordinates[1] = input.nextDouble();
yCoordinates[1] = input.nextDouble();
xCoordinates[2] = input.nextDouble();
yCoordinates[2] = input.nextDouble();
// Find the side length from the input. There probably are better ways to do this.
sides[0] = Math.sqrt(Math.pow(xCoordinates[0]-xCoordinates[1], 2)+Math.pow(yCoordinates[0]-yCoordinates[1], 2));
sides[1] = Math.sqrt(Math.pow(xCoordinates[1]-xCoordinates[2], 2)+Math.pow(yCoordinates[1]-yCoordinates[2], 2));
sides[2] = Math.sqrt(Math.pow(xCoordinates[2]-xCoordinates[0], 2)+Math.pow(yCoordinates[2]-yCoordinates[0], 2));
// Find s from the sides
double s = ( sides[0]+sides[1]+sides[2] )/2;
// Find the area.
double area = Math.sqrt(s*( s-sides[0] )*( s-sides[1] )*( s-sides[2] ));
// Print the area
System.out.println("The area of the triangle is "+area);
// Output~~~~~~~~~~~~~~~
//Enter three points for a triangle:
// 1.5
// -3.4
// 4.6
// 5
// 9.5
// -3.4
// The area of the triangle is 33.600000000000016
}'

Calculating the area of a triangle

I am new to Java. I am trying to calculate the area of a triangle using the formula:
s = (side 1 + side 2 + side 3)/2
area = square root (side (side - side 1)(side - side2)(side - side3).
If the user enter the three point as:
1.5 -3.4 4.6 5 9.5 -3.4 then the area of the triangle should be 33.6. However, my program runs, but it's giving me an incorrect answer. Below is my code.
// Import Java Scanner
import java.util.Scanner;
import java.lang.Math;
public class Ex_2_19 {
public static void main(String[] args) {
//Create a Scanner object
Scanner input = new Scanner(System.in);
float side = 0;
float area1 = 0;
float area2 = 0;
float area3 = 0;
float area4 = 0;
float calculatedarea = 0;
//Prompt the user to enter three points of a triangle
System.out.println("Enter point x1:");
System.out.println("Enter point y1:");
System.out.println("Enter point x2:");
System.out.println("Enter point y2:");
System.out.println("Enter point x3:");
System.out.println("Enter point y3:");
//Define the variables
float Pointx1 = input.nextFloat();
float Pointy1 = input.nextFloat();
float Pointx2 = input.nextFloat();
float Pointy2 = input.nextFloat();
float Pointx3 = input.nextFloat();
float Pointy3 = input.nextFloat();
//Formula to calculate the area of a triangle
side = (Pointx1 + Pointy1 + Pointx2 + Pointy2 + Pointx3 + Pointy3) / 2;
area1 = side - (Pointx1 + Pointy1);
area2 = side - (Pointx2 + Pointy2);
area3 = side - (Pointx3 + Pointy3);
area4 = side * area1 * (area2) * (area3);
calculatedarea = (float) (Math.sqrt(area4));
//calculatedarea = (float) (Math.sqrt(area1)*(area2) * (area3));
//Print result
System.out.println("The area of the triangle is " + calculatedarea);
}
}
You are trying Heron's Formula - note that a, b, c are the euclidean distance between the points, thus will need to be computed by sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)), etc., not just the sum of differences.

Trouble with getting area of figure

import java.util.Scanner;
public class Hw4Problem1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double X1 = 35.2270869;
double Y1 = -80.8431267;
double X2 = 32.0835407;
double Y2 = -81.0998342;
double X3 = 28.5383355;
double Y3 = -81.3792365;
double X4 = 33.7489954;
double Y4 = -84.3879824;
//Radius*************************]
double radius= 6731.01;
// distance=(radius)arccos(sin(x1)sin(x2)+cos(x1)cos(x2)cos( y1−y2))
double charlotteToSavannah = (radius)* Math.acos(Math.sin(X1) * Math.sin(X2)+ Math.cos(X1) * Math.cos(X2)
* Math.cos(Y1 - Y2));
double savannahToAtlanta = (radius)
* Math.acos(Math.sin(X2) * Math.sin(X4)
+ Math.cos(X2) * Math.cos(X4)
* Math.cos(Y2 - Y4));
// Get distance
// distance=(radius)arccos(sin(x1)sin(x2)+cos(x1)cos(x2)cos( y1−y2))
double atlantaToCharlotte= (radius)
* Math.acos(Math.sin(X4) * Math.sin(X1)
+ Math.cos(X4) * Math.cos(X1)
* Math.cos(Y4 - Y1));
// ******************************************************************************2
double savannahToOrlando = (radius)
* Math.acos(Math.sin(X2) * Math.sin(X3)
+ Math.cos(X2) * Math.cos(X3)
* Math.cos(Y2 - Y3));
double orlandoToAtlanta = (radius)
* Math.acos(Math.sin(X3) * Math.sin(X4)
+ Math.cos(X3) * Math.cos(X4)
* Math.cos(Y3 - Y4));
double atlantaToSavannah =
(radius)
* Math.acos(Math.sin(X4) * Math.sin(X2)
+ Math.cos(X4) * Math.cos(X2)
* Math.cos(Y4 - Y2));
// System.out.println("distance: "+distance03);
double rodistance1 = (charlotteToSavannah);
double rodistance2 = (savannahToAtlanta);
double rodistance3 = (atlantaToCharlotte);
double rodistance01 = (savannahToOrlando);
double rodistance02 = (orlandoToAtlanta);
double rodistance03 = (atlantaToSavannah);
double s1 = (rodistance1 + rodistance2 + rodistance3) / 2;
double s2 = (rodistance01 + rodistance02 + rodistance03) / 2;
//=√ s(s−side 1)(s−side 2)(s−side3)
double area1 = Math.sqrt(s1 * (s1 - rodistance1) * (s1 - rodistance2)
* (s1 - rodistance3));
double area2 = Math.sqrt(s2 * (s2 - rodistance01) * (s2 - rodistance02)
* (s2 - rodistance03));
double totalArea = (area1 + area2);
System.out.println("The area is: " + totalArea);
}
}
compute the area of land surrounded by the polygon created by these cities.
Radius: 6,371.01 km
s= (side 1+side 2+side3)/2
area_of_triangle=√ s(s−side 1)(s−side 2)(s−side3)
I am having trouble with this program I am geting the wrong answer, I am getting 7.048521505923942E7
After changing the scale to 3 and rounding is 17516629.000
It should be: 117863.342
I already try different ways and I am still getting the same answers.

Loop will not end. How do i make it exit?

Got scolded earlier for not specify the question. Promise to do better this time.
For some reason, the loop doesn't stop when i enter 0, instead it continues to print out the main method again. How do i fix this? I couldn't find out what is wrong with it, since everything else runs smoothly.
Also, I want to use Switch instead of If, but i keep getting Duplicate Variable error. Why?
import java.util.Scanner;
public class FindArea
{
// side is only input provided to cube. Area of the cube is returned
public static double cube (double side)
{
double Area;
return Area = 6 * side * side;
}
// radius is only input provided to sphere. Area of the sphere is returned
public static double sphere (double radius)
{
double Area;
return Area = 4 * 3.14 * radius * radius;
}
// radius and height are the only inputs provided to cylinder.
// Area of the cylinder is returned
public static double cylinder (double radius, double height)
{
double Area;
return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius;
}
// outerR and innerR are the only inputs provided to doughnut.
// Area of the doughnut is returned
public static double doughnut (double outerR, double innerR)
{
double Area;
return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
}
public static void main(String[] args)
{
int n = 4;
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
do
{
n = input.nextInt();
if ( n<0 || n>4) {
System.out.println("Invalid");
}
if(n == 1) {
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
}
if (n == 2) {
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
}
if (n == 3) {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
}
if (n == 4) {
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
}
System.out.println("--------");
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
} while (n != 0);
System.exit(1);
}
}
It prints the output one more time because the comparison is not made until the end of the Do-While loop.
It checks for all cases of n other than 0, and prints the output no matter what.
I think what you're looking for is more so:
n = input.nextInt();
if(n!=0){
if ( n<0 || n>4) {
System.out.println("Invalid");
}
if(n == 1) {
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
}
if (n == 2) {
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
}
if (n == 3) {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
}
if (n == 4) {
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
}
System.out.println("--------");
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
}
note the n!=0
Additionally look into the use of switch-case, once you learn them these comparisons will not only be faster, but very easy to comprehend
You can try this with your switch case -
public static void main(String[] args)
{
int n = 4;
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
n = input.nextInt();
if ( n<0 || n>4) {
System.out.println("Invalid");
}
else{
switch(n){
case 0:{
System.out.println("You enter 0 to exit");
break;
}
case 1:{
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
break;
}
case 2:{
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
break;
}
case 3: {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
break;
}
case 4:{
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
break;
}
}
}
}
i have done this using switch case. Very simple as well
public class FindArea {
// side is only input provided to cube. Area of the cube is returned
public static double cube(double side) {
double Area;
return Area = 6 * side * side;
}
// radius is only input provided to sphere. Area of the sphere is returned
public static double sphere(double radius) {
double Area;
return Area = 4 * 3.14 * radius * radius;
}
// radius and height are the only inputs provided to cylinder.
// Area of the cylinder is returned
public static double cylinder(double radius, double height) {
double Area;
return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius;
}
// outerR and innerR are the only inputs provided to doughnut.
// Area of the doughnut is returned
public static double doughnut(double outerR, double innerR) {
double Area;
return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out
.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut, Or enter 0 to exit ");
int n = 0;
double side = 0;
double Area = 0;
double radius = 0;
double height = 0;
n = input.nextInt();
switch (n) {
case 0:
System.out.println("Exit");
System.exit(1);
break;
case 1:
System.out.print("Enter side measurement of cube: ");
side = input.nextDouble();
Area = cube(side);
System.out.println("The area of the cube is: " + Area);
break;
case 2:
System.out.print("Enter radius measurement of sphere: ");
radius = input.nextDouble();
Area = sphere(radius);
System.out.println("The area of the sphere is: " + Area);
break;
case 3:
System.out.print("Enter radius measurement of cylinder: ");
radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
height = input.nextDouble();
Area = cylinder(radius, height);
System.out.println("The area of the cylinder is: " + Area);
break;
case 4:
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
Area = doughnut(outerR, innerR);
System.out.println("The area of the donut is: " + Area);
break;
default:
System.out.println("--------");
System.out.println("Invalid option selected");
System.out.println("--------");
System.out
.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
n = input.nextInt();
break;
}
}
}
Good Luck.

calculations errors in a pizza game

i have to do a pizza game ... i wrote all the code already .. it seems to work right most of it . the thing is that my numbers don't came out as they suppose to ... i know my error is in when i calculate the the cheesy crust.... can someone please help!
If the customer chooses to add cheesy crust (to anything but thin & crispy), then add the following to the basic price of the pizza:
k = ec
where:
k = total cost of cheesy crust (dollars)
e = size of pizza crust (inches)
c = cost of materials (dollars per square inch - 0.02)
The size of the pizza crust is a measure of the pizza border:
for round pizzas: e = 2π(d/2) (the circumference)
for rectangular pizzas: e = 2(L+w) (the perimeter)
where
e = size of pizza crust (inches)
d = diameter of round pizza (inches)
L = length of rectangular pizza (inches)
w = width of rectangular pizza (inches)
enter code here
import java.util.Scanner;
public class Pizza {
/*
* calculates price including tax
* #return double
*/
public static double calculatePriceWithTax(double price){
return price * (1 + 0.07);
}
/*
* calculates delivery fee if any
* #return double
*/
public static double deliveryFee(double price){
if(price < 10){
return 3;
}
else if(price >= 10 && price <= 20){
return 2;
}
else if(price >= 20 && price <= 30){
return 1;
}
else{
return 0;
}
}
/*
* calculates basic pricing for total number of pizza
* #return double
*/
public static double calculateCost(int shape, int diameter, int length, int width, int numToppings, int typeDough){
double a = area(shape, diameter, length, width);
double v = volume(a, typeDough);
double cost = a * (0.036 + numToppings * 0.025 ) + v * 0.019;
return cost;
}
/*
* calculates area of a pizza
* #return double
*/
public static double area(int shape, int diameter, int length, int width){
double a = 0.0;
if(shape == 1){ // round pizza
a = Math.PI * (diameter / 2) * (diameter / 2);
}
else if(shape == 2){ // rectangular pizza
a = length * width;
}
return a;
}
/*
* calculates volume of a pizza
* #return double
*/
public static double volume(double a, int typeDough){
double height = 0.0;
switch(typeDough){
case 1:
height = 0.1;
break;
case 2:
height = 0.25;
break;
case 3:
height = 0.5;
break;
case 4:
height = 0.9;
break;
}
return a * height;
}
/*
* calculates cost for cheesy crust
* #return double
*/
public static double calculateChessyCrustCost(int shape, int diameter, int length, int width){
return size(shape, diameter, length, width) * 0.02;
}
/*
* calculates size of pizza
* #return double
*/
public static double size(int shape, int diameter, int length, int width){
if(shape == 1){ // round pizza
return 2 * Math.PI * (diameter / 2);
}
else{ // rectangular pizza
return 2 * (length + width);
}
}
public static void main(String[] args) {
int shape; // 1 => round, 2 => rectangular
int length = 0;
int width = 0;
int diameter = 0;
int numToppings = 0; // number of toppings
int typeDough; // 1 => thin & crusty, 2 => hand tossed, 3 => pan, 4 => texas toast
boolean cheesyCrust = false; // true => add, false => don't add
int lengthCrust = 0;
int numPizza; // number of pizzas ordered
int orderType; // 1 => delivery, 2 => take-out
Scanner scanner = new Scanner(System.in);
System.out.println("This program helps you to order pizza based on your personal preferences.");
System.out.println("It notes your choices and calculates total cost for you, including tax and even delivery fee if applicable.");
System.out.println("Please fill out information for the following:");
System.out.println();
System.out.println("Pizza style:");
System.out.println("1. Round Pizza");
System.out.println("2. Rectangular Pizza");
shape = scanner.nextInt();
if(shape == 1){
System.out.println("Diameter:(inches)");
diameter = scanner.nextInt();
}
else if(shape == 2){
System.out.println("Length:(inches)");
length = scanner.nextInt();
System.out.println("Width:(inches)");
width = scanner.nextInt();}
else {throw new IllegalArgumentException("Enter enter 1 fo Round pizza or 2 for rectangualar pizza");
}
System.out.println("Number of toppings:");
numToppings = scanner.nextInt();
System.out.println("Type of dough:");
System.out.println("1. Thin & Crusty");
System.out.println("2. Classic Hand Tossed");
System.out.println("3. Pan");
System.out.println("4. Texas Toast");
typeDough = scanner.nextInt();
if(typeDough != 1){
System.out.println("Do you want to add cheest crust?[true/false]");
cheesyCrust = scanner.nextBoolean();
}
System.out.println("How many pizzas do you want to order?");
numPizza = scanner.nextInt();
System.out.println("Select your receival method:");
System.out.println("1. Delivery");
System.out.println("2. Take away");
orderType = scanner.nextInt();
double a = area(shape, diameter, length, width);
double v = volume(a, typeDough);
double baseCost = calculateCost(shape, diameter, length, width, numToppings, typeDough);
double crustCost = 0.0;
if(cheesyCrust == true){
crustCost = calculateChessyCrustCost(shape, diameter, lengthCrust, width);
}
double costOne = calculatePriceWithTax(baseCost + crustCost);
double deliveryCharge = 0.0;
if(orderType == 1){ // delivery
costOne += deliveryFee(costOne);
}
double totalCost = deliveryFee(costOne * numPizza) + (costOne * numPizza);
System.out.print("Area: ");
System.out.printf("%.2f", a);
System.out.print(" (inches square)");
System.out.println();
System.out.print("Volume:");
System.out.printf("%.2f", v);
System.out.print(" (cubic inches)");
System.out.println();
System.out.print("Base Cost: ");
System.out.printf("%.2f", baseCost);
System.out.print(" dollar");
System.out.println();
System.out.print("Cost for one pizza: ");
System.out.printf("%.2f", costOne);
System.out.print(" dollar");
System.out.println();
System.out.print("Your order has been processed. Total cost including taxes: ");
System.out.printf("%.2f", totalCost );
System.out.print(" dollars");
System.out.println();
System.out.println("Thank you for using our service.");
}
}
Problem 1 is in your area method:
a = Math.PI * (diameter / 2) * (diameter / 2);
in java, int / int = int, so you have to cast it to double.
to fix this, try the following:
a = Math.PI * (diameter / 2.0d) * (diameter / 2.0d);
The same problem is in your size method. It should look like this:
return 2 * Math.PI * (diameter / 2.0d);

Categories