Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Help Again Last Part of my Problem :(

1. ## [SOLVED] Help Again Last Part of my Problem :(

Hey guys.

Find attached the question

At a party there are guests and a cake that will be cut into equal slices to give each guest a slice. Code an application (Q3) that, given the number of guests and the radius of the cake in cms, will output the length of each slice’s arc given that it must be an exact whole number of cms. If calculations show that the length of each slice’s arc will be less than a cm, output zero for the length to indicate the cake cannot be cut.
In addition output the cake’s circumference (total arc length), the total length of the arc used by all slices and the length of the arc that remains unused.
e.g. (see p 118-9 of the prescribed text to display only 2 decimal digits)
Enter the radius of the cake (in cms): 10.5
Enter number of guests? 5
total arc (circumference = 65.97
slice arc = 13.00
used arc = 65.00
left over arc = 0.97
Now Please find the code I have done so far.

```import java.util.Scanner;
import java.text.DecimalFormat;

public class AS1Q3 {

public static void main(String[] args) {

Scanner scan = new Scanner (System.in);

double radiusNum, cirCumference, sliceArc, usedArc, leftoverArc;
int numGuests;

System.out.print("Enter the radius of the cake (in cms): ");

System.out.print("Enter the number of guests?: ");
numGuests = scan.nextInt();

cirCumference = 2 * Math.PI * radiusNum;
//System.out.println(cirCumference);

sliceArc = cirCumference / numGuests;
leftoverArc = (cirCumference) - (sliceArc * numGuests);

DecimalFormat fmt = new DecimalFormat ("0.##");

System.out.println ("\ntotal arc = "+fmt.format(cirCumference)+"\nslice arc = "+fmt.format(sliceArc));
System.out.println (fmt.format(leftoverArc));

}

}```

I seem to be having an issue getting the outputs to display properly as well as the maths behind the code to generate the values. well I think ive done my maths right :0

Basically my outputs are meant to show the following as above but I still get numbers being added after the decimal place when they need to be replaced with 0

Any advice is appreciated, I am not after the solution just need to be shown an example or guided in the right path.

Cheers
Matt

2. ## Re: Help Again Last Part of my Problem :(

Perhaps you are looking for the Math.round, Math.ceiling, and Math.floor?(assuming i understand the question you have)

3. ## The Following User Says Thank You to sunde For This Useful Post:

metalx66 (March 19th, 2011)

4. ## Re: Help Again Last Part of my Problem :(

All good, I worked it out yesterday