• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 10:39
CET 16:39
KST 00:39
  • 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 Revival - 2025 Season Finals Preview8RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12
Community News
$21,000 Rongyi Cup Season 3 announced (Jan 22-Feb 7)11Weekly Cups (Dec 29-Jan 4): Protoss rolls, 2v2 returns6[BSL21] Non-Korean Championship - Starts Jan 103SC2 All-Star Invitational: Jan 17-1822Weekly Cups (Dec 22-28): Classic & MaxPax win, Percival surprises3
StarCraft 2
General
Spontaneous hotkey change zerg Chinese SC2 server to reopen; live all-star event in Hangzhou Weekly Cups (Dec 29-Jan 4): Protoss rolls, 2v2 returns SC2 All-Star Invitational: Jan 17-18 Weekly Cups (Dec 22-28): Classic & MaxPax win, Percival surprises
Tourneys
$21,000 Rongyi Cup Season 3 announced (Jan 22-Feb 7) WardiTV Winter Cup WardiTV Mondays SC2 AI Tournament 2026 OSC Season 13 World Championship
Strategy
Simple Questions Simple Answers
Custom Maps
Map Editor closed ?
External Content
Mutation # 507 Well Trained Mutation # 506 Warp Zone Mutation # 505 Rise From Ashes Mutation # 504 Retribution
Brood War
General
Potential ASL qualifier breakthroughs? BGH Auto Balance -> http://bghmmr.eu/ I would like to say something about StarCraft BW General Discussion StarCraft & BroodWar Campaign Speedrun Quest
Tourneys
[Megathread] Daily Proleagues [BSL21] Grand Finals - Sunday 21:00 CET [BSL21] Non-Korean Championship - Starts Jan 10 SLON Grand Finals – Season 2
Strategy
Game Theory for Starcraft Simple Questions, Simple Answers Current Meta [G] How to get started on ladder as a new Z player
Other Games
General Games
Mechabellum Beyond All Reason Stormgate/Frost Giant Megathread Awesome Games Done Quick 2026! General RTS Discussion 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
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine European Politico-economics QA Mega-thread Russo-Ukrainian War Thread Trading/Investing Thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List TL+ Announced
Blogs
Physical Exercise (HIIT) Bef…
TrAiDoS
Life Update and thoughts.
FuDDx
How do archons sleep?
8882
James Bond movies ranking - pa…
Topin
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2830 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
Poland17588 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
WardiTV Invitational
14:00
Group A
Percival vs RogueLIVE!
Percival vs Classic
ByuN vs Classic
ByuN vs Rogue
Classic vs Rogue
WardiTV1663
TKL 359
IndyStarCraft 215
Rex130
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
TKL 359
IndyStarCraft 215
Rex 130
Railgan 48
SC2Nice 19
StarCraft: Brood War
Britney 42587
Shuttle 2269
Horang2 1727
Mini 1223
Larva 676
Stork 551
ZerO 547
Light 475
Rush 376
Soma 328
[ Show more ]
hero 237
firebathero 169
Zeus 131
Hyun 109
Leta 103
Sharp 103
Last 74
sorry 61
Yoon 49
Mind 39
Free 32
Aegong 31
Movie 25
HiyA 25
Terrorterran 23
Sacsri 13
yabsab 12
Shine 12
GoRush 8
Dota 2
Gorgc6435
qojqva3073
syndereN416
XcaliburYe253
BananaSlamJamma173
ODPixel106
LuMiX1
Counter-Strike
byalli570
Super Smash Bros
Mew2King80
Heroes of the Storm
Khaldor334
Other Games
singsing2419
Liquid`RaSZi1598
B2W.Neo1146
Hui .338
KnowMe113
ZerO(Twitch)25
Organizations
Other Games
EGCTV787
BasetradeTV52
StarCraft: Brood War
Kim Chul Min (afreeca) 13
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 19 non-featured ]
StarCraft 2
• StrangeGG 53
• HeavenSC 24
• Adnapsc2 4
• naamasc21
• LaughNgamezSOOP
• AfreecaTV YouTube
• sooper7s
• intothetv
• Migwel
• Kozan
• IndyKCrew
StarCraft: Brood War
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• C_a_k_e 2599
• lizZardDota2103
• HappyZerGling58
League of Legends
• Nemesis4864
• Jankos3322
Upcoming Events
IPSL
4h 22m
DragOn vs Sziky
Replay Cast
17h 22m
Wardi Open
20h 22m
Monday Night Weeklies
1d 1h
WardiTV Invitational
1d 20h
WardiTV Invitational
2 days
The PondCast
3 days
OSC
3 days
OSC
4 days
All Star Teams
5 days
INnoVation vs soO
sOs vs Scarlett
[ Show More ]
uThermal 2v2 Circuit
5 days
All Star Teams
6 days
MMA vs DongRaeGu
Rogue vs Oliveira
Sparkling Tuna Cup
6 days
OSC
6 days
Liquipedia Results

Completed

Proleague 2026-01-10
Big Gabe Cup #3
META Madness #9

Ongoing

C-Race Season 1
IPSL Winter 2025-26
BSL 21 Non-Korean Championship
OSC Championship Season 13
Underdog Cup #3
NA Kuram Kup
eXTREMESLAND 2025
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025

Upcoming

CSL 2025 WINTER (S19)
Escore Tournament S1: W4
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Rongyi Cup S3
Thunderfire SC2 All-star 2025
Nations Cup 2026
BLAST Open Spring 2026
ESL Pro League Season 23
ESL Pro League Season 23
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
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.