This discussion is archived
1 2 Previous Next 26 Replies Latest reply: Feb 21, 2013 2:27 AM by 962826 Go to original post RSS
  • 15. Re: Geometric Object Class
    962826 Newbie
    Currently Being Moderated
    So this is where I currently am. Problems I am having:

    1) I created the Array List but now what? How do I get the data I enter into the scanner to populate the array?
    2) My series of scanner statements are messed up. When I compile, the screen asks three scanner statements at once, as opposed to one, letting me fill in the answer, and then moving on to the next one. I also think I need to do an "if" statement after the first string scanner so the program knows whether to ask for a radius if it is a circle or width and height if it is a rectangle.
    3) How do I get the data (radius, width, height) entered into the driver program to tie back to the appropriate classes in the package?

    Thanks for the help and patience!

    GeometricObject:
    public abstract class GeometricObject {
         // class variable ------------------
         private String color;
         private boolean filled;
         // constructors --------------------
         public GeometricObject(String color, boolean filled) {
              super();
              this.color = color;
              this.filled = filled;
         }
         public GeometricObject() {
              super();
              this.color = "white";
              this.filled = false;
         }
         // accessors -----------------------
         public String getColor() {
              return color;
         }
         public void setColor(String color) {
              this.color = color;
         }
         public boolean getFilled() {
              return filled;
         }
         public void setFilled(boolean filled) {
              this.filled = filled;
         }
         // user-defined methods ------------
         public String showData() {
              return " Color: " + color
                        + " Filled: " + filled;
         }     
    }
    Circle:
    public class Circle extends GeometricObject {
    
         // class variables
              private double radius;
         // constructors
              public Circle(String color, boolean filled, double radius) {
                   super(color, filled);
                   this.radius = radius;
              }
              public Circle() {
                   super();
                   this.radius = 1;
              }
         // accessors
              public double getRadius() {
                   return radius;
              }
              public void setRadius(double radius) {
                   this.radius = radius;
              }     
         // user defined methods
              public double getArea() {
              return Math.PI * radius * radius;
    
         }     
    }
    Rectangle:
    public class Rectangle extends GeometricObject {
    
         // class variables
                   private double width;
                   private double length;
         // constructors
                   public Rectangle(String color, boolean filled, double width, double length) {
                        super(color, filled);
                        this.width = width;
                        this.length = length;
                   }
                   public Rectangle() {
                        super();
                        this.width = 1;
                        this.length = 1;
                   }
         // accessors
                   public double getWidth() {
                        return width;
                   }
                   public void setWidth(double width) {
                        this.width = width;
                   }
                   public double getLength() {
                        return length;
                   }
                   public void setLength(double length) {
                        this.length = length;
                   }     
         // user defined methods
                   public double getArea() {
                        return width * length;
    
                   }
                   
    }
    Driver:
    public class Driver {
    
         public static void main(String[] args) {
              Scanner scanner = new Scanner(System.in);
              boolean filled;
              // Define an ArrayList<GeometricObject> object, call it list
              ArrayList<GeometricObject> list = new ArrayList<GeometricObject>;
              
              
              // Create a scanner to get data
              System.out.println("Enter a geometric object, either circle or rectangle: ");
              String geoObject = scanner.toString();
              
              System.out.println("Enter the color of the object: ");
              String color = scanner.toString();
              
              System.out.println("Enter the radius of the circle: ");
              int radius = scanner.nextInt();
              
              System.out.println("Enter the width of the rectangle: ");
              int width = scanner.nextInt();
              
              System.out.println("Enter the height of the rectangle: ");
              int height = scanner.nextInt();
              
              
         }
         
         
    
    }
  • 16. Re: Geometric Object Class
    EJP Guru
    Currently Being Moderated
    1) I created the Array List but now what? How do I get the data I enter into the scanner to populate the array?
    You call the appropriate method on the ArrayList object.
    2) My series of scanner statements are messed up. When I compile, the screen asks three scanner statements at once, as opposed to one, letting me fill in the answer, and then moving on to the next one.
    You need to call Scanner.nextLine() after each nextInt().
    3) How do I get the data (radius, width, height) entered into the driver program to tie back to the appropriate classes in the package?
    I don't understand what you mean by 'tie back'. You've got all those object classes, now you surely have to create instance of them?
  • 17. Re: Geometric Object Class
    962826 Newbie
    Currently Being Moderated
    EJP wrote:
    1) I created the Array List but now what? How do I get the data I enter into the scanner to populate the array?
    You call the appropriate method on the ArrayList object.
    Which is how I am approaching but is giving my tons of errors:
    System.out.println("Please enter a geometric object, either Circle or Rectangle: ");
     list.add(scanner.nextLine());
                              
    System.out.println("Enter the color of the object: ");
    String color = scanner.toString();
                   
    System.out.println("Enter the radius of the circle: ");
    int radius = scanner.nextInt();
    scanner.nextLine();
                   
    System.out.println("Enter the width of the rectangle: ");
    int width = scanner.nextInt();
    scanner.nextLine(); 
                   
    System.out.println("Enter the height of the rectangle: ");
    int height = scanner.nextInt();
    scanner.nextLine();
    2) My series of scanner statements are messed up. When I compile, the screen asks three scanner statements at once, as opposed to one, letting me fill in the answer, and then moving on to the next one.
    You need to call Scanner.nextLine() after each nextInt().
    Thanks
    3) How do I get the data (radius, width, height) entered into the driver program to tie back to the appropriate classes in the package?
    I don't understand what you mean by 'tie back'. You've got all those object classes, now you surely have to create instance of them?
    Do I use scanner to create instances of the classes? And then put them into the array list?
  • 18. Re: Geometric Object Class
    Kayaman Guru
    Currently Being Moderated
    restless8052 wrote:
    Which is how I am approaching but is giving my tons of errors:
    System.out.println("Please enter a geometric object, either Circle or Rectangle: ");
    list.add(scanner.nextLine());
    Now think about what you're trying to do there. Just think what's happening, what input you're getting, what "list" actually is. I'm sure you'll realize your mistake.
    Do I use scanner to create instances of the classes?
    You've already created instances in your code. Why would you suddenly need scanner to do it (not to mention that you created an instance of Scanner)?
    And then put them into the array list?
    Yes, you'll need to create your GeometryObject instances and put them in the arraylist. After you've filled them with the appropriate information.
  • 19. Re: Geometric Object Class
    962826 Newbie
    Currently Being Moderated
    Kayaman wrote:
    restless8052 wrote:
    Which is how I am approaching but is giving my tons of errors:
    System.out.println("Please enter a geometric object, either Circle or Rectangle: ");
    list.add(scanner.nextLine());
    Now think about what you're trying to do there. Just think what's happening, what input you're getting, what "list" actually is. I'm sure you'll realize your mistake.
    I am putting an input of "String" into an Array list of objects, GeometricObject. Is that what is wrong? So the solution, I believe, is to have scanner take in an object, either Circle or Rectangle, and turn that into a GeometricObject and put it into the ArrayList. Depending on what object I create, my program will then have to prompt me for filled, color,and either radius or width and height. Am I thinking this through correctly?

    >
    Do I use scanner to create instances of the classes?
    You've already created instances in your code. Why would you suddenly need scanner to do it (not to mention that you created an instance of Scanner)?
    This is lost on me...

    >
    And then put them into the array list?
    Yes, you'll need to create your GeometryObject instances and put them in the arraylist. After you've filled them with the appropriate information.
    So this ties back to the first question - I use scanner to create my instances of GeometricObject and to fill them with the appropriate information?

    Really appreciate the patience! I know I'm a novice!
  • 20. Re: Geometric Object Class
    962826 Newbie
    Currently Being Moderated
    I may be going in the total wrong direction here now but thoughts?
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Driver {
    
         public static void main(String[] args) {
              // Create instances of GeometricObject
              // Circle c1 = new Circle();
              // Circle c2 = new Circle("red", true, 1);
              // Rectangle r1 = new Rectangle(10, 20);
              // Rectangle r2 = new Rectangle("blue", true, 1, 5);
              
              // Define an ArrayList<GeometricObject> object, call it list
              ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();
              Scanner user_input = new Scanner( System.in );
              {
                   String GeometricObject;
                   System.out.println("Enter a Geometric Object, either Circle or Rectangle: ");
                   GeometricObject = user_input.next();
                   
                   double radius;
                   System.out.println("What is the radius of the circle? ");
                   radius = user_input.nextDouble();
                   
                   double width;
                   System.out.println("What is the width of the rectangle? ");
                   width = user_input.nextDouble();
                   
                   double length;
                   System.out.println("What is the length of the rectangle? ");
                   length = user_input.nextDouble();
                   
                   String filled;
                   System.out.println("The Geometric Object is filled. Is this true or false? ");
                   filled = user_input.next();
                   
                   String color;
                   System.out.println("What is the color of the Geometric Object? ");
                   color = user_input.next();
                                  
                   String GeometricObjectCircle;
                   System.out.println("Enter a Geometric Object, either Circle or Rectangle: ");
                   GeometricObject = user_input.next();     
                   }     
              }
              
         }
    Now - how do I make it so if they enter circle or rectangle it will ask the appropriate questions? Meaning, if circle, ask for radius after it gets filled and color. And if rectangle, ask for width and length after filled and color.

    And finally - how do I then turn the data collected into an object and put it in the ArrayList?

    Edited by: restless8052 on Feb 20, 2013 8:43 AM

    Edited by: restless8052 on Feb 20, 2013 9:01 AM
  • 21. Re: Geometric Object Class
    962826 Newbie
    Currently Being Moderated
    Come on don't stop with the help now - I actually feel like I am headed in the right direction!
  • 22. Re: Geometric Object Class
    EJP Guru
    Currently Being Moderated
    You have an ArrayList<GeometricObject>. That is, a list of objects of type GeometricObject. The only things you can put into that list are objects of that type.

    Declaring a variable of name GeometricObject doesn't accomplish that, or indeed anything. You have to assemble the radius etc from the user input and then construct the appopriate instance of GeometricObject, whether Circle, Trapezoid, etc, and add that object into the list.
  • 23. Re: Geometric Object Class
    991568 Newbie
    Currently Being Moderated
    MODERATOR ACTION: Please don't hijack threads. Posts deleted.

    Edited by: EJP on 21/02/2013 12:19
  • 24. Re: Geometric Object Class
    962826 Newbie
    Currently Being Moderated
    Well I was going about it wrong all along. I went to the tutor tonight and found out we do not use scanner to enter the data, but rather it is an input file. And while the directions stated that clearly, I was not aware.

    Regardless we fixed it. In summary, I used file class to read through every line of the input file, I used tokens to parse the strings (lines) into separate indices. I was able to use the tokens to determine if it was a circle or rectangle,, and then crawl through the remainder of the properties.

    It was a mess, and after the assignment is turned in, I will post my code for reference.
  • 25. Re: Geometric Object Class
    Kayaman Guru
    Currently Being Moderated
    restless8052 wrote:
    Well I was going about it wrong all along.
    I wouldn't say you were going wrong about it. It doesn't really affect the business logic of a code, whether you get your input from a file or have a person enter it.
    Regardless we fixed it. In summary, I used file class to read through every line of the input file, I used tokens to parse the strings (lines) into separate indices. I was able to use the tokens to determine if it was a circle or rectangle,, and then crawl through the remainder of the properties.
    It was a mess, and after the assignment is turned in, I will post my code for reference.
    Do you now understand how to create objects and see what was wrong with your previous code?
  • 26. Re: Geometric Object Class
    962826 Newbie
    Currently Being Moderated
    Kayaman wrote:
    restless8052 wrote:
    Well I was going about it wrong all along.
    I wouldn't say you were going wrong about it. It doesn't really affect the business logic of a code, whether you get your input from a file or have a person enter it.
    Regardless we fixed it. In summary, I used file class to read through every line of the input file, I used tokens to parse the strings (lines) into separate indices. I was able to use the tokens to determine if it was a circle or rectangle,, and then crawl through the remainder of the properties.
    It was a mess, and after the assignment is turned in, I will post my code for reference.
    Do you now understand how to create objects and see what was wrong with your previous code?
    Yes - true - I know there are probably hundreds of ways to solve a problem using java.

    I understand how to create objects - I have a grasp on that. I would not have figured out the other steps however. This stuff is heavy! I am sure the more I program the easier it becomes but man it is tough!
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points