Professional Documents
Culture Documents
Class A
Class B extends A
Class SingleInheritance
obj.methodA();
obj.methodB();
}
Method Overloading Method Overriding
}
public void run() {
while(MyRunnableThread.myCount <= 10){
try{
System.out.println("Expl Thread:
"+(++MyRunnableThread.myCount));
Thread.sleep(100);
} catch (InterruptedException iex) {
System.out.println("Exception in thread: "+iex.getMessage());
}
}
}
}
public class RunMyThread {
public static void main(String a[]){
System.out.println("Starting Main Thread...");
MyRunnableThread mrt = new MyRunnableThread();
Thread t = new Thread(mrt);
t.start();
while(MyRunnableThread.myCount <= 10){
try{
System.out.println("Main Thread:
"+(++MyRunnableThread.myCount));
Thread.sleep(100);
} catch (InterruptedException iex){
System.out.println("Exception in main thread:
"+iex.getMessage());
}
}
System.out.println("End of Main Thread...");
}
}
Starting Main Thread...
Main Thread: 1
Expl Thread: 2
Main Thread: 3
Expl Thread: 4
Main Thread: 5
Expl Thread: 6
Main Thread: 7
Expl Thread: 8
Main Thread: 9
Expl Thread: 10
Main Thread: 11
End of Main Thread..
4A)
Java provides a new additional package in Java called java.util.stream. This
package consists of classes, interfaces and enum to allows functional-style
operations on the elements. You can use stream by importing java.util.stream
package.
o Stream does not store elements. It simply conveys elements from a source
such as a data structure, an array, or an I/O channel, through a pipeline of
computational operations.
o The elements of a stream are only visited once during the life of a stream.
Like an Iterator, a new stream must be generated to revisit the same
elements of the source.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
String line;
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileWriter;
import java.io.IOException;
5a)
java provides console based programming language environment and window based
programming environment. An applet is a window based programming environment. So
applet architecture is different than console base program.
Java applets are essentially java window programs that can run within a web
page.Applete programs are java classes that extend that java.applet.Applet class and
are enabaled by reference with HTML page. You can observed that when applet are
combined with HTML, thet can make an interface more dynamic and powerful than with
HTML alone.
While some Applet do not more than scroll text or play movements, but by
incorporating theses basic features in webpage you can make them dynamic. These
dynamic web page can be used in an enterprise application to view or manipulate data
comming from some source on the server.
The Applet and there class files are distribute through standard HTTP request and
therefore can be sent across firewall with the web page data. Applete code is
referenced automatically each time
the user revisit the hosting website.
Therefore keeps full application up
to date on each client desktop on
which it is running.
5b)
Parameters are passed to applets in NAME=VALUE pairs in <PARAM> tags between the
opening and closing APPLET tags. Inside the applet, you read the values passed through
the PARAM tags with the getParameter() method of the java.applet.Applet class.
The program below demonstrates this with a generic string drawing applet. The applet
parameter "Message" is the string to be drawn.
import java.applet.*;
import java.awt.*;
You also need an HTML file that references your applet. The following simple HTML
file will do:
<HTML>
<HEAD>
<TITLE> Draw String </TITLE>
</HEAD>
<BODY>
This is the applet:<P>
<APPLET code="DrawStringApplet" width="300" height="50">
<PARAM name="Message" value="Howdy, there!">
This page will be very boring if your
browser doesn't understand Java.
</APPLET>
</BODY>
</HTML>
This applet is very similar to the HelloWorldApplet. However rather than hardcoding
the message to be printed it's read into the variable inputFromPage from
a PARAM element in the HTML.
You pass getParameter() a string that names the parameter you want. This string
should match the name of a PARAM element in the HTML page. getParameter() returns
the value of the parameter. All values are passed as strings. If you want to get another
type like an integer, then you'll need to pass it as a string and convert it to the type you
really want.
The PARAM element is also straightforward. It occurs between <APPLET> and </APPLET>.
It has two attributes of its own, NAME and VALUE. NAME identifies which PARAM this
is. VALUE is the string value of the PARAM. Both should be enclosed in double quote
marks if they contain white space.
An applet is not limited to one PARAM. You can pass as many named PARAMs to an
applet as you like. An applet does not necessarily need to use all the PARAMs that are
in the HTML.
6A)
The java.awt package provides classes for AWT api such as TextField, Label,
TextArea, RadioButton, CheckBox, Choice, List etc.
Limitations of AWT:
The AWT defines a basic set of controls, windows, and dialog boxes that
support a usable, but limited graphical interface. One reason for the
limited nature of the AWT is that it translates its various visual
components into their corresponding, platform-specific equivalents or
peers. This means that the look and feel of a component is defined by
the platform, not by java. Because the AWT components use native code
resources, they are referred to as heavy weight.
Second, the look and feel of each component was fixed and could not be
changed. Third, the use of heavyweight components caused some
frustrating restrictions.
Java Swing is a part of Java Foundation Classes (JFC) that is used to create
window-based applications. It is built on the top of AWT (Abstract Windowing
Toolkit) API and entirely written in java.
6B)
Java JButton
The JButton class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is pushed. It
inherits AbstractButton class.
Constructor Description
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Java Applet
Applet is a special type of program that is embedded in the webpage to generate
the dynamic content. It runs inside the browser and works at client side.
Advantage of Applet
There are many advantages of applet. They are as follows:
o Secured
Drawback of Applet
o Plugin is required at client browser to execute applet.
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{