|
|
|
1000 Java Tips ebook
|
|
 

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java
programming articles and tips. Please take your copy here.
Take your copy of free "Java Technology Screensaver"!. |
|
Java Newsletters Archive: 18
|
JavaFAQ Home » Java Newsletters

*********************************************** *
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * * > The Java FAQ
Daily Tips, weekly publication < * *
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * * * * Issue No: 18 3
January 2001 * * http://www.javafaq.nu/java * * * * * * Please
recommend our FREE "100 Java Tips" book and us to your * * friends and
colleagues! * * http://javafaq.nu/java/advert/our_book.shtml
* ***********************************************
Table
of Contents
1. I am writing an application, which requires me to set and
read cookies. But it fails... 2. I would like to burn a CD from within my
code, that way I won't .. 3. I try to copy an object of my own using the
clone() method... 4. I was just wondering about the usefulness of
Interfaces... 5. I'm interested in writing a little mp3 player in
Java... 6. How can I slow down my applet? 7. On MsSQLSERVER7 I am able to
load the driver but it says unable to get socket
connection. ***********************************************
Hello
dear friends!
***********************************************
Tip
1 Q: I am writing an application, using Java Servlets, which requires me to
set and read cookies. Using the servlet API, and the
javax.servlet.http.Cookie class. I created a new cookie, and added it to the
http response, using its addCookie() method. It works okay, but fails if I
use the setDomain method, on the newly created cookie, to set the domain
of the cookie to something other than the current server.
Answer: I
suspect that is not a legal operation for any browser to accept a cookie
that has a domain inconsistent with the source of the cookie. by William
Brogden
***********************************************
Tip
2 Q: I would like to burn a CD from within my code, that way I won't have to
waste so much time making illegal copies of music. Is there a convenient way
to do this in Java?
Answer: Unfortunately Java doesn't provide any API
for this

***********************************************
Tip
3 Q: I try to copy an object of my own using the clone() method from
java.lang.Object, but this is a protected method so I can't use it. Is there
some other way to get my objective of duplicating an arbitrary
object?
Answer:If you want to clone your object, you need to make it
cloneable. To achieve this, you need to do two things:
1. implement
the interface Cloneable
2. override the method clone(), so that it a.
becomes public b. calls super.clone() c. if necessary, clones any members,
or d. if a member can't be cloned, creates a new instance.
Simple
example: public MyClass implements Cloneable { int someNumber; String
someString;
public Object clone() { // primitives and Strings are
no // problem return super.clone(); } }
In this case the
method clone() of the class MyClass returns a new instance of MyClass, where
all members have exactly the same value. That means, the object reference
'someString' points to the same object. This is called a shallow copy. In
many cases this is no problem. Strings are immutable and you do not need
a new copy. But if you need new copies of members, you have to do it in the
clone() method. Here is another simple example:
public class SomeMember
implements Cloneable { long someLong;
public Object clone()
{ return super.clone(); } }
public AnotherClass extends MyClass
{ SomeMember someMember;
public Object clone() { AnotherClass ac =
(AnotherClass)(super.clone()); if (someMember != null) { ac.someMember =
(SomeMember)(someMember.clone()); } return ac; } }
Note that
the class AnotherClass, that extends MyClass, automatically becomes
Cloneable, because MyClass is Cloneable.
Also note, that super.clone()
always returns an Object of the type of the actual object, although the
superclass doesn't know anything about that sub class. The reason is, that
Object.clone() is a native method, which just allocates new memory for the
new object and copies the bytes to that memory. Native code has it's own
ways of finding out which type to return 
***********************************************
Tip
4 Q: I was just wondering about the usefulness of Interfaces... Q: I was just
wondering about the usefulness of Interfaces. I was under the impression
that interfaces could be used to perform multiple inheritance. But an
interface only declares a method - in a very abstract way.
A class
that implements an interface needs to define its own implementation of a
certain method. What is the use of having an interface when nothing is being
gained...?
A: If two classes implements the same interface, you can get a
reference to the interface instead of the effective class without bother
what class are you managing.
This is very useful in RMI (for example) or
in any condition when you have to take an object without knowing exactly his
class, but only the interface that it implement.
For
example: public void recurseList( List l )
the generic List ensure
that you can use every List for this method (ArrayList, AbstractList,
Vector...), so your calling method can be:
ArrayList l = new ArrayList();
or Vector l = new Vector();
recurseList( l );
***********************************************
Tip
5 Q: I'm interested in writing a little mp3 player in Java... I'm interested
in writing a little mp3 player in java. I have an entirely different app
right now that plays sound (wav files), and I substituted an mp3 file for
one of the waves but it didn't work. Can anyone tell me if java even
supports mp3 files?
A: Go to the "Products & APIs" section of
java.sun.com and look for JMF (Java Media Framework). It's a library that
also supports reading MP3
files.
***********************************************
Tip
6 Q: How can I slow down my applet? I have a game applet that is running too
fast on newer systems that have high-end video cards. Its easy enough to
slow down the game by having it sleep between thread cycles, but I need to
be able to determine how fast a users machine is before I determine how
long to sleep for.
I have been muddling through the documentation but
cannot find any calls that will tell my applet what the users configuration
is as regards to CPU speed and other components they may have on their
system.
A: Simple create a new Date (), then perform a standard lengthy
operation on the order of something that takes about one second on your
machine, like a long loop, then create another new Date() and compare it to
the first. If it takes 1/2 of the time compared to your machine, then the
CPU is probably about 2 times faster. If it takes 3 times the duration
compared to your machine, the CPU is probably 1/3 as fast as
yours.
Do this dynamically, and it might help with speed changes when
there's lots of action happening as well - unless this issue is already
being dealt with using threads, that is.
***********************************************
Tip
7 Q: I am working on weblogic server 5.1 with MsSQLSERVER7 I am able to load
the driver but it says unable to get socket connection. It says connect to
the MSSQLSERVER's host and port no. How do I get these name and
value?
The MS Sql Server's host is usually the name or ip of the server
that run SQL Server, if you know the IP (ping in
console or command line, like: ping javafaq.nu), put the IP in it, it will
be faster, for the Port number, sincerely I don't remember the standard
port, but look into the SQL Server documentation and you will find it.
**********************************************************
The
Java FAQ Daily Tips is a newsletter that is only sent to those who have
specifically subscribed to it.
Copyright (c) 2000 John
Andersson *********************************************** *
* * You can find our tips on site also! * * Please recommend us and our
FREE "100 Java Tips" book to your * * friends and colleagues! * *
http://javafaq.nu/java/advert/our_book.shtml * *
* ***********************************************
Printer Friendly Page
Send to a Friend
..
Search here again if you need more info!
|
|
|