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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help Again Last Part of my Problem :(

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy [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): ");
    		radiusNum = scan.nextDouble();
     
    		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
    Last edited by metalx66; March 19th, 2011 at 09:48 PM. Reason: Solved.


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default 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. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help Again Last Part of my Problem :(

    All good, I worked it out yesterday

Similar Threads

  1. Trying to clean this part of my progam up, but cant seem to get it.
    By Mob31 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 25th, 2011, 04:49 PM
  2. can' stop working a part of code.plzz help
    By kyros in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 03:10 PM
  3. Paint only part of screen
    By bonus_clip in forum AWT / Java Swing
    Replies: 7
    Last Post: October 16th, 2010, 07:21 PM
  4. How to write to specific part of an html file using java
    By nasi in forum File I/O & Other I/O Streams
    Replies: 12
    Last Post: May 27th, 2010, 11:22 PM
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM