Sagar Dabas

Ranch Hand
+ Follow
since Nov 15, 2011
Sagar likes ...
PHP C++ Java
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Sagar Dabas

For example :

Now you can use g2d to call the methods of both Graphics and Graphics2D.
But the g can only call methods of Graphics class
9 years ago
I am trying to solve "sum of prime numbers upto N" where N<=1000000 for given number of test cases.

It is just a small modification of this code. Sieve of Eratosthenes.

I tried implementing Sieve of Atkins but that is even more slower than the above one for finding sum (Or may be I am doing something wrong).
I can see there are many unnecessary iterations done in this code for which I just skipped the body of loop by writing this statement at the beginning of body
For example : when
i =2 and j=6 => i*j = 12
i =3 and j=4 => i*j = 12 // Redundant
i =2 and j=15 => i*j = 30
i =3 and j=10 => i*j = 30 // Redundant
i =5 and j=6 => i*j = 30 // Redundant

But still it is iterating more times than necessary (only the body is not executing). How do I completely remove these redundant iterations or Is there any other way to solve this problem more efficiently ?
This code is exceeding the given time limit. Please help.
9 years ago

Jeff Verdegan wrote:

Ok then, what extending can do which implementing cannot do? Is it just the CODE REUSE ?

Yup. That's it. And that is valuable, when used properly.

Thanks you so much. I got it now.
9 years ago

Jesper de Jong wrote:

Sagar Dabas wrote:Isn't it inheritance is just a waste?

No, it's very useful. And implementing interfaces is also inheritance.

How ? Ok then, what extending can do which implementing cannot do? Is it just the CODE REUSE ?

Thanks, for reply

Seetharaman Venkatasamy wrote:
see the Jeff reply!

9 years ago
I know about IS-A relationship but I think the use of inheritance is just polymorphism when we override some methods.
Code reuse doesn't seem to be a good use of inheritance as we can do that using objects.

And what's "implementing" interfaces, isn't it doing what inheritance should do?
They provide abstraction, polymorphism, surety (it's compiler friendly contract behavior).

Isn't inheritance is just a waste ? ( I am not mature to say that )

Or for just giving an OO view to your application inheritance should be chosen? For example : Tiger is a Cat but not Catable

Please reply..
9 years ago

Winston Gutkowski wrote:

Sagar Dabas wrote:Thanks again Everyone. Keep on posting .

What do you think we do this for...our health?


No..... but for our health I know that you are a good person like Jeff or Matthew or Campbell or anyone on this forum.
9 years ago

Winston Gutkowski wrote:

Sagar Dabas wrote:In the question it was not given that we have to use classes . I made this decision.

Good decision. Java is an Object-Oriented langauge, so solutions usually work best when they're based on classes.

However, I agree with Matthew that what you've actually created are DTOs, rather than real-life Objects.

Take, for example, a Subscriber: In the real world there's likely to be a bunch of actual business logic related to something called a 'Subscriber'. For example:
1. Accounting.
2. A newsletter, sent on a regular basis.
3. Offers.
4. Preferences.
5. The things s/he "subscribes" to.
And none of your code does anything to tackle or regulate that stuff.

So, for what you needed, it sounds like you've come up with a decent solution (and well done for that); but DON'T expect that that's where it sirree Bob.

One other point for you: resist the urge to simply bang out getters and setters for everything in your classes. It may seem like it gives you a lot of flexibility, but in fact it works against encapsulation - which is one of the cornerstones of OO - and tends to lead to procedural code. Personally, I blame the "Bean" mentality for them.

As a first stage, I'd say: get rid of your setters...or at the very least the ones you don't actually need. Setters (especially public ones) are inherently dangerous because they give anyone the right to change your object - not a good thing in our security-conscious post-911 world. I suspect you'll also find that many of your getters don't actually need to be public either.

For further reading, you might want to have a look at this article. Some of it may be a bit over your head at the moment, but the message is important:
1. Write code that reflects business requirements.
2. Don't write code for anything other than (1), even if it means foregoing "catch-all" solutions that look nice on the surface.
In my experience, it's the second of those that marks the master programmer from the merely adequate.

Another thing to think about: the best programmers I know spend most of their time off the computer. It's tough when you're learning and you're racing to get things done, but coding is actually a very small part of the programming life. Understanding is what's important.

HIH, and again, well done for what you got so far.


I think I should bookmark this thread.

EDIT : Hmmm...

Thanks again Everyone. Keep on posting .
9 years ago

Jeff Verdegan wrote:

Just by having some classes that are nothing but data holders? No, that by itself is not a violation of OO and not inherently a wrong approach. But it's also impossible to say it's a correct approach. More context is needed for that, but most likely, for the scope and scale of your program, it could be reasonable, but it may depend on whether your instructor has set certain more specific requirements.

As for "we should not write classes without functionalities," that's only partly true, and, again, more context is needed.

One common mistake for beginners in OO0--whether they're brand new to programming or have programmed in other idioms previously--is that they tend to make a lot of classes that do nothing but hold data, and then a very small number of classes that do nothing but operate on the data in the other classes--often all inside one big main() or a big main() and a few other big, static methods in that class. This is what the "don't write a class without behavior" advice is about.

It's not that we should never have a class without behavior. Rather, if we find that most of our classes are just data holders, and most of the work is a separate "functional" piece of code that just operates on these classes' data, then we're not using OO for what it's meant for and good at. Some problems don't necessarily lend themselves well to rigidly sticking to OO principles, especially exercises in beginner level courses. However, whenever you find yourself writing code that gets some data out of an object, and then manipulates or operates on the data, you should be asking yourself, "Would it make more sense for the object that 'owns' this data to be doing this operation?

Thanks for the advice and explanation Jeff. Actually the project was quite big than this and it is just the data for the program.
9 years ago

Matthew Brown wrote:Well, I wouldn't say that all classes must have functionality. I'd personally say that was misleading. I often write classes that are as you describe - for example, Data Transfer Objects (where their only purpose is communication between different layers of an application). I think a more useful way to look at it is "all functionality should be in a class". Specifically, any functionality should be in the most appropriate class. That's the object-oriented approach.

Writing classes without functionality might be an indication that you're not following OO practices. But it might not - it depends what functionality is actually needed. Having it in the wrong place is the problem.

Thanks Matthew Brown. The book must be emphasizing "proper functionality", not "without functionality". Thanks for clearing up my doubt.
9 years ago

Matthew Brown wrote:The question is, do you have any behaviour that is specific to those classes? The code you've shown is pretty simple - none of the classes need to have any behaviour beyond having attributes. So what you've done so far is fine. But what will you do when, for instance, there's some specific logic related to a Group?

Thanks to both of you for replying.

In the question it was not given that we have to use classes . I made this decision.

Actually the problem was just to store the data for subscribers, groups etc.

So I used classes for this purpose. But somewhere I have read that we should not write classes without functionalities ( may be it's an OO principle).

Am I violating the principle or Is this a bad design ? What would you suggest ?

Please reply and thanks again.
9 years ago

Campbell Ritchie wrote:You are right that classes encapsulate both data (attributes) and behaviour (functionality)/.

But I have only getter and setter functions as behaviour (for just accessing the data) and nothing else in those classes.
Do you think I have done it in a wrong way?
I was very proud when I came up to this solution but then I saw this statement : Classes are all about functionalities, you should not write classes without functionality.
9 years ago
I heard : Classes are all about functionality.

But I used classes in my project for only data storage. This was the situation :

Data :
1. Subscriber -- Some attributes.
2. Newsletter -- Some attributes.
3. Group -- Some attributes.
4. Log -- Some attributes.
5. Settings -- Some attributes.

So I made classes for each and provided getter and setter functions. And for saving them I made the ArrayList of the objects of a particular type and serialized the ArrayList object.

This was the code for reading, creating and deleting the files :

By doing this my code was much shorter and flexible . Now I can add any other type if I want, ( for instance like Moderators ) without changing my present code.
I got the benefit of polymorphism too.

Do you think that was a bad decision ? Because my classes are not at all about functionalities.
9 years ago

Maneesh Godbole wrote:Did you follow all the steps mentioned here?
Did you download and configure the GWT SDK?
Did you restart eclipse after doing it?

Yaa I did ...

I installed eclipse for JAVA EE developers , now it's working fine with it.

The import feature (for plugins) in eclipse indigo also didn't work well .... so I had to download each plugin for the new eclipse again.
9 years ago
Hi Friends , It's been two days ..... I cannot get GWT plugin working .

I have installed Eclipse Indigo for Java Developers ( not Java EE ), WTP plugin and GWT plugin for eclipse .

But I can't see Web Application project option.

Please tell me what to do. I am just a beginner.

Do I have to download eclipse for J2EE ? If yes then, do I have to download these plugins again?

Please Please me..

Attached: screenshot of my eclipse.
9 years ago

Campbell Ritchie wrote:
The compiler usually expects the file to be in .class format already; it only seems to compile a new file inside the same package.

Are you saying compiler will not compile files in other package? May be I am not able to understand. Please explain.

But :
These are the lines from Horstmann and Cornell's Core Java :

Compiler first consults all import directives as possible sources for the class. Then compiler goes one step further. It looks at the source files if the source is newer than the class file. If so,the source file is recompiled automatically. Recall that you can import public classes from other packages. A source file can only contain one public class, and the names of the file and public class must match. Therefore, the compiler can easily locate source files for public classes.

9 years ago