-
help with java
can i get some help with this java question please?
"a cancellation error occurs when u are manipulating a very large number with a very small number. the large number may cancel out the smaller number. to obtain more accurate results the order of computation must be selected carefully.write a java program(with a main method) that compares the results of the summation for the following series computing from left to right and from right to left.
1 + 1/2 + 1/3 + ..... + 1/n
your program thgen should ask the user to enter a positive and greater than zero number, calculate the summation from left to right and from right to left and then display appropriate messages with the two calculated summations
example test data:
for n=50000 you should see:
left to right summation: 11.397003949278504
right to left summation: 11.397003949278519"
thanx very much to anyone...in advance!!!!!
-
can i get some help with this java question please?
Sure. What kind of help do you need? What did you find confusing? What is preventing you from writing code that meets the requirements of the problem? You have to make known what you already know. Otherwise you're likely to get answers you don't understand.
If you've begun work on this problem, please be a little more explicit about what you're doing, what you expect to happen, and what you actually observe.
Please post a short, concise, executable example of what you're trying to do. This does not have to be the actual code you are using. Write a small example that demonstrates your intent, and only that. Wrap the code in a class and give it a main method that runs it - if we can just copy and paste the code into a text file, compile it and run it without any changes, then we can be sure that we haven't made incorrect assumptions about how you are using it.
Post your code between [code] and [/code] tags. Cut and paste the code, rather than re-typing it (re-typing often introduces subtle errors that make your problem difficult to troubleshoot). Please preview your post when posting code.
Please assume that we only have the core API. We have no idea what SomeCustomClass is, and neither does our collective compiler.
If you have an error message, post the exact, complete error along with a full stack trace, if possible. Make sure you're not swallowing any Exceptions.
Help us help you solve your problem.
-
i've done this so far(below)...but when i try to put in the summation itself(1+1/2.....)it doesnt execute...how do i let the computer know that the summation is a series?
public static void main(java.lang.String[] args) {
double n;
double firstSummation; //Calculate summation from left to right
double secondSummation; //Calculate summation from right to left
System.out.print(" Please enter a positive & greater than zero integer:");
n=EasyIn.getDouble();
if(n !=0 && n>0)
{
System.out.println(n);
firstSummation=1+n;
System.out.println("left-rightresult" + firstSummation);
secondSummation=1+1/2+1/n;
System.out.println("right-leftresult" + secondSummation);
}
else
{
System.out.println("error!");
}
}
-
Remember that you are either counting up from 1 to the number represented by "n" (left to right execution) or counting down from "n" to 1 (right to left).
Does this counting up or counting down remind you of a commonly used control structure? Where you increment/decrement a counter and repeatedly execute the content until the counter reaches the set end point?
Use this control structure (twice) to write your code. And keep in mind what arithmetic function you are performing to create the value which gets added to your total.
Last edited by nspils; 08-30-2005 at 08:27 AM.
-
What do you mean by "it doesn't execute"? Please be explicit about what you expect to happen, and what you actually observe. Also, when you post code next time, don't forget the [code] and [/code] tags.
Note that your given code does not seem to reflect the assignment, especially with regard to the first summation.
-
For left to right:
Code:
public static void main(java.lang.String[] args)
{
//define some variables
double n, firstSummation=0;
//ask user for n
System.out.print(" Please enter a positive & greater than zero integer:");
n=EasyIn.getDouble();
//compute 1/1+1/2+1/3+...1/i + ... + 1/n (notice that in the formula i varies from 1 to n - for that reason the loop has i varying from 1 to n)
for(int i =1; i <=n; i++)
{
//add in the term 1/i into the sum
firstSummation += 1/i;
}
//print sum
System.out.println(firstSummation);
}
For more help, www.NeedProgrammingHelp.com
Similar Threads
-
Replies: 2
Last Post: 06-14-2006, 03:16 PM
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By Rob Abbe in forum Talk to the Editors
Replies: 44
Last Post: 01-13-2003, 02:57 PM
-
By Mike Tsakiris in forum .NET
Replies: 11
Last Post: 10-04-2002, 05:32 PM
-
By JJ in forum Enterprise
Replies: 1
Last Post: 07-06-2000, 04:50 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks