0% found this document useful (0 votes)
132 views

Java Lab

This Document covers various concepts of Java Programming Language. It can be used as a Java Lab Manual. It is used as Lab Manual in our college. It is written in Advanced English. Programs are written in such a way that you are motivated to write your own funny programs. This document is well suited as Lab Manual for Computer Science & Engineering students of JNTU Anantapur, SBTET Andhra Pradesh, Sree Vidyanikethan Engineering College, JNTU Pulivendula, JNTU Kalikiri, G Pulla Reddy College of Engineering & Technology Kurnool, JNTU Kakinada, JNTU Hyderabad, Osmania University, Andhra University, JNTU Vizianagaram, JNTU Narasaraopet, Ananthalakshmi Institute of Technology and Sciences Anantapuram, SK University (SKU), Sanskrithi School Of Engineering Puttaparthi, P.V.K.K Institute of Technology Sanapa Road, Rudrampeta, Anantapur, Andhra Pradesh, Srinivasa Ramanujan Institute of Technology Anantapur, SRIT Anantapur, Sagi Ramakrishnam Raju Engineering College Bhimavaram, Gayatri Vidya Parishad College of Engineering Visakhapatnam, RVR & JC College of Engineering Guntur, Acharya Nagarjuna University Guntur, Koneru Lakshmaiah (KL) University Guntur, SVU Tirupati, Kakatiya University Warangal, Sri Venkateswara University Tirupati, Bapatla Engineering College Bapatla, Velagapudi Ramakrishna Siddhartha Engineering College Vijayawada, V R Siddhartha Engineering College Vijayawada, VR Siddhartha Engineering College Vijayawada.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views

Java Lab

This Document covers various concepts of Java Programming Language. It can be used as a Java Lab Manual. It is used as Lab Manual in our college. It is written in Advanced English. Programs are written in such a way that you are motivated to write your own funny programs. This document is well suited as Lab Manual for Computer Science & Engineering students of JNTU Anantapur, SBTET Andhra Pradesh, Sree Vidyanikethan Engineering College, JNTU Pulivendula, JNTU Kalikiri, G Pulla Reddy College of Engineering & Technology Kurnool, JNTU Kakinada, JNTU Hyderabad, Osmania University, Andhra University, JNTU Vizianagaram, JNTU Narasaraopet, Ananthalakshmi Institute of Technology and Sciences Anantapuram, SK University (SKU), Sanskrithi School Of Engineering Puttaparthi, P.V.K.K Institute of Technology Sanapa Road, Rudrampeta, Anantapur, Andhra Pradesh, Srinivasa Ramanujan Institute of Technology Anantapur, SRIT Anantapur, Sagi Ramakrishnam Raju Engineering College Bhimavaram, Gayatri Vidya Parishad College of Engineering Visakhapatnam, RVR & JC College of Engineering Guntur, Acharya Nagarjuna University Guntur, Koneru Lakshmaiah (KL) University Guntur, SVU Tirupati, Kakatiya University Warangal, Sri Venkateswara University Tirupati, Bapatla Engineering College Bapatla, Velagapudi Ramakrishna Siddhartha Engineering College Vijayawada, V R Siddhartha Engineering College Vijayawada, VR Siddhartha Engineering College Vijayawada.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

JAVA LAB MANUAL

// 1. Program to illustrate the use of built-in data types available in java

class DataTypeDrive
{
public static void main(String[] arguments)
{
byte a=64; // Size : 1 Byte ( 8 bits) - Range : -128 to 127
char b; // Size : 2 Bytes (16 bits) - Range : 0 to 65,536.
short c=111; // Size : 2 Bytes (16 bits) - Range : -32768 to 32767
boolean d; // Size : unknown - Values: true or false
int e=46656; // Size : 4 Bytes (32 bits) - Range : -2147483648 to 2147483647
long f=1354355; // Size : 8 Bytes (64 bits) - Range : -9223372036854775808 to 9223372036854775807
float g; // Size : 4 Bytes (32 bits) - Range : 1.4e–045 to 3.4e+038
double h; // Size : 8 Bytes (64 bits) - Range : 4.9e–324 to 1.8e+308

b='v';
g=435.543f;
h=454545.457456435;
d=true;

System.out.println("a value : "+a);


System.out.println("b value : "+b);
System.out.println("c value : "+c);
System.out.println("d value : "+d);
System.out.println("e value : "+e);
System.out.println("f value : "+f);
System.out.println("g value : "+g);
System.out.println("h value : "+h);
}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 2. program to express the use of Overloading

class OverloadMan
{
public static void main(String[] Overload)
{
guess();
guess(11);
guess(34.45f,456);
guess("java");
guess(true);
}
static void guess()
{
System.out.println("I guess you don't like void");
}
static void guess(int num)
{
System.out.println("I guess you are feeding Integers");
}
static void guess(float num, int num2)
{
System.out.println("I guess you enjoy floating point numbers as well as Integers");
}
static void guess(String s)
{
System.out.println("I guess you are with String");
}
static void guess(boolean boolMan)
{
System.out.println("I guess you love either true or false");
}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 3. program to show the function of inheritance

class Monster
{
public void kill()
{
System.out.println("Monster : I shall make you dead");
}
}
class God extends Monster
{
public void life()
{
System.out.println("God : I shall make you alive");
}
}
class Human extends God
{
public void killOrLife()
{
System.out.println("Human : I shall make you face dead/alive");
}
}
public class InheritDear
{
public static void main(String[] inheritGoodQualities)
{
Human humanObject = new Human();
humanObject.killOrLife();
humanObject.life();
humanObject.kill();
}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 4. program to demonstrate the partial use of super keyword in java

class Man
{
public void features()
{
System.out.println("Man can walk");
System.out.println("Man can run");
System.out.println("Man can sleep");
System.out.println("Man can eat");
}
}
class SuperMan extends Man
{
public void features()
{

super.features(); //invokes super class method


System.out.println("\nbut\n");
System.out.println("SuperMan can fly");
System.out.println("SuperMan can jump");
System.out.println("SuperMan can help\n");
}
}
public class SuperTest
{
public static void main(String args[])
{
Man man=new SuperMan(); //Super class object can refer any subclass object
man.features();
}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 5. Program to explain the concept of Overriding

class Overriding
{
public static void main(String[] overRiding)
{
Dog dog=new Dog();
Cat cat=new Cat();
dog.makeNoise();
cat.makeNoise();
}
}
class Animal
{
public void makeNoise()
{
System.out.println("Animals make noise");
}
}
class Dog extends Animal
{
public void makeNoise()
{
System.out.println("Dog sounds bow bow");
}
}
class Cat extends Animal
{
public void makeNoise()
{
System.out.println("Cow sounds meow meow");
}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 6. Program to present the various uses of final keyword

import javax.swing.*;

public class FinalBeing extends FinalMan


{

public static void main(String[] finalBeing)


{
final String iAm = "Southpaw";

//iAm="Great"; if you let in this statement, then it creates an error, as you know that you can't modify a
final variable

System.out.println(iAm);

/* void unSelfishness()
{
System.out.println("Unselfishness greatly yields you much happiness and satisfaction");
} */ //if you let in the code which is in multi-line comments, then it promptes you an error as you can't
override a method which is declared as final

unSelfishness();

}
}
class FinalMan // extends FinalDay //you can't extend or inherit the class which is stated as final
{
public static final void unSelfishness()
{
JOptionPane.showMessageDialog(null,"Go Higher Through Unselfish Contribution");
}

}
final class FinalDay
{
void proud()
{
JOptionPane.showMessageDialog(null,"Be grateful for all the good things you did in your life");
}
}

http://www.restcomputers.com/
JAVA LAB MANUAL

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 7. Program on importing packages

import javax.swing.*; // imports all classes from javax.swing package


import java.util.Date; //imports Date class from java.util package
class ImportMedia
{
public static void main(String[] importOrExport)
{
Date todayDate = new Date();
System.out.println(todayDate);
JOptionPane.showMessageDialog(null,"Welcome to Game Changers");
}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 8. Program to point out the role of interface

class HumanBeingShow
{
public static void main(String[] showOrDrop)
{
ViratKohli VK = new ViratKohli();
MSDhoni MSD = new MSDhoni();

System.out.println("\n\n\n"+VK.name());
VK.eat();
VK.newspaper();
VK.bicycle();
VK.humanity();
VK.age();
VK.favourite();
VK.iQuote();
System.out.println("\n\n\n"+MSD.name());
MSD.eat();
MSD.newspaper();
MSD.bicycle();
MSD.humanity();
MSD.age();
MSD.favourite();
MSD.iQuote();
}
}

interface HumanBeing
{
public String name();
public void eat();
public void newspaper();
public void bicycle();
public void humanity();
public void age();
public void favourite();
public void iQuote();
}
class ViratKohli implements HumanBeing
{

public String name()


{
return "I'm Virat Kohli";
}
public void eat()
{
System.out.println("I love eating Chocolates");
}
public void newspaper()
{
System.out.println("I enjoy reading \"The Hindu\"");
}

http://www.restcomputers.com/
JAVA LAB MANUAL

public void bicycle()


{
System.out.println("My Favourite Cycle : \"Hero\" ");
}
public void humanity()
{
System.out.println("Humanity is my birth right");
}
public void favourite()
{
System.out.println("My Favourite Animal : Dog");
System.out.println("My Favourite Drink : Tequila Ley .925");
System.out.println("My Favourite Car : Audi R8");
System.out.println("My Favourite Bike : Harley Davidson");
}
public void age()
{
System.out.println("I'm born on 05/11/1988");
System.out.println("I'm not 27, I'm 16 with 11 years of experience");
}
public void iQuote()
{
System.out.println("Word Hard, Train Hard, Play with all heart - VK");
}

}
class MSDhoni implements HumanBeing
{
public void eat()
{
System.out.println("I love eating Chocolates");
}
public void newspaper()
{
System.out.println("I love reading \"The Indian Express\"");
}
public void bicycle()
{
System.out.println("My Best Cycle \"Hercules\"");
}
public void humanity()
{
System.out.println("WE CAN'T HELP EVERYONE, BUT EVERYONE CAN HELP SOMEONE");
}
public String name()
{
return "My Name is Mahendra Singh Dhoni";
}
public void age()
{
System.out.println("I'm born on 07/07/1981");
System.out.println("I'm not 34, I'm 20 with 14 years of experience");
}

http://www.restcomputers.com/
JAVA LAB MANUAL

public void iQuote()


{
System.out.println("I believe in giving more than 100% on the field, and I don't really worry about the result
if there's great commitment on the field. That's victory for me - MSD");
}
public void favourite()
{
System.out.println("My Favourite Animal : Cat");
System.out.println("My Favourite Drink : Dalmore 62");
System.out.println("My Favourite Car : Ferrari 488 SPIDER");
System.out.println("My Favourite Bike : Royal Enfield");
}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 9. Program to display the habit of exception handling (piece 1)

public class ExceptionTest


{
public static void main(String[] tryOrCatch)
{
try
{
String[] student = new String[5];
System.out.println(student[11]);
}
catch(Exception faultObject)
{
System.out.println("Exception Caught : "+faultObject);
}

}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 9. Program to display the habit of exception handling (piece 2)

class ExceptionExam
{
public static void main(String[] dreamOrDie)
{
String[] attitude=new String[11];
try
{
System.out.println("Attitude of 99"+attitude[99]);
}
catch(ArrayIndexOutOfBoundsException errorHulk)
{
System.out.println("Exception caught : "+errorHulk);
}
finally
{
attitude[10]="Never Say Die";
System.out.println("Attitude of 10 : "+attitude[10]);
}
}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 10. Program to illustrate, how we can produce our own Exceptions

class FeeDueException extends Exception


{
FeeDueException()
{
System.out.println("FeeDueException");
}
}
public class ManufacturingAnException
{
public void getProvisionals(int fee) throws FeeDueException
{
if(fee<3500)
{
throw new FeeDueException();
}
else
{
System.out.println("You can get your Provisionals");
}
}
public static void main(String[] exceptionEnemy)
{
ManufacturingAnException exceptionObject = new ManufacturingAnException();
try
{
exceptionObject.getProvisionals(3000);
}
catch(FeeDueException e)
{
System.out.println("I caught it, as you know MSD won't drop any tough catches...!!!");
}
}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 11. Program on Threads by extending Thread Class


class Dharam extends Thread
{
Thread t=new Thread();
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("Task1");
try
{
Thread.sleep(100);
}
catch(Exception iAmErrorObject)
{
System.out.println(iAmErrorObject);
}
}
}
}
class Dharam2 extends Thread
{
Thread t=new Thread();
public void run()
{
int i=0;
for(String v="Task2";i<10;i++)
{
System.out.println(v);
try
{
Thread.sleep(100);
}
catch(Exception iAmFaultMan)
{
System.out.println(iAmFaultMan);
}
}
}
}
class ThreadGuy
{
public static void main(String[] args)
{
Dharam dharamObject1=new Dharam();
Dharam2 dharamObject2=new Dharam2();
dharamObject1.start();
dharamObject2.start();
}
}

http://www.restcomputers.com/
JAVA LAB MANUAL

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 12. Program which presents the reward of join() method along with threads

class ThreadTack
{
public static void main(String[] tackleAThread) throws Exception
{
TeekThread Phenomenal = new TeekThread();
TeekThread SouthPaw = new TeekThread();
Phenomenal.start(); Phenomenal.join();
SouthPaw.start(); SouthPaw.join();
}
}
class TeekThread extends Thread
{
static int weigh=324324;
public void run()
{
try{ if(weigh==324324)
{
System.out.println("I'm Phenomenal");
Thread.sleep(10);
}
else
System.out.println("Kings Never Die");
weigh++;
}
catch(Exception e)
{

}
}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 13. Program on threads by implementing Runnable interface

class ThreadBozo implements Runnable


{
private Thread threadSoul;
private String threadName;
ThreadBozo(String active)
{
threadName = active;
System.out.println(threadName+" is Loading");
}
public void run()
{
System.out.println("Running Thread : "+threadName );
try
{
for(int bit=0;bit<=11;bit++)
{
System.out.println(threadName+" finishes Project"+bit);
Thread.sleep(50);
}
}
catch(Exception iAmBug)
{
System.out.println("Exception Arrested : "+iAmBug);
}
System.out.println("I'm "+threadName+", I'm expiring, as my time has come to leave this mighty
planet\"JAVA\"");
}
public void start()
{
System.out.println(threadName +"commences");
if (threadSoul==null)
{
threadSoul = new Thread (this, threadName);
threadSoul.start ();
}
}
}
public class ThreadDrive
{
public static void main(String args[])
{
ThreadBozo threadAce= new ThreadBozo( "Thread1");
threadAce.start();
ThreadBozo threadDeuce = new ThreadBozo( "Thread2");
threadDeuce.start();
}
}

http://www.restcomputers.com/
JAVA LAB MANUAL

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 14. Program which implements ActionListener along with Swing Components

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AWTAttack extends JFrame
{

public static void main(String[] args)


{
new AWTAssassin();
}
}
class AWTAssassin extends JFrame
{
JButton edge;
JLabel label;
AWTAssassin()
{
setVisible(true);
setSize(250,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
edge = new JButton("OK");
label = new JLabel();

edge.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent action)
{
label.setText("Welcome to the world of Assassins");
add(label);
}
});
add(edge);
}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 15. Program to convey the use of DataInputStream class from package java.io.DataInputStream

import java.io.DataInputStream;
class InputKeyboard
{
//@SuppressWarnings("deprecation")
@SuppressWarnings("deprecation")
public static void main(String readingInputFromStandardInputDevice[])
{
String a;
try

{
DataInputStream ins=new DataInputStream(System.in);
a=ins.readLine();
System.out.println(a);
}
catch(Exception iAmRealTimeEntity)
{
System.out.println(iAmRealTimeEntity);
}
}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 16. Program which accepts input as one text file and prints it along with line numbers

import java.io.*;
import java.util.*;
public class FileFellow
{
public static void main(String[] args)
{
try
{
FileInputStream f=new FileInputStream("CoderCharacteristics.txt");
LineNumberReader lr=new LineNumberReader(new InputStreamReader(f));
String data;
while((data=lr.readLine())!= null)
{
System.out.println(lr.getLineNumber()+":"+data);
}
System.out.println("Total Lines"+lr.getLineNumber());
f.close();
}
catch (Exception e)
{
System.out.println("err"+e);
}
}
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

// 17.Program on Files using Graphical User Interface

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class GreatFile extends JFrame


{
JLabel label;
JTextField textField;
JButton button;
public GreatFile()
{
setLayout(new FlowLayout());
label = new JLabel("Enter text to insert into the file");
add(label);
textField = new JTextField(10);
add(textField);
button = new JButton("Save");
add(button);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent GF)
{
try
{
String textIn= textField.getText();
FileWriter stream = new FileWriter("irrevocable.txt");
BufferedWriter bf = new BufferedWriter(stream);
bf.write(textIn);
bf.close();
}
catch(Exception king)
{

}
}
});

}
public static void main(String[] swingFile)
{
GreatFile gf = new GreatFile();
gf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gf.setVisible(true);
gf.setSize(200,120);
gf.setTitle("Great Note");
}
}

http://www.restcomputers.com/
JAVA LAB MANUAL

Output

After Entering the text in the respective text field, click on Save button

Then, a file named irrevocable.txt will be created in your current directory with the above text which you provided

http://www.restcomputers.com/
JAVA LAB MANUAL

// 18. Program to express the life cycle of Applet

import java.applet.*;
import java.awt.*;

/*
<applet code="AppletBeing.class" width=300 height=100>
</applet>
*/

public class AppletBeing extends Applet


{

public void init()


{
System.out.println("init method started");
}
public void start()
{
System.out.println("start method started");
}
public void paint(Graphics g)
{
System.out.println("paint method started");
}
public void stop()
{
System.out.println("stop method started");
}
public void destroy()
{
System.out.println("destroy method started");
}

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

http://www.restcomputers.com/
JAVA LAB MANUAL

// 19. Program to draw some graphics on Applet

import java.awt.*;
import java.applet.Applet;

/* <applet code="AppletSun.class" width="400" height="300">


</applet> */
public class AppletSun extends Applet
{

public void paint(Graphics g)


{
g.setColor(Color.RED);
g.drawOval(20,10,50,50);
g.drawLine(15,30,180,280);
g.drawRect(110,10,100,50);
g.drawString("INDIA",130,40);
g.drawOval(260,10,50,50);
g.drawLine(310,30,400,400);

Output

http://www.restcomputers.com/
JAVA LAB MANUAL

http://www.restcomputers.com/

You might also like