Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to display counts of how many objects have been created using toString method?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry How to display counts of how many objects have been created using toString method?

    Hello, I am trying to write a GUI program for my computer class which requires us to create a superclass containing basic data for creating lines, rectangles, and ovals, subclasses of that superclass which have methods specific to each, a class that draws a randomized amount of each shape (and also randomizes the color and whether or not it is filled in for rectangles and ovals), and finally one that ties it all together (the test class). Our teacher gave us code to start from, which contained the superclass and Line subclass. In it was included two toString methods, the main(supeclass) one being:

    	public String toString ()
    	{
    		return String.format ("x1 = %d...", x1...);
    	}

    and the subclass toString method:

    	public String toString (int count)
    	{
    		return String.format ("Line: %d", super.toString());
    	}

    The first one has ellipses because he intended us to be able to figure it out, which I followed up by adding in "x2 = %d", so on and so forth. From what I understand, the toString in the superclass is just there to be there essentially, as it will get overridden by the subclasses. Beyond that, I have no idea of how I would go about making a count of how many of each shape appears. Here is the rest of my code in totality...

    package project3;

    import java.awt.*;
     
    public class MyShape
    {
    	private int x1, x2, y1, y2;
    	private Color myColor;
     
     
    	public MyShape (int x1, int x2, int y1, int y2, Color myColor)
    	{
    		this.x1 = x1;
    		this.x2 = x2;
    		this.y1 = y1;
    		this.y2 = y2;
    		this.myColor = myColor;
    	}
     
    	public void setx1 (int x1)
    	{
    		this.x1 = x1;
    	}
     
    	public int getx1 ()
    	{
    		return x1;
    	}
     
    	public void setx2 (int x2)
    	{
    		this.x2 = x2;
    	}
     
    	public int getx2 ()
    	{
    		return x2;
    	}
     
    	public void sety1 (int y1)
    	{
    		this.y1 = y1;
    	}
     
    	public int gety1 ()
    	{
    		return y1;
    	}
     
    	public void sety2 (int y2)
    	{
    		this.y2 = y2;
    	}
     
    	public int gety2 ()
    	{
    		return y2;
    	}
     
    	public void setColor (Color myColor)
    	{
    		this.myColor = myColor;
    	}
     
    	public Color getColor ()
    	{
    		return myColor;
    	}
     
    	public String toString ()
    	{
    		return String.format ("x1 = %d x2 = %d y1 = %d y2 = %d", x1, x2, y1, y2);
    	}
    }

    package project3;
     
    import java.awt.*;
     
    public class MyLine extends MyShape
    {
    	int count = 0;
     
    	public MyLine (int x1, int y1, int x2, int y2, Color myColor)
    	{
    		super (x1, y1, x2, y2, myColor);
     
    		count++;
    	}
     
    	public String toString (int count)
    	{
    		return String.format ("Line: %d", super.toString());
    	}
     
    	public void draw (Graphics g)
    	{
    		g.setColor (getColor());
    		g.drawLine (getx1(), gety1(), getx2(), gety2());
    	}
    }

    package project3;
     
    import java.awt.*;
     
    public class MyRectangle extends MyShape
    {
    	private boolean fill;
     
    	public MyRectangle (int x1, int x2, int y1, int y2, Color myColor, boolean fill)
    	{
    		super (x1, x2, y1, y2, myColor);
    		this.fill = fill;
    	}
     
    	public int getUpperLeftX () {
    		return Math.min (getx1(), getx2());
    	}
    	public int getUpperLeftY () {
    		return Math.min (gety1(), gety2());
    	}
    	public int getWidth () {
    		return Math.abs (getx2() - getx1());
    	}
    	public int getHeight () {
    		return Math.abs (gety2() - gety1());
    	}
     
    	public String toString ()
    	{
    		return String.format ("Rectangle: %s", super.toString());
    	}
     
    	public void draw (Graphics g) {
    		g.setColor (getColor());
     
    		if (fill)
    			g.fillRect (getUpperLeftX (), getUpperLeftY (),
    							    		getWidth (), getHeight ());
    		else
    			g.drawRect (getUpperLeftX (), getUpperLeftY (),
    		    							getWidth (), getHeight ());	
    	}
    }


    package project3;
     
    import java.awt.*;
     
    public class MyOval extends MyShape
    {
    	private boolean fill;
     
    	public MyOval (int x1, int x2, int y1, int y2, Color myColor, boolean fill)
    	{
    		super (x1, x2, y1, y2, myColor);
    		this.fill = fill;
    	}
     
    	public int getUpperLeftX () {
    		return Math.min (getx1(), getx2());
    	}
    	public int getUpperLeftY () {
    		return Math.min (gety1(), gety2());
    	}
    	public int getWidth () {
    		return Math.abs (getx2() - getx1());
    	}
    	public int getHeight () {
    		return Math.abs (gety2() - gety1());
    	}
     
    	public String toString ()
    	{
    		return String.format ("Oval: %s", super.toString());
    	}
     
    	public void draw (Graphics g) {
    		g.setColor (getColor());
     
    		if (fill)
    			g.fillOval (getUpperLeftX (), getUpperLeftY (),
    							    		getWidth (), getHeight ());
    		else
    			g.drawOval (getUpperLeftX (), getUpperLeftY (),
    		    							getWidth (), getHeight ());	
    	}
    }

    package project3;
     
    import java.awt.*;
    import java.util.Random;
    import javax.swing.JPanel;
     
    public class DrawPanel extends JPanel
    {
    	private Random randomNumbers = new Random();
    	private MyLine lines[];
    	private MyRectangle rects[];
    	private MyOval ovals[];
    	static final long serialVersionUID = 0;
     
    	public DrawPanel()
    	{
    		final int NUMBER_COLORS = 256;	// (0-255 color values)
    		final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
    		int x1, y1, x2, y2;
    		Color color;
    		boolean fill;
     
    		setBackground (Color.WHITE);
     
    		lines = new MyLine [5 + randomNumbers.nextInt (5)];
    		rects = new MyRectangle [5 + randomNumbers.nextInt (5)];
    		ovals = new MyOval [5 + randomNumbers.nextInt(5)];
     
    		for (int count = 0; count < lines.length; count++)
    		{
    			x1 = randomNumbers.nextInt (WINDOW_WIDTH);
    			y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
    			x2 = randomNumbers.nextInt (WINDOW_WIDTH);
    			y2 = randomNumbers.nextInt (WINDOW_HEIGHT);
     
    			color = new Color (randomNumbers.nextInt (NUMBER_COLORS),
    										randomNumbers.nextInt (NUMBER_COLORS),
    											randomNumbers.nextInt (NUMBER_COLORS));
     
    			lines [count] = new MyLine (x1, y1, x2, y2, color);
    		} 
     
    		for (int count = 0; count < rects.length; count++)
    		{
    			x1 = randomNumbers.nextInt (WINDOW_WIDTH);
    			y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
    			x2 = randomNumbers.nextInt (WINDOW_WIDTH);
    			y2 = randomNumbers.nextInt (WINDOW_HEIGHT);
     
    			color = new Color (randomNumbers.nextInt (NUMBER_COLORS),
    										randomNumbers.nextInt (NUMBER_COLORS),
    											randomNumbers.nextInt (NUMBER_COLORS));
     
    			fill = randomNumbers.nextBoolean ();
     
    			rects [count] = new MyRectangle (x1, y1, x2, y2, color, fill);
    		}
    		for (int count = 0; count < ovals.length; count++)
    		{
    			x1 = randomNumbers.nextInt (WINDOW_WIDTH);
    			y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
    			x2 = randomNumbers.nextInt (WINDOW_WIDTH);
    			y2 = randomNumbers.nextInt (WINDOW_HEIGHT);
     
    			color = new Color (randomNumbers.nextInt (NUMBER_COLORS),
    										randomNumbers.nextInt (NUMBER_COLORS),
    											randomNumbers.nextInt (NUMBER_COLORS));
     
    			fill = randomNumbers.nextBoolean ();
     
    			ovals [count] = new MyOval (x1, y1, x2, y2, color, fill);
    		} 
    	}
     
    	public String statusText()
    	{      //this would be the status text that is displayed at the bottom of the JPanel window, but I don't know
                   //how to implement it correctly, this is the problem
    		return String.format (lines.toString(), rects.toString(), ovals.toString());
    	}
     
    	public void paintComponent (Graphics g)
    	{
    		super.paintComponent (g);
     
    		for (MyLine line : lines) 
    			line.draw (g);
    		for (MyRectangle rect : rects)
    			rect.draw (g);
    		for (MyOval oval : ovals)
    			oval.draw(g);
    	}
    }


    package project3;
     
    import javax.swing.*;
    import java.awt.BorderLayout;
     
    public class TestDraw {
     
    	public static void main(String[] args)
    	{
    		JLabel south = new JLabel();
     
    		final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
    		DrawPanel panel = new DrawPanel ();
    		JFrame application = new JFrame ();
     
    		application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
    		application.add(south, BorderLayout.SOUTH);
    		application.add (panel);
    		application.setSize (WINDOW_WIDTH, WINDOW_HEIGHT);
    		application.setVisible (true);
     
    	}
     
    }

    The official prompt for this is the following at the bottom of the page, with a visual representation of how it should look when the program runs:

    http://kvazar-micro.zp.ua:18888/Java...9lev1sec8.html

    I am just seeking some direction or a push in the right direction, I don't want or expect you all to give me the answer, I know how programming experts feel about people just posting to do that. :] Please help any way you can, thank you!

    PS: Everything else works like a charm, it's just figuring out how to get counts for how many objects are created.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to display counts of how many objects have been created using toString method

    how to get counts for how many objects are created.
    One way would be to have a static counter in each class that you increment in each constructor.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to display counts of how many objects have been created using toString method

    I thought about trying to do that, but I figured out the problem I was having. I was trying to use the class DrawPanel's toString method DIRECTLY instead of using the "panel" instantiation that I made in the main class...dumb mistake. I ended up just making getters for the array lengths for the each subclass and using those in the toString method. Thanks for the help though!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to display counts of how many objects have been created using toString method

    Glad you got it to work.

Similar Threads

  1. toString method
    By feldmanb700 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 09:20 PM
  2. Autoboxing and unboxing for user created objects
    By tcstcs in forum Java Theory & Questions
    Replies: 3
    Last Post: March 22nd, 2011, 07:54 AM
  3. Replies: 2
    Last Post: February 28th, 2011, 10:51 AM
  4. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM

Tags for this Thread