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

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.

Related

User input values for a Rectangle

I have a class where a user inputs the x and y coordinates for the top left corner of a perfect rectangle, as well as the length and width. The RectangleViewer constructor sets the rectangle as null and then sets all the variables to 0.
Here is what I have so far:
public class RectangleViewer {
private Rectangle rectangle;
private Scanner input = new Scanner(System.in);
public RectangleViewer() {
this.rectangle = null;
this.rectangle = new Rectangle(new Point2D.Double(0.0, 0.0), 0.0, 0.0);
}
public void inputRectangleParameters() {
System.out.print("Please enter the x coordinate of the top left point of the rectangle: ");
double xCoordinate;
xCoordinate = input.nextDouble();
System.out.print("Please enter the y coordinate of the top left point of the rectangle: ");
double yCoordinate;
yCoordinate = input.nextDouble();
System.out.print("Please enter the length of the rectangle: ");
double inputLength;
inputLength = input.nextDouble();
System.out.print("Please enter the width of the rectangle: ");
double inputWidth;
inputWidth = input.nextDouble();
}
public void initializeRectangle() {
this.rectangle = new Rectangle(new Point2D.Double(inputRectangle());
}
}
Here is the code for the Rectangle class itself:
public class Rectangle {
private double width;
private double length;
private Point2D.Double topLeftPoint;
public Rectangle(Point2D.Double topLeftPoint, double width, double length) {
this.width = width;
this.length = length;
this.topLeftPoint = topLeftPoint;
}
How would I set it up so that the values that are put in inputRectangleParameters are assigned to a rectangle that will be created in initializeRectangle?
The method inputRectangleParameters reads values from the command line. If you wish to create the rectangle in the initializeRectangle method, the read values need to be passed to it.
This could be done by e.g. returning the values in an object.
As the Rectangle class contains according attributes for the values, I'd recommend to create a rectangle object within the input method and returning it to initialize.
public Rectangle inputRectangleParameters() {
System.out.print("Please enter the x coordinate of the top left point of the rectangle: ");
double xCoordinate;
xCoordinate = input.nextDouble();
System.out.print("Please enter the y coordinate of the top left point of the rectangle: ");
double yCoordinate;
yCoordinate = input.nextDouble();
System.out.print("Please enter the length of the rectangle: ");
double inputLength;
inputLength = input.nextDouble();
System.out.print("Please enter the width of the rectangle: ");
double inputWidth;
inputWidth = input.nextDouble();
return new Rectangle(new Point2D.Double(xCoordinate,yCoordinate),inputWidth,inputLength);
}
public void initializeRectangle() {
this.rectangle = inputRectangleParameters();
}
A less clean version would be to return e.g. an array of the values with specific positions of values.
public double[] inputRectangleParameters() {
System.out.print("Please enter the x coordinate of the top left point of the rectangle: ");
double xCoordinate;
xCoordinate = input.nextDouble();
System.out.print("Please enter the y coordinate of the top left point of the rectangle: ");
double yCoordinate;
yCoordinate = input.nextDouble();
System.out.print("Please enter the length of the rectangle: ");
double inputLength;
inputLength = input.nextDouble();
System.out.print("Please enter the width of the rectangle: ");
double inputWidth;
inputWidth = input.nextDouble();
return new double[]{xCoordinate, yCoordinate, inputWidth, inputLength};
}
public void initializeRectangle() {
final double[] rectangleParameters = inputRectangleParameters();
this.rectangle = new Rectangle(new Point2D.Double(rectangleParameters[0], rectangleParameters[1]), rectangleParameters[2], rectangleParameters[3]);
}

Inputing mathematical formula into java

I am having a problem I have not been able to solve searching the internet, basically I need the user to input first the diameter of a sphere then get back the radius and from there use the formula (4 over 3 multiplied by pi (3.14) multiplied by radius (calculated from diameter - user inputted) to the power of 3.
and the other one is very similar..... 4 multiplies by pi (3.14) multiplied by radius to the power of 2.
Now the thing is every time I try to compile I get errors that the method is lossy double not int or the symbol is not recognized.
guys any help would be helpful since I cant find a solution for this.
the code is below :
import static java.lang.Math.sqrt;
import static java.lang.Math.abs;
import java.lang.*;
import java.util.Scanner;
import java.util.*;
public class RadiusConverter {
public class Pi{
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int FirstStep, Diameter, Radius, SecondStep, ThirdStep;
System.out.println ("Please enter the diameter of the Sphere: ");
int diameter =scan.nextInt();
System.out.println ("Your Shpere has a radius of: " + FirstStep);
int radius = scan.nextInt();
System.out.println ("The Volume of your Sphere is: " + SecondStep);
System.out.println("The Surface Area of your Sphere is: " + ThirdStep);
FirstStep = Diameter / 2;
SecondStep = 4 / 3 * 3.14 * (int) FirstStep * 3;
ThirdStep = 4 * 3.14 * (int) Radius * 2;
Diameter = diameter;
Radius = radius;
//------------------------------------------------------------------------
//
//
//-------------------------------------------------------------------------
scan.close();
}
}
This is the older source code I was using … which also did not work.
import static java.lang.Math.sqrt;
import static java.lang.Math.abs;
import java.lang.*;
import java.util.Scanner;
import java.util.*;
public class RadiusConverter
{
public class Pi{
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
double FirstStep, Diameter, Radius, SecondStep, ThirdStep;
System.out.println ("Please enter the diameter of the Sphere: ");
double diameter =scan.nextDouble();
System.out.println ("Your Shpere has a radius of: " + FirstStep);
double radius =scan.nextDouble();
System.out.println ("The Volume of your Sphere is: " + SecondStep);
System.out.println("The Surface Area of your Sphere is: " + ThirdStep);
FirstStep = diameter / 2;
SecondStep = 4 / 3 * Math.PI* Math.pow (FirstStep, 3);
ThirdStep = 4 * Math.PI * radius * Math.pow (2);
//------------------------------------------------------------------------
//
//
//-------------------------------------------------------------------------
scan.close();
}
}
try out this one
import java.util.Scanner;
public class RadiusConverter {
public class Pi {
}
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
int firstStep = 0, diameter = 0, radius = 0, secondStep = 0, thirdStep = 0;
System.out.print("Please enter the diameter of the Sphere: ");
diameter = scan.nextInt();
System.out.print("Please enter the radius of the Sphere: ");
radius = scan.nextInt();
firstStep = diameter / 2;
secondStep = (int) (4 / 3 * 3.14 * firstStep * 3);
thirdStep = (int) (4 * 3.14 * radius * 2);
System.out.println("Your Shpere has a radius of: " + firstStep);
System.out.println("The Volume of your Sphere is: " + secondStep);
System.out.println("The Surface Area of your Sphere is: " + thirdStep);
}
}
}
Output
Please enter the diameter of the Sphere: 250
Please enter the radius of the Sphere: 25
Your Shpere has a radius of: 125
The Volume of your Sphere is: 1177
The Surface Area of your Sphere is: 628

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.

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