Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-07-2008, 10:15 PM
Member
 
Join Date: Oct 2008
Posts: 38
Rep Power: 0
SapphireSpark is on a distinguished road
Default (Help) Fraction Summation and Exponents
I'm making a program that's supposed to calculate the sum of this:



Code:
import java.util.Scanner;
public class Project5 {
    public static void main (String[] args) {
        Scanner scan = new Scanner (System.in);
        System.out.println ("Enter n: ");
        int n = scan.nextInt();
        double sum = 0.0;
        int x=1;
        //I'm not sure if that's right, because I'm trying to make x be the numerator, so it can keep increasing by 1. Should I do that?

        while (x<=n) {
        if (x%2==1) {
           sum += (x/    **how do I say 2 to the power of n?**  );
       }else{
          sum -= (x/       **how do I say 2 to the power of n?**  );
      }
   x++
}
And how do I get it to reach that end summation, of (-1) to the power of n+1?

I'm not even sure if the stuff I have so far can possibly work. I just know that we're not allowed to use "Math.pow". Please help, I can't find any useful examples in my book or notes.

Last edited by SapphireSpark; 10-08-2008 at 05:52 AM. Reason: to update the picture so its accurate
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-08-2008, 12:39 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Quote:
calculate the sum of this
Quote:
summation, of (-1) to the power of n+1?
Can you explain with an algebraic expression or in english what you are trying to do?

Do you know what x to the power of y means?
x*x*x*x*... y times
Does that look like the place to use a loop?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-08-2008, 05:54 AM
Member
 
Join Date: Oct 2008
Posts: 38
Rep Power: 0
SapphireSpark is on a distinguished road
Default
Yes, I know what x to the power of y means. I just can't figure out how to put it in a loop.

I updated the picture, its accurate now, that's what I'm trying to do: Find the sum of the sequence that continues until that final part is reached.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-08-2008, 06:07 AM
Member
 
Join Date: Oct 2008
Posts: 38
Rep Power: 0
SapphireSpark is on a distinguished road
Default
So, if the user inputs n to equal 5, the last term in the summation sequence will be *1 times (5 over 32).

And the program displays the sum of all the terms that come before that last term, plus the last term.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-08-2008, 07:15 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,506
Rep Power: 8
Fubarable is on a distinguished road
Default
the poster is a spammer.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-08-2008, 07:18 AM
Member
 
Join Date: Oct 2008
Posts: 38
Rep Power: 0
SapphireSpark is on a distinguished road
Default
hey, I said I was sorry for the previous topic, I really need help on this one. I'm not a spammer, it was the only way I could attach that picture on top.

Could someone please help me fix the loop statement? Please?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-08-2008, 07:37 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Why don't you use the math class for that. It's most easiest as well as the best way to follow.

Code:
import java.util.Scanner;

public class FindSummation {

    public static void main(String[] args) {
        Scanner userScann = new Scanner(System.in);
        System.out.println("Enter number of terms: ");
        int terms = userScann.nextInt();
        double sum = 0;
        
        for(int i = 1; i <= terms; i++) {
            sum += (Math.pow(-1, (i + 1))) * (i / (Math.pow(2, i)));
        }
        
        System.out.println(sum);
    }

}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-08-2008, 07:37 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Originally Posted by Fubarable View Post
the poster is a spammer.
Don't worry pal. I'll monitoring him, and if so I can ban the user.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-08-2008, 07:38 AM
Member
 
Join Date: Oct 2008
Posts: 38
Rep Power: 0
SapphireSpark is on a distinguished road
Default
Thank you, but the only rule is that we are not allowed to use Math.pow. We are limited to if, else, or switch. And the result should be a double value. I've tried making this using those guildelines and examples, but I don't think its right. Can you please check my code?

Last edited by SapphireSpark; 10-08-2008 at 07:58 AM.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 10-08-2008, 07:40 AM
Member
 
Join Date: Oct 2008
Posts: 38
Rep Power: 0
SapphireSpark is on a distinguished road
Default
Quote:
Don't worry pal. I'll monitoring him, and if so I can ban the user.
Thanks, but that won't be necessary, it was only so I could attach my explanation picture. And, btw, I'm a girl, just confused by the class.

Your previous suggestion looks helpful, except for the useage of Math.pow. Is there a more basic translation for that, using simple operators, if possible?

Last edited by SapphireSpark; 10-08-2008 at 07:46 AM. Reason: Expanding
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 10-08-2008, 08:00 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Originally Posted by SapphireSpark View Post
Thank you, but the only rule is that we are not allowed to use Math.pow. We are limited to if, else, or switch. And the result should be a double value. I've tried making this using those guildelines and examples, but I don't think its right. Can you please check my code?
There are lots of ways to do it. Did you try any.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 10-08-2008, 08:03 AM
Member
 
Join Date: Oct 2008
Posts: 38
Rep Power: 0
SapphireSpark is on a distinguished road
Default
Yes, I tried, and I thought maybe something to the nth power was a loop statement, but I have no idea how to make it work. The book doesn't mention variable powers in this chapter, and the examples from class were more like (value*value).

Also, for the first term, its not really 1, its (1/2), so that's not an integer, right? Will that mess up the result? All the class examples used int, so I'm not sure if I should change it to double, although it sounds right.

Last edited by SapphireSpark; 10-08-2008 at 08:12 AM.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 10-08-2008, 08:59 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Here is the simple way to do it

Code:
import java.util.Scanner;

public class FindSummation {

    public static void main(String[] args) {
        Scanner userScann = new Scanner(System.in);
        System.out.println("Enter number of terms: ");
        int n = userScann.nextInt();
        double sum = 0, newSum = 0;
        
        double d1 = 1, d2 = 1;
        int temp = 0;
        
        for(int j = 1; j <= n; j++) {     
            sum += (Math.pow(-1, (j + 1))) * (j / (Math.pow(2, j)));
        
        }
        
        for(int i = 1; i <= n; i++) {
            
            // Value of (-1)^(n+1)
            do {
                d1 *= (-1);
                temp++;
            }while(temp < (i + 1));
            temp = 0;
        
            // Value of 2^n
            do {
                d2 *= 2;
                temp++;
            }while(temp < i);
            temp = 0;
        
            newSum += (d1 * i) / d2; // Summation on each iteration
            
            // Clear all dummy values
            d1 = 1;
            d2 = 1;
        }
        System.out.println(sum + " " + newSum);
    }

}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 10-08-2008, 09:38 AM
Member
 
Join Date: Oct 2008
Posts: 38
Rep Power: 0
SapphireSpark is on a distinguished road
Default
Thank you very much. So, 'd1' is the numerator, and temp is the one changing it? What is *= doing to it?
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 10-08-2008, 10:10 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Actually 'd1' is not numerator, but in mathematically it is. It's calculate the part,

Quote:
(-1) to the power (n + 1)
temp is an iterator which is used to keep track of it.

*= is shorten form of expressing statements. In Java it's called as compound statement. So following two statements are same.

Code:
d1 *= (-1);

d1 = d1 * (-1);
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 10-08-2008, 09:57 PM
Member
 
Join Date: Oct 2008
Posts: 38
Rep Power: 0
SapphireSpark is on a distinguished road
Default
Ok, thanks, I remember that. I can also see the application of the do/while statement, and why it works well for this, it controls the loop so it only runs up to n. It is useful, despite how my teacher said its not as useful as the regular while statement.

That's also a great way to make an exponent, I can definitely use that method later, as well. Thank you so much for the help, my notes are starting to make more sense now, as well.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 10-09-2008, 01:17 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Remember for the do{} while() loop, you ALWAYS do it once. If there is a possiblity of 0 entries in an array or a null pointer, you'll have to do a separate test before starting the loop to keep from getting an exception.
The while(){} loop can be done 0 times.
Which to use depends on the application.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 10-09-2008, 03:44 AM
Senior Member
 
Join Date: Sep 2008
Posts: 564
Rep Power: 2
emceenugget is on a distinguished road
Default
You know to get the sign, it's really inefficient to create a loop for something so simple. I'm sure it doesn't really matter for your assignment, but you should just set d1, or whatever the necessary variable is, based on whether n is even or odd. Just an idea.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 10-09-2008, 04:59 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Originally Posted by SapphireSpark View Post
Ok, thanks, I remember that. I can also see the application of the do/while statement, and why it works well for this, it controls the loop so it only runs up to n. It is useful, despite how my teacher said its not as useful as the regular while statement.

That's also a great way to make an exponent, I can definitely use that method later, as well. Thank you so much for the help, my notes are starting to make more sense now, as well.
Basically do-while loops guaranteed to execute at least once, and then check the condition. While is not like that, check the condition before execute the body.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 10-09-2008, 05:01 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Originally Posted by emceenugget View Post
You know to get the sign, it's really inefficient to create a loop for something so simple. I'm sure it doesn't really matter for your assignment, but you should just set d1, or whatever the necessary variable is, based on whether n is even or odd. Just an idea.
Ya, it's nice. Those kind of thing come in design level. That's the advantage of well plane design for an application.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Big Fraction 1.00 JavaBean Java Announcements 1 03-26-2008 05:24 AM
Trouble getting Julia fraction correct. yllawwally New To Java 0 12-21-2007 03:02 AM


All times are GMT +2. The time now is 05:39 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org