Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com new to programming and java, need help for class

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 09-26-2004, 11:47 PM PM User | #1 reneeccski New Coder   Join Date: Sep 2004 Posts: 12 Thanks: 0 Thanked 0 Times in 0 Posts new to programming and java, need help for class Hello everyone! I was hoping someone might be able to give me a hand with the program I am writing for my intro cs class. We are studying algorithms and implementing them with java. I am totally new to programming, though I DO remember programming my TI-85 graphing calculator to output formulas back in high school several years ago! I am struggling with the code, not the math. Here is the problem: for f(x) = 0.1x^3 + 3.6 create an algorithm to find the root of the function and implement it in java For the algorithm, I am using Newton's Method to find the zero. Newton's Method is: xn+1 = xn - ( f(xn) / ( f'(xn) ) Here is the code I have written: Code: ```public class root { public static void main(String[] args) { //begin arguments //introduce variable u (the "guess" or [x sub n]), variable v ("guess +" or [x sub n+1]) double u=-10000; double v; //for function for(u=-10000; u<=10000; v=(u-(((0.3*u*u*u)+36)/((0.9*u)*(0.9*u))))) {if ((abs(double u))-(abs(double v)) <= 0.001); {System.out.println("The root of the function is " ); //I used Newton's Method here. In general terms, the formula is: //(x sub n+1) = (x sub n) - [ f(x) / f'(x) ] } } } }``` Could someone tell me where I'm going wrong? I want the solution accurate to 0.001. The root is actually appriximately -3.301. It is very frustrating to try to implement algorithms in code when my instructor isn't actually teaching java to the class and just expects us to be able to do it! --Renee Last edited by Antoniohawk; 09-27-2004 at 12:48 AM..
 09-27-2004, 12:47 AM PM User | #2 Antoniohawk Senior Coder   Join Date: Aug 2002 Location: Kansas City, Kansas Posts: 1,518 Thanks: 0 Thanked 2 Times in 2 Posts Welcome to Coding Forums. In the future, make sure to wrap your code in [code] tags so that it will retain the formatting. I'm a beginner to Java so I may not be able to help you much. About the only thing that I know is proper formatting. It's really unfair for a teacher to dump a project on you when you haven't programmed in Java before. Code: ```public class root { public static void main(String[] args) { //begin arguments //introduce variable u (the "guess" or [x sub n]), variable v ("guess +" or [x sub n+1]) double v; //for function for(double u = -10000; u <= 10000; v = (u - (((0.3 * u * u * u) + 36) / ((0.9 * u) * (0.9 * u))))){ if ((abs(double u))-(abs(double v)) <= 0.001) System.out.println("The root of the function is " ); } //I used Newton's Method here. In general terms, the formula is: //(x sub n+1) = (x sub n) - [ f(x) / f'(x) ] } }``` That's a start, there's probably a lot more needs to be done to make it work though. I have to go mow the lawn at the moment, but I'll try to help out some more when I'm done. __________________ xhtml :: attributes . tags . vs. CSS :: Box Model Hack [CF T-Shirts] [CP Resources] [ithium hosting] [A New Generation of Tree Menu] Last edited by Antoniohawk; 09-27-2004 at 01:00 AM..
09-27-2004, 01:17 AM   PM User | #3
Spookster
Supreme Overlord

Join Date: May 2002
Location: Marion, IA USA
Posts: 6,264
Thanks: 4
Thanked 83 Times in 82 Posts
Quote:
 Originally Posted by Antoniohawk It's really unfair for a teacher to dump a project on you when you haven't programmed in Java before.

lol I had a programming language course in college that required us to write a mid-side program in a different language every week and usually languages we had never seen or used before.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster

09-27-2004, 01:31 AM   PM User | #4
Antoniohawk
Senior Coder

Join Date: Aug 2002
Location: Kansas City, Kansas
Posts: 1,518
Thanks: 0
Thanked 2 Times in 2 Posts
Quote:
 Originally Posted by Spookster lol I had a programming language course in college that required us to write a mid-side program in a different language every week and usually languages we had never seen or used before.
Now that is just crazy!

Back to the problem at hand (before we were rudely interrupted by Spooks ) I think that the problem lies in your for loop. As you have it right now, the for loop will probably run forever because there is no iterating variable that makes u any bigger. What you probably need to do is put that v = blah into inside of the loop.
Code:
```public class testeroo {

public static void main(String[] args) { //begin arguments

//introduce variable u (the "guess" or [x sub n]), variable v ("guess +" or [x sub n+1])
double v;

//for function

for(double u = -10000; u <= 10000; u++){
v = (u - (((0.3 * u * u * u) + 36) / ((0.9 * u) * (0.9 * u))));

if (Math.abs(u) - Math.abs(v) <= 0.001)
System.out.println("The root of the function is " + v);
}

//I used Newton's Method here.  In general terms, the formula is:

//(x sub n+1) = (x sub n) - [ f(x) / f'(x) ]
}
}```
Edit:
Also, you need to use Math.abs(u) and Math.abs(v). The last problem might lie in the fact that you need to ouput something. Right now your output is "The root of the function is ", but there's nothing after it.

Last edited by Antoniohawk; 09-27-2004 at 01:39 AM..

 09-27-2004, 08:33 AM PM User | #5 reneeccski New Coder   Join Date: Sep 2004 Posts: 12 Thanks: 0 Thanked 0 Times in 0 Posts Thanks! Thanks for the help. I think I see where I was going wrong now. When I ran the program, I got a bunch of outputs so I will have to tweak that a little but I think that should send me off in the right direction.
 09-28-2004, 07:05 AM PM User | #6 reneeccski New Coder   Join Date: Sep 2004 Posts: 12 Thanks: 0 Thanked 0 Times in 0 Posts My program In case anyone cares...here's what my final program looks like (and it works...got a little help from my instructor today after class...I was getting there at least!): Code: ```public class Root2 { public static void main(String[] args) { //begin arguments //introduce variable u (the "guess" or [x sub n]), variable v ("guess +" or [x sub n+1]) double v; double u; //for function for(u = -100; u <= 100; u=u+.0001) { v = u - (((0.1 * u * u * u) + 3.6) / (0.3 * u * u)); if (Math.abs(v - u)<= 0.0001) System.out.println("The root of the function is " + v);//end if }//end for //I used Newton's Method here. In general terms, the formula is: // (x sub n+1) = (x sub n) - [ f(x) / f'(x) ] }//end main }//end class```

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 04:40 PM.