Spreadsheet_Excel_Writer: Fatal error: Call to undefined method PEAR_Error::setLandscape()
The sheet name length has to be less than or equal to 31 characters. This is one possible problem for this error.
Notes from “Sun Certified Programmer for Java 6 Study Guide”
1. The static modifier is used to create variable and methods that will exist independently of any instances created for the class. All static members exist before you ever make a new instance of a class, and there will be only one copy of a static member regardless of the number of instances of that class. In other words, all instances of a given class share the same value for any given static variable.
Two SCJP questions
As an example for a generic class we will use a very simple container. A Basket can contain only one element.
Here the source code:
public class Basket<E> { private E element; public void setElement(E x) { element = x; } public E getElement() { return element; } }
We will store fruits in the baskets:
class Fruit { } class Apple extends Fruit { } class Orange extends Fruit { }
What would Java 1.5 do with the following source code?
Basket<Fruit> basket = new Basket<Fruit>(); // 1 basket.setElement(new Apple()); // 2 Apple apple = basket.getElement(); // 3
a)The source code is OK. Neither the compiler will complain, nor an exception during the runtime will be thrown.
b)Compile error in the line 2.
c)Compile error in the line 3.
Answer: c
The line 2 is ok. The line 3 however will cause a runtime error. The result type of the methode getElement of Basket is Fruit. We cannot assign a Fruit to a variable of the type Apple without a cast.
Question :
Let’s stay with our baskets. What do you think about the following source code?
Basket<Fruit> basket = new Basket<Fruit>(); basket.setElement(new Apple()); Orange orange = (Orange) basket.getElement();
a)e source code is OK. Neither the compiler will complain, nor an exception during the runtime will be thrown
b)mpile error in the line 2.
c)mpile error in the line 3.
d)ClassCastException will be thrown in the line 3.
Answer : d
Both Apples and Oranges are Fruits and can be inserted into a Basket. That is why the cast is necessary in the line 3.
During the runtime the JVM checks the cast in line 3 and throws a ClassCastException since an Apple is not a Orange.
There a lot of mock questions in this website
SCJP SE 5.0 official exam by Sun with solutions
1. Given the two source files:
1. package com.sun; 2. public class PkgAccess { 3. public static int tiger = 1414; 4. }
A simple example for building a Swing GUI application
Firstly, we need a container like a Frame, a Window, or an Applet to display components like panels, buttons, text areas etc.
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; public class Main extends JFrame{ public static void main(String[] args) { JFrame frame = new JFrame("Frame Title");
Create some components such as panels, buttons, text areas etc.
final JTextArea comp = new JTextArea(); JButton btn = new JButton("click");
Add you components to your display area and arrange or layout components using the LayoutManagers.
frame.getContentPane().add(comp,BorderLayout.CENTER); frame.getContentPane().add(btn, BorderLayout.SOUTH);
Attach listener to your components. Interacting with a Component causes an Event to occur. To associate a user action with a component, attach a listener to it.
btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ comp.setText("Button has been clicked"); } });
public interface ActionListener extends EventListener
The listener interface is for receiving action events. The class (Main, in this case) that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component’s addActionListener method. When the action event occurs, that object’s actionPerformed method is invoked.
Show the Frame.
int width = 300; int height = 300; frame.setSize(width, height); frame.setVisible(true); }
How to Win Friends and Influence People
I got the book “How to Win Friends and Influence People”. I read some part, it is good to be that way. Like it says “The more you get out from this book ,the more you get from life”.
Here is the fundamental techniques in handling people:
Code example with Java Hashtable
Here is an example of using the class Hashtable. I haven’t use id yet, but from many places I met with this class, so I decide to practice with it. I get this code from google but change a little bit to make it work.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Hashtable; import java.util.Map; import java.util.TreeMap; public class Main{ public static void main(String[] args) throws IOException{ int key; try{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("How many elements you want to enter to the hash table : "); int n = Integer.parseInt(in.readLine()); Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>(); for(int i = 0; i < n; i++){ System.out.print("Enter key for the hash table : "); key = Integer.parseInt(in.readLine()); System.out.print("Enter value for the key : "); hashTable.put(key, in.readLine()); } Map<Integer, String> map = new TreeMap<Integer, String>(hashTable); System.out.println(map); } catch(NumberFormatException ne){ System.out.println(ne.getMessage() + " is not a legal value."); System.out.println("Please enter a numeric value."); System.exit(1); } } }
Comments(0)