• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 04:36
CEST 10:36
KST 17:36
  • 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
TL.net Map Contest #22 - Voting & Ladder Map Selection1Code S Season 2 (2026) - RO8 Preview4[ASL21] Finals Preview: Two Legacies21Code S Season 2 (2026) - RO12 Preview2herO wins GSL Code S Season 1 (2026)7
Community News
StarCraft II 5.0.16 PTR Patch Notes may 26th93Weekly Cups (May 18-25): MaxPax wins doubles0Crank Gathers Season 4: BW vs SC2 Team League4Weekly Cups (May 11-17): Classic wins double1Code S Season 1 (2026) - RO8 Results2
StarCraft 2
General
Changing from 12 to 8 is just asking for StarCraft TL Poll: How do you feel about the 5.0.16 PTR balance changes? StarCraft II 5.0.16 PTR Patch Notes may 26th Weekly Cups (May 11-17): Classic wins double TL.net Map Contest #22 - Voting & Ladder Map Selection
Tourneys
RSL Revival: Season 5 - Qualifiers and Main Event GSL Code S Season 2 (2026) Sparkling Tuna Cup - Weekly Open Tournament Crank Gathers Season 4: BW vs SC2 Team League GSL Code S Season 1 (2026)
Strategy
[G] Having the right mentality to improve
Custom Maps
[D]RTS in all its shapes and glory <3 [A] Nemrods 1/4 players
External Content
Welcome to the External Content forum Mutation # 527 Hell Train The PondCast: SC2 News & Results Mutation # 526 Rubber and Glue
Brood War
General
Soma's ASL Finals Review OGN to release AI-upscaled StarLeague from Feb 24 FlaShFTW vs A.Alm Grudge Match Event BGH Auto Balance -> http://bghmmr.eu/ VPN experiences
Tourneys
[ASL21] Grand Finals [Megathread] Daily Proleagues Escore Tournament StarCraft Season 2 [BSL22] WB Final & LB Semis - Saturday 21:00 CEST
Strategy
Any training maps people recommend? Muta micro map competition [G] Hydra ZvZ: An Introduction Fighting Spirit mining rates
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread ZeroSpace Megathread Path of Exile Dawn of War IV
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
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas TL Mafia Community Thread Five o'clock TL Mafia
Community
General
Russo-Ukrainian War Thread US Politics Mega-thread Things Aren’t Peaceful in Palestine Trading/Investing Thread Dating: How's your luck?
Fan Clubs
The herO Fan Club!
Media & Entertainment
[Manga] One Piece Anime Discussion Thread [Req][Books] Good Fantasy/SciFi books
Sports
2024 - 2026 Football Thread McBoner: A hockey love story TeamLiquid Health and Fitness Initiative For 2023 Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread Facing Challenges in Mobile App Development
TL Community
The Automated Ban List
Blogs
Esportsmanship: How to NOT B…
TrAiDoS
Why RTS gamers make better f…
gosubay
ramps on octagon
StaticNine
ASL S21 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2114 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
Poland17756 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
RSL Revival
07:00
Season 5: Playoffs Day 6
Clem vs RogueLIVE!
herO vs TBD
Tasteless1589
IntoTheiNu 731
Ryung 459
IndyStarCraft 164
3DClanTV 110
Rex101
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Tasteless 1589
Ryung 459
IndyStarCraft 164
Livibee 103
Rex 101
StarCraft: Brood War
firebathero 4141
Larva 360
Horang2 330
JulyZerg 46
Noble 16
Sacsri 13
IntoTheRainbow 13
League of Legends
JimRising 568
Counter-Strike
Stewie2K1240
Other Games
summit1g9414
Mew2King83
amsayoshi35
RuFF_SC230
Organizations
Counter-Strike
PGL232
StarCraft: Brood War
lovetv 24
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 12 non-featured ]
StarCraft 2
• Berry_CruncH127
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Stunt678
Upcoming Events
Maestros of the Game
4h 24m
SHIN vs Nicoract
Rogue vs Gerald
ByuN vs Shameless
Cure vs TriGGeR
OSC
4h 24m
IPSL
7h 24m
Dragon vs Artosis
dxtr13 vs Hawk
Showmatch
7h 24m
Percival vs Lambo
ByuN vs Clem
YoungYakov vs GuMiho
ByuN vs Creator
BSL
10h 24m
Wardi Open
1d 3h
Monday Night Weeklies
1d 7h
Replay Cast
1d 15h
Sparkling Tuna Cup
2 days
WardiTV Spring Champion…
2 days
[ Show More ]
Maestros of the Game
2 days
The PondCast
3 days
Kung Fu Cup
3 days
uThermal 2v2 Circuit
3 days
Maestros of the Game
3 days
Replay Cast
3 days
Replay Cast
4 days
WardiTV Spring Champion…
4 days
Maestros of the Game
4 days
Replay Cast
5 days
uThermal 2v2 Circuit
5 days
Maestros of the Game
5 days
Replay Cast
6 days
Solar vs Classic
uThermal 2v2 Circuit
6 days
GSL
6 days
Liquipedia Results

Completed

Escore Tournament S2: King of Kings
2026 GSL S1
Heroes Pulsing #1

Ongoing

2026 KK StarCraft Pro League
BSL Season 22
IPSL Spring 2026
KCM Race Survival 2026 Season 2
KK 2v2 League Season 1
Acropolis #4
CSCL: Masked Kings S4
YSL S3
SCTL 2026 Spring
WardiTV Spring 2026
Maestros of the Game 2
2026 GSL S2
RSL Revival: Season 5
Murky Cup 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026

Upcoming

BSL 22 Non-Korean Championship
CSLAN 4
Blizzard Classic Cup 2026
Kung Fu Cup 2026 Grand Finals
CranK Gathers Season 4: BW vs SC2 Team League
HSC XXIX
uThermal 2v2 2026 Main Event
Heroes Pulsing #3
Heroes Pulsing #2
Esports World Cup 2026
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
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 © 2026 TLnet. All Rights Reserved.