Results 1 to 16 of 16
  1. #1
    chango77745 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default Printing issues...

    I have created a loop structure in my testing class, and after I want it to print out the output, but it does not seem to print. Any ideas?

    Here is my code:

    Quote:

    --------------------------------------------------------------------------------
    /**
    *
    */
    package frogs;

    /**
    * @author epena
    *
    */
    public class FrogTester {

    public static void main(String[] args) {

    Frog myFrog = new Frog();
    System.out.println("Position :" + myFrog.getPosition());
    System.out.println("Maximum Position: " + myFrog.getMaximumPosition());
    System.out.println("Mimimum Position: " + myFrog.getMinimumPosition());
    System.out.println("Hops Taken: " +myFrog.getHopsTaken());
    //Do this for hops

    Frog kermit = new Frog();
    for (int i=0; i<100;i++){
    kermit.hop();
    }
    System.out.println("After 100 hops: \n" + kermit);

    int posSum = 0;
    int distSum = 0;
    int minPosSum = 0;
    int maxPosSum = 0;
    int numFrogs = 10000;

    System.out.println("Experiement: 10 frogs hopping 100 times");
    for (int i=0; i <numFrogs; i++){
    Frog frog = new Frog();
    hopFrog(frog, 100);
    posSum += frog.getPosition();
    distSum += Math.abs(frog.getPosition());
    minPosSum += frog.getMinimumPosition();
    maxPosSum += frog.getMaximumPosition();

    }

    System.out.println("Average Final Position " + posSum / (double)numFrogs);
    System.out.println("Average Final Distance " + distSum / (double)numFrogs);
    System.out.println("Average Final Minimum Position " + minPosSum / (double)numFrogs);
    System.out.println("Average Final Maximum Position " + maxPosSum / (double)numFrogs);

    Frog thisFrog = new Frog();
    thisFrog.hop(1000);
    System.out.println("This Frog: " + thisFrog);

    }

    public static void hopFrog (Frog thisFrog, int n){
    for (int i =0; i < n; i ++);
    thisFrog.hop();

    }
    Frog kirby = new Frog();{
    int q=0;
    while(q == 50){
    kirby.hop(50);}
    System.out.println("Kirby : "+ kirby);

    }
    }



    --------------------------------------------------------------------------------


    It compiles and runs just fine, but I do not get the last print message. System.out.println("Kirby : "+ kirby); does not print out...

    In addition, I am supposed to Use a while loop to have kirby hop until it reaches position 50. Output the number of hops that it requires.
    Did that do that correctly?

  2. #2
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    1. use CODE tags like this <CODE> code here </CODE> Except replace the < and > with [ and ].
    I have been looking over your code so annoying trying to figure out where methods end and start.
    2. Its fine to post the whole class in this case since it's small, but put the part with error in red, or in bold, or underline etc.
    3. What is the kirby supposed to print out? Kirby is an object, not a string. What was the expected output?
    4. Just a tip, try to break the program into methods, not all in the main. And make constructors and fields. Ok now I see that there is a hop method...
    5. A suggestion, You're using a lot of frog objects but your only method is static with a frog argument.. it would be much simpler to make it non-static and then the calling frog object will be used.
    6. A suggestion, You have no FrogTester constructor. If you plan it to be empty anyway, it is still good practice to put in an empty constructor, and not use the default one included when there isn't one.
    7. I've now identified your problem:
    Java Code:
    Frog kirby = new Frog();[COLOR="Red"]{[/COLOR]
    int q=0;
    while(q == 50){
    kirby.hop(50);}
    System.out.println("Kirby : "+ kirby);
    [COLOR="red"]}[/COLOR]
    The code Frog Kirby is making a new Frog field in your class called kirby - ok..
    The problem: those red braces, what are they? You have code outside any methods - there just in braces. Did you mean to include that in the main Method? You can't just have stray code like that. Hope this helped.
    -MK12
    Last edited by MK12; 02-13-2009 at 05:04 PM.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  3. #3
    chango77745 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    I am sorry, I did not put in the Frog class, I just put in the main method. Here is the frog class with the constructors and methods.

    Java Code:
     /**
     * 
     */
    package frogs;
    import java.util.*;
    
    public class Frog {
    	
    	private int hopsTaken;
    	private int position;
    	private int minimumPosition;
    	private int maximumPosition;
    	
    	private static Random rand = new Random();
    	
    	//Constructor
    	public Frog (){
    		hopsTaken = 0;
    		position = 0;
    		minimumPosition = 0;
    		maximumPosition = 0;
    	}
    	public int getPosition(){
    		return position;
    	}
    	public int getHopsTaken(){
    		return hopsTaken;
    	}
    	public int getMinimumPosition(){
    	return minimumPosition;
    	}
    	public int getMaximumPosition(){
    	return maximumPosition;
    	}
    	public void hop (int n){
    		for (int i=0; i < n; i++)
    			this.hop();
    	
    	}
    	public void hop(){
    	int nextVal;
    	nextVal = rand.nextInt(2);
    	if (nextVal == 0)
    		position++;
    	else 
    		position--;
    	hopsTaken++;
    	if (position < minimumPosition)
    	minimumPosition = position;
    	if (position < maximumPosition)
    		maximumPosition = position;
    	}
    	
    	
    	
    	public String toString(){
    		String output = "Frog at " + position + " after " + hopsTaken + " hops ";
    		output += "with minimum position " + minimumPosition + " and maximum position " + maximumPosition;
    		return output;
    		
    	}
    	
    
    }

  4. #4
    chango77745 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    In addition, I have fixed the braces, but the last print command will not print out. I left a comment, also is my while loop correct??
    I am supposed to Use a while loop to have kirby hop until it reaches position 50. Output the number of hops that it requires.
    Did that do that correctly?
    Java Code:
    /**
     * 
     */
    package frogs;
    
    /**
     * @author epena
     *
     */
    public class FrogTester {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		System.out.println("FrogTester written by Evan Pena");
    		
    		Frog myFrog = new Frog();
    		System.out.println("Position :" + myFrog.getPosition());
    		System.out.println("Maximum Position: " + myFrog.getMaximumPosition());
    		System.out.println("Mimimum Position: " + myFrog.getMinimumPosition());
    		System.out.println("Hops Taken: " +myFrog.getHopsTaken());
    		//Do this for hops
    		
    		Frog kermit = new Frog();
    		for (int i=0; i<100;i++){
    			kermit.hop();
    		}
    		System.out.println("After 100 hops: \n" + kermit);
    		
    		int posSum = 0;
    		int distSum = 0;
    		int minPosSum = 0;
    		int maxPosSum = 0;
    		int numFrogs = 10000;
    		
    		System.out.println("Experiement: 10 frogs hopping 100 times");
    		for (int i=0; i <numFrogs; i++){
    			Frog frog = new Frog();
    			hopFrog(frog, 100);
    			posSum += frog.getPosition();
    			distSum += Math.abs(frog.getPosition());
    			minPosSum += frog.getMinimumPosition();
    			maxPosSum += frog.getMaximumPosition();
    			
    		}
    			
    			System.out.println("Average Final Position " + posSum / (double)numFrogs);
    			System.out.println("Average Final Distance " + distSum / (double)numFrogs);
    			System.out.println("Average Final Minimum Position " + minPosSum / (double)numFrogs);
    			System.out.println("Average Final Maximum Position " + maxPosSum / (double)numFrogs);
    			
    		
    	
    		
    		Frog thisFrog = new Frog();
    		thisFrog.hop(1000);
    		System.out.println("This Frog: " + thisFrog);
    		
    		
    
    	}
    	
    	public static void hopFrog (Frog thisFrog, int n){
    		for (int i =0; i < n; i ++);
    			thisFrog.hop();
    			
    	}
    	Frog kirby = new Frog();
    		int q=0;{
    		while(q == 50)
    			kirby.hop(50);
    		System.out.println("Kirby : "+ kirby);
    		//This is the part that is not printing.
    	
    	
    }
    }

  5. #5
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    First of all, I think you meant to do kirby.toString(), not just kirby. Secondly,
    Java Code:
    Frog kirby = new Frog();
    		int q=0;{
    		while(q == 50)
    			kirby.hop(50);
    		System.out.println("Kirby : "+ kirby);
    		//This is the part that is not printing.
    	
    	
    }
    It's still outside any method. look, this is incorrect:
    Java Code:
    public class Example {
         private static int field; // declaring fields
         public static void main(String[] args) {
              field = 1;
         }
         { System.out.println("h");} // INCORRECT. braces make no difference
         System.out.println("h"); // both these lines are wrong. They are not in a method
         // you can't have code like that outside a method.
    }
    
    // HERE's the right way:
    
    public class Example {
         private static int field; // declaring fields
         public static void main(String[] args) {
              field = 1;
              runApp();
         }
         private static void runApp() {
              System.out.println("h");
         }
    }
    
    // OR another right way:
    
    public class Example {
         private static int field; // declaring fields
         public static void main(String[] args) {
              field = 1;
              System.out.println("h");
         }
    }
    Understand now?
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    fyi, if you println(Object), it automatically uses the .toString() method. pretty handy and not exactly the most widely known tidbit (i learned this a couple months ago... though i've never really found myself in a situation where i wanted to use it). it would be more useful if it was also extended to automatically call .toString() in any case where String is expected, though that'd probably a hell of a lot more annoying to debug than to call .toString() whenever you wanted... ok i'll stop.

  7. #7
    chango77745 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    Even when I call the toString method on Kirby it still does not print it out. However, if you look at above Kirby you will find an object called thisFrog. The way I printed thisFrog works just fine. Try running the program and seeing what you get.

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    my post was actually pointed to mk12 (he said to use kirby.toString(), not just kirby). but if you read mk12's post, you'd see the solution to your problem.

  9. #9
    chango77745 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    Ahh It is still not printing and I am confident it is within the main class

    [code]
    /**
    *
    */
    package frogs;

    /**
    * @author epena
    *
    */
    public class FrogTester {

    /**
    * @param args
    */
    public static void main(String[] args) {
    System.out.println("FrogTester written by Evan Pena");

    Frog myFrog = new Frog();
    System.out.println("Position :" + myFrog.getPosition());
    System.out.println("Maximum Position: " + myFrog.getMaximumPosition());
    System.out.println("Mimimum Position: " + myFrog.getMinimumPosition());
    System.out.println("Hops Taken: " +myFrog.getHopsTaken());
    //Do this for hops

    Frog kermit = new Frog();
    for (int i=0; i<100;i++){
    kermit.hop();
    }
    System.out.println("After 100 hops: \n" + kermit);

    int posSum = 0;
    int distSum = 0;
    int minPosSum = 0;
    int maxPosSum = 0;
    int numFrogs = 10000;

    System.out.println("Experiement: 10 frogs hopping 100 times");
    for (int i=0; i <numFrogs; i++){
    Frog frog = new Frog();
    hopFrog(frog, 100);
    posSum += frog.getPosition();
    distSum += Math.abs(frog.getPosition());
    minPosSum += frog.getMinimumPosition();
    maxPosSum += frog.getMaximumPosition();

    }

    System.out.println("Average Final Position " + posSum / (double)numFrogs);
    System.out.println("Average Final Distance " + distSum / (double)numFrogs);
    System.out.println("Average Final Minimum Position " + minPosSum / (double)numFrogs);
    System.out.println("Average Final Maximum Position " + maxPosSum / (double)numFrogs);




    Frog thisFrog = new Frog();
    thisFrog.hop(1000);
    System.out.println("This Frog: " + thisFrog);

    Frog kirby = new Frog();
    int q=0;
    while(q == 50)
    kirby.hop(50);
    kirby.toString();
    //This is the part that is not printing.
    }
    public static void hopFrog (Frog thisFrog, int n){
    for (int i =0; i < n; i ++);
    thisFrog.hop();

    }


    }



    [code]

  10. #10
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    well, now you aren't even printing it. you're just converting it to a string and doing nothing with it.

  11. #11
    chango77745 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    haha sorry, that was a dumb mistake.

    Unfortunatly when I print the message I get this:
    Kirby: Frog at 0 after 0 hops with minimum position 0 and maximum position 0
    So there must be something wrong with my while loop.

    I am supposed to Use a while loop to have kirby hop until it reaches position 50. Output the number of hops that it requires.
    Did that do that correctly?

  12. #12
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    So you figured out that I didn't mean it has tot be in the class, but that it must be in a method? all code except declaring & initializing fields must be in a method. For example, putting the println() in the main(String[] args) method, or in another method which the main calls (if you want it to run). OK. (bytheway you forgot CODE tags in your post) You fixed the mistake that mcn pointed out, so now it actually prints out the frog object,right? (and just type the object not .toString() because it automatically does that, as mcn informed us)... Ok:

    Unfortunatly when I print the message I get this:
    Kirby: Frog at 0 after 0 hops with minimum position 0 and maximum position 0
    What is it supposed to print out? as far as I can see you are only printing out the Frog object, which prints its toString method, which is this:
    Java Code:
    public String toString(){
    		String output = "Frog at " + position + " after " + hopsTaken + " hops ";
    		output += "with minimum position " + minimumPosition + " and maximum position " + maximumPosition;
    		return output;	
    	}
    So, isn't what you said it is printing out what it is supposed to print out? explain please. Also, on your while loop, you left out braces. Please post you're up-to-date code with code tags.
    Java Code:
    int q=0;
    while(q == 50)
    That is your while loop(with the braces left out), and there is another problem: It will never execute because q is 0 and it only goes through loop while q is 50. did you mean:
    Java Code:
    Frog kirby = new Frog();
    int q=0;
    while(q [COLOR="Red"]<=[/COLOR] 50) [COLOR="red"]{[/COLOR]
    kirby.hop(50);
    [COLOR="red"]System.out.println[/COLOR]([COLOR="red"]kirby[/COLOR]);
    [COLOR="Red"]q++;[/COLOR]
    [COLOR="red"]}[/COLOR]
    Hope this helped
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  13. #13
    chango77745 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    When I print out the loop you gave me it prints this

    Frog at 4 after 50 hops with minimum position -6 and maximum position -6
    Frog at 0 after 100 hops with minimum position -6 and maximum position -6
    Frog at 8 after 150 hops with minimum position -6 and maximum position -6
    Frog at 4 after 200 hops with minimum position -6 and maximum position -6
    Frog at 0 after 250 hops with minimum position -6 and maximum position -6
    Frog at 2 after 300 hops with minimum position -6 and maximum position -6
    Frog at 14 after 350 hops with minimum position -6 and maximum position -6
    Frog at 30 after 400 hops with minimum position -6 and maximum position -6
    Frog at 20 after 450 hops with minimum position -6 and maximum position -6
    Frog at 18 after 500 hops with minimum position -6 and maximum position -6
    Frog at 14 after 550 hops with minimum position -6 and maximum position -6
    Frog at 10 after 600 hops with minimum position -6 and maximum position -6
    Frog at 0 after 650 hops with minimum position -6 and maximum position -6
    Frog at -10 after 700 hops with minimum position -12 and maximum position -12
    Frog at -4 after 750 hops with minimum position -12 and maximum position -12
    Frog at -4 after 800 hops with minimum position -12 and maximum position -12
    Frog at -6 after 850 hops with minimum position -12 and maximum position -12
    Frog at -4 after 900 hops with minimum position -12 and maximum position -12
    Frog at -2 after 950 hops with minimum position -12 and maximum position -12
    Frog at -4 after 1000 hops with minimum position -12 and maximum position -12
    Frog at -6 after 1050 hops with minimum position -12 and maximum position -12
    Frog at -12 after 1100 hops with minimum position -12 and maximum position -12
    Frog at -22 after 1150 hops with minimum position -23 and maximum position -23
    Frog at -20 after 1200 hops with minimum position -26 and maximum position -26
    Frog at -20 after 1250 hops with minimum position -26 and maximum position -26
    Frog at -30 after 1300 hops with minimum position -32 and maximum position -32
    Frog at -38 after 1350 hops with minimum position -41 and maximum position -41
    Frog at -36 after 1400 hops with minimum position -41 and maximum position -41
    Frog at -36 after 1450 hops with minimum position -44 and maximum position -44
    Frog at -26 after 1500 hops with minimum position -44 and maximum position -44
    Frog at -44 after 1550 hops with minimum position -44 and maximum position -44
    Frog at -40 after 1600 hops with minimum position -45 and maximum position -45
    Frog at -38 after 1650 hops with minimum position -45 and maximum position -45
    Frog at -36 after 1700 hops with minimum position -45 and maximum position -45
    Frog at -20 after 1750 hops with minimum position -45 and maximum position -45
    Frog at -28 after 1800 hops with minimum position -45 and maximum position -45
    Frog at -28 after 1850 hops with minimum position -45 and maximum position -45
    Frog at -34 after 1900 hops with minimum position -45 and maximum position -45
    Frog at -36 after 1950 hops with minimum position -45 and maximum position -45
    Frog at -36 after 2000 hops with minimum position -45 and maximum position -45
    Frog at -30 after 2050 hops with minimum position -45 and maximum position -45
    Frog at -32 after 2100 hops with minimum position -45 and maximum position -45
    Frog at -34 after 2150 hops with minimum position -45 and maximum position -45
    Frog at -40 after 2200 hops with minimum position -45 and maximum position -45
    Frog at -34 after 2250 hops with minimum position -45 and maximum position -45
    Frog at -24 after 2300 hops with minimum position -45 and maximum position -45
    Frog at -24 after 2350 hops with minimum position -45 and maximum position -45
    Frog at -24 after 2400 hops with minimum position -45 and maximum position -45
    Frog at -22 after 2450 hops with minimum position -45 and maximum position -45
    Frog at -14 after 2500 hops with minimum position -45 and maximum position -45
    Frog at -14 after 2550 hops with minimum position -45 and maximum position -45
    I want it just to print one line...

  14. #14
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Show me exactly what you want it to print and what you got before please. And your CODE tagged code so far.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  15. #15
    chango77745 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    For example:

    Java Code:
    	Frog thisFrog = new Frog();
    		thisFrog.hop(1000);
    		System.out.println("This Frog: " + thisFrog);
    Prints out
    This Frog: Frog at 26 after 1000 hops with minimum position -9 and maximum position -9
    It does not print out 1000 hops at a time

  16. #16
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Quote Originally Posted by chango77745 View Post
    For example:

    Java Code:
    	Frog thisFrog = new Frog();
    		thisFrog.hop(1000);
    		System.out.println("This Frog: " + thisFrog);
    Prints out


    It does not print out 1000 hops at a time
    Try this:
    Java Code:
    Frog thisFrog = new Frog();
    for(int i = 0; i < [COLOR="Red"]However long you want it to  be[/COLOR]; i++) {
         thisFrog.hop(1000);
         System.out.println("This Frog: " + thisFrog);
    }
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. JList issues
    By aneesahamedaa in forum AWT / Swing
    Replies: 7
    Last Post: 12-15-2009, 05:16 PM
  2. Array issues
    By joka22 in forum New To Java
    Replies: 6
    Last Post: 10-07-2008, 12:05 PM
  3. ScrollPane Issues..
    By hanifa in forum AWT / Swing
    Replies: 4
    Last Post: 09-11-2008, 09:18 AM
  4. iterator issues
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-12-2008, 02:43 PM
  5. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 02:16 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
  •