Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

Faz Ali

Greenhorn
+ Follow
since Dec 02, 2010
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
5
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Faz Ali

Ok, I've confirmed the CLASSPATH environment variable is set to C:\Program Files\QuickTime\QTSystem\QTJava.zip;
I've confirmed this by using "set CLASSPATH" in CMD which prints the following:


C:\Users\FaZ>set CLASSPATH
CLASSPATH=C:\Program Files\QuickTime\QTSystem\QTJava.zip;

C:\Users\FaZ>



For testing purposes, I've put both the Book and Student .java files in the same directory at the path C:\Users\FaZ\Desktop\Code. I've since browsed to this directory via CMD and then run the following command which works absolutely fine:


Regarding putting code into C:\Program Files, this was just for quick testing purposes. I absolutely do not put code in there, I just used that directory with this specific example.

The next thing I've tried is the following, which I expected to fail since I am explicity defining a classpath in my javac invocation which does not contain the current directory:

C:\Users\FaZ\Desktop\Code>javac -cp "C:\Program Files" Student.java
Student.java:5: cannot find symbol
symbol : class Book
location: class Student
Book book = new Book();
^
Student.java:5: cannot find symbol
symbol : class Book
location: class Student
Book book = new Book();
^
2 errors

C:\Users\FaZ\Desktop\Code>



Since I can confirm that I have a CLASSPATH environment variable which is set to something, can I also then confirm that the books statements about java and javac not looking in the current directory is erroneous? - It seems the only time it doesn't look in the current directory is when a classpath is defined in the command line. I'm running Windows 7 with Java version "1.6.0_24" - Java(TM) SE Runtime Environment (build 1.6.0_24-b07) - Java HotSpot(TM) Client VM (build 19.1-b02, mixed mode, sharing) if that makes a difference?
10 years ago
Hi All,

I'm just reviewing chapter 10 of the SCJP Java 6 study guide by Kathy Sierra and Bert Bates in which a paragraph highlights:


A very common situation occurs in which java or javac complains that it can't find a class file, and yet you see that the file is IN the current directory! When searching for class files, the java and javac commands don't search the current directory by default. You must tell them to search there.



Now I tested this, using the following simple classes, both of which are in the same folder (C:\Java Code)



browing to this directory in CMD and then using the command 'javac Student.java && java Student' both classes compiled fine and class Student ran. - Why hasn't it complained that it can't find the Book class?

I have a CLASSPATH environment variable but it is set to the following C:\Program Files\QuickTime\QTSystem\QTJava.zip;
Since I don't have a . present in the classpath environment variable and I haven't used the -classpath or -cp option when compiling and running the Student class, how is it that java and javac were able to find the Book class?
To test this more thoroughly, the next thing I did was delete the CLASSPATH environment variable altogether and retried the java and javac commands which again worked fine.
Lastly, I recreated the CLASSPATH environment variable and set it to C:\Program Files\QuickTime\QTSystem\QTJava.zip;C:\Program Files i.e. adding another directory but not onewhere my source files are. - Compilation and running of class Student worked this time also.
Any explanation would be welcome as the only thing I can think of at the moment is that this paragraph is erroneous? - It seems that these commands always look at the current directory by default irrespective of what is set in the CLASSPATH environment variable. The only time they don't is when you explicitly set a classpath using the java and javac commands via the -classpath/-cp options? I.e. something alongs the lines of the following:

C:\Java Code>javac -cp C:\Program Files Student.java
error: Class names, 'Files', are only accepted if annotation processing is expli
citly requested
Student.java:5: cannot find symbol
symbol : class Book
location: class Student
Book book = new Book();
^
Student.java:5: cannot find symbol
symbol : class Book
location: class Student
Book book = new Book();
^
3 errors[/code]



If someone could clarify the above, I would be very grateful.
10 years ago

Rob Spoor wrote:

Faz Ali wrote:

Rob Spoor wrote:And you forgot to call super.paintComponent(g) in your paintComponent method.



Why may I ask. - I haven't read that far ahead so maybe it's something that will come up but if you could explain it now, that would be great


super.paintComponent(g) makes sure the panel is drawn with a clean slate - all custom drawings will be removed. Currently, if the values in your locations array would change the repainting would only add the new locations, not remove the old ones.



Hmm, that doesn't seem true. If you run the program you will see, it adds a new line whilst simultaneously removing the old line?
10 years ago

Rob Spoor wrote:And you forgot to call super.paintComponent(g) in your paintComponent method.



Why may I ask. - I haven't read that far ahead so maybe it's something that will come up but if you could explain it now, that would be great
10 years ago

Ulf Dittmer wrote:Seems to work fine for me. What, exactly, are you experiencing?



Sometimes the mouse clicks just do not register. I added System.out.println(clickCounter); in the code to detect when they are registered and sometimes I am definitely clicking the panel but it just doesn't register. There's no other way for we to describe what's happening.
10 years ago
Hi all,

I've just had my first dab in Java 2d graphics and have created a quick and rather crude program which draws a line between two points. The problem I am experiencing is that a lot of the mouse clicks just do not register on the JPanel. Any idea how to fix this? My code is below:

10 years ago

Stephan van Hulst wrote:Well, I don't really want to be a party pooper, but my point is that CardList really doesn't add anything that LinkedList doesn't already have. The only difference is that you check for null elements, and you don't allow duplicates. The former is good, but you shouldn't restrict duplicates from being added to your class (some card games, like Canasta, shuffle two or even three standard decks together).

So really, all that your class does is check for null elements. Your design would be a lot cleaner if you just dropped the class completely, and let specific card games keep track of their own LinkedList<Card>, and make them responsible for not adding any null elements to it.

Here's an example of what I mean. Here, the game is responsible for maintaining all the cards:



To be honest with you, when I was creating that class, I was wondering the same thing; I'm not really doing anything thats not already in the LinkedList class and it just feels wrong. What you've highlighted makes perfect sense and I'm already making the changes. Sometimes OO really gets to me because I find it hard when to decide if I need to create a class myself or if I should use an already existing class which should have a limited interface than one offered by another class. - Such is the case with this example whereby a deck in my game will not use every method implemented by linkedlist but therefore I ask myself if I usually ask myself should I still use it.
10 years ago

Stephan van Hulst wrote:Well, the Deque interface really already specifies most of this behaviour. You could just use the LinkedList class, which implements both the List and the Deque interfaces.
What you could do is define a static method in your Card class that returns a LinkedList<Card> containing the standard 52 cards.

If you're really looking to get the exercise, you can declare your CardHolder class to implement java.util.List<Card> and java.util.Deque<Card>, and implement all the methods yourself. You can also extend AbstractList<Card> to get you started.

This way, you will only have one class (CardHolder), and let the card games that use CardHolders (for instance, as decks, discard piles or hands) be responsible for using them correctly.



Thanks for the input Stephan. I have taken it on board and what I've come up with is below. Essentially it is a wrapper around a linkedList collection which implements all the methods a 'card list' minimally needs to have. The card games which use them will be responsible for implementing them and this class can therefore be used as a deck, discard pile, hand etc. Please let me know if this is what you meant:

10 years ago

Stephan van Hulst wrote:Well, why do you need a base class (I'm assuming it's Cardholder?) and what functionality are your other classes going to add to it? It's not very clear from your example.

Can you explain what CardHolder does that wouldn't be available in a List<PlayingCard>?



It's best if I explain the problem with the classes I have written (without any use of interfaces, abstract classes, inheritance etc)

First, I have a regular deck which is instantiated with 52 standard deck, adds cards to the end of the deck, deals cards from the top of the deck, can be shuffled and lastly returns its size:



Next up we have a shreddedDeck which is very similar to a PlayingDeck with some slight variations. It adds cards to the beginning of the list and removes from the end of the list. The exact opposite of the PlayingDeck. Also, it cannot be shuffled. It shares several method names with PlayingDeck but only has the same implementation for getSize(). Also it has an extra method which returns the faceCard without removing it from the deck:



Lastly I have a hand class which is also similar to the PlayingDeck and ShreddedDeck classes but with slight variations and the addition of methods for sorting and adding cards in certain indices. It has an addCard() and getSize() method which are exactly the same as the PlayingDeck implementations but all other methods are unique:



Looking at these 3 classes, there have some shared method names and some shared implementations. The are also unique methods between the classes but in essense, they are all 'Card Holders'. My aim is to design these classes with OO in mind. I must admit, I am only beginning thinking of OO so any guidance as to what I can do with these 3 classes is appreciated.
By the way, very sorry for the very long post but I thought actual code would better illustrate my dilemma.
10 years ago
I am creating a base class called Deck which will hold cards and will consist of an ArrayList instance variable. Obviously this variable will be private for encapsulation reasons but then how will any class extending the Deck class be able to make changes to methods such as addCard(), removeCard() without access to the ArrayList?
The only thing I can think of is an accessor method to the ArrayList but then that exposes the entire implementation of the class and really makes encapsulation go down the toilet no? - It's the same as making the ArrayList public

The reason I mention this is because I am making a set of interfaces and classes for a card game which will consist of the following:


Some guidance on the best approach will be appreciated, thanks.
10 years ago

Jimmy Clark wrote:



This is a similar design I came up with but the problem is the shuffle() method which is not something that a ShreddedPlayingCardDeck should have therefore should not be a part of its API which it will be when implementing that interface.
Hi All,

I've decided to go over some old code I wrote to make it a lot more OO friendly but am having trouble with two classes in particular which are extremely similar but also quite different:



The problem I have is that PlayingDeck is the standard deck we all know and love when playing card games such as Poker etc. ShreddedDeck is a deck which will be populated with cards which have been discarded from the PlayingDeck. That is why the implementation of the addCard() and removeCard() is different, the constructor is also different between these classes. Nevertheless, these classes are quite similar so should I add/is there scope for say an interface or an abstract class somewhere?
I was thinking about a Deck Interface with the methods add(), remove(), size() but decks are also shuffled but the shreddedDeck() should never be shuffled.
Any advice/input would be welcome.

Mike Simmons wrote:Unlike other Set implementations, TreeSet doesn't actually use equals() at all. It uses compareTo() instead - if compareTo() returns zero, that means the two objects are "equal" as far as the TreeSet is concerned. Right now, both your Lamborghinis have the same age, and compareTo says they are therefore equal to each other, and the TreeSet allows only one of the Lamborghinis to remain in the set as a result.

If you want to test your hashCode() and equals() methods, use a HashSet, not a TreeSet.

If you want to use a TreeSet and still see all three cars, you need to modify compareTo() so that two different Lamborghinis are not equal. Perhaps after you compare the ages, if the ages are equal, don't return 0 (yet) but instead compare something else, like vehicleID.



Very quick reply! tyvm
I didn't write the code to test the hashCode and equals() methods, I just wrote the Car class primarily to test the TreeSet which is the reason for it being so very basic and implementing Comparable. I wish this book (Kathy & Bert SCJP6 study guide) made clear that TreeSet uses compareTo() rather than equals(); it doesn't mention this fact at all
Again, tyvm
10 years ago
The SCJP6 study guide I am using states that "Your good friend the equals() method determines wheter two objects are identical (in which case only one can be in the set)"
The code I've written below is not following the above statement as far as I can see. There must be something obviously wrong with my code but I can't see it. I cannot figure out why the code is not allowing car3 to be added to the set since equality between cars is based solely on the vehicleID which is unique between my 3 instances. I'm pretty sure I've overidden the equals() method correctly yet whenever I run this code, the System.out.println line in the equals() method does not run when the cars are added to the set and only 2 of the 3 are being added.



The output from the above code is:

2
Aston Martin DBS 29086335
Lamborghini LP640 343465734


It should be:

3
Aston Martin DBS 29086335
Lamborghini LP640 343465734
Lamborghini LP640 297853624


Can anyone point me in the right direction?

Thanks
10 years ago

Wouter Oet wrote:Couple of remarks:

Try to "Program against an interface": Set<Card> set = new HashSet<Card>();
Use an enum/boolean for declaring ascending/descending instead of an int.
Some code of the constructor is the same as the setter for cardSuitOrder. Try to eliminate that.

Rename some variables e.g. in compare() you have
int x = 0; //Card c1 suit index
Why not use
int firstCardSuitIndex = 0;

In compare() I would use LinkedHashSet.toArray(T[]) instead of LinkedHashSet.toArray() which makes the casting redundant.
I would create 2 more Comparators, one for Value (with asc/desc) and one for Suit (also with asc/desc) and then delegate the compare method to them.
The compare method contains a lot of duplicate code and the method is long. Try to split it up into methods which can be reused.



Great feedback Wouter, thanks.
I'm implementing all your changes apart from having multiple Comparators. - Having the single one seems logical to me since the code is not too complex and accommodates any values provided via the setters. If I split it up into multiple Comparators based on the diferent sort orders, I would end up with either duplicate code (constructors) or more classes/interfaces to prevent the duplicates code which I think would be overkill for this situation.
10 years ago