Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: how to solve this question ? please help me

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy how to solve this question ? please help me

    Create an abstract class called GeometricFigure. Each figure includes a height, a width, a figure type and an area. Include an abstract method to determine the area of the figure. Create two subclasses called Square and Triangle. Create an application that demonstrates creating objects of both subclasses and store them in an array


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: how to solve this question ? please help me

    Which part of this is giving you trouble?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to solve this question ? please help me

    can you give the code for this question ? i just need the code

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to solve this question ? please help me

    Quote Originally Posted by pejagusta View Post
    can you give the code for this question ? i just need the code
    Giving the code would not be the right thing to do, read this instead

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: how to solve this question ? please help me

    Quote Originally Posted by pejagusta View Post
    can you give the code for this question ? i just need the code
    Sure, want to trade homework? I'll do yours if you do mine...
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Member
    Join Date
    May 2013
    Posts
    96
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: how to solve this question ? please help me

    Quote Originally Posted by pejagusta View Post
    can you give the code for this question ? i just need the code
    Sure.

    abstract class GeometricFigure implements MandatoryBackgroundChecks extends AHelpingHand throws Frisbees
    {
         private String WhatToDo = "Do your own homework.";
     
         protected void message()
          {
          System.out.println(WhatToDo);
           }
    }

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to solve this question ? please help me

    i done for this code but i have many error

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,080
    My Mood
    Mellow
    Thanks
    184
    Thanked 653 Times in 640 Posts

    Default Re: how to solve this question ? please help me

    Thanks for the update.

    And how can we help? We can't see your code or your errors. What are you expecting us to do?

  9. #9
    Member
    Join Date
    May 2013
    Posts
    96
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: how to solve this question ? please help me

    Quote Originally Posted by pejagusta View Post
    i done for this code but i have many error
    What do you mean? Did my code not work? Or did you write something and you're having trouble with it? If you post your code (remember to use code tags) and we'll see what we can do.

  10. #10
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to solve this question ? please help me

    class Triangle {
    private int width, height;
    public Triangle(int w, int h) {
    width = w;
    height = h;
    }
    public void setWidth(int w) {
    width = w;
    }
    public void setHeight(int h) {
    height = h;
    }
    }

    class Triangle extends Square {
    public Square(int size) {
    setWidth(size);
    setHeight(size);
    }
    }

    public Square(int size) { (error: invalid method declaration; return type required line 2)

  11. #11
    Member
    Join Date
    May 2013
    Posts
    96
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: how to solve this question ? please help me

    First off, please use code tags for your code.

    Secondly, I'm not sure where you're going with this. It looks like you declared the Triangle class twice.

    Third, the statement public Square(int size) looks like you're defining a method, but there are 2 problems: 1) it's not inside any sort of class and 2) it doesn't have a return type.

    Look at this first block of code you posted:

    class Triangle 
    {
            private int width, height;
     
            public Triangle(int w, int h) 
                    {
                    width = w;
                    height = h;
                    }  //End of constructor method.
     
              public void setWidth(int w)  
                    {
                    width = w;
                     }  //End of setWidth method
     
               public void setHeight(int h) 
                   {
                    height = h;
                    } //End of setHeight method.
    }   //End of class body.

    Do you see how each of those three methods is contained completely within the body of the Triangle class? That's kind of where your error message is coming from, because you didn't do that later on. When you write public Square(int size) , you're not inside any sort of class.

    The other thing is that you're extending the wrong thing according to the parameters of the assignment. You need to declare a class called GeometricFigure and extend THAT. Define GeometricFigure as abstract by preceding the class declaration with the keyword abstract. So like if you wanted your Triangle class to be abstract, instead of class Triangle you would write abstract class Triangle.

    Inside the class body, define variables and methods to match the things that every shape has. Your instructor has even told you exactly what those are: height, width, type, and area. Then use inheritance (the extends keyword) to define two classes called Square and Triangle. DO NOT define height, width, type, and area for those two objects; by extending GeometricFigure, you can use the height, width, type, and area attributes from that class.

    Think of it like this.

     
    public class abstractionDisplay  //Primary program.
    {
     
         public static void main(String[] args)
         {
          Son me = new Son("Steve");
           me.secretFamilyChiliRecipe();
         }
    } //End of main.
     
    abstract class Dad  //Abstract class
    {
        protected String lastName = "Smith";
     
        protected void secretFamilyChiliRecipe()
               {
                System.out.println("Let's whip up some of our famous " +lastName +" family chili!");
               }
    }  //End of abstract class.
     
    class Son extends Dad  //Concrete class.
    {
         private String firstName;
     
          Son(String name)
               {
                this.firstName = name;
                System.out.println("My name is " +this.firstName +" " +this.lastName +" of the " +this.lastName +" family.");
                }
    }  // End of concrete Son class.

    Notice how the object "me" was of type "Son", yet it accessed a method defined in the class "Dad"? Did you notice how I referenced the "lastName" string for the Son class even though I didn't define one in the Son class? That's what inheritance lets you do; you extend a class and you can access its methods and variables as well as the methods and variables within your own class.

    I hope that helps.

  12. #12
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to solve this question ? please help me

    (DO NOT define height, width, type, and area for those two objects; by extending GeometricFigure, you can use the height, width, type, and area attributes from that class) what do you mean? can give examples?

  13. #13
    Member
    Join Date
    May 2013
    Posts
    96
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: how to solve this question ? please help me

    Quote Originally Posted by pejagusta View Post
    (DO NOT define height, width, type, and area for those two objects; by extending GeometricFigure, you can use the height, width, type, and area attributes from that class) what do you mean? can give examples?
    Actually, now that I look at the assignment as given:

    Create an abstract class called GeometricFigure. Each figure includes a height, a width, a figure type and an area. Include an abstract method to determine the area of the figure. Create two subclasses called Square and Triangle. Create an application that demonstrates creating objects of both subclasses and store them in an array

    I'm a little perplexed myself. Height, width, and area are higher level attributes that you could define in an abstract class and then have a concrete class inherit. Squares and triangles both have those. Figure type, though, that definitely seems like something that would have to be unique to the concrete classes (square and triangle). Unless I'm missing something here.

    Do you understand the example I gave earlier? Do you see how I defined a lastName variable and a secretFamilyChiliRecipe() method in the Dad class, then used them in the Son class, but never at any time actually created an instance of the Dad class? I accessed those through the Son class. The abstract class "Dad" was not instantianted and it could not be. That's kinda what you have to do with your project. Create a GeometricFigure class with height, width, and area, plus methods to handle those. Area will have to be computed through a method, but you have some leeway with what you do with height and width. You could declare them as protected and set them directly, or declare them as private and write getter and setter methods for them. Going the route of protected is probably easier.

    Still don't get what your instructor wants with that figure type field though.

  14. The Following User Says Thank You to mstabosz For This Useful Post:

    pejagusta (September 23rd, 2013)

  15. #14
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to solve this question ? please help me

    can u give me the output for my question .

  16. #15
    Member
    Join Date
    May 2013
    Posts
    96
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: how to solve this question ? please help me

    What question?

  17. #16
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to solve this question ? please help me

    Create an abstract class called GeometricFigure. Each figure includes a height, a width, a figure type and an area. Include an abstract method to determine the area of the figure. Create two subclasses called Square and Triangle. Create an application that demonstrates creating objects of both subclasses and store them in an array

    i want the output .

    --- Update ---

    what is your question ?

  18. #17
    Member
    Join Date
    May 2013
    Posts
    96
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: how to solve this question ? please help me

    The output depends on the input. If you wrote this, then gave a height and width of 4, you would get an area of 16 for the square and 8 for the triangle.

Similar Threads

  1. Need Help to Solve The 2 Question...
    By pineman in forum Java Theory & Questions
    Replies: 3
    Last Post: June 1st, 2012, 09:48 AM
  2. Need Help to Solve The 2 Question...
    By pineman in forum Object Oriented Programming
    Replies: 4
    Last Post: June 1st, 2012, 09:43 AM
  3. cant solve the question by my self..
    By spydahmeen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2012, 07:47 AM
  4. please help solve this question
    By thatguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 29th, 2012, 06:53 PM
  5. using while to solve this question !
    By deathpain in forum Java Theory & Questions
    Replies: 2
    Last Post: November 19th, 2011, 03:46 PM