Вы находитесь на странице: 1из 10

5C)

Class A

public void methodA()

System.out.println("Base class method");

Class B extends A

public void methodB()

System.out.println("Child class method");

Class SingleInheritance

public static void main(String args[])

B obj = new B();

obj.methodA();

obj.methodB();

}
Method Overloading Method Overriding

Method overloading is used to Method overriding is used to provide


increase the readability of the the specific implementation of the
program. method that is already provided by its
super class.

Method overloading is Method overriding occurs in two


performed within class. classesthat have IS-A (inheritance)
relationship.

In case of method In case of method


overloading, parameter must be overriding, parameter must be same.
different.

Method overloading is the example Method overriding is the example


of compile time polymorphism. of run time polymorphism.

In java, method overloading can't be Return type must be same or


performed by changing return type of covariant in method overriding.
the method only. Return type can be
same or different in method
overloading. But you must have to
change the parameter.

1. class OverloadingExample{ 1. class Animal{


2. static int add(int a,int b){return a+b;2. void eat(){System.out.println("eating..."
} );}
3. static int add(int a,int b,int c){return 3. }
a+b+c;} 4. class Dog extends Animal{
4. } 5. void eat(){System.out.println("eating br
ead...");}
6. }
4B)
package java.threads;

class MyRunnableThread implements Runnable{

public static int myCount = 0;


public MyRunnableThread(){

}
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.

Stream provides following features:

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 Stream is functional in nature. Operations performed on a stream does not


modify it's source. For example, filtering a Stream obtained from a collection
produces a new Stream without the filtered elements, rather than removing
elements from the source collection.

o Stream is lazy and evaluates code only when required.

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;

public class TextFileReadingExample3 {

public static void main(String[] args) {


try {
FileReader reader = new FileReader("MyFile.txt");
BufferedReader bufferedReader = new BufferedReader(reader);

String line;

while ((line = bufferedReader.readLine()) != null) {


System.out.println(line);
}
reader.close();

} catch (IOException e) {
e.printStackTrace();
}
}

}
import java.io.FileWriter;
import java.io.IOException;

public class TextFileWritingExample1 {

public static void main(String[] args) {


try {
FileWriter writer = new FileWriter("MyFile.txt", true);
writer.write("Hello World");
writer.write("\r\n"); // write new line
writer.write("Good Bye!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}

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.*;

public class DrawStringApplet extends Applet {

private String defaultMessage = "Hello!";

public void paint(Graphics g) {

String inputFromPage = this.getParameter("Message");


if (inputFromPage == null) inputFromPage = defaultMessage;
g.drawString(inputFromPage, 50, 25);

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)

Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-


based applications in java.

Java AWT components are platform-dependent i.e. components are


displayed according to the view of operating system. AWT is heavyweight
i.e. its components are using the resources of OS.

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.

 The use of native peers led to several problems. First, because of


variations between operating systems, a component might look, or even
act, differently on different platforms. This variability threatened java’s
philosophy: write once, run anywhere.

 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.

 Due to these limitations Swing came and was integrated to java.

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.

 Unlike AWT, Java Swing provides platform-independent and lightweight


components.
 The javax.swing package provides classes for java swing API such as
JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu,
JColorChooser etc.

There are a few other advantages to Swing over AWT:

 Swing provides both additional components and added functionality to AWT-


replacement components
 Swing components can change their appearance based on the current "look and
feel" library that's being used. You can use the same look and feel as the platform
you're on, or use a different look and feel
 Swing components follow the Model-View-Controller paradigm (MVC), and thus can
provide a much more flexible UI.
 Swing provides "extras" for components, such as:
 Icons on many components
 Decorative borders for components
 Tooltips for components
 Swing components are lightweight (less resource intensive than AWT)
 Swing provides built-in double buffering
 Swing provides paint debugging support for when you build your own components

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.

JButton class declaration


Let's see the declaration for javax.swing.JButton class.

1. public class JButton extends AbstractButton implements Accessible

Commonly used Constructors:

Constructor Description

JButton() It creates a button with no text and icon.

JButton(String s) It creates a button with the specified text.

JButton(Icon i) It creates a button with the specified icon object.

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 It works at client side so less response time.

o Secured

o It can be executed by browsers running under many plateforms, including


Linux, Windows, Mac Os etc.

Drawback of Applet
o Plugin is required at client browser to execute applet.

Lifecycle of Java Applet


 Applet is initialized.
 Applet is started.
 Applet is painted.
 Applet is stopped.
 Applet is destroyed

import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{

public void paint(Graphics g){


g.drawString("welcome",150,150);
}
}

Вам также может понравиться