Java Lab
Java Lab
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;
Output
http://www.restcomputers.com/
JAVA LAB MANUAL
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
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
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()
{
Output
http://www.restcomputers.com/
JAVA LAB MANUAL
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
import javax.swing.*;
//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
Output
http://www.restcomputers.com/
JAVA LAB MANUAL
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
{
http://www.restcomputers.com/
JAVA LAB MANUAL
}
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
Output
http://www.restcomputers.com/
JAVA LAB MANUAL
}
}
Output
http://www.restcomputers.com/
JAVA LAB MANUAL
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
Output
http://www.restcomputers.com/
JAVA LAB MANUAL
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
http://www.restcomputers.com/
JAVA LAB MANUAL
Output
http://www.restcomputers.com/
JAVA LAB MANUAL
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AWTAttack extends JFrame
{
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
}
}
});
}
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
import java.applet.*;
import java.awt.*;
/*
<applet code="AppletBeing.class" width=300 height=100>
</applet>
*/
Output
http://www.restcomputers.com/
JAVA LAB MANUAL
http://www.restcomputers.com/
JAVA LAB MANUAL
import java.awt.*;
import java.applet.Applet;
Output
http://www.restcomputers.com/
JAVA LAB MANUAL
http://www.restcomputers.com/