7 Replies - 607 Views - Last Post: 14 June 2017 - 07:47 AM Rate Topic: -----

#1 Heathersmithx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 07-November 16

Printing Shapes using Overriden toString Method in Shape Class

Posted 07 June 2017 - 09:01 PM

I'm having difficulties trying to print out the descriptions of my shape objects.
I have overridden the Objects.toString method, to use the Shape.toString method instead
I don't think the other toString Methods really need to Override the Shape.toString method except for in the case where I want to also print out the volume for the 3d shapes.

So far I have an abstract superclass called Shape. It's supposed to use the Shape.toString
to print out the description on all of my subclasses.

I have two subclasses below that called twoDimensional and threeDimensional which are also abstract
Nothing gets added to the toString method in these two classes, but they add information such as getDimension
which is printed in the Shape.toString Method.

Then from there... I have a bunch of 2d and 3d shapes that have their own methods that override
the Shape methods getArea(), getShape(). I'm not even sure that I need to override or even include
a toString method in these lower shape classes.

When I try to run the program i get some errors:

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%s'
	at java.util.Formatter.format(Unknown Source)
	at java.util.Formatter.format(Unknown Source)
	at java.lang.String.format(Unknown Source)
	at Shape.toString(Shape.java:49)
	at twoDimensionalShape.toString(twoDimensionalShape.java:53)
	at Square.toString(Square.java:54)
	at java.lang.String.valueOf(Unknown Source)
	at java.lang.StringBuilder.append(Unknown Source)
	at java.util.AbstractCollection.toString(Unknown Source)
	at java.lang.String.valueOf(Unknown Source)
	at java.io.PrintStream.println(Unknown Source)
	at ShapesApp.main(ShapesApp.java:25)


I've tried debugging, but I'm still trying to get the hang of that, and what it all means.

Here is my Main Shape Class which is supposed to be the SuperClass

/**
 * Shape Class
 * Abstract class that gathers the area and shape type
 * 
 * Gets the area, and the name of the shape and prints
 * out the results.
 * 
 * @author Heather Smith
 *
 */
public abstract class Shape {


	/** 
	 * Default Constructor
	 */
	public Shape()
	{
	}
	/**
	 * Returns the area of an object. Method is not yet defined. 
	 * Abstract Method
	 */
	public abstract double getArea();
	
	/**
	 * Abstract Method not defined yet
	 * getShape is defined in the subclasses
	 */
	public abstract String getShape();
	
	/**
	 * Abstract Method not yet defined
	 * Is defined in subClasses
	 * @return
	 */
	public abstract String getDimension();
	

	/**
	 * Prints a description of the shape
	 * with it's area if it's a two dimensional shape
	 * and it's area and volume if it's a three dimensional
	 * shape. 
	 */
	@Override
	public String toString()
	{
		return String.format("%s%s%s%s %.2f", "The area of this " + getDimension() 
		+ getShape() + " is: " + getArea());
		
	}


}




/**
 * Creates two dimensional shape objects
 * 
 * Creates two dimensional shape objects such as circle, square, or triangle
 * gets the area of the shape
 * @author Heather Smith
 * @see Shape
 *
 */
public abstract class twoDimensionalShape extends Shape {
	
	String dimension = " two dimensional ";
	
	/**
	 * Default Empty constructor
	 */
	public twoDimensionalShape()
	{
	}
	
	/**
	 * Abstract getArea Method defined in subclass
	 * Returns the area of the two dimensional shape
	 */
	public abstract double getArea();

	/**
	 * Abstract getShape Method
	 * Returns the shape of the object
	 * in a string. 
	 */
	
	
	public abstract String getShape();
	/**
	 * Method returns the dimension of the 
	 * two dimensional shape.
	 * @return dimension : String
	 */
	
	
	public String getDimension(){
		return dimension;
	}
	
	/**
	 * Returns the Description of the 
	 * two dimensional object 
	 * with it's area. 
	 * @return Shape Description 
	 */
	public String toString(){
		return String.format("%s", super.toString());
	}
	
	
}




public class Square extends twoDimensionalShape{
	
	
	private double length;
	private double width;
	private String shape = " square ";
	
	
	/**
	 * Constructs a new Square object. 
	 * 
	 * Gets the length and width of a square
	 * to create a new square object. 
	 * @param width
	 * @param length
	 * 
	 */
	public Square(double width, double length)
	{
		this.width = width;
		this.length = length;
		
	}
	/**
	 * Gets the area of a square object
	 * by multiplying the length by the width.
	 * @return returns length * width
	 */
	@Override
	public double getArea()
	{
		return length * width;
	}
	/**
	 * Gets the description of this shape
	 * which is a square. 
	 * @returns shape : String
	 */
	@Override
	public String getShape(){
		return this.shape;
	}
	
	/**
	 * Gets the description of a the shape
	 * and prints out the area and the description. 
	 * 
	 */
	
	
	public String toString()
	{
		return String.format("%s", super.toString());
		
	}

}




public class Circle extends twoDimensionalShape{

	private double radius;
	private String shape = " circle ";
	
	/**
	 * Creates a new Circle Object with the radius
	 * 
	 * @param radius
	 */
	public Circle (double radius){
		this.radius = radius;
		
	}
	/**
	 * Method to find the Area of a circle Object
	 * Returns the Area of a circle
	 * with the formula Area = PI (Radius * Radius)
	 * @param radius
	 * @return area
	 */
		public double getArea(){
			return  Math.PI * Math.pow(radius, 2);
		}
		
		
		/**
		 * Returns a description of this shape
		 * which is a circle. 
		 * @return shape : String
		 */
		@Override
		public String getShape(){
			return this.shape;
		}
		
		/**
		 * 
		 * @return
		 */
		public String description(){
			return String.format("%s" ,super.toString());
		
	}
}





public class Triangle extends twoDimensionalShape {

	private double base, height;
	private String shape = " triangle ";
	
	
	/**
	 * Constructs a new Triangle Object
	 * @param height
	 * @param base
	 */
	public Triangle(double height, double base){
		this.base = base;
		this.height = height;
	}
	
	/**
	 * Returns the area of a triangle 
	 * given the following formula
	 * base * height / 2
	 * @return area
	 */
	@Override
	public double getArea() {
		
		return (base * height)/2;
	}

	/**
	 * Returns the name of this shape
	 */
	@Override
	public String getShape() {
		return shape;
	}
	/**
	 * Prints the Description of this 
	 * triangle with it's area. 
	 * 
	 * @see twoDimensionalShape#toString()
	 */
	@Override
	public String toString(){
		return String.format("%s", super.toString());
	}

}




Three Dimensional Class
public abstract class threeDimensionalShape extends Shape {
	
	
	String dimension = " three dimensional ";
	
	/**
	 * Default Empty constructor
	 */
	public threeDimensionalShape()
	{
	}
	
	/**
	 * Abstract getArea Method defined in subclass
	 * Returns the area of the two dimensional shape
	 */
	public abstract double getArea();
	
	/**
	 * Returns the volume of the current 
	 * three dimensional Shape. 
	 * @return volume
	 */
	
	public abstract double getVolume();

	/**
	 * Abstract getShape Method
	 * Returns the shape of the object
	 * in a string from sub-class 
	 */
	public abstract String getShape();
	
	
	/**
	 * Method returns the dimension of the 
	 * three dimensional shape.
	 * @return dimension : String
	 */
	
	public String getDimension(){
		return dimension;
	}
	
	public String toString(){
		return String.format("\n%s%s %.2f", super.toString() + "\nThe Volume of this shape is" + getVolume());
	}

}




public class Sphere extends threeDimensionalShape {

	private double radius;
	private String shape = " sphere ";
	
	
	public Sphere(double radius){
		this.radius = radius;
		// 
		 //  
	}
	
	/**
	 * Returns the area of a sphere
	 * using the formula
	 * Sphere Surface Area = 4 * π * r2
	 * @return area
	 */
	@Override
	public double getArea() {
		return 4 * Math.PI * Math.pow(radius, 2);
	}

	/**
	 * Returns the Volume of a sphere
	 * using the formula
	 * Sphere Volume = 4/3 * π * r3
	 * @return Volume
	 */
	@Override
	public double getVolume() 
	{
		return (4 * Math.PI * Math.pow(radius, 3)) / 3;
	}

	/**
	 * Returns the current Shape of this Object
	 * Which is a sphere. 
	 */
	@Override
	public String getShape() {
		return this.shape;
	}
	
	/**
	 * Prints a Description of this shape
	 * with the area and volume. 
	 */
	@Override
	public String toString(){
		return String.format("%s", super.toString());
	}
}




public class Cube extends threeDimensionalShape {

	private double side;
	private String shape = " cube ";
	
	public Cube(double side){
		this.side = side;
	}

	/**
	 * Returns the area of a cube
	 * using the formula
	 * Area = 6 * (SideLength * SideLength)
	 * 
	 * @return area
	 */
	@Override
	public double getArea()
	{
		return 6 * (Math.pow(side, 2));
	}

	/**
	 * Returns the Volume of a Cube
	 * using the formula
	 * (side * side * side) = Volume
	 * @return Volume
	 */
	@Override
	public double getVolume() {
		return (Math.pow(side, 3));
	}

	@Override
	public String getShape() {
		return this.shape;
	}
	
	@Override
	public String toString(){
		return String.format("%s", super.toString());
	}
}




public class Tetrahedron extends threeDimensionalShape{

	private double side;
	private String shape = " tetrahedron ";
	
	/**
	 * Constructs a new Tetrahedron
	 * assuming all sides are equal length
	 * @param side Length
	 * 
	 */
	public Tetrahedron(double side){
		this.side = side;
	
	}
	
	/**
	 * Returns the area of a Tetrahedron
	 * using the following formula: 
	 * The area of the base and the lateral faces form the surface O.
	 * There is O = 4 * A (triangle) = sqr(3)aČ
	 * @return Area
	 */
	@Override
	public double getArea() {
		double s = (side * 3) / 2;
        double a = Math.sqrt(s * Math.pow(s - side, 3));
        return (double)(4 * a);
	}
	
	/**
	 * Calculates the Volume of a Tetrahedron
	 * using the following formula
	 * Volume = (side ^3) / (sqr(2) * 6)
	 * @return Volume
	 */
	@Override
	public double getVolume() {
		return (Math.pow(side, 3)) / (Math.sqrt(2) * 6);
	}

	/**
	 * Returns a Description of the current shape
	 * which is a tetrahedron
	 * @return shape
	 */
	@Override
	public String getShape() {
		return shape;
	}
	
	/**
	 * Returns a Description of the shape
	 * along with it's area and volume. 
	 * 
	 */
	@Override
	public String toString(){
		return String.format("%s", super.toString());
		
	}

}



Here is my application of this code

ShapeAPP

import java.util.ArrayList;

public class ShapesApp  {

	public static void main(String[] args) {

		
		ArrayList<Shape> shapes = new ArrayList<Shape>();
		
		
		//Adding shapes to the myShapes arraylist of two dimensional form
		
		shapes.add(new Square(5, 5));
		shapes.add(new Circle(5));
		shapes.add(new Triangle(10, 20));
		
		//Adding shapes to the myShapes arrayList of three dimensional form
		
		shapes.add(new Sphere(10));
		shapes.add(new Cube(20));
		shapes.add(new Tetrahedron(6));
		
		//Printing the objects in the Array List
		
		//System.out.println(shapes);
		
	    System.out.println("The Shape list.");
			for( Shape aShape : shapes )
	      System.out.println(aShape);
	   

	}

}




I THINK that's all of them.


So my program doesn't run at all because of that error message that I'm getting.
I've tried to modify all of the String.formats on my toString methods, and It doesn't
look wrong to me. So I don't know what to do. It's all due tomorrow.
Thanks for all your help! I know it's a lot.

Is This A Good Question/Topic? 0
  • +

Replies To: Printing Shapes using Overriden toString Method in Shape Class

#2 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,569
  • Joined: 12-December 12

Re: Printing Shapes using Overriden toString Method in Shape Class

Posted 07 June 2017 - 10:58 PM

Post specific language questions in the language forum.
Question moved to Java forum.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,569
  • Joined: 12-December 12

Re: Printing Shapes using Overriden toString Method in Shape Class

Posted 07 June 2017 - 11:58 PM

The error refers to line 49 and the symbol '%s'. Can you describe what '%s' is for and how it is used in String.format?

You aren't using String.format correctly. Review the documentation or your book on how it works and get basic examples working first.
Was This Post Helpful? 1
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3425
  • View blog
  • Posts: 15,493
  • Joined: 20-September 08

Re: Printing Shapes using Overriden toString Method in Shape Class

Posted 08 June 2017 - 01:17 AM

Another couple of points:

a. Class names begin uppercase. See http://technojeeves....yle-conventions
b. At least one of the classes has a description method. That's redundant. toString should be used for that. In any case, method names are verbs
Was This Post Helpful? 1
  • +
  • -

#5 Heathersmithx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 07-November 16

Re: Printing Shapes using Overriden toString Method in Shape Class

Posted 08 June 2017 - 02:55 PM

View Postandrewsw, on 07 June 2017 - 11:58 PM, said:

The error refers to line 49 and the symbol '%s'. Can you describe what '%s' is for and how it is used in String.format?

You aren't using String.format correctly. Review the documentation or your book on how it works and get basic examples working first.



I really struggle with String.format. It seems like everything online doesn't really go over the basics. I'll check again.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3425
  • View blog
  • Posts: 15,493
  • Joined: 20-September 08

Re: Printing Shapes using Overriden toString Method in Shape Class

Posted 08 June 2017 - 02:59 PM

http://docs.oracle.c.../Formatter.html

is what you should study.
Was This Post Helpful? 0
  • +
  • -

#7 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Printing Shapes using Overriden toString Method in Shape Class

Posted 08 June 2017 - 03:00 PM

Look at the API doc: http://docs.oracle.c.../Formatter.html
Was This Post Helpful? 0
  • +
  • -

#8 Heathersmithx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 07-November 16

Re: Printing Shapes using Overriden toString Method in Shape Class

Posted 14 June 2017 - 07:47 AM

My problem ended up being that I joined all of my strings together into one string using + when I should have used commas to separate the stuff in toString.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1