Roberta Fine

Greenhorn
+ Follow
since Apr 23, 2013
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Roberta Fine

This is a lot of very helpful information. I am going to look it over in detail and practice making some card decks with some different data structures.
I'll post again soon with some questions that come from doing that, because you never know until you do it.... Cheers!...
10 years ago


I hope you mean a 'vector' in the generic sense, and not java.util.Vector, which is basically a legacy class. ArrayList is almost always preferable to a Vector.


Yes, I do mean 'vector' in the generic sense, I do realize that java.util.Vector is basically a legacy class.
However, I'm always up for reminders, pointers, so thank you for the tip.
As for ArrayLists, I am very new to them. I just posted an answer to Sheriff Ritchie with edited code,
so I can answer that more easily after I get his response.


I'm also not quite sure why you would need two arrays. A standard playing deck consists of 52/54 cards, each of which is unique (apart from the Jokers, but you could make those "unique" too if you want).


I'm thinking along the lines of this link, http://www.oopweb.com/Java/Documents/ThinkCSJav/Volume/chap11.htm.
It says:
"If we want to define a new object to represent a playing card, it is pretty obvious what the instance variables should be: rank and suit. It is not as obvious what type the instance variables should be. One possibility is Strings, containing things like "Spade" for suits and "Queen" for ranks. One problem with this implementation is that it would not be easy to compare cards to see which had higher rank or suit."
Does this make sense?
~~~~~~~~~~~~~~~~~~~~~~
And the link you posted is great, http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#d5e12291.
It's a bit beyond my expertise right now, however, the only way to learn it is to practice it.
~~~~~~~~~~~~~~~~~~~~~~
Please advise on using integers for the rank and suit values. I'm not exactly sure how an enum would work in this case.
Thanks in advance.


10 years ago

What I showed was a List of Lists of Strings, which is what you asked about initially.



So, I looked at this link:
http://stackoverflow.com/questions/1474954/working-with-a-list-of-lists-in-java
and it showed a different way of declaring a list of lists.
If I tweak the line of code that is in the post from:

to:

and then include the rest of the post's code (although I had to add the datatype of String:

then it works and the output is:
~~~~~~~~~~~~~~~~~~~~~
[[Campbell, Ritchie], [Roberta, Fine]]
~~~~~~~~~~~~~~~~~~~~~
Is this what you mean? I definitely want to learn it correctly, thanks...
10 years ago
Thanks for the info about comments, I'll keep them brief... ; )
And, I know it's vector of vectors rather than 2D, I caught that after I posted, thanks for the reminder.

As for the code you posted, I put it in Eclipse and had to make a couple of tweaks:



~~~~~~~~~~~~~~~~~~~~~~~~

Output is:

[Campbell, Ritchie, Roberta, Fine]

~~~~~~~~~~~~~~~~~~~~~~~~

I'm not sure if I tweaked your code correctly, that's what the compiler wanted.
If I did it right, then the output isn't what I'm aiming for. I'd like no brackets,
and my name under your name. It might be the "addAll" method, however, Eclipse would not take "add."

Also, I am moving toward making a deck of cards. Per the API, http://www-scf.usc.edu/~jkershaw/hw3/Deck.html#createDeck%28%29

createDeck

public java.util.Vector createDeck()

This method is used to create the actual deck of cards to be used in the game. It creates 4 sets of 13 cards to represent every card with suits. then two jokers are added at the end.

Returns:
This method will return a Vector with all the card data stored within it.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Therefore, I would like to make the deck myself using two arrays and then load them into a vector.
Why, because a vector resizes dynamically which is useful when dealing out cards.
Also, the same card won't get dealt twice.
So, would an ArrayList accomplish these same tasks?
Thanks in advance!


10 years ago
As for the last code block, I realize that if I add another string to include element 1,
as in:
String t = (String) ((Vector) outer.get(0)).get(1);
it outputs both A & B.
However, I don't understand how this example is of a multidimensional vector.
10 years ago
Hello, I am familiar with how to add array data to 2D vector in C++. (I do realize Java's Vector is essentially replaced with ArrayList, etc....
I just want to clarify the "mechanics" of how to fill a vector of vectors)

An example of a C++ program is:

CODE BEGIN

#include <iostream>
#include <vector>

using namespace std;

int main()
{
// declare arrays to load into 2D vector
int typeArray[4] = {55,66,77,88};
int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};

// declare int type vector of vectors
vector< vector <int> > myVector(4, vector<int> (14,0));

// assign one array per ROW
for (int i = 0; i < myVector.size(); i++) {

myVector[i][0] = typeArray[i];

for (int j = 1; j < myVector[i].size(); j++) {

myVector[i][j] = valArray[j - 1];

}

}

// print vector to screen with 4 ROWS, 13 COLUMNS

for (int k = 0; k < myVector.size(); k++) {

for (int l = 0; l < myVector[k].size(); l++) {

cout << myVector[k][l] << ' ';
}
cout << '\n';
}

cout << " yes" << endl; // generic addon to indicate end of program on console screen
system("Pause"); // to view output on console screen
return 0;

} // end main
~~~~~~~~~~~~~~~~~~~~~~~~~~
OUTPUT

55 1 2 3 4 5 6 7 8 9 10 10 10 11
66 1 2 3 4 5 6 7 8 9 10 10 10 11
77 1 2 3 4 5 6 7 8 9 10 10 10 11
88 1 2 3 4 5 6 7 8 9 10 10 10 11
yes
Press any key to continue . . .
~~~~~~~~~~~~~~~~~~~~~~~~~~~

A similar starting example in Java would be:



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*****My first question is how do I add row and column definitions within the vector declaration as in Cplusplus?*****

Cplusplus example defined:
// 4 is the length or number of rows; 14 is the width of number of columns; '0' is the value all cells are initialized to
vector< vector <int> > myVector(4, vector<int> (14,0));


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I've tried working with the code in the link below and am not able to output rows and columns.
http://www.java2s.com/Tutorial/Java/0140__Collections/MultidimensionalVectorsVectorsofVectors.htm


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The output is only:

A

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Thanks in advance for the feedback.


10 years ago
Hello Bartenders,

Thank you for the feedback. I like the alternate loop method Winston, I'll keep that in my toolkit.
And James as for Joda-Time and DateUtils classes, I'll look into those next and see how they compare.
As it turns out, because of both the simplicity and complexity (including pitfalls) of these classes, I have learned a LOT about
abstraction, encapsulation, inheritance, getters and setters, and manipulating displays to be "human readable."
And, I'm thinking that's a good thing out here on the ranch....
Thank you again pardners!

10 years ago
This is a follow up post to my other post about the Calendar Class ENUM DayOfWeek question.

***My question would be to ask for feedback from the moderators regarding the value of this class and method in creating calendar related applications***

This is new information and solves (for me) the purpose of outputting the TEXT form of the days of the week using the class DateFormatSymbols, and
the getWeekdays() method in addition to a for loop. Code example at the end of this post.

Per http://docs.oracle.com/javase/7/docs/api/java/text/DateFormatSymbols.html,
"DateFormatSymbols is a public class for encapsulating localizable date-time formatting data, such as the names of the months, the names of the days of the week, and the time zone data. DateFormat and SimpleDateFormat both use DateFormatSymbols to encapsulate this information."

Per http://devmanuals.com/tutorials/java/corejava/DateTimes/DateFormatSymbolsgetWeekdays.html,
"The getWeekdays() method returns a string array of each weekdays' name."

The thing to note is that the CONSTANT FIELD VALUES for the days of the week start with SUNDAY = 1 thru SATURDAY = 7. There is no 0th value.
The String array that holds the text version of the days of the week does have a 0th element, however, it is empty.
Thus, the for loop should start with -1- to begin with SUNDAY.
Here is the code per the link above:



Output is:
Full name of day : Sunday
Full name of day : Monday
Full name of day : Tuesday
Full name of day : Wednesday
Full name of day : Thursday
Full name of day : Friday
Full name of day : Saturday

------------------------------------------------------------

For me, this brings the process of using the Calendar and Date classes along with the related classes for formatting the output of the fields full circle.
10 years ago
K Sheriff, thank you for all of the guidance and feedback, I learned a lot.
Until the next round up, have a good one.
10 years ago
Thank you for pointing out the 1.8 version aspect of my question about the DayOfWeek enum, I would not have known to look for that.
And, I have read many posts discussing the various issues with Date and Calendar classes, so I hear you on "it's about time..."...; )

What I would like to know is if you recommend creating my own ENUM and coding with it rather than getting it per this example:

------------------------------------
This link: http://www.ehow.com/way_5776652_java-1_5-enum-tutorial.html talks about this idea.
They post the following "code" as an example with reasons why it's preferable:



The link basically says:
The above code, besides being cleaner and more readable, addresses problems with Constant Field Values such as other classes ignoring the constants,
and using their own constants.
With the enum, clients are forced to use only the enumerated values themselves, and debuggers will show the enumerated value’s name and not an integer.
In addition, enums have a number of other advantages. For example, they can be used with the new for-each loop feature which was added in Java 1.5.0.
The Calendar class could include a method like this which iterates through all the days and prints out their names.


--------------------

The main goal for me is to have the first day of the week be Monday as 1 through Sunday as 7. The confusion for me is that the ENUM would technically
have the values of 0 - 6 per the index values.

-------------------------------------------
My last question is about "singleton instance." What exactly does that mean in regard to the upcoming DayOfWeek ENUM?
How does it work in relation to the Constant Field Values already in force with DAY_OF_WEEK?
Thanks in advance for your response.
10 years ago
My question is about the the difference between the DayOfWeek ENUM and the DAY_OF_WEEK field in terms of the values they hold.
This is what I have learned so far, and code is below:
-------------------------------------
The abstract class java.util.Calendar has the Field:
public static final int DAY_OF_WEEK Field Number for Get and Set indicating the day of the week.
This Field takes Values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.
The Constant Field Values for each of these days is as follows:

SUNDAY = 1, MONDAY = 2, TUESDAY = 3, WEDNESDAY = 4, THURSDAY =5, FRIDAY =6, and SATURDAY =7.
This info is found on: http://download.java.net/jdk7/archive/b123/docs/api/constant-values.html#java.util.Calendar.DAY_OF_WEEK.
-------------------------------------
Then there is the DayOfWeek ENUM.
java.lang.Object
java.lang.Enum<DayOfWeek>
javax.time.calendar.DayOfWeek

It consists of Seven Constants that describe the days of the week MONDAY through SUNDAY.
The Integer values of the DayOfWeek constants range from MONDAY = 1 through SUNDAY = 7.
This is off by one day backwards from the Constant Field Values noted above.
.
And per the link, using the defined constants (DayOfWeek.FRIDAY) makes your code more readable. ( per http://docs.oracle.com/javase/tutorial/datetime/iso/enum.html)

Also, per http://cr.openjdk.java.net/~sherman/threeten/old/javax/time/calendar/DayOfWeek.html#MONDAY,
The ENUM Constant Detail says this about MONDAY:

public static final DayOfWeek MONDAY

The singleton instance for the day-of-week of Monday. This has the numeric value of 1.

---------------------------------
So, provided you don't manipulate the fields:
If you create an object using the Calendar class, you will actually get an object of the concrete class GregorianCalendar.
If you call the time using getTime() and then the DAY_OF_WEEK using the get method, it will display the current day of the week
with the Constant Field Value int rather than the ENUM numeric value noted above.
So, if it's Monday, the compiler will display a Constant Field int Value of 2.

(Code to follow)
----------------------------------
So, my question is what is the difference between the DayOfWeek ENUM and the DAY_OF_WEEK field in terms of the values they hold?
I realize that the ENUM has singleton instances which I don't really understand, and the DAY_OF_WEEK has Constant Field Values which I basically understand.
Please explain, thanks in advance:



----------------------
OUTPUT:
This is formatted time of OBJECT myCalendar with dot operator and getTime() method:
Fri Oct 04 17:39:54 GMT-08:00 2013

This is built in FIRST DAY OF THE WEEK: 1
This is the text form of the CURRENT DAY OF THE WEEK : Friday
This is the int form of the CURRENT DAY OF THE WEEK: 6
This is the CURRENT DAY OF THE MONTH: 4





10 years ago
I found Jasper de Jong's answer and code above to be a very clear example of the relationships between the java.util.Date, java.util.Calendar and java.util.GregorianCalendar classes.
In my studying of these closely, I found the following:

1. Date Class is mainly for a current timestamp using SimpleDateFormat to manage the time display:
EXAMPLE:
Also, as of JDK 1.1, the Date Class was not amenable to internationalization, and many methods are deprecated.
So, use Calendar Class to convert between dates and time fields.

2. Calendar Class is an abstract encapsulation of OBJECT Date and cannot be instantiated.
GregorianCalendar Class is a CONCRETE IMPLEMENTATION or SUBCLASS of Calendar and is used to create an OBJECT.

3. You can use the getInstance() method of the Calendar Class to return an OBJECT of the GregorianCalendar Class.
EXAMPLE:
OR
You can instantiate an OBJECT of the GregorianCalendar class by using the "new" keyword:
EXAMPLE:
OR


4. An important point is that there are CONSTANT FIELDS already assigned to the Calendar class.
Example is HOUR = 10. ( SEE http://docs.oracle.com/javase/6/docs/api/constant-values.html)
So, to get the CURRENT HOUR, TIME, etc... you must use the Calendar class name with the GET and SET methods.
EXAMPLE:


Otherwise, you will display the value already set as a CONSTANT FIELD.


10 years ago
Thank you for all of the assist and Paul too!
See how a little program can have so many different aspects?
I try to learn as much as I can from the small ones, so when the big ones come,
it will be easier.
Hmmmm....speaking of bigger programs, the next one I'm posting is a lot bigger.
It's an animation in an applet (from the same teacher and class) and it has some
very interesting points.
There's a bug in it too (at least one).
I'll post tomorrow, thanks again, have a great night!
10 years ago
We crossed paths when I corrected my last post....would you please look again?
And, you are really helping me, I do appreciate it!
10 years ago

I understand the condition of the "else" statement.
I could see in the debugger that actionPerformed tested the condition after "numDays" increased to 7,
rather than "numDays" increasing to 7 and then immediately dropping to the "else" condition.
However, no matter where else (ha ha) I put "else" statement, it would output the paint method to the screen after the first day was entered...??

Here is the corrected code:

10 years ago