• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 16:48
CET 22:48
KST 06:48
  • 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
ByuL: The Forgotten Master of ZvT28Behind the Blue - Team Liquid History Book19Clem wins HomeStory Cup 289HomeStory Cup 28 - Info & Preview13Rongyi Cup S3 - Preview & Info8
Community News
Weekly Cups (Feb 16-22): MaxPax doubles0Weekly Cups (Feb 9-15): herO doubles up2ACS replaced by "ASL Season Open" - Starts 21/0258LiuLi Cup: 2025 Grand Finals (Feb 10-16)46Weekly Cups (Feb 2-8): Classic, Solar, MaxPax win2
StarCraft 2
General
Terran AddOns placement How do you think the 5.0.15 balance patch (Oct 2025) for StarCraft II has affected the game? Nexon's StarCraft game could be FPS, led by UMS maker ByuL: The Forgotten Master of ZvT Oliveira Would Have Returned If EWC Continued
Tourneys
PIG STY FESTIVAL 7.0! (19 Feb - 1 Mar) SEL Doubles (SC Evo Bimonthly) WardiTV Team League Season 10 RSL Season 4 announced for March-April The Dave Testa Open #11
Strategy
Custom Maps
Publishing has been re-enabled! [Feb 24th 2026] Map Editor closed ?
External Content
Mutation # 514 Ulnar New Year The PondCast: SC2 News & Results Mutation # 513 Attrition Warfare Mutation # 512 Overclocked
Brood War
General
TvZ is the most complete match up BGH Auto Balance -> http://bghmmr.eu/ Soma Explains: JD's Unrelenting Aggro vs FlaSh ACS replaced by "ASL Season Open" - Starts 21/02 BW General Discussion
Tourneys
[Megathread] Daily Proleagues [LIVE] [S:21] ASL Season Open Day 1 ASL Season 21 Qualifiers March 7-8 Small VOD Thread 2.0
Strategy
Soma's 9 hatch build from ASL Game 2 Fighting Spirit mining rates Simple Questions, Simple Answers Zealot bombing is no longer popular?
Other Games
General Games
Nintendo Switch Thread Battle Aces/David Kim RTS Megathread Path of Exile Beyond All Reason New broswer game : STG-World
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
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine UK Politics Mega-thread YouTube Thread Mexico's Drug War
Fan Clubs
The IdrA Fan Club The herO Fan Club!
Media & Entertainment
[Manga] One Piece [Req][Books] Good Fantasy/SciFi books Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion TL MMA Pick'em Pool 2013
World Cup 2022
Tech Support
Laptop capable of using Photoshop Lightroom?
TL Community
The Automated Ban List
Blogs
YOUTUBE VIDEO
XenOsky
Unintentional protectionism…
Uldridge
ASL S21 English Commentary…
namkraft
Inside the Communication of …
TrAiDoS
My 2025 Magic: The Gathering…
DARKING
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2012 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
Poland17674 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
AI Arena Tournament
20:00
RO8
Laughngamez YouTube
DaveTesta Events
18:15
The Dave Testa Open #11
davetesta52
Liquipedia
PSISTORM Gaming Misc
16:55
FSL s10 TeamLeague: ASH vs PTB
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
UpATreeSC 170
gerald23 83
StarCraft: Brood War
Britney 20912
ZZZero.O 39
NaDa 12
Dota 2
LuMiX2
League of Legends
JimRising 170
Super Smash Bros
hungrybox749
Heroes of the Storm
Khaldor279
Other Games
gofns13053
tarik_tv6036
summit1g5496
Grubby3479
Beastyqt802
B2W.Neo729
crisheroes280
mouzStarbuck213
ToD186
Liquid`Hasu159
KnowMe149
ZombieGrub51
trigger12
Organizations
Other Games
gamesdonequick894
Counter-Strike
PGL223
StarCraft 2
angryscii 36
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• printf 87
• Response 6
• IndyKCrew
• sooper7s
• AfreecaTV YouTube
• intothetv
• Kozan
• LaughNgamezSOOP
• Migwel
StarCraft: Brood War
• Pr0nogo 2
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota253
League of Legends
• Doublelift2406
Other Games
• imaqtpie941
• Shiphtur257
Upcoming Events
Replay Cast
2h 12m
PiG Sty Festival
11h 12m
Clem vs Serral
Maru vs ShoWTimE
Sparkling Tuna Cup
12h 12m
uThermal 2v2 Circuit
17h 12m
Replay Cast
1d 11h
Wardi Open
1d 14h
Monday Night Weeklies
1d 19h
Replay Cast
2 days
Replay Cast
3 days
Replay Cast
4 days
[ Show More ]
The PondCast
4 days
KCM Race Survival
4 days
Replay Cast
5 days
Replay Cast
6 days
CranKy Ducklings
6 days
Liquipedia Results

Completed

Proleague 2026-02-26
LiuLi Cup: 2025 Grand Finals
Underdog Cup #3

Ongoing

KCM Race Survival 2026 Season 1
Acropolis #4 - TS5
Jeongseon Sooper Cup
Spring Cup 2026
[S:21] ASL SEASON OPEN 2nd Round
[S:21] ASL SEASON OPEN 2nd Round Qualifier
WardiTV Winter 2026
PiG Sty Festival 7.0
Nations Cup 2026
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 2025

Upcoming

ASL Season 21: Qualifier #1
ASL Season 21: Qualifier #2
ASL Season 21
Acropolis #4 - TS6
Acropolis #4
IPSL Spring 2026
CSLAN 4
HSC XXIX
uThermal 2v2 2026 Main Event
Bellum Gens Elite Stara Zagora 2026
RSL Revival: Season 4
NationLESS Cup
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
FISSURE Playground #3
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
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.