Note that there are some explanatory texts on larger screens.

plurals
  1. POCount number of times a method is called?
    primarykey
    data
    text
    <p>i'm trying to count the number of times a method is called. I have 3 classes at the moment, Circle.java, Picture.java and PictureTest.java</p> <p>In the circle.java there is;</p> <pre><code>public class Circle { private int diameter; private int xPosition; private int yPosition; private String color; private boolean isVisible; private int instances; /** * Create a new circle at default position with default color. */ public Circle() { diameter = 30; xPosition = 20; yPosition = 60; color = "blue"; isVisible = false; instances = 0; instances++; System.out.println(instances); } </code></pre> <p>In the Picture.java:</p> <pre><code>public class Picture { private Circle sun; /** * Constructor for objects of class Picture */ public Picture() { // nothing to do... instance variables are automatically set to null } /** * Draw this picture. */ public void draw() { sun = new Circle(); sun.changeColor("yellow"); sun.moveHorizontal(180); sun.moveVertical(-10); sun.changeSize(60); sun.makeVisible(); sun = new Circle(); sun.changeColor("yellow"); sun.moveHorizontal(180); sun.moveVertical(-10); sun.changeSize(60); sun.makeVisible(); sun = new Circle(); sun.changeColor("yellow"); sun.moveHorizontal(180); sun.moveVertical(-10); sun.changeSize(60); sun.makeVisible(); } </code></pre> <p>And the PictureTest.java is where the main is:</p> <pre><code>public class PictureTest { public static void main(String[] args) { Picture p = new Picture(); p.draw(); } } </code></pre> <p>When I run it, instead of it printing 3 because there are three suns, it prints:</p> <pre><code>1 1 1 </code></pre> <p>I'm not completely sure why this is, but I think it might have something to do with the fact that I'm printing from the Circle class so every time a circle is created it prints the 1. I've tried to change it so it prints the instances from the main in PictureTest but I can't get it working.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload