JP-0
class StarPattern1
{
public static void main(String args[])
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++) ///for(j=1;j<=4){4*4} ///for(j=4;j>=i;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}
class StarPattern4 { public static void main(String args[]) {
for(int i=1;i<=4;i++) { for(int j=4;j>=i;j--) //for(int j=1;j<=i;j++) {
System.out.print(" "); } for(int k=1;k<=i;k++) //for(int k=4;k>=i;k--) { System.out.print("*"); } System.out.println(); } } }
Print “Hello World”
Code:-
class Simple
{
public static void main(String args[])
{
System.out.println("Hello world");
}
}
Output:-
Write a java program to display table of a number
Code:-
class Simple
{
public static void main(String args[])
{
int val=1;
int i;
System.out.println("Table of 2");
for(i=1;i<11;i++)
{
val=2*i;
System.out.println(val);
}
}
}
Output:-
Create a new class with name demo
Code:-
class demo
{
void Table(int x)
{
int val=1;
int i;
System.out.println("Table of "+x);
for(i=1;i<11;i++)
{
val=x*i;
System.out.println(val);
}
}
}
class Simple
{
public static void main(String args[])
{
demo i1=new demo();
i1.Table(6);
}
}
Output:-
Practical 1A
Write a java code to create a class and copy constructor
Code:-
public class Fruit
{
private double fprice;
private String fname;
Fruit(double fPrice,String fName)
{
fprice=fPrice;
fname=fName;
}
Fruit(Fruit fruit)
{
System.out.println("\nAfter invoking Copy Constructer:\n");
fprice=fruit.fprice;
fname=fruit.fname;
}
double showPrice()
{
return fprice;
}
String showName()
{
return fname;
}
public static void main(String args[])
{
Fruit f1=new Fruit(450,"Mango");
System.out.println("Name of the First Fruit: "+f1.showName());
System.out.println("Price of the First Fruit: "+f1.showPrice());
Fruit f2=new Fruit(f1);
System.out.println("Name of the Second Fruit: "+f2.showName());
System.out.println("Price of the Second Fruit: "+f2.showPrice());
}
}
Output:-
Write a java program to demonstrate Constructer Overloading
Code:-
class Box
{
double width, height, depth;
Box(double w,double h,double d)
{
width=w;
height=h;
depth=d;
}
Box()
{
width=height=depth=0;
}
Box(double len)
{
width=height=depth=len;
}
double volume()
{
return width*height*depth;
}
}
public class Test
{
public static void main(String args[])
{
Box mybox1=new Box(10,20,15);
Box mybox2=new Box();
Box mycube=new Box(7);
double vol;
vol=mybox1.volume();
System.out.println("Volume of mybox1: "+vol);
vol=mybox2.volume();
System.out.println("Volume of mybox2: "+vol);
vol=mycube.volume();
System.out.println("Volume of mycube: "+vol);
}
}
Output:-
Write a java program for default constructor
Code:-
class Bike11
{
Bike11()
{
System.out.println("Bike is created");
}
public static void main(String args[])
{
Bike11 b=new Bike11();
}
}
Output:-
Practical 1B
Write a program to implement method overloading
Code:-
class Adder
{
static int add(int a,int b)
{
return a+b;
}
static int add(int a,int b,int c)
{
return a+b+c;
}
}
class TestOverloading
{
public static void main(String args[])
{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}
}
Output:
Practical 1C
Write a program to implement static methods
Code:
class Calculate
{
static int cube(int x)
{
return x*x*x;
}
public static void main(String args[])
{
int result=Calculate.cube(5);
System.out.print(result);
}
}
Output:
Write java program to illustrate static variables
Code:
> With static keyword
class Counter
{
static int count=0;
Counter()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Output:
> Without static variable
class Counter
{
int count=0;
Counter()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Output:
1a.) Write a Java program to implement types of inheritance
Simple Inheritance
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}
}
Output:
Multilevel Inheritance
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
}
class TestInheritance
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
Output:
Hierarchical Inheritance
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
}
}
class TestInheritance
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
}
}
Output:
1b.) Write a java program to implement Method Overriding
Code:
class Vehicle
{
void run()
{
System.out.println("Vehicle is running");
}
}
class Bike2 extends Vehicle
{
void run()
{
System.out.println("Bike is running safely");
}
public static void main(String args[])
{
Bike2 obj=new Bike2();
obj.run();
}
}
Output:
2.) Write a java program to implement Abstract class
Code:
abstract class Shape
{
abstract void draw();
}
class Rectangle extends Shape
{
void draw()
{
System.out.println("Drawing rectangle");
}
}
class Circle extends Shape
{
void draw()
{
System.out.println("Drawing Circle");
}
}
class TestAbstraction
{
public static void main(String args[])
{
Shape s=new Circle();
s.draw();
}
}
Output:
3.) Write a java program to implement interface
Code:
interface printable
{
void print();
}
class A6 implements printable
{
public void print()
{
System.out.println("Hello");
}
public static void main(String args[])
{
A6 obj=new A6();
obj.print();
}
}
Output:
Code 2:
interface Drawable
{
void draw();
}
class Rectangle implements Drawable
{
public void draw()
{
System.out.println("drawing rectangle");
}
}
class Circle implements Drawable
{
public void draw()
{
System.out.println("drawing circle");
}
}
class TestInterface
{
public static void main(String args[])
{
Drawable d=new Circle();
d.draw();
}
}
Output:
Practical 3A
Write a Java Program to raise built in exception such as
> Arithmetic exception
Code:
class ArithmeticException_Demo
{
public static void main(String args[])
{
try
{
int a = 30, b = 0;
int c = a / b; // cannot divide by zero
System.out.println("Result = " + c);
}
catch (ArithmeticException e)
{
System.out.println("Can't divide a number by 0");
}
}
}
Output:
> Arrayindexoutofbounds exception
Code:
class ArrayIndexOutOfBound_Demo
{
public static void main(String args[])
{
try
{
int a[] = new int[5];
a[6] = 9;
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index is Out Of Bounds");
}
}
}
Output:
> Classnotfound exception
Code:
public class GFG
{
public static void main(String args[])
{
try
{
Class.forName("GeeksForGeeks");
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
}
}
Output:
> Filenotfound exception
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
class File_notFound_Demo
{
public static void main(String args[])
{
try
{
File file = new File("E:// file.txt");
FileReader fr = new FileReader(file);
}
catch (FileNotFoundException e)
{
System.out.println("File does not exist");
}
}
}
Output:
> IO exception
Code:
import java.io.*;
class Geeks
{
public static void main(String args[])
{
try
{
FileInputStream f = null;
f = new FileInputStream("abc.txt");
int i;
while ((i = f.read()) != -1)
{
System.out.print((char)i);
}
f.close();
}
catch (IOException e)
{
System.out.println("IO Exception");
}
}
}
Output:
> Nullpointer exception
Code:
class NullPointer_Demo
{
public static void main(String args[])
{
try
{
String a = null; // null value
System.out.println(a.charAt(0));
}
catch (NullPointerException e)
{
System.out.println("NullPointerException..");
}
}
}
Output:
> Numberformat exception
Code:
class NumberFormat_Demo
{
public static void main(String args[])
{
try
{
int num = Integer.parseInt("akki");
System.out.println(num);
}
catch (NumberFormatException e)
{
System.out.println("Number format exception");
}
}
}
Output:
> Stringindexoutofbounds exception
Code:
class StringIndexOutOfBound_Demo
{
public static void main(String args[])
{
try
{
String a = "This is like chipping ";
char c = a.charAt(24);
System.out.println(c);
}
catch (StringIndexOutOfBoundsException e)
{
System.out.println("StringIndexOutOfBoundsException");
}
}
}
Output:
PRACTICAL 3B
Write a java program to define user defined exception
Code:
class MyException extends Exception
{
public MyException(String s)
{
super(s);
}
}
public class Main
{
public static void main(String args[])
{
try
{
throw new MyException("Java Practical");
}
catch (MyException ex)
{
System.out.println("Caught User Defined Exception");
System.out.println(ex.getMessage());
}
}
}
Output:
Code:
class MyException extends Exception
{
}
public class setText
{
public static void main(String args[])
{
try
{
throw new MyException();
}
catch (MyException ex)
{
System.out.println("Caught Exception");
System.out.println(ex.getMessage());
}
}
}
Output:
Creating Threads
Code:
public class StartExp1 extends Thread
{
public void run()
{
for(i=0;i<=6;i++)
{
System.out.println(i);
}
}
public static void main(String[] args)
{
StartExp1 t1= new StartExp1();
t1.start();
}
}
- class Multi extends Thread{
- public void run(){
- System.out.println("thread is running...");
- }
- public static void main(String args[]){
- Multi t1=new Multi();
- t1.start();
- }
- }
Output:
By implementing Runnable Interface
Code:
public class StartExp2 implements Runnable
{
public void run()
{
System.out.println("Threading is running...");
}
public static void main(String[] args)
{
StartExp2 m1= new StartExp2();
Thread t1= new Thread(m1);
t1.start();
}
}
Output:
JAVA Swing
Q.1) Write a java program to design a swing application that randomly changes the colour on button click
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Swing1 implements ActionListener {
JFrame f;
Swing1() {
f = new JFrame();
JButton a = new JButton("blue");
a.setBounds(40, 100, 100, 40);
a.setBackground(Color.BLUE);
JButton b = new JButton("red");
b.setBounds(150, 100, 100, 40);
b.setBackground(Color.RED);
a.addActionListener(this);
b.addActionListener(this);
f.add(a);
f.add(b);
f.setSize(400, 500);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("red")) {
f.getContentPane().setBackground(Color.RED);
} else if (e.getActionCommand().equals("blue")) {
f.getContentPane().setBackground(Color.BLUE);
}
}
public static void main(String args[]) {
Swing1 c = new Swing1();
}
}
Output:
> Text Field
Code:
import javax.swing.*;
public class SwingExample {
public static void main(String[] args) {
JFrame f = new JFrame();
JTextField textField = new JTextField();
textField.setBounds(130, 100, 100, 40);
JButton button = new JButton("Click Here");
button.setBounds(130, 150, 100, 40);
f.add(textField);
f.add(button);
f.setSize(400, 500);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
> Image
Code:
import javax.swing.*;
public class SwingExample {
public static void main(String[] args) {
JFrame f = new JFrame();
JTextField textField = new JTextField();
textField.setBounds(130, 100, 100, 40);
JButton button = new JButton(new ImageIcon("C:\\Car.jfif"));
button.setBounds(130, 150, 100, 40);
f.add(textField);
f.add(button);
f.setSize(400, 500);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
import java.awt.*;
import java.awt.event.*;
public class MouseMotionAdapterExample extends MouseMotionAdapter {
Frame f;
MouseMotionAdapterExample() {
f = new Frame ("Mouse Motion Adapter");
f.addMouseMotionListener (this);
f.setSize (300, 300);
f.setLayout (null);
f.setVisible (true);
}
public void mouseDragged (MouseEvent e) {
Graphics g = f.getGraphics();
g.setColor (Color.ORANGE);
g.fillOval (e.getX(), e.getY(), 20, 20);
}
public static void main(String[] args) {
new MouseMotionAdapterExample();
}
}
------
import java.awt.*;
import java.awt.event.*;
public class KeyAdapterExample extends KeyAdapter {
Label l;
TextArea area;
Frame f;
KeyAdapterExample() {
f = new Frame ("Key Adapter");
l = new Label();
l.setBounds (20, 50, 200, 20);
area = new TextArea();
area.setBounds (20, 80, 300, 300);
area.addKeyListener(this);
f.add(l);
f.add(area);
f.setSize (400, 400);
f.setLayout (null);
f.setVisible (true);
}
public void keyReleased (KeyEvent e) {
String text = area.getText();
String words[] = text.split ("\\s");
l.setText ("Words: " + words.length + " Characters:" + text.length());
}
public static void main(String[] args) {
new KeyAdapterExample();
}
}
--------
import java.awt.*;
import java.awt.event.*;
public class MouseAdapterExample extends MouseAdapter {
Frame f;
MouseAdapterExample() {
f = new Frame ("Mouse Adapter");
f.addMouseListener(this);
f.setSize (300, 300);
f.setLayout (null);
f.setVisible (true);
}
public void mouseClicked (MouseEvent e) {
Graphics g = f.getGraphics();
g.setColor (Color.BLUE);
g.fillOval (e.getX(), e.getY(), 30, 30);
}
public static void main(String[] args) {
new MouseAdapterExample();
}
}
------------
import java.awt.*;
import java.awt.event.*;
public class AdapterExample {
Frame f;
AdapterExample() {
f = new Frame ("Window Adapter");
f.addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
f.dispose();
}
});
f.setSize (400, 400);
f.setLayout (null);
f.setVisible (true);
}
public static void main(String[] args) {
new AdapterExample();
}
}
----------
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent(){
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
b.addActionListener(this);
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
tf.setText("Welcome");
}
public static void main(String args[]){
new AEvent();
}
}
---------
import java.awt.*;
import java.awt.event.*;
public class KeyListenerExample extends Frame implements KeyListener {
Label l;
TextArea area;
KeyListenerExample() {
l = new Label();
l.setBounds (20, 50, 100, 20);
area = new TextArea();
area.setBounds (20, 80, 300, 300);
area.addKeyListener(this);
add(l);
add(area);
setSize (400, 400);
setLayout (null);
setVisible (true);
}
public void keyPressed (KeyEvent e) {
l.setText ("Key Pressed");
}
public void keyReleased (KeyEvent e) {
l.setText ("Key Released");
}
public void keyTyped (KeyEvent e) {
l.setText ("Key Typed");
}
public static void main(String[] args) {
new KeyListenerExample();
}
}
---------
import java.awt.*;
import java.awt.event.*;
public class MouseListenerExample extends Frame implements MouseListener{
Label l;
MouseListenerExample(){
addMouseListener(this);
l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
}
}
Comments
Post a Comment