Monday, July 30, 2007

Tips on Collection in java

Let's start with a quiz
As is often the case, I am going to begin this lesson with a little quiz just to make sure that you are awake. Is the following statement True or False?
The TreeSet class is a direct implementation of the Collection interface.The answer is False. The TreeSet class is not a direct implementation of the Collection interface. Rather, the TreeSet class is a direct implementation of the SortedSet interface. The SortedSet interface extends the Set interface, and the Set interface extends the Collection interface.
The root of the collection hierarchy
The Collection interface is the root of the collection hierarchy. JDK 1.3 doesn't provide any direct implementations of the Collection interface. All of the implementations of the interfaces in the Java Collections Framework implement one of the subinterfaces of the Collection interface.
What does Sun say about this?
Here is what the Sun documentation has to say on the topic of the Collection interface:
"The SDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired."
The Sun documentation also states:
"Bags or multisets (unordered collections that may contain duplicate elements) should implement this interface directly."
However, JDK 1.3 does not provide any implementations for bags or multisets. If you need collections of these types, you will need to define the implementation classes yourself.
What about duplicate elements?
Some implementations of Collection allow duplicate elements, and others do not. Implementations of the List interface (such as ArrayList) allow duplicate elements. Implements of Set and SortedSet (such as TreeSet) do not allow duplicate elements. This was illustrated in a previous lesson entitled Data Structures in Java: Part 5, The Core Collection Interfaces.
A sample program in that lesson created two collection objects and applied the polymorphic add() method to add the same elements to each collection. One of the collection objects was of type ArrayList, and the other collection object was of type TreeSet. The elements added to each collection contained one pair of duplicate elements. The duplicate element was automatically excluded from the TreeSet object, but was retained in the ArrayList object.
So, what is a set?
According to Sun, a Set is a "collection that contains no duplicate elements ... this interface models the mathematical set abstraction." An object of type Set is typically used to model collections such as Social Security numbers where duplicates are not allowed.
And, what is a list?
Also according to Sun, a List is "An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list."
Ordered is not the same as sorted
Note that an ordered collection is not the same as a sorted collection. The fact that the collection is ordered derives from the fact that each element in the collection has a specific position specified by an index. In a sorted collection, the position of each element is determined by its value relative to the values of its predecessors and successors.
Sun goes on to say, "Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all."
Is ascending sort order always required?
Not all implementations of the Collection interface maintain the elements in ascending sort order. Some may, and others do not. For example, as discussed above, implementations of the List interface (such as ArrayList) do not maintain their elements in sorted order at all. In other words, the position of an element in an ArrayList does not depend on the value of the element.
On the other hand, implementations of the interfaces named SortedSet (such as TreeSet) and SortedMap do maintain their elements in sorted order. However, that order is not necessarily ascending. When an object is instantiated from a class that implements the SortedSet interface, the sorting order for that object can be established by providing an object instantiated from a class that implements the Comparator interface. In that case, the author of the implementing class determines the order imposed on the elements in the collection.
Does case matter in String objects?
For example, if your SortedSet object contains references to String objects, the natural ascending sort would take the difference between upper case and lower case characters into account. However, you might prefer that case be ignored when establishing the sorted order. You can accomplish this by providing an object of a class that implements the Comparator interface and which defines the compare() and equals() methods in such a way as to eliminate case considerations for comparisons of String objects. (The lesson entitled Swing from A to Z: Analyzing Swing Components, Part 1, Concepts contains a sample program named Introspect03 that implements the Comparator interface for exactly this purpose.)
Subinterfaces have more stipulations
As you progress down the inheritance hierarchy, you find that additional stipulations apply at each level of inheritance. As an example, according to Sun, "The Set interface places additional stipulations, beyond those inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add, equals and hashCode methods."
The important point is that specific subinterfaces of the Collection interface can define requirements that do not apply to all subinterfaces of the Collection interface. For example, the add() method of the Set interface stipulates the following:
"Adds the specified element to this set if it is not already present."On the other hand, the add() method of the Collection interface simply states:
"Ensures that this collection contains the specified element."Thus, the contract for the add() method of an object of a class that implements the Set interface is more specialized than the contract for the add() method of an object of a class that implements Collection interface.
An additional stipulation on the constructor for a Set object is that all constructors must create a set that contains no duplicate elements.
Stipulations on SortedSet
The SortedSet interface extends the Set interface. The SortedSet interface contains the following stipulation that makes it more specialized than a Set.
"A set that further guarantees that its iterator will traverse the set in ascending element order, sorted according to the natural ordering of its elements (see Comparable), or by a Comparator provided at sorted set creation time."
Let's end with a quiz
I'm going to finish this lesson with several questions in the form of a quiz. To ensure that this is a learning experience, I will provide an explanation in addition to the answer for each question.
Q1 True or False? A collection that implements the List interface maintains its elements in ascending alphanumeric order.
The answer to question 1 is false. Unlike collections that implement the SortedSet interface, the order of the elements in a collection that implements the List interface is not based on the values of the objects referred to by the elements in the list.
Q2 True or False? A collection that implements the List interface is an unordered collection.
The answer to question 2 is also false. A collection that implements the List interface is an ordered collection (also known as a sequence). According to Sun, "The user of the interface has precise control over where in the list each element is inserted." Elements can be inserted and retrieved on the basis of their integer index (position in the list) using the following methods:
add(int index, Object element) get(int index)
Valid index values are positive integers that begin with zero. When the add method is used to insert an element at a specific position in the sequence, the element currently at that position (if any) and any subsequent elements are shifted toward higher index values to make room for the new element.
Another version of the add method takes a reference to an object as an incoming parameter and appends the specified element to the end of the collection.
The get method simply returns the element at the specified position in the collection.
The List interface also declares various other methods that can be used to manipulate the contents of the collection.
Q3 True or False? A collection that implements the List interface is allowed to contain duplicate values.
The answer to question 3 is true. Unlike a collection that implements the Set interface, a collection that implements the List interface is typically allowed to contain duplicate values. More formally, according to Sun, "lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all."
Q4 True or False? The contracts of the methods in the List interface are the same as the contracts of the methods inherited from the Collection interface.
The answer to question 4 is false. According to Sun, "The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator, add, remove, equals, and hashCode methods."
For example, the iterator() method (for both the List and Collection interfaces) returns an iterator over the elements in the collection. For the Collection interface, there are no guarantees concerning the order in which the elements are returned by the methods of the Iterator object.
On the other hand, the iterator() method for the List interface returns an iterator over the elements in the collection in proper sequence, where the sequence is determined by the numeric index. In other words, when you invoke the methods of the Iterator object on a List, the elements will be returned in the proper sequence as determined by a numeric index.
Similarly, according to Sun, the SortedSet interface "guarantees that its iterator will traverse the set in ascending element order, sorted according to the natural ordering of its elements (see Comparable), or by a Comparator provided at sorted set creation time."
SummaryIn this lesson you learned that all of the implementations of the interfaces in the Java Collections Framework implement one of the subinterfaces of the Collection interface. You learned that a Set object cannot contain duplicate elements, but a List object can contain duplicate elements.
You learned about the difference between ordered collections and sorted collections. You also learned about ascending order and the natural ordering of objects. In addition, you learned how more specialized stipulations are placed on interfaces as you progress down the interface inheritance hierarchy of the Java Collections Framework.

Sunday, July 29, 2007

Good definitions

 

 

Doctor:


A person who kills

your ills by pills, and kills you with his

bills.





Cigarette:


A pinch of

tobacco rolled in paper with fire at one

end a fool on the other.





Compromise:


The art of dividing

a cake in such a way that everybody

believes he got the biggest piece.





Dictionary:


A place where

success comes before work.





Conference Room:


A place where

everybody talks, nobody listens and

everybody disagrees later on.





Classic:


A book which

people praise, but do not read.





Smile:


A curve that can

set a lot of things straight.





Office:


A place where you

can relax after your strenuous home life.





Yawn:


The only time

some married men ever get to open their

mouth.





Etc.:


A sign to make

others believe that you know more than

you actually do.





Committee:


Individuals who

can do nothing individually and sit to

decide that nothing can be done together.





Lecture:


An art of

transferring information from the notes of

the Lecturer to the notes of the students

withoutpassing through "the minds of either"



Conference:


The confusion of

one man multiplied by the number

present.





Experience:


The name men

give to their mistakes.





Atom Bomb:


An invention to

end all inventions.





Diplomat:


A person who tells

you to go to hell in such a way that you

actually look forward to the trip.





Opportunist:


A person who

starts taking bath if he accidentally falls

into a river.





Optimist:


A person who

while falling from Eiffel tower says in

midway "See I am not injured yet."





Divorce:


Future tense of

marriage.





Miser:


A person who

lives poor so that he can die rich.





Father:


A banker provided

by nature.





Criminal:


A guy no different

from the rest....except that he got caught.





Boss:


Someone who is

early when you are late and late when you

are early.





Politician:


One who shakes

your hand before elections and your

confidence after.

 

Thursday, July 26, 2007

Syllabus for IES : Indian Engineering Services Examination

ELECTRICAL ENGINEERING PAPER – I (For both objective and conventional types papers) 1. EM Theory Electric and magnetic fields. Gauss’s Law and Amperes Law. Fields in dielectrics, conductors and magnetic materials. Maxwell’s equations. Time varying fields. Plane-Wave propagating in dielectric and conducting media. Transmission lines. 2. Electrical Materials Band Theory, Conductors, Semi-conductors and Insulators. Super-conductivity. Insulators for electrical and electronic applications. Magnetic materials. Ferro and ferri magnetism. Ceramics, Properties and applications. Hall effect and its applications. Special semi conductors. 3. Electrical Circuits Circuits elements. Kirchoff’s Laws. Mesh and nodal analysis. Network Theorems and applications. Natural response and forced response. Transient response and steady state response for arbitrary inputs. Properties of networks in terms of poles and zeros. Transfer function. Resonant circuits. Threephase circuits. Two-port networks. Elements of two-element network synthesis. 4. Measurements and Instrumentation Units and Standards. Error analysis, measurement of current, Voltage, power, Power-factor and energy. Indicating instruments. Measurement of resistance, inductance, Capacitance and frequency. Bridge measurements. Electronic measuring instruments. Digital Voltmeter and frequency counter. Transducers and their applications to the measurement of non-electrical quantities like temperature, pressure, flow-rate displacement, acceleration, noise level etc. Data acquisition systems. A/D and D/A converters. 5. CONTROL SYSTEMS. Mathematical modelling of physical systems. Block diagrams and signal flow graphs and their reduction. Time domain and frequency domain analysis of linear dynamical system. Errors for different type of inputs and stability criteria for feedback systems. Stability analysis using Routh-Hurwitz array, Nyquist plot and Bode plot. Root locus and Nicols chart and the estimation of gain and phase margin. Basic concepts of compensator design. State variable matrix and its use in system modelling and design. Sampled data system and performance of such a system with the samples in the error channel. Stability of sampled data system. Elements of non-linear control analysis. Control system components, electromechanical, hydraulic, pneumatic components. ELECTRICAL ENGINEERING PAPER – II (For both objective and conventional types papers) 1. Electrical Machines and Power Transformers Magnetic Circuits - Analysis and Design of Power transformers. Construction and testing. Equivalent circuits. Losses and efficiency. Regulation. Auto-transformer, 3-phase transformer. Parallel operation. Basic concepts in rotating machines. EMF, torque, basic machine types. Construction and operation, leakage losses and efficiency. B.C. Machines. Construction, Excitation methods. Circuit models. Armature reaction and commutation. Characteristics and performance analysis. Generators and motors. Starting and speed control. Testing, Losses and efficiency. Synchronous Machines. Construction. Circuit model. Operating characteristics and performance analysis. Synchronous reactance. Efficiency. Voltage regulation. Salient-pole machine, Parallel operation. Hunting. Short circuit transients. Induction Machines. Construction. Principle of operation. Rotating fields. Characteristics and performance analysis. Determination of circuit model. Circle diagram. Starting and speed control. Fractional KW motors. Single-phase synchronous and induction motors. 2. Power systems Types of Power Stations, Hydro, Thermal and Nuclear Stations. Pumped storage plants. Economics and operating factors. Power transmission lines. Modeling and performance characteristics. Voltage control. Load flow studies. Optimal power system operation. Load frequency control. Symmetrical short circuit analysis. ZBus formulation. Symmetrical Components. Per Unit representation. Fault analysis. Transient and steady-state stability of power systems. Equal area criterion. Power system Transients. Power system Protection Circuit breakers. Relays. HVDC transmission. 3. ANALOG AND DIGITAL ELECTRONICS AND CIRCUITS Semiconductor device physics, PN junctions and transistors, circuit models and parameters, FET, Zener, tunnel, Schottky, photo diodes and their applications, rectifier circuits, voltage regulators and multipliers, switching behavior of diodes and transistors. Small signal amplifiers, biasing circuits, frequency response and improvement, multistage amplifiers and feed-back amplifiers, D.C. amplifiers, Oscillators. Large signal amplifiers, coupling methods, push pull amplifiers, operational amplifiers, wave shaping circuits. Multivibrators and flip-flops and their applications. Digital logic gate families, universal gates-combination circuits for arithmetic and logic operational, sequential logic circuits. Counters, registers, RAM and ROMs. 4. MICROPROCESSORS Microprocessor architecture-Instruction set and simple assembly language programming. Interfacing for memory and I/O. Applications of Micro-processors in power system. 5. COMMUNICATION SYSTEMS Types of modulation; AM, FM and PM. Demodulators. Noise and bandwidth considerations. Digital communication systems. Pulse code modulation and demodulation. Elements of sound and vision broadcasting. Carrier communication. Frequency division and time division multiplexing, Telemetry system in power engineering. 6. POWER ELECTRONICS Power Semiconductor devices. Thyristor. Power transistor, GTOs and MOSFETS. Characteristics and operation. AC to DC Converters; 1-phase and 3-phase DC to DC Converters; AC regulators. Thyristor controlled reactors; switched capacitor networks. Inverters; single-phase and 3-phase. Pulse width modulation. Sinusoidal modulation with uniform sampling. Switched mode power supplies.

Problem after installing MYSQL on WIN32

You need to remember there are two parts to mysql, the server and theclient(s). Mysqld is the server, mysql is the command line client. Thetraffic light is courtesy of another program called winmysqladmin and isoptional.If you start mysqld from a command prompt, it will not return to thecommand prompt. Perhaps that is what you mean by "Windows's promptcrash..". This is quite correct, the server is now running and that isdetected by winmysqladmin, so the green light comes on.The best way to run mysqld is as a Windows service. The manual coversthis but you can run it from a command prompt or from Windows Startup ifyou want.Once the server is running you can start up a client to talk to it.You can start the command line client from a command prompt by enteringC:\mysql\bin\mysql -u -h -p The default user name is root, the default host is localhost and thereis no default for the database.For exampleC:\mysql\bin\mysql -uroot -p testLogs you in as root, will prompt for root's password (which is empty bydefault after installation) and will connect you to the test database. Iam assuming you have installed mysql to c:\mysql, if not adjust thepath. Just enteringC:\mysql\bin\mysqlWill log you into the server (if any) on your local machine as root(providing root has no password) but will not connect you to anydatabase. This is what happens if you just double click mysql.exe fromExplorer.You say you saw the one line help prompt, so you got that far.There is no SQL command "mysqlshow" so that won't work and many othercommands won't work unless you are connected to a database.You can connect to a database by enteringUSE test;and pressing enter. There is a database called "test" created duringinstallation. If you enterSHOW databases;You will see there is also a database called "mysql" where the serverkeeps its administrative information.Hope this gets you going but it is all in the manual in section 2.2.

Tuesday, July 17, 2007

To pass more then one request from jsp page to java servlet

Request attributes only last the life of one request.A "request" is asking the server for one URLThe "response" is what the server sends back.So every click of a link, every form submission, every image downloaded - each of them is a seperate request.You are sending two different requests1 - load page with initial form (ie Send.jsp) (setting request attribute)2 - load page with result ( /Receive ) (trying to retrieve request attribute)Because you have two seperate requests, you can't use request attributes to pass data, and have to use session.If you use RequestDispatcher or to forward/include to other JSP resources. You are still on the same request but transferring the control to another resource. In this scenario as you have the same request, request attributes can be used.The classic example is - invoke a servlet- servlet loads data from database into request attributes- servlet calls RequestDispatcher.forward("/someJspPage.jsp");- jsp page retrieves request attributes.




request.setAttribute versus session.setAttribute:


My understanding is that request.setAttribute only make the key available in the following jsp page. But session.setAttribute will make the key available in many pages, as long as in the same session, and browser not close.For example, the flow like page1->page2->page3->page4. session.setAttribute will make the key available in all pages. But if we use request.setAttribute in page2, then only page3 can get the key value set in page2.