Carlos Obregon

Greenhorn
+ Follow
since May 26, 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
0
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 Carlos Obregon

Jeanne Boyarsky wrote:I'm not worried about the memory



But if you tend to convert every parameter, an attribute you could waste memory. Right, with primitives is not that bad, but ith references is crucial so that the GC can reclaim the memory ASAP. So that would be a good guideline, right?

Jeanne Boyarsky wrote:but I would make that a static method.



That was exactly what I thought, a class with only a method with attributes that are related to that only method, seems like a good fit for a class with one static method. I also think that makes the method easier to use.

Jeanne Boyarsky wrote:If the method did a lot of work and passed the data around, it could make sense to use this design though.



The problem for me, was this concrete code. Is too short and clouds the intation of the method. I agree with what you said: I'm almost sure that there is a refactoring recipe that says something along the lines (or was in Effective Java)
14 years ago
Recently I had a disagreement with a coworker over this peace of code:



I argued that in that code the concept of attributes was confused with that of parameters. My argument was that that code make the multiply() method extremely complicated, because for each new multiplication I want to perform needs a previously call to setN() and setM(). Also it wastes memory because n and m will life over the entire course of the existence of a Multiplication object but they are only needed in the call of just one of the class's methods. And finally it confuses in the use of multiplication() because it's now no clear how many numbers it needs to be performed.

He argued that it all depended of the design pursued.

I know that programming is not an exact science, but sometimes you can draw a line to separate some do not's and do's, isn,t?

Is there some rules in the attribute v.s. parameter dilemma? I google it, but came empty-handed.

Thanks.
14 years ago
Thanks for your kind replies.

I will try to better explain my problem:

I have two (possibly more) classes Book and Magazine. They share some common attributes, like a String title and a String id. Obviously they will share also getters/setters for these attributes.

Books have a 13 digit ISBN id while Magazine have an 8 digit ISSN id, so they both will implement the setId(String) method differently, one will throw an exception if the parameter is not a valid ISBN and the other one will thrown an exception if the parameter is not a valid ISSN.

Both classes have a common super class Item which is abstract, because is the first non-Object class in their tree hierarchy. I must implement setId(String) so its derived classes can call super.setId(String) to change the value of the private attribute id of the item class, but making it public seem wrong as this method is not meant to be used directly, rather is meant to be overriden by the derived class (to provide a custom way to validate the id parameter)

My solution was to make the method protected, that way any derived class should be forced to override it (and given the opportunity to provide a custom check mechanism for valid id) if they want to have a public setId(String) method. I don't know if that is a good design decision and that was my question.

Thanks again.
14 years ago
I'm sorry to put code with mistakes!

My question is: I have an abstract class with a method setId() that is implement differently by its sub classes. My first thought was to make the method abstract and the attribute id private.

On the subclasses then I was trying to implement the setId() method to throw an exception based on a simple test, but because id was private (and the setId() of the super class was abstract) it was imposible to implement in that way.

The solution I used was to make the super setId() method protected (which is as close to being abstract as I can think) and make the setId() of the subclasses called it if they don't thrown an exception. Is this good design? Or how would I implement the setId() of the sub classes?

I hope I make myself understood!

Thanks.
14 years ago
I don't really understand your question. Maybe you can post some code to make your question more clear!
14 years ago
I have a doubt about a protected method; I began with this design:



Then I re-wrote my design to:



Is that a good design?

Thanks!


[HENRY:Enabled BB code]
14 years ago
I have heard such harsh comments about finalize() but shouldn't I, for example, serialize an object on finalize?

I can see why releasing sources on finalize could be dangerous, but If I want to save the state of an object I don't care when it will be saved, as long as I'm sure it will be saved, doesn't it?

Thanks.
14 years ago
I'm trying to catch a number from a Scanner. I thought that I could catch the InputMismatchException and re-ask the user to submit another real number.

I use this but if I raise the exception it keeps in an infinite loop:



Thanks!
14 years ago
Since I learned OOP I have never used protected members. I always thought they break encapsulation.

I read somewhere that while protected fields are bad, that sometimes protected methods make a lot of sense.

They use the clone() method as an example but I can't really understand what's the advantage of clone() beign protected rather than public.

Any thoughts?

Thanks a lot.
14 years ago
No it doesn't. It complains about not finding the classes from the package javax.servlet so I assumed that the attribute classpath is wrong.

Thanks.
15 years ago
Hi!

I'm using this command line to compile a class:

javac -classpath "C:\Program Files\apache-tomcat-6.0.18\lib\servlet-api.jar" -d classes src/Test.java

Is this the corresponding build.xml:

<pre><?xml version="1.0" ?>
<project name="test" default="compile">
<target name="compile">
<javac classpath="C:\Program Files\apache-tomcat-6.0.18\bin\servlet-api.jar" srcdir="src" destdir="classes" />
</target>
</project></pre>

Thanks!
15 years ago
Yeah! You are right it's a mistype I needed to type as the url http://localhost:8080/ch1/Serv1

Thanks.
15 years ago
Hi!

I have used Java for a while but I'm not sure how to enforce MVC in a Swing application.

Normally I have a POJO's to represent the Model part and I have a main JFrame class which is also an ActionListener

My questions are: Do I create a single ActionListener to handle all events? Do I create an ActionListener that delegates based on the type of event? What happens if I also need a MouseListener?

Thanks!
15 years ago
I'm not sure I'm following.

Everytime I tried something I shutdown Tomcat first, the I restarted. Is there anything else to do to restart the context/Tomcat?

I did not understand what you said about the web.xml file

As I come to understand the thing is:

Everything that Tomcat will use must be in the directory "webapps"

Inside that directory there must be a directory per app.

Inside each app root directory there must be a WEB-INF directory. Inside it there must be a "classes" directory and under it there must be directories that matches the packages you are using.

Static context that you intent your client to use must be in the root or a directory structure not under WEB-INF.

The web.xml must be in the app root directory.

Right? Is that so for every Tomcat version or it may have changed?

Thanks a lot.
15 years ago
The URL I'm using is:

http://localhost:8080/ch1/Serv1/

Its a 404 error it says "required resource is not available"

15 years ago