Sie sind auf Seite 1von 18

Program no: 1

Object: - Write a program in java for INHERITANCE

class A{
int a;
float b;
void Show(){
System.out.println("b in super class: " + b);
}

class B extends A{
int a;
float b;
B( int p, float q){
a = p;
super.b = q;
}
void Show(){
super. Show();
System.out.println("b in super class: " + super.b);
System.out.println("a in sub class: " + a);
}

public static void main(String[] args){


B subobj = new B(1, 5);
subobj.Show();
}
}
Program no 2

Object: - Write a program in java for Method Overriding

In runtime polymorphism, the method to be invoked is determined at the run time. The
pratical of run time polymorphism is method overriding. When a subclass contains a
method with the same name and signature as in the super class then it is called as method
overriding.
class A{
public void fun1(int x){
System.out.println("int in A");
}
public void fun1(int x,int y){
System.out.println("int and int");
}
}

class C extends A{
public void fun1(int x){
System.out.println("int in C");
}
}

public class D{
public static void main(String[] args){
A obj;

obj= new A(); // line 1


obj.fun1(2); // line 2 (prints "int in A")

obj=new C(); // line 3


obj.fun1(2); // line 4 (prints "int in C")
}
}
Program no:-3

Object: - Write a program in java to create multiple threads

class NewThread implements Runnable {


String name; //name of thread
Thread t;
NewThread (String threadname) {
name = threadname;
t = new Thread (this, name);
System.out.println ("New thread: " + t);
t.start (); //Start the thread
}

//This is the entry point for thread.


public void run () {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted");
}
System.out.println(name + " exiting.");
}
}

class MultiThreadDemo {
public static void main (String args[]) {
new NewThread("One"); //start threads
new NewThread("Two");
new NewThread("three");

try {
// wait for other threads to end
Thread.sleep(10000);
}catch(InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
Program no: - 4

Object: Write a program in java to create border layout manager

import java.awt.*;
import java.awt.event.*;

public class BorderLayoutExample {

public static void main(String[] args) {


Frame frame= new Frame("BorderLayout Frame");
Panel pa= new Panel();
Button ba1= new Button();
Button ba2=new Button();
Button ba3=new Button();
Button ba4=new Button();
Button ba5=new Button();
frame.add(pa);
pa.setLayout(new BorderLayout());
pa.add(new Button("Wel"), BorderLayout.NORTH);
pa.add(new Button("Come"), BorderLayout.SOUTH);
pa.add(new Button("Rose"), BorderLayout.EAST);
pa.add(new Button("India"), BorderLayout.WEST);
pa.add(new Button("RoseIndia"), BorderLayout.CENTER);
frame.setSize(300,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}

Output this program:


Program no: - 5

Object: Write a program in java to create overloading

Method overloading means having multiple methods with same name but with different
signature (number, type and order of parameters).

class OverloadDemo {
void test () {
System.out.println("No parameters");
}

//Overload test for one integer parameter .


void test (int a) {
System.out.println("a: " + a);
}

//Overload test for two integer parameters.


void test (int a, int b) {
System.out.println("a and b:" + a + " " + b);
}

//overload test for a double parameter


double test (double a) {
System.out.println("double a: " +a);
return a*a;
}
}

class Overload {
public static void main (String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;

//call all versions of test()


ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.250);
System.out.println("Result of ob.test(123.25): " + result);
}
}
Program no-6

Object: - Write a program in java for keyboard Event Handling

public class Demo extends JFrame


{
JPanel contentPane ;
JTextArea jTextAreaScreen = new JTextArea () ;
//...blah blah blah

private void init () throws Exception


{
jPanelControls.setBorder ( BorderFactory.createEtchedBorder () ) ;
contentPane = ( JPanel ) getContentPane () ;
//..blah blah blah

//trying to get jTextAreaScreen set to display number zero after user presses zero from
keyboard.
public class KeyHandler implements KeyListener
{
public void keyPressed(KeyEvent event)
{
int keyCode = event.getKeyCode();

if (keyCode == KeyEvent.VK_0)
{
jTextAreaScreen.setText("0");
}
}
public void keyReleased (KeyEvent event)
{
}

public void keyTyped(KeyEvent event)


{
char keychar = event.getKeyChar();

if (keychar == '0')
{
jTextAreaScreen.setText("0");

}
}
}
}
}
Program no: 7

Object: - Write a program in java for creating a Simple Applet

//Write a program in java for creating a Simple Applet

import java.awt.*;
import java. applet.*;
/*
<applet code="Simple Applet" width=200 height=60>
</applet>
*/

public class Simple Applet extends Applet {


public void paint (Graphics g) {
g.drawString ("A Simple Applet", 20, 20);
}
}
Program no:-8

Object: - Write a program in java for Illustrate menus

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="menudemo" width=250 height=250>
</applet>
*/
// create a subclass of Frame
class MenuFrame extends Frame{
String msg ="";
checkboxMenuitem debug, test;

MenuFrame(String title) {
super(title);

// create menu bar and add it to frame


menuBar = new menuBar();
setMenu Bar(mbar);

//create the menu items


Menu file=new menu("file");
MenuItem item1,item2,item3,item4,item5;
file.add(item1 =new MenuItem("new..."));
file.add(item2 =new MenuItem("open..."));
file.add(item3 =new MenuItem("close"));
file.add(item4 =new MenuItem("-"));
file.add(item5 =new MenuItem("Quit..."));
mbar.add(file);
Menu edit =new menu("edit");
MenuItem item6,item7,item8,item9;
edit.add(item6 =new MenuItem("Cut"));
edit.add(item7 =new MenuItem("copy"));
edit.add(item8 =new MenuItem("paste"));
edit.add(item9 =new MenuItem("-"));
menu sub =new menu("special"));
menuItem item10,item11,item12;
sub.add(item10 =new MenuItem("first"));
sub.add(item11 =new MenuItem("second"));
sub.add(item12 =new MenuItem("third"));
edit.add(sub);

// these are checkable menu items


debug = new checkBoxMenuItem("debug");
edit.add(debug);
test =new checkbaxMenuItem("Debug");
edit.add(test);

mbar.add(edit);
//create an object to handle action and item events
MyMenuHandeler = new MyMenuHandler(this);
// register it to receive those events
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
item5.addActionListener(handler);
item6.addActionListener(handler);
item7.addActionListener(handler);
item8.addActionListener(handler);
item9.addActionListener(handler);
item10.addActionListener(handler);
item11.addActionListener(handler);
item12.addActionListener(handler);
debug.addActionListerner(handler);
test.addItemListener(handler);

//create an object to handle window events


MyWindowAdapter = new MyWindowAdapter(this);
// register it to receive those events
addWindowListener(adapter);
}

public void paint (graphics g){


g.drawString(msg,10,200);
if(debug.getState())
g.drawString("Debug is on.",10,220);
else
g.drawString("Debug is off.",10,220);

if (test.getState())
g.drawString("Testing is on.".10,240);
else
g.drawString("Debug is("Testing is off.",240));
}
}
class MyWindowAdapter extends WindowAdapter {
MenuFrame menuFrame;
public MyWindowAdapter(MenuFrame menuFrame) {
this.menuFrame = menuFrame;
}
public void windowClosing(windowEvent we) {
menuFrame.SetVisible(false);
}
}

class MyMenuHandler implements ActionListener,ItemListener {


menuFrame menuFrame;
public MyMenuHandler(MenuFrame menuFrame) {
this.menuFrame = menuFrame;
}
//Handle action events
public void actionPerFormed(ActionEvent ae) {
String msg = "you selected";
String arg = (String)ae.getActionCommand();
if(arg.equals("New.."))
msg +="New.";
else if(arg.equals("Open..."))
msg +="Open.";
else if(arg.equals("Close"))
msg +="Close.";
else if(arg.equals("Quit..."))
msg +="Quit.";
else if(arg.equals("Edit"))
msg +="Edit.";
else if(arg.equals("Cut"))
msg +="Cut.";
else if(arg.equals("Copy"))
msg +="Copy.";
else if(arg.equals("Paste"))
msg +="Paste.";
else if(arg.equals("First"))
msg +="First.";
else if(arg.equals("Second"))
msg +="Second.";
else if(arg.equals("Third"))
msg +="Third.";
else if(arg.equals("Debug"))
msg +="Debug.";
else if(arg.equals("Testing"))
msg +="Testing.";
menuFrame.msg = msg;
menuFrame.repaint();
}
}
//Create frame window.
public class MenuFrame ("Menu Demo");
Frame f;
public void init() {
f = new MenuFrame("Menu Demo");
int width = Integer.parseInt(getParameter("width"));
int height = Integer.parseInt(getParameter("height"));

setSize(new Dimension(width, height));

f.setSize(width, height);
f.setVisible(true);
}

public void start() {


f.setVisible(true);
}
public void stop() {
f.setVisible(false);
}
}
Program no: - 9

Object: Write a program in java for Text fields

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="TextFieldDemo" width=380 height=150>
</applet>
*/

public class TextFieldDemo extends Applet


implements ActionListener {

TextField name, pass;

public void init() {


Label namep = new Label("Name: ", Label.RIGHT);
Label passp = new Label("Password: ", Label.RIGHT);
name = new TextField(12);
pass = new TextField(8);
pass.setEchoChar('?');

add(namep);
add(name);
add(passp);
add(pass);

//register to receive action events


name.addActionListener(this);
pass.addActionListener(this);
}
//User pressed Enter.
public void actionPerformed(ActionEvent ae) {
repaint();
}
public void paint(Graphics g) {
g.drawString("Name: " +name.getText(), 6, 60);
g.drawString("Selected text in name: "
+name.getSelectedText(), 6, 80);
g.drawString("Password:" + pass.getText(),6, 100);
}
}
Here is the output:
Program no: - 10

Object: - Write a program in java to create and manipulate Scroll bars

import java.awt.*;
import java.awt.event.*;
import java. applet.*;
/*
<applet code= "SBDemo" width=300 height=200>
</apple>
*/
public class SBDemo extends Applet
implements AdjustmentListener, MouseMotionListener {
String msg = "";
Scrollbar vertSB, horzSB;

public void init() {


int width = Integer.parseInt(getParameter("width"));
int height = Integer.parseInt(getParameter("height");
String msg = "";
Scrollbar vertSB, horzSB;

public void init() {


int width = Integer.parseInt(getParameter("width"));
int height = Integer.parseInt(getParameter("height");

verSB = new Scrollbar(Scrollbar.VERTICAL,0, 1,


0, height);
horzSB = new Scrollbar(Scrollbar.HORIZONTAL,
0, 1, 0, width);
add(vertSB);
add(horzSB);

//register to receive adjustment events


vertSB.addAdjustmentListner(this);
horzSB.addAdjustmentListener(this);

addMouseMotionListener(this);
}

public void adjustmentValueChanged(AdjustmentEvent ae) {


repaint();
}
//Upadte scroll bars to reflect mouse dragging.
public void mouseDragged(MouseEvent me) {
int x = me.getX();
int y = me.getY();
vertSB.setValue(Y);
horzSB.setValue(X);
repaint();
}

//Necessary for MouseMotionListener


public void paint (Graphics g)

Here is the output:


Program no: - 11
Object: - write a program in java for illustrating menus.

import java.awt.*;
import java.awt.event.*;

public class MainWindow extends Frame {


public MainWindow() {
super("Menu Window");
setSize(400, 400);
FileMenu fileMenu = new FileMenu(this);
HelpMenu helpMenu = new HelpMenu(this);
MenuBar mb = new MenuBar();
mb.add(fileMenu);
mb.add(helpMenu);
setMenuBar(mb);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
exit();
}
});
}

public void exit() {


setVisible(false);
dispose();
System.exit(0);
}

public static void main(String args[]) {


Main Window w = new Main Window();
w.setVisible(true);
}
}

class FileMenu extends Menu implements ActionListener {


MainWindow mw;
public FileMenu(MainWindow m) {
super("File");
mw = m;
MenuItem mi;
add(mi = new MenuItem("Open"));
mi.addActionListener(this);
add(mi = new MenuItem("Close"));
mi.addActionListener(this);
add(mi = new MenuItem("Exit"));
mi.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {


String item = e.getActionCommand();
if (item.equals("Exit"))
mw.exit();
else
System.out.println("Selected FileMenu " + item);
}
}

class HelpMenu extends Menu implements ActionListener {


MainWindow mw;
public HelpMenu(MainWindow m) {
super("Help");
mw = m;
MenuItem mi;
add(mi = new MenuItem("Basics"));
mi.addActionListener(this);
add(mi = new MenuItem("Advanced"));
mi.addActionListener(this);
addSeparator();
add(mi = new CheckboxMenuItem("Manual"));
mi.addActionListener(this);

Menu subMenu = new Menu("Miscellaneous");


subMenu.add(mi = new MenuItem("Help"));
mi.addActionListener(this);
subMenu.add(mi = new MenuItem("Other Option"));
mi.addActionListener(this);
add(subMenu);
}

public void actionPerformed(ActionEvent e) {


String item = e.getActionCommand();
if (item.equals("Basics"))
System.out.println("Basics");
else if (item.equals("Help"))
System.out.println("Help");
}
}

Output of the program:


Program no:-12

Object: - Write a program in java for Mouse Event Handling

import java.awt.*;
import java.awt.event.*;

public class MouseClick {


Label lbl;
public static void main(String[] args) {
MouseClick MC = new MouseClick();
}

public MouseClick(){
Frame f = new Frame("Checking the mouse click");
Panel p = new Panel();
Button button = new Button("Click Me");
button.addMouseListener(new MyMouseListener());
p.add(button, BorderLayout.NORTH);
f.add(p,BorderLayout.NORTH);
lbl = new Label("Roseindia.net");
f.add(lbl, BorderLayout.CENTER);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
f.setSize(400,400);
f.setVisible(true);
}

public class MyMouseListener extends MouseAdapter{


public void mouseClicked(MouseEvent me){
String str = lbl.getText();
if (str.equals("Roseindia.net")){
lbl.setText("You have clicke the
button.");
}
else if (str.equals("You have clicke the
button.")){
lbl.setText("Roseindia.net");
}
}
}
}

Das könnte Ihnen auch gefallen