Results 1 to 6 of 6
  1. #1
    super is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Question While Program prints 0, I want it to print pi in 4 digit increments.

    So,
    I have a program that should be giving Pi as an answer. It is supposed to calculate 4 digits of Pi every full for loop.
    You input a number and it gives the square number of digits of Pi without the decimal. But all it gives is 0. What am I doing wrong?

    Specs:
    Eclipse,
    A package of 3 class files in which i converted a QBasic program (that runs flawlessly) to java with my own brain,
    the flawless QBasic Program,
    and a windows computer with JDK 1.7

    Later, I want to write the increments into a file in succession, like 31415...

    picolor/CreatePi.java:
    Java Code:
    package picolor;
    
    
    public class CreatePi {
    	public static void main(String[] args) {
    		CreatePi p = new CreatePi();
    		p.callseverything();
    		}
    	
    	
    	public void callseverything() {
    		OpenWrite.oned();
    		System.out.println(Piculator.Escalator());
    
    	
    	}
    	
    	}
    picolor/OpenWrite.java
    Java Code:
    package picolor;
    import java.util.*;
    
    public class OpenWrite{
    	static int no = new Integer(0);
    	int oned = new Integer(0);
    	int twod = new Integer(0);
    	 static Scanner in = new Scanner(System.in);
    
    	public static String oned() 
    	{
    		int oned;
    		System.out.print("Choose a whole number): ");
    		oned = in.nextInt();
    		
    		
    		no=0;
    		no = oned * oned;
    		String yes = Integer.toString(no);
    		return yes;
    	}
    	
    
    }
    picolor/Piculator.java
    Java Code:
    package picolor;
    public class Piculator
    {
    	static int a = new Integer(10000);
    	int b = new Integer(0);
    	 static int c = new Integer(0);
    	 static int d = new Integer(0);
    	 static int e = new Integer(0);
    	 static int g = new Integer(0);
    	 static int h = new Integer(0);
    	int i = new Integer(1);
    	int j = new Integer(0);
    	int n = new Integer(0);
    	 static int digits = new Integer(0);
    	int q = new Integer(2000);
    	 static String s = new String();
    	 static String w = new String();
    	 static int k;
    	String z = new String();
    	
    	public static int Escalator() 
    	{
    	
    		int n = OpenWrite.no;
    		int en = 0;
    		for(int i = 1; i<=n; i = i + 1)
    		{
    			for(c = n; n<=14; n-=14)
    			{
    				d = 0;
    				for(h = c;c<=1;h=c-1)
    				{
    					d = d * h + h * a;
    					h = d ;
    					g = h * 2 - 1;
    					d = (d / g);
    					h = h - d * g;	
    					k=e+ (d/a);
    					System.out.println(k);
    				}
    				s=Integer.toString(k);
    	    
    				if (s.substring(0,1).equals(" "))
    				{
    					int l = s.length();
    					//s = RIGHT$(s, LEN(s) - 1);
    					s = Integer.toString(l - 1);
    					//w = RIGHT$("000" + s, 4);
    					w = w + "000" + s.substring(0,1);
    					//System.out.print(RIGHT$("000" + s, 4));   
    	    
    					digits = digits + 4;
    					//System.out.print
    					en = d % a;
    	    			}
    			}
    			
    		}
    		return en;
    	}
    
    }
    And the QBasic program
    Java Code:
    SCREEN 13
    INPUT "Do you want to Generate (G) pi or Draw (D) pi?:", yesno$
    IF yesno$ = "G" THEN GOTO 200
    IF yesno$ = "D" THEN GOTO 400
    200
    OPEN "PI.TXT" FOR OUTPUT AS #3
    CLS
    PRINT ""
    CLOSE #3
    J = 0
    INPUT "Please enter a number that has a square root.", bd%
    od% = SQR(bd%) - 1
    td% = SQR(bd%) - 1
    n% = od% * td%
    n% = (n% / 4) * 14
    DIM F#(n%)
    FOR i% = 1 TO n%
        F#(i%) = 2000
    NEXT
    digits% = 0
    a% = 10000
    e% = 0
    FOR c% = n% TO 14 STEP -14
        d% = 0
        FOR b% = c% TO 1 STEP -1
            d# = d# * b% + F#(b%) * a%
            g% = b% * 2 - 1
            F#(b%) = d#
            d# = INT(d# / g%)
            F#(b%) = F#(b%) - d# * g%
        NEXT
        s$ = STR$(e% + INT(d# / a%))
        IF LEFT$(s$, 1) = " " THEN s$ = RIGHT$(s$, LEN(s$) - 1)
        w$ = RIGHT$("000" + s$, 4)
        PRINT RIGHT$("000" + s$, 4);
        digits% = digits% + 4
        IF digits% MOD 64 = 0 THEN PRINT
        e% = d# MOD a%
        OPEN "PI.TXT" FOR APPEND AS #1
        PRINT #1, w$;
        CLOSE #1
    NEXT
    PRINT Z$
    400
    CLS
    REM this is the program that converts a file to a string
    OPEN "PI.TXT" FOR INPUT AS #2
    INPUT #2, ZA$
    Z$ = Z$ + ZA$
    o$ = Z$
    FOR x = 0 TO od%
        FOR Y = 0 TO td%
            J = J + 1
            IF MID$(o$, J, 1) = "0" THEN PSET (x, Y), 0
            IF MID$(o$, J, 1) = "1" THEN PSET (x, Y), 1
            IF MID$(o$, J, 1) = "2" THEN PSET (x, Y), 2
            IF MID$(o$, J, 1) = "3" THEN PSET (x, Y), 3
            IF MID$(o$, J, 1) = "4" THEN PSET (x, Y), 4
            IF MID$(o$, J, 1) = "5" THEN PSET (x, Y), 5
            IF MID$(o$, J, 1) = "6" THEN PSET (x, Y), 6
            IF MID$(o$, J, 1) = "7" THEN PSET (x, Y), 7
            IF MID$(o$, J, 1) = "8" THEN PSET (x, Y), 8
            IF MID$(o$, J, 1) = "9" THEN PSET (x, Y), 9
            a$ = INKreY$
            IF a$ = "q" THEN END
        NEXT Y
    NEXT x
    CLOSE #2
    END

    This program isn't finished, but if you know basic, then you might be able to figure out what I am doing.
    The whole reason I am porting to java is to gain more memory usage.
    Java can use a lot of memory - over 200,000 megabytes - compared to QBasic's measly 64 kilobytes.

    Thanks to all who help out!

  2. #2
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: While Program prints 0, I want it to print pi in 4 digit increments.

    Well, the variable names are meaningless, you call methods that don't exist, and there are no comments. Add a ton of print statements and figure out what is going on.
    Last edited by Reeling; 10-24-2012 at 02:03 AM.

  3. #3
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: While Program prints 0, I want it to print pi in 4 digit increments.

    A couple of things. I haven't gone over the entire code, but I would first ask why this is in three classes? They work to do one thing, and should be part of the same class. (minor note: by convention, Classes usually have leading caps, methods are usually leading lower-case. This is not a java requirement, but common naming practice.)

    You state that you're using Eclipse. Use Eclipse's debug to step through the program, and inspect the variable values after inputs. You may be able to find your trouble spot very quickly.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: While Program prints 0, I want it to print pi in 4 digit increments.

    You're also doing a lot of unneeded declaration and initialization. If you aren't going to use a variable outside the context of a method, then make the variable local (especially with static methods). You can also initialize much more simply:

    Java Code:
    package picolor;
    public class Piculator{
        public static int Escalator() {
            int a = 10000;
            int b = 0;
            int c = 0;
            int n = OpenWrite.no;
            //...
    Also, as others have mentioned, your variable names are garbage. It's impossible to see what's going on. Use useful names like userInput, divisor, scalar, interestRate, SOME_FINAL_CONSTANT, etc...

    Imagine a 30,000 line program that just cycled through the alphabet for variable names.... ugh.

  5. #5
    super is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Lightbulb Almost Solved!

    O.K, so I took all of you guys' suggestions.
    1. I added lots and lots of print statements
    a. I figured out that the 'for' statements weren't working. I think I fixed them.
    2. I merged together two of the three files.
    a. It's very messy, so please point out all of my mistakes.
    3. I removed lots of meaningless gunk (the extra variables)

    Now that I changed it up a bit, I can't figure out how to initialize and get the result of "TheActualPi".
    The result of TheActualPi should be Pi.
    Here is my code:

    OpenWrite.java
    I changed it to be more readable:
    Java Code:
    package picolor;
    import java.util.*;
    
    public class OpenWrite{
    	 public static int ProductOfUserInput = 0;
    	static int UserInput = new Integer(0);
    	 static Scanner in = new Scanner(System.in);
    
    	public static int oned() 
    	{
    		int onedim;
    		System.out.print("Choose a whole number): ");
    		onedim = in.nextInt();
    		
    		
    		UserInput=0;
    		UserInput = onedim * onedim;
    		return UserInput;
    	}
    	
    
    }
    The second one has the 'main' method in it.
    CreatePi.java
    I merged this with Piculator:
    Java Code:
    package picolor;
    
    public class CreatePi {
    
    	public static void main(String args[]) {	
    		OpenWrite.oned();
    		System.out.println( /*TheActualPi, variable w*/ );
    		
    		/*Method??*/
    
    		new Piculator(){
    		Integer a = new Integer(10000);
    		Integer c = new Integer(0);
    		Integer d = new Integer(0);
    		Integer e = new Integer(0);
    		Integer g = new Integer(0);
    		Integer h = new Integer(0);
    		Integer i = new Integer(1);
    		Integer digits = new Integer(0);
    	    String s = new String("");
    	    String w = new String("");
    	    
    	    /*
    	     * Diagnostic = "letter" + the variable
    	     * */
    	    
    	    //Calculations	    
    		private String TheActualPi(){
    	    for(i = 1; i<=OpenWrite.ProductOfUserInput; i = i + 1)
    	    {
    	    	for(c = OpenWrite.ProductOfUserInput; c<=14; c-=14){
    	    		{  
    	    			System.out.println("c"+c);
    	    			}
    	    		d = 0;
    				for(h = c;h<=1;h=c-1)
    	    		{
    	    			d = d * h + h * a;
    					System.out.println("d"+d);
    	    			}
    	    					h = d ;
    	    					System.out.println("h"+h);
    	    					g = h * 2 - 1;
    	    					System.out.println("g"+g);
    	    					d = (d / g);
    	    					System.out.println("d"+d);
    	    					h = h - d * g; 
    	    					System.out.println("h"+h);
    	    					k=e+ (d/a);
    	    					System.out.println(k);
    	    					}
    	    	s=Integer.toString(k);
    	    	System.out.println(s);
    	    	if (s.substring(0,1).equals(" "))
    	    	{
    	    		int l = s.length();
    	    		System.out.println("l"+l);
    	    		s = Integer.toString(l - 1);
    	    		System.out.println("s"+s);
    	    		w = w + "000" + s.substring(0,1);
    	    		System.out.println("w"+w);
    	    		digits = digits + 4;
    	    		if (digits == digits%64){
    	    			System.out.println("pi"+e=d%a);
    	    		}
    	    	}
    		}
    		return w;
    		}
    		};
        }
    }
    Thanks to Reeling, SJF, quad64bit, and whoever helps out!
    Last edited by super; 10-29-2012 at 02:41 AM. Reason: Comments don't display correctly...

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: While Program prints 0, I want it to print pi in 4 digit increments.

    Does that compile? If not and you can't understand the compiler's messages, post them and someone may be able to explain what they mean.

    ---

    But, first, I seriously think you should take some time to think about the task. Know how you are going to calculate these digits if pi. Break it down into small steps, and be so sure of those steps that you could explain it to (a suitably intelligent and sympathetic) someone else. Then start to write code. Attending to each compiler message as it arises.

    I can see things in that second class that the compiler would grumble about. And I could explain what they mean. But I don't know what that main() method is doing (not the overall aim - you described that - but, step by step, what it is doing to to achieve that aim). Moreover I'm lazy and would probably expect *you* to tell me what you intend it to do. Hence the value of fully understanding the nuts and bolts of what you're doing before code.

Similar Threads

  1. Replies: 4
    Last Post: 04-27-2012, 10:43 AM
  2. set increments in data plot
    By lamorak24 in forum Advanced Java
    Replies: 1
    Last Post: 12-19-2011, 08:01 AM
  3. Replies: 0
    Last Post: 01-23-2011, 01:37 AM
  4. ++/--increments for (C/JAVA) explained!!
    By _ShivamKapoOr_ in forum New To Java
    Replies: 5
    Last Post: 09-24-2010, 04:02 PM
  5. Pre- and post-increments
    By SirFalcon in forum New To Java
    Replies: 6
    Last Post: 10-29-2009, 04:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •