Reputation: 231
so I need to use a method to print out the contents of my rectangle and circle objects. I want them formatted something like this
GeometricObject [color=red, filled=false, dateOfCreation=Wed Feb 11 12:21:51 EST
2015]
Circle [ radius= 4.0 Area=50.27 Perimeter=25.13 ]
I was given the method signature so i must use it, I have tried two different ways to test whether the object is either a rectangle or circle and then take the correct action but i cannot get them to print anything (I believe I may be missing something from my public class hw2redo more specifically my recreateObject method). The logic I was aiming for is that if the object is equal to a circle (in my recreateObject method I have two return signatures of either circle or rectangle) then it would use the the printCircle method in the Circle class, but no output. My second logical path was if the objects list contained the word "Rectangle" anywhere in it, of course that would mean it is a rectangle object and would access the printRectangle method in the Rectangle class but again nothing. However, I will note if i just use
((Circle) o).printCircle();
for example, it will invoke the printCircle method for all of my circle objects and throw an exception once it reaches my first rectangle object (I think I'm close!).
private static void showObjects(ArrayList<GeometricObject> list) {
for(GeometricObject o : list) {
if ((o.equals("Circle")))
((Circle) o).printCircle();
if (o.equals(list.contains("Rectangle")))
((Rectangle) o).printRectangle();
}
}
Here is the entirety of my code if your interested.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
public class hw2redo
{
public static void main(String[] args) throws FileNotFoundException {
GeometricObject g = null;
File diskFile = new File("file.txt");
Scanner diskScanner = new Scanner(diskFile);
ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();
while(diskScanner.hasNext()){
String geolist = diskScanner.nextLine();
g = recreateObject(geolist);
list.add(g);
}
diskScanner.close();
/* while (diskScanner.hasNext()) {
String list = diskScanner.nextLine();
g = recreateObject(list);
}
diskScanner.close();*/
showObjects(list);
}
private static GeometricObject recreateObject(String data) {
String[] list = data.split(",");
String geoObject = list[0];
if (geoObject.equals("Circle")) {
String color = list[1];
boolean filled = Boolean.valueOf(list[2]);
double radius = Double.valueOf(list[3]);
return new Circle(radius, color, filled);
}
if (geoObject.equals("Rectangle")) {
String color = list[1];
boolean filled = Boolean.valueOf(list[2]);
double height = Double.valueOf(list[3]);
double width = Double.valueOf(list[4]);
return new Rectangle(width, height, color, filled);
}
return null;
}
private static void showObjects(ArrayList<GeometricObject> list) {
for(GeometricObject o : list) {
if ((o.equals("Circle")))
((Circle) o).printCircle();
if (o.equals(list.contains("Rectangle")))
((Rectangle) o).printRectangle();
}
}
}
abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
private String data;
/** Construct a default geometric object */
public GeometricObject() {
super();
dateCreated = new java.util.Date();
//this.data = data;
}
/** Construct a geometric object with the specified color
* and filled value */
public GeometricObject(String color, boolean filled) {
super();
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,
its getter method is named 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 "GeometricObject [color=" + color +", filled="+ filled + ", dateOfCreation=" + dateCreated +
"]\n";
}
}
class Circle extends GeometricObject
{
private double radius;
public Circle() {
super();
radius = 1;
}
public Circle(double radius,
String color, boolean filled) {
super(color, filled);
this.radius = radius;
setColor(color);
setFilled(filled);
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/** Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}
class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
super();
}
public Rectangle(
double width, double height, String color, boolean filled) {
super(color, filled);
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
/** Return area */
public double getArea() {
return width * height;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
public void printRectangle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + width);
}
}
My file.txt (the objects I'm trying to recreate) conatins
Circle,green,false,4.0
Circle,blue,false,2.0
Circle,blue,true,7.0
Rectangle,orange,true,10.0,6.0
Rectangle,green,false,5.0,11.0
Rectangle,red,true,14.0,12.0
Any help or hints in the right direction would be greatly appreciated. Thanks a ton!
Upvotes: 0
Views: 1932
Reputation: 983
I don't see .equals()
method overridden in your code so that you can use
if ((o.equals("Circle")))
in showObjects()
method.
You need to either handle .equals()
method appropriately in your code or you can use
if ( o instanceof Circle)
instead of if ((o.equals("Circle")))
in showObjects()
method. You may need to do the same for your other objects (like Rectangle
mentioned above) too.
Upvotes: 1
Reputation: 24157
I need to use a method to print out the content s of my rectangle and circle objects.
Circle [ radius= 4.0 Area=50.27 Perimeter=25.13 ]
Any help or hints in the right direction would be greatly appreciated.
Here is a hint for Circle
class. Use the following method to print circle:
public void printCircle() {
System.out.println("Circle [" +
"radius=" + radius +
" Area=" + getArea() +
" Perimeter=" + getPerimeter() +
' ]');
}
I have not checked your logic as you need to learn it yourself :).
Upvotes: 0