Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

11-21-2018 , 08:19 PM
Quote:
Originally Posted by Chips Ahoy
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.
Programming homework and newbie help thread Quote
11-22-2018 , 08:46 PM
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.
Programming homework and newbie help thread Quote
11-23-2018 , 08:46 PM
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.

Programming homework and newbie help thread Quote
11-24-2018 , 03:52 AM
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.
Programming homework and newbie help thread Quote
11-24-2018 , 04:01 AM
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.
Programming homework and newbie help thread Quote
11-24-2018 , 10:20 AM
Ok I have this now and it works.

Programming homework and newbie help thread Quote
11-24-2018 , 08:03 PM
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.
Programming homework and newbie help thread Quote
11-24-2018 , 11:45 PM
I'll change that stuff, thanks for the tips.

I miss using python for everything :'(
Programming homework and newbie help thread Quote
05-11-2019 , 11:27 AM
Hi guys, im a bit stuck on this project. I can answer 1 and 3 but im a little stumped by 2. I think the answer could be either group age or name, but am unsure how to arrive at the proper conclusion

CRAP, how do I insert pictures from my local machine in here?
Programming homework and newbie help thread Quote
05-11-2019 , 12:15 PM
Programming homework and newbie help thread Quote
05-11-2019 , 12:15 PM
Quote:
Originally Posted by de4df1sh
CRAP, how do I insert pictures from my local machine in here?
Upload them to an online image host, such as https://imgur.com, then copypaste their provided bbcode in your post here (or use the direct link in an image tag if you can).

edit: I see you got it
Programming homework and newbie help thread Quote
05-11-2019 , 12:16 PM
Quote:
Originally Posted by _dave_
Upload them to an online image host, such as https://imgur.com, then copypaste their provided bbcode in your post here (or use the direct link in an image tag if you can).

edit: I see you got it
Hah we were seconds apart!
Programming homework and newbie help thread Quote
05-11-2019 , 12:37 PM
I think it has to be "name".
Programming homework and newbie help thread Quote
05-11-2019 , 12:38 PM
So I think that the name would be the primary key because the group is dependant on that. Is that right?
Programming homework and newbie help thread Quote
05-11-2019 , 12:39 PM
Quote:
Originally Posted by _dave_
I think it has to be "name".
hah we did it again
Programming homework and newbie help thread Quote
05-11-2019 , 12:46 PM
Yes, for each name the group is distinct - which makes sense, "Susan" is only "6 years old" in any row, and there can not be more than one "Susan" according to the instructions. You can't use "age group" as a primary key since it has multiple records associated with it - 6 has Susan and Carrie.

What are your answers to 1 and 3?
Programming homework and newbie help thread Quote
05-11-2019 , 12:52 PM
Quote:
Originally Posted by _dave_
Yes, for each name the group is distinct - which makes sense, "Susan" is only "6 years old" in any row, and there can not be more than one "Susan" according to the instructions. You can't use "age group" as a primary key since it has multiple records associated with it - 6 has Susan and Carrie.

What are your answers to 1 and 3?
I'll condense it down as I have a tendency to fluff things out for appearence purposes.

1. This is at least 1NF because all of the values are atomic, our columns are unique. Even though we have some repeating values this still qualifies.

3. The problem here is that Jaqueline shares value with others such as age and group. We cannot trace the visual basic game back because it could be either susan or charlotte
Programming homework and newbie help thread Quote
05-11-2019 , 01:13 PM
Actually, I think it probably wants you to use a composite primary key of (name, game). This pairing is unique.
Programming homework and newbie help thread Quote
05-11-2019 , 09:53 PM
Quote:
Originally Posted by de4df1sh
I'll condense it down as I have a tendency to fluff things out for appearence purposes.

1. This is at least 1NF because all of the values are atomic, our columns are unique. Even though we have some repeating values this still qualifies.
That's correct.

2. This is a poor question, but the answer is { GIRL, GAME } as that's the smallest number of columns which is unique for each row. The reason it sucks as a question is that "assume the tuples shown are the only possible tuples for all time" is an unrealistic assumption and not how you'd normally go about choosing a key. This choice of key will fall apart if a product gets introduced in the Games category which is called "Mirror", for example, then the key would need to be { GIRL, GAME, CATEGORY }. In a properly normalized database { GAME, CATEGORY } would be off in a sub-table anyway.

Quote:
Originally Posted by de4df1sh
3. The problem here is that Jaqueline shares value with others such as age and group. We cannot trace the visual basic game back because it could be either susan or charlotte
I'm not sure what you're trying to say here. Here's a definition for "deletion anomaly":

Quote:
Occurs when a record is deleted and this results in the loss of other data that only occurs in that record.
Defining "other data" here is a bit tricky because it's not entirely clear to me what this table even represents. Is it a record of sale? Let's assume it is. In that case the loss of "Jacqueline", "Visual Basic" and "Prog. Languages" are all deletion anomalies. The aim of deleting that tuple is to remove the record of sale, but a side-effect is that we lose the information of the existence of the three things above. The price is also unique information lost, but it's not a deletion anomaly because my understanding is that's the data we're intending to delete.

You're not wrong to be confused by those questions, they're not well constructed.

Last edited by ChrisV; 05-11-2019 at 10:09 PM.
Programming homework and newbie help thread Quote
05-12-2019 , 02:54 AM
Quote:
Originally Posted by ChrisV
That's correct.

2. This is a poor question, but the answer is { GIRL, GAME } as that's the smallest number of columns which is unique for each row. The reason it sucks as a question is that "assume the tuples shown are the only possible tuples for all time" is an unrealistic assumption and not how you'd normally go about choosing a key. This choice of key will fall apart if a product gets introduced in the Games category which is called "Mirror", for example, then the key would need to be { GIRL, GAME, CATEGORY }. In a properly normalized database { GAME, CATEGORY } would be off in a sub-table anyway.



I'm not sure what you're trying to say here. Here's a definition for "deletion anomaly":



Defining "other data" here is a bit tricky because it's not entirely clear to me what this table even represents. Is it a record of sale? Let's assume it is. In that case the loss of "Jacqueline", "Visual Basic" and "Prog. Languages" are all deletion anomalies. The aim of deleting that tuple is to remove the record of sale, but a side-effect is that we lose the information of the existence of the three things above. The price is also unique information lost, but it's not a deletion anomaly because my understanding is that's the data we're intending to delete.

You're not wrong to be confused by those questions, they're not well constructed.
Thanks, Chris I appreciate your input. Fortunately, I have until Sunday 12a to submit the answers. Since I'm transferring out I'm happy to bring up any and all critical feedback.
Programming homework and newbie help thread Quote

      
m