Mahesh Murugaiyan

Greenhorn
+ Follow
since Jun 25, 2009
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
8
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mahesh Murugaiyan

Thanks Ninad. Thanks Paul, yes, thats makes total sense. Other than enum, interface(constants) are there any other entitiy that is implicitly static ? I will add this to my study notes.
Hello,

This is Question #3 in practice exam 2 in Bates/Sierra's Practice Exam book.
Numinor is a stand alone class with the "Members" enum. How can this enum(which is a non-static member of Numinor) accessed without creating an object for Numinor class?

Hello,

Question(s) on generic typing with polymorphism. The ceritification book says


public void foo(List<?> list) { }
public void foo(List<Object> list) { }


If there IS a difference (and we're not yet saying there is), what is it?

There IS a huge difference. List<?>, which is the wildcard <?> without the keywords extends or super, simply means "any type." So that means any type of List can be assigned to the argument. That could be a List of <Dog>, <Integer>, <JButton>, <Socket>, whatever. And using the wildcard alone, without the keyword super (followed by a type), means that you cannot ADD anything to the list referred to as List<?>.



Q1: If i cant add anything to List<?> what is the point of allowing this?

Consider this example:



Q2: Why im not able to add a Animal object to List<? extends Animal> animals2? doesnt it mean "allow all object that are of type Animal or extends Animal"? it doesnt allow me to add both Animal and Dog.

I have used Generics in Collections since 1.5 for the past 5 yrs or so. Still, this behaviour is puzzling me. Ref materials are not helping too much either. hence the long post!

Thanks in advance.
Got it. I did a map.get(new ..) and the response was null. Did the hashCode, equals and now everything is cool. Thanks!!!
Hello,

In the code given below, i did not write a overridden hashCode(). Then how is it finding the object back properly?


Thanks
Mahesh
Thank you Dennis and Fred!!!
I came across this question while taking the certpal test.

I get a error on the "pant = new int[5]"; which is, "Type mismatch: cannot convert from int[] to int[][]" I dont understand why i am getting this error. pant is being initialized as a single dimensional array. Why the compiler sees it otherways?

Thanks
hello. im preparing for SCJP 1.6 and came across these two questions which confuses me a bit. i tried these out and they work as mentioned in the book, still i need some clarifications:

Question 1:

2. Question 2:

- I read that all numerical literals are int by default. so in the first example, the int 200 is narrowed down to short and then boxed into Short. (is it also because 200 can fit in short easily?)

- then, why cant the 7 which can fit in the short is being accepted as a parameter? while short story = 200; works, why cant an int be passed as a parameter?

Thanks
Mahesh
I agree to all the points of Ritchie. You have to think about re-structuring the logic.

- The reason why your tester class did not ask to continue is, say if i did not pick the guess first time, the guess returns a message say, "Too Low". the while loop in #22 checks if the gameOne.isGameOver is true. because i had the wrong guess, it isn't. so the flow doesn't continue into the loop and the code exits.

GuessingGame.Guess
- Why do you need a loop in the guess?
- dont you think having just one variable currentDiff would take the guesser closer to the number? Comparing the currentDiff with the previous differential may miss lead some time.

- To start with, I have re-structured just the tester class in a way to make it readable and modular. See the tester class below: I have moved the logic in main to a playGame method. To facilitate this you have to change the GuessingGame.guess to return a boolean instead of String. you can simply print the messages in the guess method itself. and work on the Guess logic.




The code above is not a perfect solution yet. Atleast it will get you into the second and further attempts which wasn't working for you before.
11 years ago
When a class containing the main method should be public to run along with the (public)class name should be the name of the .java file, why it isnt the same case for enum? Below code runs just fine?! Should the compiler complain about the name of the enum(CoffeeSize) not being same as name of the .java file (coffeesize.java) ?

file name coffeesize.java

11 years ago
Hi Kumar,

Its interesting to see why removeRange is marked as protected. I will watch this thread to see if some one explains.

For the UnsupportedOperationException, getNextSet() should work properly.
How are you passing the list into this method? is that list obtained using Collections.unmodifiablelist ?

- Mahesh
11 years ago
Hi Brian,

You can add a explicit type cast which will covert the string into a "double" or just do a scanDouble().




- Mahesh
11 years ago

That's not completely true and what actually happens is a good demonstration of why the Calendar class is implemented as a factory. If you look at the source code for the Calendar.getInstance() method you will see it calls the createCalendar method and that decides what type of Calendar subclass to return based on the locale. In other words you can use the same code wherever your program is run and an instance of the appropriate class will be returned



Hello, The calendar.getInstance, if no locale is passed, and if the locale is not Japan, by default returns a Gregorian Calendar. Have a look at the code snippet and its output below:



The above code outputs:

java.util.GregorianCalendar[time=1329329597419,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,
dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,
startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],
firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2012,MONTH=1,WEEK_OF_YEAR=7,WEEK_OF_MONTH=3,DAY_OF_MONTH=15,DAY_OF_YEAR=46,DAY_OF_WEEK=4,
DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=13,SECOND=17,MILLISECOND=419,ZONE_OFFSET=-28800000,DST_OFFSET=0]


The Calendar.getInstance() indeed works as you said. Gets the Locale, and returns a specific subclass implementation based on Locale.

The getInstance() method abstracts all this locale checking and subclass creation from the end user which is the objective of a factory method!

Cheers
Mahesh
Hi Glen,

Calendar, DateFormat, NumberFormat are all Abstract classes.

In Java terms, we cannot instantiate any abstract class. Though they serve specific purpose, they are better when refined further by their sub classes. The subclasses of these classes add more functionality. For example, the Calendar.getInstance would give you a Gregorian Calendar, DateFormat.getInstance would give you a SimpleDateFormat.

Both GregorianCalendar, SimpleDateFormat extends their abstract class, and provide more value add.

About the factory method:

1. A Factory Method is a design pattern. Simply put, a factory method will abstract all the complexities involved in instantiating and initializing a object. In this case, while using Calendar.getInstance, we have no idea about how the object was created and presented back to you! you don't know what are the arguments passed, what default value to set etc.

2. Apart from just hiding the complexities, another cool aspect is, if the same object needs to be created in multiple places, then you can avoid having the initialization code every where. Its more clearer than having a private method constructing the object. By moving this piece of code into its own class, your class can worry about the actual business than about creating/initializing bits and pieces.

Cheers
Mahesh