Results 1 to 4 of 4
  1. #1
    Melodia is offline Member
    Join Date
    Sep 2013
    Posts
    18
    Rep Power
    0

    Default Trouble with polymorphism exercise

    I have a rather simple question about a polymorphism exercise. While it is a practical exercise, this particular question is arguably more theory than code. The exercise is below:

    Write a program that helps you manage products that belong to different
    categories. Each product should have product ID, name, price and quantity
    parameters. In the beginning of the program, populate an ArrayList with several
    different kinds of chairs, tables, dressers, spoons, forks, knives, plates, keyboards,
    TV sets, mobile phones and mp3 players. Some products are taxable, which should
    be implemented using Taxable interface. By default tax rate is 12%. This rate
    applies to all kitchenware. Electronic devices are also taxable, but have a 10% tax
    rate. TV sets are calculated differently, and have a 16% tax rate. All kitchenware
    should have a material parameter. If this parameter is set to “plastic”, there
    should be an additional 5% tax on the product.
    TV sets should have an additional parameter called display size. Mp3 players
    should have an additional parameter called memory size. Dressers should have a
    parameter dimensions, which itself is a class that contains 3 integers.
    All classes that implement Taxable interface should have a taxReturn method, that
    displays how much tax will be paid.


    It's nice that they tell me Taxable has to be an interface, so I don't need to think about that. I'm also pretty certain that Kitchenware and Electronic should be abstract classes. I also think Furniture, which is what everything else comes under, should be an abstract class. All of these abstract classes should extend Product, but only Kitchenware and Electronic should implement Taxable. My question is what should Product be? My guess would be an abstract class, using the "protected" access modifier on the variables, to ensure that they can be inherited.

    An example of how I'm doing this would result in something like this:

    Abstract class product is extended by abstract class Kitchenware, which implements Taxabale. Abstract class Kitchenware is extended by the concrete class Knife.

    So on and do on...

    I'm unsure if this is an acceptable way to do this, and no text I've read on polymorphism has made it particularly clear to me. Am I on the right track or no?

    By the way, if what I'm, asking is unclear to anyone, I can do a UML class diagram on lucidchart which may help clarify things and post the image here.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,959
    Rep Power
    8

    Default Re: Trouble with polymorphism exercise

    This kind of stuff is just as much an art as it is a science. The exercise is more about showing you understand the differences between interfaces, abstract classes, regular classes, child classes, etc. than it is about getting the design exactly right. As long as you can argue for your design in a reasonable way, you'll get a fine grade.

    Asking us how we'd design it is a little like asking us what color you should use in a painting. Everything you've said so far seems reasonable to me though.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Melodia is offline Member
    Join Date
    Sep 2013
    Posts
    18
    Rep Power
    0

    Default Re: Trouble with polymorphism exercise

    I wasn't really asking you to tell me exactly how to design it. I just wasn't sure that the way I had in mind was legitimate. I didn't want to do the whole thing and then find out later on that I need to start all over again. As long as I know I'm not doing anything that's considered bad practice, I'm quite happy. Thank you!

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Trouble with polymorphism exercise

    "
    My guess would be an abstract class, using the "protected" access modifier on the variables, to ensure that they can be inherited.
    "
    That's the only bit that I would quibble.
    The member data of a class should not be directly accesible outside of the class, and that includes child classes.
    Provide protected accessors rather than making the data itself protected.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Compile-time Polymorphism or Run-time Polymorphism ?
    By dejulial in forum New To Java
    Replies: 1
    Last Post: 03-06-2012, 07:14 PM
  2. How to use Polymorphism
    By nfsmwbe in forum New To Java
    Replies: 10
    Last Post: 01-16-2012, 02:15 PM
  3. Having trouble with programming exercise, help?
    By atac57 in forum New To Java
    Replies: 1
    Last Post: 01-12-2012, 06:11 AM
  4. Polymorphism
    By blug in forum New To Java
    Replies: 3
    Last Post: 10-11-2010, 10:35 AM
  5. what is polymorphism
    By Nari in forum New To Java
    Replies: 5
    Last Post: 04-04-2008, 03:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •