• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 19:16
CET 01:16
KST 09:16
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10[ASL20] Finals Preview: Arrival13
Community News
[TLMC] Fall/Winter 2025 Ladder Map Rotation13Weekly Cups (Nov 3-9): Clem Conquers in Canada4SC: Evo Complete - Ranked Ladder OPEN ALPHA8StarCraft, SC2, HotS, WC3, Returning to Blizzcon!45$5,000+ WardiTV 2025 Championship7
StarCraft 2
General
[TLMC] Fall/Winter 2025 Ladder Map Rotation Mech is the composition that needs teleportation t RotterdaM "Serral is the GOAT, and it's not close" RSL Season 3 - RO16 Groups C & D Preview TL.net Map Contest #21: Winners
Tourneys
RSL Revival: Season 3 Sparkling Tuna Cup - Weekly Open Tournament Constellation Cup - Main Event - Stellar Fest Tenacious Turtle Tussle Master Swan Open (Global Bronze-Master 2)
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 500 Fright night Mutation # 499 Chilling Adaptation Mutation # 498 Wheel of Misfortune|Cradle of Death Mutation # 497 Battle Haredened
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ FlaSh on: Biggest Problem With SnOw's Playstyle What happened to TvZ on Retro? SnOw's ASL S20 Finals Review BW General Discussion
Tourneys
[Megathread] Daily Proleagues Small VOD Thread 2.0 [BSL21] RO32 Group D - Sunday 21:00 CET [BSL21] RO32 Group C - Saturday 21:00 CET
Strategy
PvZ map balance Current Meta Simple Questions, Simple Answers How to stay on top of macro?
Other Games
General Games
Path of Exile Clair Obscur - Expedition 33 Should offensive tower rushing be viable in RTS games? Stormgate/Frost Giant Megathread Nintendo Switch Thread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread SPIRED by.ASL Mafia {211640}
Community
General
US Politics Mega-thread About SC2SEA.COM Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread Canadian Politics Mega-thread
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
Movie Discussion! [Manga] One Piece Anime Discussion Thread Korean Music Discussion Series you have seen recently...
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
Dyadica Gospel – a Pulp No…
Hildegard
Coffee x Performance in Espo…
TrAiDoS
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Reality "theory" prov…
perfectspheres
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2145 users

The Big Programming Thread - Page 688

Forum Index > General Forum
Post a Reply
Prev 1 686 687 688 689 690 1032 Next
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
Profile Blog Joined May 2003
24492 Posts
December 05 2015 19:48 GMT
#13741
You guys have no idea how helpful you have been for me lol
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
Profile Joined March 2011
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.
"Do not try to make difficult things possible, but make simple things simple." - David Platt on Software Design
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2015-12-05 19:53:16
December 05 2015 19:52 GMT
#13743
Thanks eddie, I needed to type cast it that makes sense

the actual syntax was Database.Media.add((Media(newMusic)); though
Blitzkrieg0
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2015-12-05 19:59:22
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(...);
Database.Media.add(newMovie);
Media newMusic = new Music(...);
Daatabase.Media.add(newMusic);


Understanding 'is a' and 'has a' relationships is a very important piece of understanding and writing Java.
I'll always be your shadow and veil your eyes from states of ain soph aur.
supereddie
Profile Joined March 2011
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?
"Do not try to make difficult things possible, but make simple things simple." - David Platt on Software Design
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
December 05 2015 20:07 GMT
#13746
Blitz was right about the mistake I was making
WolfintheSheep
Profile Joined June 2011
Canada14127 Posts
Last Edited: 2015-12-05 20:12:08
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 It just better captures what those variables are all about imho. Of course, you have to think about your project first. For instance if you store "age" in a variable, then you'd better have a way to change that age every year. However, if you store "birthdate", you can just compute the age, and birthdate is immutable.

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 . Nothing wrong with final variables, but the requirements are more than just "I know this will only be set once from now until forever", but also "I can guarantee that the first time it's set will be 100% correct". And any time that requires direct user input is absolute terrible for the latter.
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.
Average means I'm better than half of you.
Blitzkrieg0
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2015-12-05 20:20:08
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 It just better captures what those variables are all about imho. Of course, you have to think about your project first. For instance if you store "age" in a variable, then you'd better have a way to change that age every year. However, if you store "birthdate", you can just compute the age, and birthdate is immutable.

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 . Nothing wrong with final variables, but the requirements are more than just "I know this will only be set once from now until forever", but also "I can guarantee that the first time it's set will be 100% correct". And any time that requires direct user input is absolute terrible for the latter.
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 methodclass and a setter method.

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.
I'll always be your shadow and veil your eyes from states of ain soph aur.
WolfintheSheep
Profile Joined June 2011
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 It just better captures what those variables are all about imho. Of course, you have to think about your project first. For instance if you store "age" in a variable, then you'd better have a way to change that age every year. However, if you store "birthdate", you can just compute the age, and birthdate is immutable.

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 . Nothing wrong with final variables, but the requirements are more than just "I know this will only be set once from now until forever", but also "I can guarantee that the first time it's set will be 100% correct". And any time that requires direct user input is absolute terrible for the latter.
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?
Average means I'm better than half of you.
supereddie
Profile Joined March 2011
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:

class A{}
class B extends A{}

...

ArrayList<A> l = new ArrayList<>();
B b = new B();
l.add(b);
"Do not try to make difficult things possible, but make simple things simple." - David Platt on Software Design
Blitzkrieg0
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2015-12-05 20:42:49
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:

class A{}
class B extends A{}

...

ArrayList<A> l = new ArrayList<>();
B b = new B();
l.add(b);


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 It just better captures what those variables are all about imho. Of course, you have to think about your project first. For instance if you store "age" in a variable, then you'd better have a way to change that age every year. However, if you store "birthdate", you can just compute the age, and birthdate is immutable.

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 . Nothing wrong with final variables, but the requirements are more than just "I know this will only be set once from now until forever", but also "I can guarantee that the first time it's set will be 100% correct". And any time that requires direct user input is absolute terrible for the latter.
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 {
// do error checking
this.value = value;
}


versus

public void setExample(Value value) throws Exception {
this.value = value;
}

// some other class

try {
validator.check(form);
Example.setExample(value);
} catch (Exception e) {
// do something
}


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.
I'll always be your shadow and veil your eyes from states of ain soph aur.
supereddie
Profile Joined March 2011
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:

class A{}
class B extends A{}

...

ArrayList<A> l = new ArrayList<>();
B b = new B();
l.add(b);


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?
"Do not try to make difficult things possible, but make simple things simple." - David Platt on Software Design
Blitzkrieg0
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2015-12-05 22:15:20
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:

class A{}
class B extends A{}

...

ArrayList<A> l = new ArrayList<>();
B b = new B();
l.add(b);


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.
I'll always be your shadow and veil your eyes from states of ain soph aur.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
December 06 2015 01:18 GMT
#13754
From what it sounds like it seems the List that travis was using was never parametized. E.g. I assume Database.Media refers to a static arraylist by other posts. It should be declared as supereddie had mentioned, with a type parameter.

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).
RIP GOMTV. RIP PROLEAGUE.
Manit0u
Profile Blog Joined August 2004
Poland17435 Posts
Last Edited: 2015-12-06 02:33:13
December 06 2015 02:30 GMT
#13755
Why not just do it like that?

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:

public abstract class BaseClass {
protected Integer firstInt;
protected Integer secondInt;
protected String firstString;
protected String secondString;
protected String thirdString;

/* getters and setters for the above */

public String getDataAsString() {
return new StringJoiner(" ")
.add(this.firstInt.toString())
.add(this.secondInt.toString())
.add(this.firstString)
.add(this.secondString)
.add(this.thirdString)
.toString();
}
}


2. Make your concrete classes extend it.
3. Simply call getDataAsString() method on each object to get what you want.
Time is precious. Waste it wisely.
iaretehnoob
Profile Joined June 2004
Sweden741 Posts
December 06 2015 15:45 GMT
#13756
Seems like a terrible idea to do that, just to save some typing.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2015-12-06 17:03:55
December 06 2015 17:00 GMT
#13757
okay so back to working on my java project

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
Profile Joined March 2009
Germany3557 Posts
December 06 2015 17:05 GMT
#13758
You declare a method in the media class that you will call to provide that info string you are after when you print your long list of media. In the movie and music classes that are derived from the media class, you will implement that method to make it prepare different info strings.
"My goal is to replace my soul with coffee and become immortal."
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2015-12-06 17:08:38
December 06 2015 17:06 GMT
#13759
AH, nice!
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
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2015-12-06 17:11:30
December 06 2015 17:08 GMT
#13760
ninja'd

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.


class Animal {
public String talk() {
return "Animal";
}
}

class Cat extends Animal {
public String talk() {
return "Meow!";
}
}

class Dog extends Animal {
public String talk() {
return "Woof!";
}
}
There is no one like you in the universe.
Prev 1 686 687 688 689 690 1032 Next
Please log in or register to reply.
Live Events Refresh
Replay Cast
23:00
WardiTV Mondays #59
CranKy Ducklings103
LiquipediaDiscussion
BSL 21
20:00
ProLeague - RO32 Group D
JDConan vs Semih
Dragon vs Dienmax
Tech vs NewOcean
TerrOr vs Artosis
ZZZero.O261
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Nathanias 181
Ketroc 50
StarCraft: Brood War
Artosis 580
ZZZero.O 247
NaDa 30
Light 8
yabsab 6
Dota 2
monkeys_forever230
NeuroSwarm55
League of Legends
JimRising 476
Counter-Strike
fl0m1623
Super Smash Bros
hungrybox564
AZ_Axe126
Heroes of the Storm
Khaldor170
Other Games
summit1g4465
Grubby4110
ToD143
Maynarde114
febbydoto3
Organizations
Other Games
EGCTV863
gamesdonequick703
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 20 non-featured ]
StarCraft 2
• Hupsaiya 77
• RyuSc2 40
• HeavenSC 25
• musti20045 24
• Adnapsc2 15
• Migwel
• AfreecaTV YouTube
• sooper7s
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
StarCraft: Brood War
• HerbMon 4
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota21387
• Ler49
League of Legends
• Doublelift3145
Other Games
• imaqtpie1722
Upcoming Events
Wardi Open
11h 45m
Monday Night Weeklies
16h 45m
Replay Cast
22h 45m
WardiTV Korean Royale
1d 11h
BSL: GosuLeague
1d 20h
The PondCast
2 days
Replay Cast
2 days
RSL Revival
3 days
BSL: GosuLeague
3 days
RSL Revival
4 days
[ Show More ]
WardiTV Korean Royale
4 days
RSL Revival
5 days
WardiTV Korean Royale
5 days
IPSL
5 days
Julia vs Artosis
JDConan vs DragOn
RSL Revival
6 days
Wardi Open
6 days
IPSL
6 days
StRyKeR vs OldBoy
Sziky vs Tarson
Replay Cast
6 days
Liquipedia Results

Completed

Proleague 2025-11-14
Stellar Fest: Constellation Cup
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
SOOP Univ League 2025
YSL S2
BSL Season 21
CSCL: Masked Kings S3
SLON Tour Season 2
RSL Revival: Season 3
META Madness #9
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.