I can't figure out for the life of me why the names of the square and rectangle won't show up....can anyone help?

public class Square extends NamedShape implements Shape 
{
	public void Length() 
	{
		System.out.printf("Please Enter Length Value: ", this.getLength());
	}
	
	public Square(String name) {
		super(name);
		System.out.printf("Shape:", this.getName());
	}

}

and

public class Rectangle extends NamedShape implements Shape
{
	@Override
	public void Length() 
	{
		System.out.printf("Please Enter Length Value: ");
	}
	
	public Rectangle(String name) {
		super(name);
		System.out.printf("Shape:", this.getName());
	}


}

and finally:

public class AreaFinder {
		private Shape[] shapes;	
		private int population;		
		
		public AreaFinder() {
			shapes = new Shape[100];
		}
		
		public void populate() {
			addShape(new Square("Square"));
			addShape(new Rectangle("Rectangle"));
		}

		private void addShape(Shape a) {
			shapes[population++] = a;
		}
		
		public static void main(String[] args) {
			AreaFinder shapes = new AreaFinder();
			shapes.populate();
		}
		

		public void LengthFinder() 
		{
			System.out.printf("Length Values Called");
			for(int i = 0; i < population; i++) 
			{
				shapes[i].Length();
			}
		}
}
public class NamedShape 
{
	private String name;
	public double length;
	double newlength;
	double side_length;
	
	public String getName() {
		return name;
	}
	
	public Double getLength()
	{
		return newlength;
	}
	
	public NamedShape(double newlength)
	{
		this.length = newlength;
		System.out.printf("\t");		
	}
	
	public NamedShape(String name) {
		this.name = name;
			System.out.printf("\t");
		}
}

Edited 6 Years Ago by PDB1982: n/a

You are not using the printf properly - you need to tell the method where do you want the String to be printed. Try this:

public Square(String name) 
{
   super(name);
   System.out.printf("Shape: %s", this.getName());
}

Edited 6 Years Ago by apines: n/a

Should the this.getName() return the name "New Square" that is specified in the AreaFinder class?

this.getName() will call the getName() method of the calling instance. Square gets its getName() method from NamedShape, so it will print its name - "Square"

That's what I thought, but the output right now is:

Shape:     Shape:

yes, If I add the code that you suggested, then it just prints out:

Shape: &s    Shape:

I'm so stupid....I was inserting the wrong symbol, because I was trying to go quick...your solution was correct.

Thank You!

This article has been dead for over six months. Start a new discussion instead.