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 10 of 10

Thread: writing a method to add fractions?

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default writing a method to add fractions?

    Okay so I have to write a method to compute the following series: m(i)= 1/3 + 2/5 +....+ (i / 2i+1) and write a test program that displays a table " i = m(i)" 1=0.3333 2=0.7333....all the way down to 20 which is 9.2480. I have written something and cannot seem to get the sum of the fractions to display . I could be going about this the completely wrong way but this was my attempt. Please give suggestions!

    public class ExtraCredit1
    {
    	public static void main(String[] args)
    	{
    		double num;
    		double sum = 1;
     
    	for (int i = 1; i <= 20; i++)
    	{
    		System.out.println("" + i + " " + sum + "");
    	}
    }
    	public static double m(double i)
    	{
    		double sum = 1;
    	    sum += (double) i / (2*i + 1);
     
    		return sum;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: writing a method to add fractions?

    cannot seem to get the sum of the fractions to display
    What happens when you compile and execute the code? Copy the output and paste it here. Add some comments to the posted output showing what the output should look like.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: writing a method to add fractions?

    When I compile it shows the 'i' = 1,2,3,4....20 but the m(i) is 0,0,0,0.... and needs to be 0.3333, 0.7333.... all the way to 9.2480... its supposed to add all the fractions together and display... 1/3 = 0.3333, 1/3 + 2/5 = 0.7333...

    Its not supposed to show the actual fraction in the output just;
    i m(i)
    1 = 0.3333
    2 = 0.7333
    3
    4

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: writing a method to add fractions?

    What does the program print out when it executes? Can you copy the exact output and not interpret it in text?

    BTW m is a poor name for a method. The name of a method should be a verb saying what the method is doing.

    Where is the m method called?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: writing a method to add fractions?


  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: writing a method to add fractions?

    BTW m is a poor name for a method. The name of a method should be a verb saying what the method is doing.

    Where is the m method called?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: writing a method to add fractions?

    I understand it is a poor name for a method however thats what the teachers instruction are. Do I need to call the method into my println?

    --- Update ---

    import java.text.DecimalFormat;
     
    public class ExtraCredit1
    {
    	public static void main(String[] args)
    	{
    		double num;
    		double sum = 0;
     
    	DecimalFormat formatter = new DecimalFormat("0.0000");
     
    	for (int i = 1; i <= 20; i++)
    	{
    		System.out.println("" + i + "  =  " + formatter.format(m(i)) + "");
    	}
    }
    	public static double m(double i)
    	{
    		double sum = 0;
    	    sum += (double) i / (2*i + 1);
     
    		return sum;
    	}
    }


    --- Update ---

    now it displays the fractions but it isnt adding them up after each iteration

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: writing a method to add fractions?

    Do I need to call the method
    If a method is NOT called, it does not execute. If it does not execute, it will not compute any value.

    --- Update ---

    it isnt adding them up after each iteration
    If you want to accumulate the sum of previous values with the current value, you need to save the sum of the previous values.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: writing a method to add fractions?

    okay so....
    	public static double m(double i)
    	{
    		double sum = 0;
    	    sum += (double) (i / (2*i + 1));
                total = sum + sum
    		return total; //Will this return the accumulative total?
    	}
    }


    --- Update ---

    no that doesnt work either... hmm

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: writing a method to add fractions?

     total = sum + sum
    Did that code compile? It's better if you compile and execute the code BEFORE posting it.

    That looks the same as: total = 2*sum;

    Where is the value from the last call to m() saved? That is where the new value for the fraction needs to be added to the old value of sum
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Writing a getColor() method
    By KristopherP in forum Java Theory & Questions
    Replies: 14
    Last Post: April 23rd, 2013, 06:33 PM
  2. [SOLVED] Java static method writing to a file
    By maple1100 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 4th, 2013, 05:12 PM
  3. Writing a method to draw a rotatable rectangle using Graphics2D
    By johnpooley3 in forum AWT / Java Swing
    Replies: 3
    Last Post: May 18th, 2012, 09:28 AM
  4. Writing output from a void method to a file
    By TheWhopper858 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 9th, 2011, 05:21 AM
  5. Writing clone() method for LinkedList
    By vluong in forum Collections and Generics
    Replies: 6
    Last Post: October 27th, 2009, 08:41 AM