this is like the end of the semester project, so it's a culmination of everything we are supposed to have learned
and things are really starting to make sense now
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
Deleted User 3420
24492 Posts
December 05 2015 19:48 GMT
#13741
this is like the end of the semester project, so it's a culmination of everything we are supposed to have learned and things are really starting to make sense now | ||
supereddie
Netherlands151 Posts
December 05 2015 19:48 GMT
#13742
On December 06 2015 04:39 travis wrote: Now I am getting a warning, is it a legitimate warning, or just because eclipse doesn't understand what is happening I have a "media" class, and it is a super class for my "movies" class, and my "music" class. The music and movies have some stuff in common, and the project requires basic use of inheritance, so I structured it this way. Plus you need to have an arraylist for media, not an arraylist for movies and an arraylist for music. so, in my code I do: Movies newMovie = new Movies(title, copiesAvailable, rating); Database.Media.add(newMovie); as well as Music newMusic = new Music(title, copiesAvailable, artist, songs); Database.Media.add(newMusic); but java is giving me a warning "the method add(object) belongs to the raw type of Arraylist, References to the generic type of arraylist need to be parameterized". Can I just ignore this warning or is there something else I should be doing Try Database.Media.add<Media>(newMusic); Maybe Java doesn't have 'type inference' as C# does. | ||
Deleted User 3420
24492 Posts
December 05 2015 19:52 GMT
#13743
the actual syntax was Database.Media.add((Media(newMusic)); though ![]() | ||
Blitzkrieg0
United States13132 Posts
December 05 2015 19:53 GMT
#13744
On December 06 2015 04:52 travis wrote: Thanks eddie, I needed to type cast it that makes sense the actual syntax was Database.Media.add((Media(newMusic); though ![]() My addition at the bottom of the last page got lost. The proper way to do this... On December 06 2015 04:52 travis wrote: Can I just ignore this warning or is there something else I should be doing When you create the object it needs to be a Media. The ArrayList holds Media. Movie is a Media. Music is a Media. When you get the objects from the ArrayList later polymorphism will know which methods to use. Media newMovie = new Movies(...); Understanding 'is a' and 'has a' relationships is a very important piece of understanding and writing Java. | ||
supereddie
Netherlands151 Posts
December 05 2015 20:04 GMT
#13745
On December 06 2015 04:52 travis wrote: Thanks eddie, I needed to type cast it that makes sense the actual syntax was Database.Media.add((Media(newMusic)); though ![]() Hmmm, not really. That cast should be unnessesary because of type inheritance, like BlitzKrieg said. If you've properly modelled you class hiearchy it should work without casting. Maybe it depends on the way you've declared the ArrayList? | ||
Deleted User 3420
24492 Posts
December 05 2015 20:07 GMT
#13746
| ||
WolfintheSheep
Canada14127 Posts
December 05 2015 20:09 GMT
#13747
On December 06 2015 04:36 Acrofales wrote: Show nested quote + On December 06 2015 04:25 WolfintheSheep wrote: It's still not a bad idea to use setters. Really, the entire point of having getters and setters is to control how values go in and out of the class, so you only have to look in one place if things need to change or if things go wrong. Realistically, for smaller projects, you don't need them because you know roughly what everything is going to do. Eventually, though, you'll come to situations where you're not in control of everything, and the scope of your project extends beyond your initial expectations, so it's good to have the framework in place so you don't have to do major code rewrites every time. (Of course, C# is so much nicer for getters and setters..."public string Word { get; set; }"...) Yeah sure. I still prefer final and setting them directly in the constructor ![]() You'd also better be bloody sure you know at construction time what the value of those variables is, otherwise birthdate will be 0 (or null depending on long or object storage) forever. But if those are requirements, a final variable is great (and by definition doesn't need a setter). Birth date is a great example, because imagine what happens with data entry mistakes, or "faked" data ![]() On December 06 2015 04:43 Blitzkrieg0 wrote: Show nested quote + On December 06 2015 04:39 Acrofales wrote: On December 06 2015 04:32 Blitzkrieg0 wrote: On December 06 2015 04:19 travis wrote: What about using setters in a constructor? For purposes of this specific project I am doing, I won't need to ever set those values once they have been constructed, so I feel like I don't really need to use setters (though getters are needed). Is there a point to using setters in a constructor ? The point of a setter is that another class can't access the private field. The constructor can change the private field in the class because it is part of the class. Using the setter in the constructor doesn't make sense for this reason. Partially, and the debate still rages. But it is often argued that using the setter from within the class is also best, because it better controls the flow. By going to "setFoo" and finding everything that references it, you will know EVERYWHERE that could possibly be fucking with the value of foo. If you allow class methods to set foo without calling setFoo, then it's harder to debug (you now have to find everywhere within the class that sets foo, and everywhere outside the class that calls setFoo). In any other method sure, but in the constructor? Using the setter in the constructor just feels stupid to me. Imagine that you have any kind of validation for that variable. Like, checking that a number is non-negative, for something simple. You could check at every single point where the value is set, or you could have a single validation in the Setter, and call it every time. | ||
Blitzkrieg0
United States13132 Posts
December 05 2015 20:13 GMT
#13748
On December 06 2015 05:09 WolfintheSheep wrote: Show nested quote + On December 06 2015 04:36 Acrofales wrote: On December 06 2015 04:25 WolfintheSheep wrote: It's still not a bad idea to use setters. Really, the entire point of having getters and setters is to control how values go in and out of the class, so you only have to look in one place if things need to change or if things go wrong. Realistically, for smaller projects, you don't need them because you know roughly what everything is going to do. Eventually, though, you'll come to situations where you're not in control of everything, and the scope of your project extends beyond your initial expectations, so it's good to have the framework in place so you don't have to do major code rewrites every time. (Of course, C# is so much nicer for getters and setters..."public string Word { get; set; }"...) Yeah sure. I still prefer final and setting them directly in the constructor ![]() You'd also better be bloody sure you know at construction time what the value of those variables is, otherwise birthdate will be 0 (or null depending on long or object storage) forever. But if those are requirements, a final variable is great (and by definition doesn't need a setter). Birth date is a great example, because imagine what happens with data entry mistakes, or "faked" data ![]() Show nested quote + On December 06 2015 04:43 Blitzkrieg0 wrote: On December 06 2015 04:39 Acrofales wrote: On December 06 2015 04:32 Blitzkrieg0 wrote: On December 06 2015 04:19 travis wrote: What about using setters in a constructor? For purposes of this specific project I am doing, I won't need to ever set those values once they have been constructed, so I feel like I don't really need to use setters (though getters are needed). Is there a point to using setters in a constructor ? The point of a setter is that another class can't access the private field. The constructor can change the private field in the class because it is part of the class. Using the setter in the constructor doesn't make sense for this reason. Partially, and the debate still rages. But it is often argued that using the setter from within the class is also best, because it better controls the flow. By going to "setFoo" and finding everything that references it, you will know EVERYWHERE that could possibly be fucking with the value of foo. If you allow class methods to set foo without calling setFoo, then it's harder to debug (you now have to find everywhere within the class that sets foo, and everywhere outside the class that calls setFoo). In any other method sure, but in the constructor? Using the setter in the constructor just feels stupid to me. Imagine that you have any kind of validation in the setter. Like, checking that a number is non-negative, for something simple. You could check at every single point where the value is set, or you could have a single validation in the Setter, and call it every time. Putting validation code in your setter would be bad practice I think. You should have a validation If it is that simple then it is probably worthwhile, but in general you aren't going to be validating a single value like that but an entire form. | ||
WolfintheSheep
Canada14127 Posts
December 05 2015 20:18 GMT
#13749
On December 06 2015 05:13 Blitzkrieg0 wrote: Show nested quote + On December 06 2015 05:09 WolfintheSheep wrote: On December 06 2015 04:36 Acrofales wrote: On December 06 2015 04:25 WolfintheSheep wrote: It's still not a bad idea to use setters. Really, the entire point of having getters and setters is to control how values go in and out of the class, so you only have to look in one place if things need to change or if things go wrong. Realistically, for smaller projects, you don't need them because you know roughly what everything is going to do. Eventually, though, you'll come to situations where you're not in control of everything, and the scope of your project extends beyond your initial expectations, so it's good to have the framework in place so you don't have to do major code rewrites every time. (Of course, C# is so much nicer for getters and setters..."public string Word { get; set; }"...) Yeah sure. I still prefer final and setting them directly in the constructor ![]() You'd also better be bloody sure you know at construction time what the value of those variables is, otherwise birthdate will be 0 (or null depending on long or object storage) forever. But if those are requirements, a final variable is great (and by definition doesn't need a setter). Birth date is a great example, because imagine what happens with data entry mistakes, or "faked" data ![]() On December 06 2015 04:43 Blitzkrieg0 wrote: On December 06 2015 04:39 Acrofales wrote: On December 06 2015 04:32 Blitzkrieg0 wrote: On December 06 2015 04:19 travis wrote: What about using setters in a constructor? For purposes of this specific project I am doing, I won't need to ever set those values once they have been constructed, so I feel like I don't really need to use setters (though getters are needed). Is there a point to using setters in a constructor ? The point of a setter is that another class can't access the private field. The constructor can change the private field in the class because it is part of the class. Using the setter in the constructor doesn't make sense for this reason. Partially, and the debate still rages. But it is often argued that using the setter from within the class is also best, because it better controls the flow. By going to "setFoo" and finding everything that references it, you will know EVERYWHERE that could possibly be fucking with the value of foo. If you allow class methods to set foo without calling setFoo, then it's harder to debug (you now have to find everywhere within the class that sets foo, and everywhere outside the class that calls setFoo). In any other method sure, but in the constructor? Using the setter in the constructor just feels stupid to me. Imagine that you have any kind of validation in the setter. Like, checking that a number is non-negative, for something simple. You could check at every single point where the value is set, or you could have a single validation in the Setter, and call it every time. Putting validation code in your setter would be bad practice I think. You should have a validation method and a setter method. Not...really? I mean, the whole point of a Setter is encapsulating how the variable is changed. Sure, have a validation method, but what's the point of having a Setter to control the input if you're going to have your logic outside of it? | ||
supereddie
Netherlands151 Posts
December 05 2015 20:19 GMT
#13750
On December 06 2015 05:07 travis wrote: Blitz was right about the mistake I was making I find that odd. Are you sure you've declared the ArrayList properly and extended the Music and Movies class from Media? I just tested the following and I don't get an error:
| ||
Blitzkrieg0
United States13132 Posts
December 05 2015 20:27 GMT
#13751
On December 06 2015 05:19 supereddie wrote: Show nested quote + On December 06 2015 05:07 travis wrote: Blitz was right about the mistake I was making I find that odd. Are you sure you've declared the ArrayList properly and extended the Music and Movies class from Media? I just tested the following and I don't get an error:
It was never an error. It is a warning about bad practice. On December 06 2015 05:18 WolfintheSheep wrote: Show nested quote + On December 06 2015 05:13 Blitzkrieg0 wrote: On December 06 2015 05:09 WolfintheSheep wrote: On December 06 2015 04:36 Acrofales wrote: On December 06 2015 04:25 WolfintheSheep wrote: It's still not a bad idea to use setters. Really, the entire point of having getters and setters is to control how values go in and out of the class, so you only have to look in one place if things need to change or if things go wrong. Realistically, for smaller projects, you don't need them because you know roughly what everything is going to do. Eventually, though, you'll come to situations where you're not in control of everything, and the scope of your project extends beyond your initial expectations, so it's good to have the framework in place so you don't have to do major code rewrites every time. (Of course, C# is so much nicer for getters and setters..."public string Word { get; set; }"...) Yeah sure. I still prefer final and setting them directly in the constructor ![]() You'd also better be bloody sure you know at construction time what the value of those variables is, otherwise birthdate will be 0 (or null depending on long or object storage) forever. But if those are requirements, a final variable is great (and by definition doesn't need a setter). Birth date is a great example, because imagine what happens with data entry mistakes, or "faked" data ![]() On December 06 2015 04:43 Blitzkrieg0 wrote: On December 06 2015 04:39 Acrofales wrote: On December 06 2015 04:32 Blitzkrieg0 wrote: On December 06 2015 04:19 travis wrote: What about using setters in a constructor? For purposes of this specific project I am doing, I won't need to ever set those values once they have been constructed, so I feel like I don't really need to use setters (though getters are needed). Is there a point to using setters in a constructor ? The point of a setter is that another class can't access the private field. The constructor can change the private field in the class because it is part of the class. Using the setter in the constructor doesn't make sense for this reason. Partially, and the debate still rages. But it is often argued that using the setter from within the class is also best, because it better controls the flow. By going to "setFoo" and finding everything that references it, you will know EVERYWHERE that could possibly be fucking with the value of foo. If you allow class methods to set foo without calling setFoo, then it's harder to debug (you now have to find everywhere within the class that sets foo, and everywhere outside the class that calls setFoo). In any other method sure, but in the constructor? Using the setter in the constructor just feels stupid to me. Imagine that you have any kind of validation in the setter. Like, checking that a number is non-negative, for something simple. You could check at every single point where the value is set, or you could have a single validation in the Setter, and call it every time. Putting validation code in your setter would be bad practice I think. You should have a validation method and a setter method. Not...really? I mean, the whole point of a Setter is encapsulating how the variable is changed. Sure, have a validation method, but what's the point of having a Setter to control the input if you're going to have your logic outside of it? public void setExample(Value value) throws Exception { versus public void setExample(Value value) throws Exception { If you have a separate validator class I think it makes more sense to do it the second way so that your Action class or whatever is handling the data can have an instance of the Validator and use it on all the form data. The Bean or whatever is holding the data shouldn't have a validator. I think I increased the scope of this beyond what was being discussed though. Edited my previous post. | ||
supereddie
Netherlands151 Posts
December 05 2015 21:19 GMT
#13752
On December 06 2015 05:27 Blitzkrieg0 wrote: Show nested quote + On December 06 2015 05:19 supereddie wrote: On December 06 2015 05:07 travis wrote: Blitz was right about the mistake I was making I find that odd. Are you sure you've declared the ArrayList properly and extended the Music and Movies class from Media? I just tested the following and I don't get an error:
It was never an error. It is a warning about bad practice. What about it is bad practice? Isn't that one of the reasons why you use inheritance? | ||
Blitzkrieg0
United States13132 Posts
December 05 2015 21:58 GMT
#13753
On December 06 2015 06:19 supereddie wrote: Show nested quote + On December 06 2015 05:27 Blitzkrieg0 wrote: On December 06 2015 05:19 supereddie wrote: On December 06 2015 05:07 travis wrote: Blitz was right about the mistake I was making I find that odd. Are you sure you've declared the ArrayList properly and extended the Music and Movies class from Media? I just tested the following and I don't get an error:
It was never an error. It is a warning about bad practice. What about it is bad practice? Isn't that one of the reasons why you use inheritance? I didn't write java conventions so I can't answer that. I just know why his static analysis tool threw the warning and how to fix it. If I was working with code the way you wrote it I doubt I'd even notice the difference besides Eclipse underling it in yellow text and telling me it's wrong. From a practical point of view you're getting rid of a type cast by changing it yourself. The compiler should be able to do that for you, but it is a low priority warning so it isn't a serious matter. | ||
teamamerica
United States958 Posts
December 06 2015 01:18 GMT
#13754
On December 06 2015 03:26 travis wrote: Thanks guys ![]() followup question, for some of you that have used arraylists a bunch my arraylist holds objects, and the objects have like, 2 ints and 3 strings Is there an easier way to print a string of all the fields (ints and strings in every element) than to make a loop go through each element, and manually add every int and string of each element into one long string and then print the string or is that what I need to do? I was hoping there would be like, a method that would do that for me I saw toString and tried that but it just returns the reference of the element of the arraylist instead of the actual fields of the element of the arraylist so where I want something like "Tim 5 The Matrix Interstellar Joe 10 Independence Day", instead I am getting "mediaRentalManager.Customer@4e50df2emediaRentalManager.Customer@1d81eb93mediaRentalManager.Customer@7291c18f" So, do I need to do this the long way I described? Use Java 8 stream and StringJoiner! Use some new features. Or at least use StringJoiner b/c it saves you from dealing with a trailing or leading delimiter very easily. Also override toString() on your objects movies.stream().map(Movie::toString())collect(Collectors.joining(" "); My thoughts on setter vs getter are use them in your public classes - following java bean pattern useful for integrating with other frameworks (e.g. Spring, Jackson). | ||
Manit0u
Poland17187 Posts
December 06 2015 02:30 GMT
#13755
Assuming all of your classes follow the pattern of 2 ints and 3 strings what you should do is this: 1. Create an abstract class:
2. Make your concrete classes extend it. 3. Simply call getDataAsString() method on each object to get what you want. | ||
iaretehnoob
Sweden741 Posts
December 06 2015 15:45 GMT
#13756
| ||
Deleted User 3420
24492 Posts
December 06 2015 17:00 GMT
#13757
so now I have a new situation I have my movies class, and my music class. they have different fields, they are both subclasses of media class media has the fields that are shared by music and movies they are stored in an arraylist of type media so now I have to make a method that returns the list of media as an organized string but.. I don't really understand this, because for each index of media I don't know if it was a movie, or music and movies need to return ratings like PG or R in the string and music needs to return artists and songs in the string so to be honest I don't even know what's happen if I take a media object, and try to look at Media.artist but it turns out that index is a movie so there is no media.artist. isn't that going to give me some sort of error? do I need to check to make sure each media index is a movie before i look for media artist? how do I even check if a media object is also a music object ? edit: do I use: if (Media instanceof Movie) and is this the way I should be going about this? | ||
Ropid
Germany3557 Posts
December 06 2015 17:05 GMT
#13758
| ||
Deleted User 3420
24492 Posts
December 06 2015 17:06 GMT
#13759
ok I will get to trying to do that and see if I do it right that makes a lot more sense than the ideas that were coming up in my head lol wait... won't that just use the method that is in the media class? and completely ignore the overridden methods in my subclasses? | ||
Blisse
Canada3710 Posts
December 06 2015 17:08 GMT
#13760
This is Java? Your Media object should expose a protected property (String getPrettyName() potentially...). Then your Movie and Music subclasses can override that property specifically for displaying Movie and Music. This is an OO concept called polymorphism you should be familiar with. As long as the Media object was instantiated as a subclass (Movie or Music), then the method invocation will call the subclass's overriden method.
| ||
| ||
![]() StarCraft 2 StarCraft: Brood War Dota 2 Counter-Strike Super Smash Bros Heroes of the Storm Other Games Grubby15610 summit1g5001 FrodaN3926 shahzam267 Pyrionflax193 ArmadaUGS157 Skadoodle81 Trikslyr81 ToD64 ZombieGrub47 nookyyy ![]() JuggernautJason26 rubinoeu7 HTOMario1 Organizations Other Games StarCraft 2 StarCraft: Brood War
StarCraft 2 • StrangeGG StarCraft: Brood War![]() • LUISG ![]() • Hupsaiya ![]() • tFFMrPink ![]() ![]() • IndyKCrew ![]() • AfreecaTV YouTube • sooper7s • intothetv ![]() • Kozan • Migwel ![]() • LaughNgamezSOOP • Laughngamez YouTube Dota 2 League of Legends Other Games |
Korean StarCraft League
PiG Sty Festival
MaxPax vs Classic
Dark vs Maru
SC Evo Complete
[BSL 2025] Weekly
Online Event
Replay Cast
SOOP Global
ByuN vs Zoun
Rogue vs Bunny
PiG Sty Festival
herO vs Rogue
ByuN vs SKillous
Sparkling Tuna Cup
BSL Nation Wars 2
[ Show More ] Online Event
Replay Cast
The PondCast
|
|