Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7866 > unrolled thread
| Started by | bob <bob@coolgroups.com> |
|---|---|
| First post | 2011-09-12 01:23 -0700 |
| Last post | 2011-09-12 19:30 -0400 |
| Articles | 14 — 7 participants |
Back to article view | Back to comp.lang.java.programmer
StatsTable object bob <bob@coolgroups.com> - 2011-09-12 01:23 -0700
Re: StatsTable object Jeff Higgins <jeff@invalid.invalid> - 2011-09-12 10:00 -0400
Re: StatsTable object Robert Klemme <shortcutter@googlemail.com> - 2011-09-12 18:53 +0200
Re: StatsTable object Jeff Higgins <jeff@invalid.invalid> - 2011-09-12 13:09 -0400
Re: StatsTable object Jeff Higgins <jeff@invalid.invalid> - 2011-09-12 13:23 -0400
Re: StatsTable object markspace <-@.> - 2011-09-12 09:03 -0700
Re: StatsTable object Jeff Higgins <jeff@invalid.invalid> - 2011-09-12 12:59 -0400
Re: StatsTable object markspace <-@.> - 2011-09-12 09:58 -0700
Re: StatsTable object Lew <lewbloch@gmail.com> - 2011-09-12 15:30 -0700
Re: StatsTable object Robert Klemme <shortcutter@googlemail.com> - 2011-09-12 18:52 +0200
Re: StatsTable object Knute Johnson <nospam@knutejohnson.com> - 2011-09-12 10:17 -0700
Re: StatsTable object Robert Klemme <shortcutter@googlemail.com> - 2011-09-12 19:19 +0200
Re: StatsTable object Arne Vajhøj <arne@vajhoej.dk> - 2011-09-12 19:28 -0400
Re: StatsTable object Arne Vajhøj <arne@vajhoej.dk> - 2011-09-12 19:30 -0400
| From | bob <bob@coolgroups.com> |
|---|---|
| Date | 2011-09-12 01:23 -0700 |
| Subject | StatsTable object |
| Message-ID | <9ac3f834-0633-434c-86b0-49ed543f3e81@z18g2000yqb.googlegroups.com> |
I'm creating an object that represents a table of sports stats with
the player name as the first column and stat name as first row.
Here's an example:
Touchdowns Yards Rushing Yards Passing
Dan Marino 2 200 55
Chad Henne 8 700 53
Brett Favre 7 300 44
Emmitt Smith 4 400 108
What's the best way to represent this object in Java?
I'm thinking of this:
class StatsTable {
vector<String> strings;
int numColumns;
int numRows;
}
What do you all think of this representation? I think it would be
better if it had a more 2-dimensional feel, but I don't want to
complicate things.
[toc] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2011-09-12 10:00 -0400 |
| Message-ID | <j4l32o$v4v$1@dont-email.me> |
| In reply to | #7866 |
On 09/12/2011 04:23 AM, bob wrote:
> I'm creating an object that represents a table of sports stats with
> the player name as the first column and stat name as first row.
> Here's an example:
>
> Touchdowns Yards Rushing Yards Passing
> Dan Marino 2 200 55
> Chad Henne 8 700 53
> Brett Favre 7 300 44
> Emmitt Smith 4 400 108
>
> What's the best way to represent this object in Java?
>
> I'm thinking of this:
>
> class StatsTable {
> vector<String> strings;
> int numColumns;
> int numRows;
> }
>
> What do you all think of this representation? I think it would be
> better if it had a more 2-dimensional feel, but I don't want to
> complicate things.
import java.util.Vector;
public class Scratch {
public static void main(String[] args) {
StatsTable stats = new StatsTable();
stats.numColumns = 4;
stats.numRows = 5;
stats.strings = new Vector<String>();
stats.strings.add("Player");
stats.strings.add("Touchdowns");
stats.strings.add("Yards Rushing");
stats.strings.add("Yards Passing");
stats.strings.add("Dan Marino");
stats.strings.add("Chad Henne");
stats.strings.add("Brett Favre");
stats.strings.add("Emmitt Smith");
stats.strings.add("2");
stats.strings.add("8");
stats.strings.add("7");
stats.strings.add("4");
stats.strings.add("200");
stats.strings.add("700");
stats.strings.add("300");
stats.strings.add("400");
stats.strings.add("55");
stats.strings.add("53");
stats.strings.add("44");
stats.strings.add("108");
}
static class StatsTable {
Vector<String> strings;
int numColumns;
int numRows;
}
}
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-09-12 18:53 +0200 |
| Message-ID | <9d6roaFb8U2@mid.individual.net> |
| In reply to | #7895 |
On 12.09.2011 16:00, Jeff Higgins wrote:
> On 09/12/2011 04:23 AM, bob wrote:
>> I'm creating an object that represents a table of sports stats with
>> the player name as the first column and stat name as first row.
>> Here's an example:
>>
>> Touchdowns Yards Rushing Yards Passing
>> Dan Marino 2 200 55
>> Chad Henne 8 700 53
>> Brett Favre 7 300 44
>> Emmitt Smith 4 400 108
>>
>> What's the best way to represent this object in Java?
>>
>> I'm thinking of this:
>>
>> class StatsTable {
>> vector<String> strings;
>> int numColumns;
>> int numRows;
>> }
>>
>> What do you all think of this representation? I think it would be
>> better if it had a more 2-dimensional feel, but I don't want to
>> complicate things.
>
> import java.util.Vector;
>
> public class Scratch {
>
> public static void main(String[] args) {
> StatsTable stats = new StatsTable();
> stats.numColumns = 4;
> stats.numRows = 5;
> stats.strings = new Vector<String>();
> stats.strings.add("Player");
> stats.strings.add("Touchdowns");
> stats.strings.add("Yards Rushing");
> stats.strings.add("Yards Passing");
> stats.strings.add("Dan Marino");
> stats.strings.add("Chad Henne");
> stats.strings.add("Brett Favre");
> stats.strings.add("Emmitt Smith");
> stats.strings.add("2");
> stats.strings.add("8");
> stats.strings.add("7");
> stats.strings.add("4");
> stats.strings.add("200");
> stats.strings.add("700");
> stats.strings.add("300");
> stats.strings.add("400");
> stats.strings.add("55");
> stats.strings.add("53");
> stats.strings.add("44");
> stats.strings.add("108");
> }
>
> static class StatsTable {
> Vector<String> strings;
> int numColumns;
> int numRows;
> }
>
> }
>
>
Note that you can also do:
List<String> names = Arrays.asList("Player", "Touchdowns" ,...);
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2011-09-12 13:09 -0400 |
| Message-ID | <j4le4n$fh9$1@dont-email.me> |
| In reply to | #7898 |
On 09/12/2011 12:53 PM, Robert Klemme wrote: > On 12.09.2011 16:00, Jeff Higgins wrote: >> On 09/12/2011 04:23 AM, bob wrote: > > Note that you can also do: > Yep. Point being that the problem was underspecified. I think you are right that bob should specify his public interface and then consider his implementation.
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2011-09-12 13:23 -0400 |
| Message-ID | <j4levi$mad$1@dont-email.me> |
| In reply to | #7903 |
On 09/12/2011 01:09 PM, Jeff Higgins wrote: > On 09/12/2011 12:53 PM, Robert Klemme wrote: >> On 12.09.2011 16:00, Jeff Higgins wrote: >>> On 09/12/2011 04:23 AM, bob wrote: > >> >> Note that you can also do: >> > Yep. Point being that the problem was underspecified. > I think you are right that bob should specify his > public interface and then consider his implementation. > I use too few words. The point of my post was to elicit a response from the OP such like: No. I meant to add Strings like this to the <vector>: "Dan Marino,2,200,55". And then we could continue the discussion with more information.
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-09-12 09:03 -0700 |
| Message-ID | <j4lah6$lik$1@dont-email.me> |
| In reply to | #7866 |
On 9/12/2011 1:23 AM, bob wrote:
> What do you all think of this representation? I think it would be
> better if it had a more 2-dimensional feel, but I don't want to
> complicate things.
I don't like it. I think it would be better to represent the rows as
classes, and then compose the object as a collection of those objects.
I know this sounds like a bit of extra work, but in the long run it's
probably better.
For example, if you wanted to use a JTable, which is a table for GUI
display, having the actual types as ints, double, etc. instead of
strings would be a big advantage because the default display/editing
will use those types and "do the right thing" in many cases.
Something like:
public class PlayerStats {
String name;
int touchdowns;
double yardsRushing;
double yardsPassing;
... getters/setters...
public int getPropertyCount() { return 4; }
public int getPropertyNames() {
return new String[] { "Name","Touchdows","Yards Rushing",
"Yards Passing", };
}
public class StatsTable {
ArrayList<PlayerStats> playerStats;
... don't need "rows" use playerStats.getSize()
... columns: use getPropertyCount()...
}
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2011-09-12 12:59 -0400 |
| Message-ID | <j4ldhp$bg7$1@dont-email.me> |
| In reply to | #7896 |
On 09/12/2011 12:03 PM, markspace wrote:
> On 9/12/2011 1:23 AM, bob wrote:
>
>> What do you all think of this representation? I think it would be
>> better if it had a more 2-dimensional feel, but I don't want to
>> complicate things.
>
>
> I don't like it. I think it would be better to represent the rows as
> classes, and then compose the object as a collection of those objects. I
In some case it might be <better> to represent the
table as a collection of column class instances.
> know this sounds like a bit of extra work, but in the long run it's
> probably better.
>
> For example, if you wanted to use a JTable, which is a table for GUI
> display, having the actual types as ints, double, etc. instead of
> strings would be a big advantage because the default display/editing
> will use those types and "do the right thing" in many cases.
>
> Something like:
>
> public class PlayerStats {
> String name;
> int touchdowns;
> double yardsRushing;
> double yardsPassing;
> ... getters/setters...
>
> public int getPropertyCount() { return 4; }
> public int getPropertyNames() {
> return new String[] { "Name","Touchdows","Yards Rushing",
> "Yards Passing", };
> }
>
> public class StatsTable {
> ArrayList<PlayerStats> playerStats;
> ... don't need "rows" use playerStats.getSize()
> ... columns: use getPropertyCount()...
> }
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-09-12 09:58 -0700 |
| Message-ID | <j4ldoe$ak9$1@dont-email.me> |
| In reply to | #7899 |
On 9/12/2011 9:59 AM, Jeff Higgins wrote: > > In some case it might be <better> to represent the > table as a collection of column class instances. > This is a good point, which I'm just going to quote for emphasis. My reply was pretty hasty and off the cuff. If there's better or more specific use cases involved, by all means modify the code to suit you better.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-09-12 15:30 -0700 |
| Message-ID | <77042a11-369e-4a18-a290-5c5a6729927f@glegroupsg2000goo.googlegroups.com> |
| In reply to | #7896 |
markspace wrote:
bob wrote:
>> What do you all think of this representation? I think it would be
>> better if it had a more 2-dimensional feel, but I don't want to
>> complicate things.
>
> I don't like it. I think it would be better to represent the rows as
> classes, and then compose the object as a collection of those objects.
> I know this sounds like a bit of extra work, but in the long run it's
> probably better.
+1
> For example, if you wanted to use a JTable, which is a table for GUI
> display, having the actual types as ints, double, etc. instead of
> strings would be a big advantage because the default display/editing
> will use those types and "do the right thing" in many cases.
>
> Something like:
>
> public class PlayerStats {
> String name;
> int touchdowns;
> double yardsRushing;
> double yardsPassing;
> ... getters/setters...
>
> public int getPropertyCount() { return 4; }
> public int getPropertyNames() {
> return new String[] { "Name","Touchdows","Yards Rushing",
> "Yards Passing", };
> }
>
> public class StatsTable {
> ArrayList<PlayerStats> playerStats;
> ... don't need "rows" use playerStats.getSize()
> ... columns: use getPropertyCount()...
> }
Plus, 'vector' is not a type in the standard API. If it's a custom type, it needs to follow the naming conventions. If you meant 'java.util.Vector', don't use that type.
There is almost no excuse really to use 'java.util.Vector' in new code. It was (largely) supplanted by 'ArrayList' (and the 'Collections.synchronizedList()' or 'synchronizedCollection()' versions thereof) about thirteen YEARS ago.
(The exception is in order to deal with legacy code that contains vestigial 'Vector' references, but that is not the case for the OP.)
Others have mentioned that there might be use cases for using a table-oriented representation, but that seems on the face of it inappropriate for this exercise. I suggest sticking with the object-oriented approach initially unless you can explain why the table oriented approach is better.
Also, I notice the OP's first "row" lacked a label for the player name.
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-09-12 18:52 +0200 |
| Message-ID | <9d6rlrFb8U1@mid.individual.net> |
| In reply to | #7866 |
On 12.09.2011 10:23, bob wrote:
> I'm creating an object that represents a table of sports stats with
> the player name as the first column and stat name as first row.
> Here's an example:
>
> Touchdowns Yards Rushing Yards Passing
> Dan Marino 2 200 55
> Chad Henne 8 700 53
> Brett Favre 7 300 44
> Emmitt Smith 4 400 108
>
> What's the best way to represent this object in Java?
>
> I'm thinking of this:
>
> class StatsTable {
> vector<String> strings;
> int numColumns;
> int numRows;
> }
>
> What do you all think of this representation? I think it would be
> better if it had a more 2-dimensional feel, but I don't want to
> complicate things.
Dunno. I would start with the public interface, i.e. define what you
want to do with it. Only then I would think about representation.
Just one thing: in 2011 I would certainly *not* use Vector. And I'd
make my fields private.
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <nospam@knutejohnson.com> |
|---|---|
| Date | 2011-09-12 10:17 -0700 |
| Message-ID | <j4ler4$k9r$1@dont-email.me> |
| In reply to | #7897 |
On 9/12/2011 9:52 AM, Robert Klemme wrote: > > Just one thing: in 2011 I would certainly *not* use Vector. And I'd make > my fields private. > and final. -- Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-09-12 19:19 +0200 |
| Message-ID | <9d6t8aFhe3U1@mid.individual.net> |
| In reply to | #7904 |
On 12.09.2011 19:17, Knute Johnson wrote: > On 9/12/2011 9:52 AM, Robert Klemme wrote: >> >> Just one thing: in 2011 I would certainly *not* use Vector. And I'd make >> my fields private. >> > > and final. That again depends on the intended usage. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2011-09-12 19:28 -0400 |
| Message-ID | <4e6e95ac$0$309$14726298@news.sunsite.dk> |
| In reply to | #7904 |
On 9/12/2011 1:17 PM, Knute Johnson wrote: > On 9/12/2011 9:52 AM, Robert Klemme wrote: >> Just one thing: in 2011 I would certainly *not* use Vector. And I'd make >> my fields private. > > and final. If there are no setters, then ... Arne
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2011-09-12 19:30 -0400 |
| Message-ID | <4e6e962d$0$309$14726298@news.sunsite.dk> |
| In reply to | #7866 |
On 9/12/2011 4:23 AM, bob wrote:
> I'm creating an object that represents a table of sports stats with
> the player name as the first column and stat name as first row.
> Here's an example:
>
> Touchdowns Yards Rushing Yards Passing
> Dan Marino 2 200 55
> Chad Henne 8 700 53
> Brett Favre 7 300 44
> Emmitt Smith 4 400 108
>
> What's the best way to represent this object in Java?
>
> I'm thinking of this:
>
> class StatsTable {
> vector<String> strings;
> int numColumns;
> int numRows;
> }
>
> What do you all think of this representation? I think it would be
> better if it had a more 2-dimensional feel, but I don't want to
> complicate things.
I agree with several of the other posters:
- Stats class with 4 properties
- StatsTable class with an ArrayList<Stats>
Arne
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web