• We've upgraded our forums. Please post any issues/requests in this thread.

My First Programming Question (Java)

spy2520

New Member
Joined
Mar 8, 2006
Messages
498 (124.5 / day) 4295.8173263889
Likes
16
Processor Celeron 430 (Conroe-L)
Motherboard ASUS Rampage Formula
Cooling Water, Zalman Reserator XT
Memory Corsair Dominator PC2-8500 DDR2-1000
Video Card(s) Visiontek 2600 PRO
Storage Maxtor Maxline 3, more Maxtors
Display(s) Acer P243w 24"
Case Aspire X-Navigator ATXA9N-BK
Audio Device(s) Mbox
Power Supply Thermaltake Toughpower 750w
Software Protools and a bunch of racing games...
#1
Ok guys, this isnt for school. I'm just bored and and messing with code.

I have a card class, i have a face card class that extends card.

If i have an array of type card that also contains face cards (its a deck of cards), can i access the a parameters that are exclusive to the face card class in the array?
 

spy2520

New Member
Joined
Mar 8, 2006
Messages
498 (124.5 / day) 4295.8173263889
Likes
16
Processor Celeron 430 (Conroe-L)
Motherboard ASUS Rampage Formula
Cooling Water, Zalman Reserator XT
Memory Corsair Dominator PC2-8500 DDR2-1000
Video Card(s) Visiontek 2600 PRO
Storage Maxtor Maxline 3, more Maxtors
Display(s) Acer P243w 24"
Case Aspire X-Navigator ATXA9N-BK
Audio Device(s) Mbox
Power Supply Thermaltake Toughpower 750w
Software Protools and a bunch of racing games...
#2
i just solved my problem another way, but i'd still like to hear different answers.

Just in case mine is more than necessary.
 

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (4605.6666666667 / day) 3960.6685763889
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#3
Someone will be along soon with more Java experience than me.
My suggestion is that you post the code that worked for you, and let everyone give their input as to how you did it and any ideas on improving it.
That way not only will you get suggestions, but other people can peruse the code in case they want to do something similar. :toast:
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
20,902 (6967.3333333333 / day) 3346.0248263889
Likes
9,994
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#4
You have to change the access modifiers on properties in the Face class to "protected" if you want an inherited class to have access to it. "private" will hide it from everything except itself.

@Kreig: It's the same as C#. XD
 

spy2520

New Member
Joined
Mar 8, 2006
Messages
498 (124.5 / day) 4295.8173263889
Likes
16
Processor Celeron 430 (Conroe-L)
Motherboard ASUS Rampage Formula
Cooling Water, Zalman Reserator XT
Memory Corsair Dominator PC2-8500 DDR2-1000
Video Card(s) Visiontek 2600 PRO
Storage Maxtor Maxline 3, more Maxtors
Display(s) Acer P243w 24"
Case Aspire X-Navigator ATXA9N-BK
Audio Device(s) Mbox
Power Supply Thermaltake Toughpower 750w
Software Protools and a bunch of racing games...
#5
You have to change the access modifiers on properties in the Face class to "protected" if you want an inherited class to have access to it. "private" will hide it from everything except itself.

@Kreig: It's the same as C#. XD
I'll post the code in a bit. But what i'm looking at is "public enum faces {king, queen, jack, ace}" and subsequently "public faces face;" as part of FaceCard. Are you saying i should use protected instead of public?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
20,902 (6967.3333333333 / day) 3346.0248263889
Likes
9,994
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#6
Enums are static/const. In order to access an Enumerator, you have to go through the class name. For example, if Faces is in the class "Enumerators", you'd have to access Faces via "Enumerators.Faces". You can access it directly from inside of the Enumerators class. For example, "Enumorators.MyMethod()" could access Faces just by using "Faces".


If your array is accessible, you can access everything that's accessible about it including the items it contains.
 

spy2520

New Member
Joined
Mar 8, 2006
Messages
498 (124.5 / day) 4295.8173263889
Likes
16
Processor Celeron 430 (Conroe-L)
Motherboard ASUS Rampage Formula
Cooling Water, Zalman Reserator XT
Memory Corsair Dominator PC2-8500 DDR2-1000
Video Card(s) Visiontek 2600 PRO
Storage Maxtor Maxline 3, more Maxtors
Display(s) Acer P243w 24"
Case Aspire X-Navigator ATXA9N-BK
Audio Device(s) Mbox
Power Supply Thermaltake Toughpower 750w
Software Protools and a bunch of racing games...
#7
I'm currently using FaceCard.Faces. But if i wanted to do something as simple as print the array, how could i print the face, suit, and value of every card considering that only 16 cards will have a face. It is easy with corresponding toString() methods, but if i wanted to do anything more than print...i just gotta stare at it for another hour and everything will make sense. Thats how it usually works.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
20,902 (6967.3333333333 / day) 3346.0248263889
Likes
9,994
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#8
You should have a class "Card" which defines the value of the card, the suit of the card, and any other attributes. In "Card" override ".toString()" to output the Card how you want it to be displayed.


I think you use this to override it:
Code:
public override string toString()
{
}
 

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (4605.6666666667 / day) 3960.6685763889
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#9
No. Make the "Card" class Protected so that only derived classes (eg. FaceCards) have access to it. Depending on what you are doing, you may want to derive classes for the face cards from the base card class (eg. public class King extends Card) and put the properties and methods in there for the "King" card.

When you are creating classes, each subclass should build upon the presceding base class. So you would want a card class that definds everything for a card, then perhaps a face card class that defines the things common to all face cards, and then a class for each individual face card.
 

spy2520

New Member
Joined
Mar 8, 2006
Messages
498 (124.5 / day) 4295.8173263889
Likes
16
Processor Celeron 430 (Conroe-L)
Motherboard ASUS Rampage Formula
Cooling Water, Zalman Reserator XT
Memory Corsair Dominator PC2-8500 DDR2-1000
Video Card(s) Visiontek 2600 PRO
Storage Maxtor Maxline 3, more Maxtors
Display(s) Acer P243w 24"
Case Aspire X-Navigator ATXA9N-BK
Audio Device(s) Mbox
Power Supply Thermaltake Toughpower 750w
Software Protools and a bunch of racing games...
#10
As far as this program goes, all face cards have the same attributes besides the face. Except the ace of course.
 

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (4605.6666666667 / day) 3960.6685763889
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#11
That's fine, so you have your card base class which has thing common to all cards.
You have a face class which is derived from card, which has what facce cards have in common.
You have a regular card class, and an Ace classs derived from cards which define their specific properties and methods.
 

spy2520

New Member
Joined
Mar 8, 2006
Messages
498 (124.5 / day) 4295.8173263889
Likes
16
Processor Celeron 430 (Conroe-L)
Motherboard ASUS Rampage Formula
Cooling Water, Zalman Reserator XT
Memory Corsair Dominator PC2-8500 DDR2-1000
Video Card(s) Visiontek 2600 PRO
Storage Maxtor Maxline 3, more Maxtors
Display(s) Acer P243w 24"
Case Aspire X-Navigator ATXA9N-BK
Audio Device(s) Mbox
Power Supply Thermaltake Toughpower 750w
Software Protools and a bunch of racing games...
#12
i see. Thats alot of classes. So if done properly, i can declare an array of cards that could hold all these types?

As in a CardDeck class that contains an array of 52 cards.
 

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (4605.6666666667 / day) 3960.6685763889
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#13
You can use any type of collection object (array, List, etc.) to hold the 52 cards.
What game are you thinking about? That can make a difference how many classes you need.
 

spy2520

New Member
Joined
Mar 8, 2006
Messages
498 (124.5 / day) 4295.8173263889
Likes
16
Processor Celeron 430 (Conroe-L)
Motherboard ASUS Rampage Formula
Cooling Water, Zalman Reserator XT
Memory Corsair Dominator PC2-8500 DDR2-1000
Video Card(s) Visiontek 2600 PRO
Storage Maxtor Maxline 3, more Maxtors
Display(s) Acer P243w 24"
Case Aspire X-Navigator ATXA9N-BK
Audio Device(s) Mbox
Power Supply Thermaltake Toughpower 750w
Software Protools and a bunch of racing games...
#14
blackjack for instance. simple as can be.