• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

My First Programming Question (Java)

Joined
Mar 8, 2006
Messages
498 (0.08/day)
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...
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?
 
Joined
Mar 8, 2006
Messages
498 (0.08/day)
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...
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
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
Location
Cheeseland (Wisconsin, USA)
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
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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
 
Joined
Mar 8, 2006
Messages
498 (0.08/day)
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...
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
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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.
 
Joined
Mar 8, 2006
Messages
498 (0.08/day)
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...
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
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
Location
Cheeseland (Wisconsin, USA)
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.
 
Joined
Mar 8, 2006
Messages
498 (0.08/day)
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...
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
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
Location
Cheeseland (Wisconsin, USA)
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.
 
Joined
Mar 8, 2006
Messages
498 (0.08/day)
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...
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
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
Location
Cheeseland (Wisconsin, USA)
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.
 
Joined
Mar 8, 2006
Messages
498 (0.08/day)
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...
blackjack for instance. simple as can be.
 
Top