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");
		}
}

Recommended Answers

All 8 Replies

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());
}

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:

Did you fix the printf method?

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!

You are welcome :) Please mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.