Two Plus Two Publishing LLC
Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > >

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 11-21-2018, 08:19 PM   #1976
matrat
adept
 
matrat's Avatar
 
Join Date: Jan 2011
Location: Fasting
Posts: 880
Re: Programming homework and newbie help thread

Quote:
Originally Posted by Chips Ahoy View Post
I see. How about if you filter operations which add products. Make an add method for each type you accept. Then treat everything as a product after that.
Ya I suppose I could do something like this. It's not very explicit on how it's to be done. Thanks everyone.
matrat is offline   Reply With Quote
Old 11-22-2018, 08:46 PM   #1977
ChrisV
Carpal \'Tunnel
 
ChrisV's Avatar
 
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 39,825
Re: Programming homework and newbie help thread

That's a horribly written assignment. It's not clear whether you're supposed to write your own Collection, or just wrap one like you've done. Have you covered implementing interfaces yet?

Then there's no good way to subclass GenericOrder in the way that it wants you to do. I guess I'd do what Chips suggested, have a guard clause on your add method which throws an exception if you try to add Products of the wrong types. By the way, "takes an arbitrary number of different classes" flat out does not make sense, I guess he means "objects" not "classes".

One thing that is clear is that you are supposed to write a generic class:

Quote:
Design a generic container called GenericOrder that acts as a collection of an arbitrary number of objects in Products.java. Design a mechanism that gives each instance of the container a unique identifier. Implement as many methods as necessary. You must use Java generics features.
So your class signature is going to need to be:

Quote:
public class GenericOrder<T> {
And I guess you want to wrap a List<T> or something. It's a dumb assignment because you're "designing a collection" that actually just wraps an already existing collection. But I can't believe that he wants you to implement Collection<T> from scratch, that seems too difficult for where you're at.

After that, subclass GenericOrder to ComputerOrder, override any method which adds to the collection, and throw an IllegalArgumentException if the object(s) aren't instances of one of the allowed classes, then call super.whateverMethod().

Last edited by ChrisV; 11-22-2018 at 08:55 PM.
ChrisV is offline   Reply With Quote
Old 11-23-2018, 08:46 PM   #1978
matrat
adept
 
matrat's Avatar
 
Join Date: Jan 2011
Location: Fasting
Posts: 880
Re: Programming homework and newbie help thread

Ya overall I really dislike the class. It's offered at an online university and I'm taking it as a transfer credit so I don't need to take the B&M version at my school. The entire instruction is just getting assigned dozens of pages to read from the textbook for each unit with no additional video lectures or notes/slides to go over the most important sections. Lots of stuff in the textbook readings is just entirely not necessary for the course and is just too much to absorb. The first assignment I did I also thought had poorly written instructions, but as a saving grace the grading doesn't seem to be overly strict.

Anyways, ya there's no way I'm supposed to design a collection from scratch like you were possibly saying, it's just really poorly worded. This is an intro to Java course for people who only have a couple of comp sci classes under their belt, and this is the second assignment.

As for doing this: public class GenericOrder<T> {, I see that everywhere online when looking this stuff up, but the textbook only have examples similar to what I've done below with using <Product>. What I have so far is compiling at least, not sure if it actually works yet tho. Still need to add the illegal argument exception.

matrat is offline   Reply With Quote
Old 11-24-2018, 03:52 AM   #1979
ChrisV
Carpal \'Tunnel
 
ChrisV's Avatar
 
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 39,825
Re: Programming homework and newbie help thread

I mean the assignment definitely states that the class you write is supposed to be generic, which yours isn't, but with how poorly the rest of the assignment is worded, who knows. There's actually no point making it a generic class, because you're always using it for Products, so you could easily be correct that writing a generic class is not intended. I'd check with your tutor if possible.

Couple other comments:

Your method for making a unique identifier is no good, most obviously because it resets to 1 any time the program restarts. Using static variables for global state is generally frowned on(*), it has other, more technical issues. For example it will cause problems if an application ever has to be load-shared across more than one server (when each server will have its own copy of the static variable) and it can cause thread-safety issues. The usual way of producing unique identifiers from nowhere is to use GUIDs, or java.util.UUID in Java. New one up in the constructor.

Secondly, the way you've subclassed ComputerOrder is insufficient, because I can still do this:

Quote:
ComputerOrder order = new ComputerOrder();
Cheese deliciousCheese = new Cheese();
order.add(deliciousCheese);
The general version of add() is still available from the superclass. Rather than creating new methods for each subtype allowed in ComputerOrder, override the original add method and throw an IllegalArgumentException if the Product is not of an allowed type. That's not how we'd normally do something like that, by the way, because it's setting traps for users of the class, but it's forced on us by the assignment.

* If you're wondering "well what the hell do I use for global state, if I don't use static variables?" then the answer is: 1. Avoid having global state whenever possible and 2. if it's unavoidable, use a singleton. This is in a footnote because it's advanced and not very relevant, just thought I'd put it in in case you were wondering.
ChrisV is offline   Reply With Quote
Old 11-24-2018, 04:01 AM   #1980
ChrisV
Carpal \'Tunnel
 
ChrisV's Avatar
 
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 39,825
Re: Programming homework and newbie help thread

Just a bizarre assignment though, like the assignment is to create a subclass, but instead of being a nice neat demonstration of polymorphism, you have to do explicit type-checking in the method and throw exceptions if someone tries to do something naughty with a ComputerOrder. It's not at all a good demonstration of OO programming.
ChrisV is offline   Reply With Quote
Old 11-24-2018, 10:20 AM   #1981
matrat
adept
 
matrat's Avatar
 
Join Date: Jan 2011
Location: Fasting
Posts: 880
Re: Programming homework and newbie help thread

Ok I have this now and it works.

matrat is offline   Reply With Quote
Old 11-24-2018, 08:03 PM   #1982
ChrisV
Carpal \'Tunnel
 
ChrisV's Avatar
 
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 39,825
Re: Programming homework and newbie help thread

Yup looks good. Going to be amused if the teacher tells you you weren't supposed to do it that way.

Couple pieces of pedantic nittery: It's considered good practice to annotate overridden methods with @Override, see When do you use Java's @Override annotation and why? Also, standard is to use "public void addProduct...". Java's default access level is for stuff to only be accessible within the same package. Use public whenever you have no reason to want to restrict access. It doesn't actually matter here but it's just similar to nit stuff in English like capitalizing words properly, makes you look like you know what you're doing.

Edit: While I'm nitting it up, since you're learning OO, slightly better than productArray.add(product) is super.add(product). That way GenericOrder's implementation of add() could be changed in the future and the ComputerOrder class doesn't have to know or care.

Last edited by ChrisV; 11-24-2018 at 08:21 PM.
ChrisV is offline   Reply With Quote
Old 11-24-2018, 11:45 PM   #1983
matrat
adept
 
matrat's Avatar
 
Join Date: Jan 2011
Location: Fasting
Posts: 880
Re: Programming homework and newbie help thread

I'll change that stuff, thanks for the tips.

I miss using python for everything :'(
matrat is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Forum Jump


All times are GMT -4. The time now is 12:22 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright 2008-2017, Two Plus Two Interactive
 
 
Poker Players - Streaming Live Online