1.
What
is Unicode?
Unicode and Ascii code are both
ways that computer languages store characters as numbers. Ascii stands for
"American Standard Code for Information Interchange" and it allows
encoding for 128 characters. Fine for the English language, but not enough for
others.
Unicode can handle 100,000 characters, so by using this encoding scheme, Java allows programmers to work with printed languages from around the world!
Unicode can handle 100,000 characters, so by using this encoding scheme, Java allows programmers to work with printed languages from around the world!
2.
Explain the advantage of
using Unicode.
To enable a computer system for storing text and
numbers which is understandable by humans, there should be a code that
transforms characters into numbers.
Unicode is a standard of defining the relevant code
by using character encoding. Character encoding is the process for assigning a
number for every character. The central objective of Unicode is to unify
different language encoding schemes in order to avoid confusion among computer
systems that uses limited encoding standards such as ASCII, EBCDIC etc.
Java Unicode:
The evolution of Java was the time when the Unicode
standards had been defined for very smaller character set. Java was designed
for using Unicode Transformed Format (UTF)-16, when the UTF-16 was designed.
The ‘char’ data type in Java originally used for representing 16-bit Unicode.
Hence Java uses Unicode standard.
3.
Explain two-dimensional array with example.
Two-dimensional arrays are defined as "an
array of arrays". Since an array type is a first-class Java type, we can
have an array of ints, an array of Strings, or an array of Objects. For
example, an array of ints will have the type int[]. Similarly we can have
int[][], which represents an "array of arrays of ints". Such an
array is said to be a two-dimensional array.
The command
The command
int[][] A =
new int[3][4];
declares a variable, A, of type int[][], and it
initializes that variable to refer to a newly created object. That object is an
array of arrays of ints. Here, the notation int[3][4] indicates that there are
3 arrays of ints in the array A, and that there are 4 ints in each of those
arrays.
public class
twoDimension{
public static void main(String[] args) {
int[][] a2 = new int[10][5];
for (int i=0; i<a2.length; i++) {
for (int j=0; j<a2[i].length; j++) {
a2[i][j] = i;
System.out.print(" " + a2[i][j]);
}
System.out.println("");
}
}
}
4.
Discuss
the steps involved in handling Event in Java.
Event-Driven Programming
The event-driven programming model,
describe below:
The basic idea of how we'll implement event
handling in Java involves several steps:
a) When creating classes, we will implement an interface (or
interfaces) for handling the events.
o
ActionListener
is the interface for many GUI components.
o
ItemListener
is the interface for radio buttons and checkboxes.
o
ListSelectionListener
is the interface for lists.
o
MouseListener
is the interface for mouse events.
o
KeyListener
is the interface for keyboard events.
b) Whenever we create GUI components that will trigger
events, we must register them with the event handler. In other words, we must
have code that says "this widget could trigger an event you should be
listening for."
c) We write code to handle the events that happen. Java will
call the appropriate code at the appropriate time.
o
We have a general method called actionPerformed that catches ActionListener events. We'll generally check for the
source of the event (which button, text field, etc. was it?) and then do
something as a result of it.
o
Similarly we have general method called itemStateChanged that catches ItemListener events. We'll generally
check for the source of the event (which checkbox, or radio button was it?) and
then do something as a result of it.
o
There are specific methods in the mouse and keyboard
interfaces we'll implement based of various events, e.g. clicks, double-clicks,
right-clicks, some key was pressed, some key was released, etc.
5.
Write an applet program where you may input
two numbers and get the result of the product of those two numbers.
A calculator program with java
Step 1:-
Save the file calculator.java
………………………………………………………………………………………
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class calculator extends
Applet
implements ActionListener
{
TextField t1,t2,t3;
Label l1,l2,l3;
Button b1,b2,b3;
public void init()
{
l1=new Label("1st input");
add(l1);
t1=new TextField(20);
add(t1);
l2=new Label("2nd input");
add(l2);
t2=new TextField(20);
add(t2);
l3=new Label("result");
add(l3);
t3=new TextField(20);
add(t3);
b1=new
Button("Add");
add(b1);
b1.addActionListener(this);
b2=new Button("Subtract");
add(b2);
b2.addActionListener(this);
b3=new Button("Multiply");
add(b3);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{ int
a=Integer.parseInt(t1.getText());
int b=Integer.parseInt(t2.getText());
int c;
if(e.getSource()==b1)
{
c=a+b;
t3.setText(String.valueOf(c));
}
if(e.getSource()==b2)
{
c=a-b;
t3.setText(String.valueOf(c));
}
if(e.getSource()==b3)
{
c=a*b;
t3.setText(String.valueOf(c));
}
}
}
For Compilation type :- javac
calculator.java
…………………………………………………………………………
Step 2: Html file
Save the file calculator.html
<applet
code="calculator.class" width=200 height=200>
</applet>
……………………………………………………………………….
Step 3: Output of the program
To run the program type:-
appletviewer calculator.java
No comments:
Post a Comment