Java begineer problem
Page 2 of 2 Goto page Previous  1, 2
Razacka2




Posts: 2832
Location: Sweden
PostPosted: Wed, 8th Dec 2010 22:43    Post subject:
Thanks. though I think the problem should have been more clear on what difference was, it felt like a very unclear tip. I had to use more maths here then coding since i knew that the absolute value is the distance between a point and origo.
Back to top
Razacka2




Posts: 2832
Location: Sweden
PostPosted: Mon, 13th Dec 2010 17:22    Post subject:
I've created a program for something called eudoxus recursive series which is like this:
http://mathbin.net/56527

and this is the code

Code:

import javax.swing.*;

public class eudoxesseries {
   public static void main(String[] arg) {
      
      String k = JOptionPane.showInputDialog(null,"För vilket n?");
      if (k == null)
         System.exit(0);
      long v = Long.parseLong(k);
      long n = 0;
      long an = 0;
      long bn = 0;
      long divison = 0;
      long anneg1 = 1;
      long bnneg1 = 1;
      while (n < v ) {
         an = anneg1 + bnneg1;
         bn = an + anneg1;
         bnneg1 = bn;
         anneg1 = an;
         n = n+1;
      }
      divison = bn/an;
      JOptionPane.showMessageDialog(null, "För n = " + k + ": " + "\n" + "an= " + an + "\n" + "bn= " + bn + "\n"
            + "an/bn= " + divison);
      System.exit(0);
   }
}


however when I use long and when it calculates the divison it won't come out with decimals. i've tried putting divison = (double) bn/an;

but eclipse won't let me, it says cannot convert from double to long :S

So what do you guys think, Is the program fine or can i make changes to it to make it look cleaner?

But yeah primarily, how do i get division to work with long and can I improve the program in any way?

edit: about the swedish, it's only to type in for what n you wish the program to calculate for.
Back to top
Frant
King's Bounty



Posts: 24433
Location: Your Mom
PostPosted: Mon, 13th Dec 2010 19:32    Post subject:
Hmm, shouldn't you use float if you want decimals?


Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn!

"Thank you to God for making me an Atheist" - Ricky Gervais
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Mon, 13th Dec 2010 19:47    Post subject:
Try it with java.math.BigDecimal?
Back to top
Razacka2




Posts: 2832
Location: Sweden
PostPosted: Mon, 13th Dec 2010 20:17    Post subject:
Solved it

Code:


import javax.swing.*;

public class eudoxesseries {
   public static void main(String[] arg) {
      
      String k = JOptionPane.showInputDialog(null,"För vilket n?");
      if (k == null)
         System.exit(0);
      long v = Long.parseLong(k);  //kommer verka som gränsen för hur långt loopen skall gå.
      long n = 0;
      long an = 0;
      long bn = 0;
      double divison;
      long anneg1 = 1; //startvärdet för an
      long bnneg1 = 1; //startvärdet för bn
      while (n < v ) {
         an = anneg1 + bnneg1; //formeln an = an-1 + bn-1
         bn = an + anneg1; // formeln bn = an + an-1
         bnneg1 = bn; // görs om för att verka som bn-1 i nästa fall
         anneg1 = an; // görs om för att verka som an-1 i nästa fall.
         n = n+1; // räknare som håller reda på vilket n vi är på.
      }
      divison = (double) bn/an; // utför divisionen bn/an
      JOptionPane.showMessageDialog(null, "För n = " + k + ": " + "\n" + "an= " + an + "\n" + "bn= " + bn + "\n"
            + "an/bn= " + divison); // skriver ut resultatet.
      System.exit(0);
   }
}


just made division as a double instead and since an and bn are of the type long i just put (division) infront. thus division = (double) bn/an;

But yeah, i wanna use long so an and bn don't come out as for example: 6.134535E18.
Back to top
Razacka2




Posts: 2832
Location: Sweden
PostPosted: Wed, 25th May 2011 21:02    Post subject:
So yeah, been some time since I posted in this thread but I thought i'd ask what books you can recommend on learning java?

I've learned a bit now, like how to use loops, arrays, a bit about objects etc so I have a pretty much basic understanding of java. Now I feel like I need a good book and I have no idea which ones that are good. Any ideas?
Back to top
Page 2 of 2 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner Goto page Previous  1, 2
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group