11 Replies - 2536 Views - Last Post: 16 September 2012 - 06:45 PM Rate Topic: -----

#1 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

I'm encountering problems with my Tester class

Posted 12 September 2012 - 09:50 PM

Hello,

I am trying to use inheritance between 4 classes. The task is to create a Circle class that extends an Ellipse class that extends a GeometricObject class, but I'm having problems with my tester class.

Can anyone check out my code to see where I'm having missteps?

I want to be able to let the user select every option about the object from the JOptionPane window, but I know I'm not organizing this correctly. I am including the code for all four classes....

I know that I'm not parsing the input from the user, but I'm starting to get overwhelmed...please help me!

Here's the Tester:
public class TestDriver {
	public static void main(String[] args) {

		Object[] shapeList = {"Circle", "Ellipse", "Geometric Object"};
		Object[] colorList = {"White", "Red", "Blue"};
		Object[] optionList = {"Yes", "No"};
		Object[] calculateList = {"Area", "Perimeter"};
		

		String shape = (String)JOptionPane.showInputDialog(null, "Please select your shape:", "Shape", JOptionPane.PLAIN_MESSAGE, null, shapeList, "Circle");
		String color = (String)JOptionPane.showInputDialog(null, "Please select your color:", "Color", JOptionPane.PLAIN_MESSAGE, null, colorList, "White");
		String answer = (String)JOptionPane.showInputDialog(null,"Please select whether your shape is filled:", "Decision", JOptionPane.QUESTION_MESSAGE, null, optionList, "Yes");
		//String filled = new String(answer);
		String calculateOption = (String)JOptionPane.showInputDialog(null, "Please select a calculation:", "Calculation", JOptionPane.PLAIN_MESSAGE, null, calculateList, "Area");
		
		if (shape == "Circle") {
			Circle myCircle = new Circle();
			System.out.println("A circle " + myCircle.toString());
			System.out.println("The color is " + myCircle.getColor());
			System.out.println("Is the shape filled? " + myCircle.isFilled());
			System.out.println("The radius is " + myCircle.getRadius());
			System.out.println("The perimeter is " + myCircle.getPerimeter());
			System.out.println("The area is " + myCircle.getArea());
		}
			else if (shape == "Ellipse") {
				Ellipse myEllipse = new Ellipse();
				System.out.println("An ellipse " + myEllipse.toString());
				System.out.println("The color is " + myEllipse.getColor());
				System.out.println("Is the shape filled? " + myEllipse.isFilled());
				System.out.println("The perimeter is " + myEllipse.getPerimeter());
				System.out.println("The area is " + myEllipse.getArea());
			}
			else {
				GeometricObject myGeometricObject = new GeometricObject();
				System.out.println("A geometric object " + myGeometricObject.toString());
				System.out.println("The color is " + myGeometricObject.getColor());
				System.out.println("Is the shape filled? " + myGeometricObject.isFilled());
				System.out.println("The perimeter is " + myGeometricObject.getPerimeter());
				System.out.println("The area is " + myGeometricObject.getArea());
			}
	}
}


And the Circle class...
import static java.lang.Math.*;

public class Circle extends Ellipse {
	private double radius;

	public Circle() {
	}
	
	public Circle(double radius) {
		//look into this!
		super.getArea(radius, radius);
		this.radius = radius;
	}
	
	public double getRadius() {
		return radius;
	}
	
	public void setRadius(double radius) {
		this.radius = radius;
	}
	
	public double getPerimeter() {
		double perimeter = PI * radius * 2;
		return perimeter;
	}
	
	public String toString(){
		return "The circle is created on " + getDateCreated() + "\nThe color is: " + getColor() + "\nIs it filled? " + isFilled() +
				"\nThe perimeter is: " + getPerimeter() + "\nThe area is: " + getArea(radius, radius);
	}
}
	


The Ellipse class...
import static java.lang.Math.*;

public class Ellipse extends GeometricObject {
	
	private double lengthA;
	private double lengthB;
	
	public Ellipse() {
	}
	
	public Ellipse(double lengthA, double lengthB) {
		this.lengthA = lengthA;
		this.lengthB = lengthB;
	}
	
	public double getMajorAxis() {
		return lengthA;
	}
	
	public double getMinorAxis() {
		return lengthB;
	}
	
	public void setMajorAxis(double lengthA) {
		this.lengthA = lengthA;
	}
	
	public void setMinorAxis(double lengthB) {
		this.lengthB = lengthB;
	}
	
	public double getPerimeter() {
		lengthA = PI * Math.sqrt(2 * (Math.pow(lengthA, 2)) + (Math.pow(lengthB, 2))) - ((Math.pow(lengthA - lengthB, 2)) / 2);
		lengthB = PI * ((3 * (lengthA + lengthB)) - Math.sqrt((3 * lengthA + lengthB) * (lengthA + 3 * lengthB)));
		double perimeter = (lengthA + lengthB) / 2;
		return perimeter; 
	}
	
	public double getArea(double lengthA, double lengthB) {
		double area = PI * lengthA * lengthB;
		return area;
	}
	
	public String toString() {
		return "The ellipse is created on " + getDateCreated() + "\nThe color is: " + getColor() + "\nIs it filled? " + isFilled() +
				"\nThe perimeter is: " + getPerimeter() + "\nThe area is: " + getArea(lengthA, lengthB);
	}
	
}



And finally the GeometricObject class...
import static java.lang.Math.PI;

public class GeometricObject {

	private String color = "white";
	private boolean filled;
	private java.util.Date dateCreated;

	/** Construct a default geometric object */
	public GeometricObject() {
		dateCreated = new java.util.Date();
	}

	/**Construct a geometric object so that the user can select
	the color and whether or not it is filled*/
	public GeometricObject(String color, boolean filled) {
		dateCreated = new java.util.Date();
		this.color = color;
		this.filled = filled;
	}

	/** Return color */
	public String getColor() {
		return color;
	}

	/** Set a new color */
	public void setColor(String color) {
		this.color = color;
	}

	/** Return filled. Since filled is boolean,
	so, the get method name is isFilled */
	public boolean isFilled() {
		return filled;
	}

	/** Set a new filled */
	public void setFilled(boolean filled) {
		this.filled = filled;
	}

	/** Get dateCreated */
	public java.util.Date getDateCreated() {
		return dateCreated;
	}

	/** Return a string representation of this object */
	public String toString() {
		return "created on " + dateCreated + "\ncolor: " + color +
		      " and filled: " + filled;
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: I'm encountering problems with my Tester class

#2 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: I'm encountering problems with my Tester class

Posted 13 September 2012 - 01:06 AM

Please post the errors you're getting next time.

I looked through your code, it seems like you did not import JOptionPane in your TestDriver class. You need to include this: import javax.swing.JOptionPane at the beginning of your TestDriver.java file. Also, your GeometricObject class does not have getPerimeter() and getArea() methods, and hence the "cannot find symbol" errors are shown. Remove these two lines.

System.out.println("The perimeter is " + myGeometricObject.getPerimeter());
System.out.println("The area is " + myGeometricObject.getArea());


This post has been edited by Cuzzie: 13 September 2012 - 01:08 AM

Was This Post Helpful? 1
  • +
  • -

#3 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Re: I'm encountering problems with my Tester class

Posted 13 September 2012 - 01:42 AM

Sorry, I didn't grab the JOptionPane import when I copied the code

Here is my updated TesterClass. This is my error message:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
at homeworkOne.TestDriver.main(TestDriver.java:48)

I know that isn't my only problem, but I'm just trying to work out a little at a time. How do I take the user input and assign it to the objects in a cleaner way? I feel like this code can be about half the size it is right now.

import javax.swing.JOptionPane;

public class TestDriver {
	public static void main(String[] args) {
		Object[] majorAxis = {4.0, 6.0, 8.0, 10.0, 12.0};
		Object[] minorAxis = {2.0, 3.0, 4.0, 5.0, 6.0};
		Object[] radiusValue = {2.0, 3.0, 4.0, 5.0, 6.0};
		Object[] shapeList = {"Circle", "Ellipse", "Geometric Object"};
		Object[] optionList = {"Yes", "No"};
		Object[] calculateList = {"Area", "Perimeter"};
		

		String shape = (String)JOptionPane.showInputDialog(null, "Please select your shape:", "Shape", JOptionPane.PLAIN_MESSAGE, null, shapeList, "Circle");
		
		if (shape.equals("Ellipse")) {
			
			String color = JOptionPane.showInputDialog("What color would you like your ellipse?");
			String calculation = (String)JOptionPane.showInputDialog(null, "Please select a calculation:", "Calculation", JOptionPane.PLAIN_MESSAGE, null, calculateList, "Area");
			Double length1 = (Double)JOptionPane.showInputDialog(null, "Enter a value for the major axis:", "MajorAxis", JOptionPane.PLAIN_MESSAGE, null, majorAxis, 8.0);
			Double length2 = (Double)JOptionPane.showInputDialog(null, "Enter a value for the minor axis:", "MinorAxis", JOptionPane.PLAIN_MESSAGE, null, minorAxis, 4.0);
			Boolean option = (Boolean)JOptionPane.showInputDialog(null,"Would you like your circle filled in?", "Decision", JOptionPane.PLAIN_MESSAGE, null, optionList, "Yes");
			
			Ellipse myEllipse = new Ellipse(length1, length2, color, option);
			
			System.out.println("You have created a " + shape + " colored " + color + " with a major axis of " + majorAxis +
					"and a minor axis of " + minorAxis);
			System.out.println("The calculation you chose was: " + calculation);
				if (calculation.equals("Area")) {
			System.out.println("Your " + shape + " has an area of " + myEllipse.getArea(length1, length2) + " and is " + option); 
			}
				
				else if (calculation.equals("Perimeter")) {
					System.out.println("Your " + shape + " has a perimeter of " +
			myEllipse.getPerimeter() + " and is " + option);
				}
			
			
			System.out.println("An ellipse " + myEllipse.toString());
			
		}
		
			else if (shape.equals("Circle")) {
				String color = JOptionPane.showInputDialog("What color would you like your circle?");
				String calculation = (String)JOptionPane.showInputDialog(null, "Please select a calculation:", "Calculation", JOptionPane.PLAIN_MESSAGE, null, calculateList, "Area");
				Double radius = (Double)JOptionPane.showInputDialog(null, "Enter a value for the radius:", "Radius", JOptionPane.PLAIN_MESSAGE, null, radiusValue, 8.0);
				Boolean option = (Boolean)JOptionPane.showInputDialog(null,"Would you like your circle filled in?", "Decision", JOptionPane.PLAIN_MESSAGE, null, optionList, "Yes");
				
				Circle myCircle = new Circle(radius, color, option);
				System.out.println("You have created a " + shape + " colored " + color + " with a radius of " + radius);
				System.out.println("The calculation you chose was: " + calculation);
					if (calculation.equals("Area")) {
				System.out.println("Your " + shape + " has an area of " + myCircle.getArea(radius, radius) + " and is " + option); 
					}
					else if (calculation.equals("Perimeter")) {
						System.out.println("Your " + shape + " has a perimeter of " +
				myCircle.getPerimeter() + " and is " + option);
					}
				System.out.println("A circle " + myCircle.toString());
				
			}
				
				else {
					GeometricObject myGeometricObject = new GeometricObject();
					System.out.println("A geometric object " + myGeometricObject.toString());
				}
		
		}
	
}


Was This Post Helpful? 0
  • +
  • -

#4 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: I'm encountering problems with my Tester class

Posted 13 September 2012 - 02:46 AM

Well, the problem comes from this line..

Boolean option = (Boolean)JOptionPane.showInputDialog(null,"Would you like your circle filled in?", "Decision", JOptionPane.PLAIN_MESSAGE, null, optionList, "Yes");



The input that the user enters will be taken in as String, but here you're casting it into Boolean. You will have to retrieve the user input, and check if it's "yes" or "no". If it's "yes", then make "option" true, or else make it false.

String optionStr = (String)JOptionPane.showInputDialog(null,"Would you like your circle filled in?", "Decision", JOptionPane.PLAIN_MESSAGE, null, optionList, "Yes");
boolean option = false;

if(option.equals("yes"){

     option = true;

}


Was This Post Helpful? 1
  • +
  • -

#5 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Re: I'm encountering problems with my Tester class

Posted 13 September 2012 - 09:24 AM

Yes, that is a good idea, however when I try that approach it gives me basically the same error:

"Cannot invoke equals(String) on the primitive type boolean"

Arghhh!!!

Thanks for all of your help, by the way.

Adam
Was This Post Helpful? 0
  • +
  • -

#6 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Re: I'm encountering problems with my Tester class

Posted 13 September 2012 - 09:31 AM

Nevermind. I got it. I changed "option.equals" to "optionStr.equals"

I think everything should be working now.

Thank you Cuzzle!!!

View PostRenaldoTheNeck, on 13 September 2012 - 09:24 AM, said:

Yes, that is a good idea, however when I try that approach it gives me basically the same error:

"Cannot invoke equals(String) on the primitive type boolean"

Arghhh!!!

Thanks for all of your help, by the way.

Adam

Was This Post Helpful? 0
  • +
  • -

#7 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Re: I'm encountering problems with my Tester class

Posted 13 September 2012 - 10:13 AM

Actually, there is one more problem...

I'm having problems passing the boolean into my code on line 31. Here's the error message:

"The operator + is undefined for the argument type(s) String, void"


public class TestDriver {
	public static void main(String[] args) {
		
		Object[] majorAxis = {4.0, 6.0, 8.0, 10.0, 12.0};
		Object[] minorAxis = {2.0, 3.0, 4.0, 5.0, 6.0};
		Object[] radiusValue = {2.0, 3.0, 4.0, 5.0, 6.0};
		Object[] shapeList = {"Circle", "Ellipse", "Geometric Object"};
		Object[] optionList = {"filled", "not filled"};
		Object[] calculateList = {"Area", "Perimeter"};
		
		String shape = (String)JOptionPane.showInputDialog(null, "Please select your shape:", "Shape", JOptionPane.PLAIN_MESSAGE, null, shapeList, "Circle");
		
		if (shape.equals("Ellipse")) {
			
			String color = JOptionPane.showInputDialog("What color would you like your ellipse?");
			String calculation = (String)JOptionPane.showInputDialog(null, "Please select a calculation:", "Calculation", JOptionPane.PLAIN_MESSAGE, null, calculateList, "Area");
			Double length1 = (Double)JOptionPane.showInputDialog(null, "Enter a value for the major axis:", "MajorAxis", JOptionPane.PLAIN_MESSAGE, null, majorAxis, 8.0);
			Double length2 = (Double)JOptionPane.showInputDialog(null, "Enter a value for the minor axis:", "MinorAxis", JOptionPane.PLAIN_MESSAGE, null, minorAxis, 4.0);
			String optionStr = (String)JOptionPane.showInputDialog(null,"Would you like your circle filled in?", "Decision", JOptionPane.PLAIN_MESSAGE, null, optionList, "filled");
			boolean option = false;
			if (optionStr.equals("filled")) {
				option = true;
			}
			Ellipse myEllipse = new Ellipse(length1, length2, color, option);
			
			System.out.println("You have created a " + shape + " colored " + color + "." + "\nThe major axis is " + myEllipse.getMajorAxis() +
					" and the minor axis is " + myEllipse.getMinorAxis());
			System.out.println("The calculation you chose was: " + calculation);
				if (calculation.equals("Area")) {
			System.out.println("Your " + shape + " has an area of " + myEllipse.getArea(length1, length2) + " and is " +
				 myEllipse.setFilled(option) + ".");
			}



This post has been edited by RenaldoTheNeck: 13 September 2012 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: I'm encountering problems with my Tester class

Posted 13 September 2012 - 02:02 PM

myEllipse.setFilled(option)

setFilled() is a void method. You just cannot append a void to a String (the String that you are building as parameter for the println() method

This post has been edited by pbl: 13 September 2012 - 02:03 PM

Was This Post Helpful? 1
  • +
  • -

#9 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Re: I'm encountering problems with my Tester class

Posted 15 September 2012 - 09:25 AM

I think I"ve solved a lot of the issues I was having. Now I'm trying to create a loop to continuously prompt the user if they'd like to create a shape. How do I make this "do-while" loop more effective? If I put the "do" above the String questionStr, the scope is off for the "while" to reference the variable.

import javax.swing.JOptionPane;

public class TestDriver {
	public static void main(String[] args) {
		Object[] questionAnswer = {"Yes", "No"};
		Object[] shapeList = {"Circle", "Ellipse", "Geometric Object"};
		Object[] optionList = {"Filled", "Not filled"};
		Object[] calculateList = {"Area", "Perimeter"};
		
		String questionStr = (String)JOptionPane.showInputDialog(null,"Would you like to create a shape?", "Decision", JOptionPane.PLAIN_MESSAGE, null, questionAnswer, "Yes");

		if (questionStr.equals("No")) 
			System.exit(0);
		
		do{
			
		String shape = (String)JOptionPane.showInputDialog(null, "Please select your shape:", "Shape", JOptionPane.PLAIN_MESSAGE, null, shapeList, "Circle");
		String color = JOptionPane.showInputDialog("What color would you like your shape to be?");
		String optionStr = (String)JOptionPane.showInputDialog(null,"Would you like your shape filled in?", "Decision", JOptionPane.PLAIN_MESSAGE, null, optionList, "Filled");
	
		boolean option = false;
		
		if (optionStr.equals("Filled")) 
				option = true;
			
				System.out.println("Your shape is " + shape);
				
		
			if (shape.equals("Geometric Object")) {
				GeometricObject myGeometricObject = new GeometricObject(color, option);
				System.out.println(myGeometricObject.toString());
				System.exit(0);
				}
			
		String calculation = (String)JOptionPane.showInputDialog(null, "Please select a calculation:", "Calculation", JOptionPane.PLAIN_MESSAGE, null, calculateList, "Area");
		
				if (shape.equals("Ellipse")) {
					double majorAxis = Double.parseDouble(JOptionPane.showInputDialog("Enter a value for major axis:"));
					double minorAxis = Double.parseDouble(JOptionPane.showInputDialog("Enter a value for minor axis:"));
			
						Ellipse myEllipse = new Ellipse(majorAxis, minorAxis, color, option);
						
						System.out.println(myEllipse.toString() + "\nThe major axis is: " + myEllipse.getMajorAxis() + 
								"\nThe minor axis is: " + myEllipse.getMinorAxis());
								
							if (calculation.equals("Area")) {
								System.out.println("The area is: " + myEllipse.calculateArea(majorAxis, minorAxis));
							}
				
							else if (calculation.equals("Perimeter")) {
								System.out.println("The perimeter is: " + myEllipse.calculatePerimeter());
							}
				}
		
				else if (shape.equals("Circle")) {
				
					double radius = Double.parseDouble( JOptionPane.showInputDialog("Enter radius:"));
				
					Circle myCircle = new Circle(radius, color, option);
			
						if (calculation.equals("Area")) {
							System.out.println(myCircle.toString() + "\nThe area is: " + myCircle.calculateArea(radius, radius)); 
						}
						else if (calculation.equals("Perimeter")) {
							System.out.println(myCircle.toString() + "\nThe perimeter is: " + myCircle.calculatePerimeter());
						}
				}
				
		} while(questionStr.equals("Yes"));

	}
	
}





This post has been edited by RenaldoTheNeck: 15 September 2012 - 09:28 AM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: I'm encountering problems with my Tester class

Posted 15 September 2012 - 09:17 PM

The easiest way an infinite loop with a break

   while(true) {
      ... answer question
      if(answer says to exit)
         break;   // <--- exit while loop
      ... continue code here
   }
   ... statement executed after the break in the while loop


Was This Post Helpful? 1
  • +
  • -

#11 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Re: I'm encountering problems with my Tester class

Posted 16 September 2012 - 02:58 PM

Everything works like a charm! Thanks for everything Cuzzie and pbl!!
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: I'm encountering problems with my Tester class

Posted 16 September 2012 - 06:45 PM

Glad we could help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1