Java
Java
Source Code:
import java.io.*;
class lamp
{
boolean ison;
void turnon()
{
ison=true;
System.out.println("Light on?"+ison);
}
void turnoff()
{
ison=false;
System.out.println("Light on?"+ison);
}
public static void main(String[] args)
{
lamp led =new lamp();
lamp halogen =new lamp();
led.turnon();
halogen.turnoff();
}
}
OUTPUT:
Light on?true
Light on?false
2)Program Using Constructor
Source Code:
import java.io.*;
class student
{
int rollno;
String name;
student(int rollno,String name)
{
this.rollno=rollno;
this.name=name;
}
public static void main(String[] args)
{
student st=new student(01,"Bhooshi");
System.out.println("studentname="+st.name+" student rollno"+st.rollno);
}
}
OUTPUT:
Source Code:
import java.io.*;
class command
{
public static void main(String[] args)
{
System.out.println("command line Arguments");
System.out.println("The given String are");
for(String str:args)
{
System.out.println(str);
}
}
}
OUTPUT:
Source Code:
import java.util.Random;
class generaterandom
{
public static void main(String[] agrs)
{
Random random=new Random();
System.out.println("A random int:"+random.nextInt());
System.out.println("A random int from 0 to 49:"+random.nextInt(50));
System.out.println("A random double:"+random.nextDouble());
System.out.println("A random float:"+random.nextFloat());
System.out.println("A random long:"+random.nextLong());
}
}
OUTPUT:
A random int:-575546384
A random int from 0 to 49:9
A random double:0.9877748287289448
A random float:0.26873827
A random long:3476157426425303757
5) Program using Vector
Source Code:
import java.util.Vector;
class v
{
public static void main(String[] args)
{
Vector<String> vec=new Vector<>();
vec.add("Tiger");
vec.add("Dog");
vec.add("Lion");
vec.add("Deer");
vec.add("Elepahant");
vec.add("Rat");
vec.add("Cat");
System.out.println("Elements in the vector:"+vec);
System.out.println("Size of vector:"+vec.size());
vec.remove("Deer");
System.out.println("Elements in the vector after deletion:"+vec);
}
}
OUTPUT:
Source code:
import java.util.StringTokenizer;
public class TD1
{
public static void main(String[] args)
{
StringTokenizer obj = new StringTokenizer("Welcome to study tonight");
while(obj.hasMoreTokens())
{
System.out.println(obj.nextToken());
}
}
}
OUTPUT:
Welcome
to
study
tonight
7) Program Using Single Inheritance
Source Code:
import java.io.*;
class personaldetails
{
void display()
{
System.out.println("Student Name: Bhooshitha");
System.out.println("Phone Number:9488614140");
System.out.println("Place:Gobi");
}
}
class academydetails extends personaldetails
{
void display1()
{
System.out.println("Collage:Gobi Arts and Science collage");
System.out.println("Department:IoT");
System.out.println("Seemester 1:90%");
System.out.println("Semester 2:80%");
System.out.println("Semester 3:85%");
System.out.println("Semester 4:78%");
System.out.println("Semester 5:90%");
System.out.println("Semester 6:95%");
System.out.println("******");
}
}
class student
{
public static void main(String[] args)
{
academydetails a=new academydetails();
a.display();
a.display1();
}
}
OUTPUT:
Student Name: Bhooshitha
Phone Number:9488614140
Place:Gobi
Collage:Gobi Arts and Science collage
Department:IoT
Seemester 1:90%
Semester 2:80%
Semester 3:85%
Semester 4:78%
Semester 5:90%
Semester 6:95%
******
8) Program using Interface
Source Code:
import java.io.*;
class vehicle
{
void display()
{
System.out.println("This is a vehicle");
}
}
interface car
{
void carInfo();
}
interface Bike
{
void bikeInfo();
}
class Twowheeler implements Bike
{
public void bikeInfo()
{
System.out.println("This is a two wheeler bike");
}
}
class Fourwheeler implements car
{
public void carInfo()
{
System.out.println("This is a four wheeler car");
}
}
public class interface1
{
public static void main(String[] args)
{
vehicle veh = new vehicle();
veh.display();
Twowheeler twowheeler = new Twowheeler();
twowheeler.bikeInfo();
Fourwheeler fourwheeler = new Fourwheeler();
fourwheeler.carInfo();
}
}
OUTPUT:
This is a vehicle
This is a twowheeler bike
This is a fourwheeler car
9)Program using String Class
Source Code:
import java.io.*;
public class StringExample1
{
public static void main(String[] args)
{
String str1="Hello";
String str2=",Java!";
String combinedstr=str1+str2;
System.out.println("Combined String:"+combinedstr);
int length=combinedstr.length();
System.out.println("length of the string:"+length);
Boolean containsjava = combinedstr.contains("Java");
System.out.println("contains 'java':"+containsjava);
String UpperCaseStr = combinedstr.toUpperCase();
String LowerCaseStr = combinedstr.toLowerCase();
System.out.println("Uppercase:"+UpperCaseStr);
System.out.println("Lowercase:"+LowerCaseStr);
String substr1=combinedstr.substring(0,5);
String substr2=combinedstr.substring(6);
System.out.println("substring1:"+substr1);
System.out.println("substring2:"+substr2);
Boolean startsWithHello=combinedstr.startsWith("Hello");
Boolean endsWithJava=combinedstr.endsWith("Java");
System.out.println("Starts With 'Hello':"+startsWithHello);
System.out.println("Ends With 'Java':"+endsWithJava);
String[] words=combinedstr.split(",");
for(String word:words)
{
System.out.println(word);
}
}
}
Output:
Combined String:Hello,Java!
length of the string:11
contains 'java':true
Uppercase:HELLO,JAVA!
Lowercase:hello,java!
substring1:Hello
substring2:Java!
Starts With 'Hello':true
Ends With 'Java':false
Hello
Java!
10) Program Using String Buffer Class
Source Code:
import java.io.*;
public class stringbufferexample
{
public static void main(String[] args)
{
StringBuffer stringBuffer=new StringBuffer("Hello");
stringBuffer.append(",");
stringBuffer.append("Java!");
System.out.println("Appended string:"+stringBuffer);
System.out.println("Inserted string:"+stringBuffer);
stringBuffer.insert(5,"Awesome");
stringBuffer.reverse();
System.out.println("ReversedString:"+stringBuffer);
stringBuffer.delete(5,13);
System.out.println("DeletedString:"+stringBuffer);
System.out.println("Updated string:"+stringBuffer);
}
}
OUTPUT:
Appended string:Hello,Java!
Inserted string:Hello,Java!
ReversedString:!avaJ,emosewAolleH
DeletedString:!avaJolleH
Updated string:!avaJolleH
11)Program Using Exception Handling
Source Code:
import java.io.*;
class Ex
{
public static void main(String[] args)
{
try
{
int result =divide(10,0);
System.out.println("Result"+result);
}
catch(ArithmeticException e)
{
System.out.println("An error occured:"+e.getMessage());
}
finally
{
System.out.println("end of program");
}
}
public static int divide(int divident,int diviser)
{
return divident/diviser;
}
}
}
}
OUTPUT:
An error occured:/ by zero
end of program
12)Thread Based Application
Source Code:
import java.io.*;
class myThread extends Thread
{
private String threadName;
public myThread(String name)
{
this.threadName=name;
}
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println(threadName + " -count:"+i);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
public class threadexample
{
public static void main(String[] agrs)
{
myThread thread1 = new myThread("Thread1");
myThread thread2 = new myThread("Thread2");
thread1.start();
thread2.start();
try
{
thread1.join();
thread2.join();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Main thread finished");
}
}
OUTPUT:
Thread1 -count:1
Thread2 -count:1
Thread1 -count:2
Thread2 -count:2
Thread1 -count:3
Thread2 -count:3
Thread1 -count:4
Thread2 -count:4
Thread1 -count:5
Thread2 -count:5
Main thread finished
13)Program Using Package
Source Code:
person/student.java
package person;
public class student
{
int age = 20;
String gender="F";
String name = "Bhooshi";
int E = 90;
int T = 80;
int M = 85;
int J = 96;
int A = 80;
public void display()
{
System.out.println("Student Details");
System.out.println("Age"+age);
System.out.println("Name"+name);
System.out.println("English:"+E);
System.out.println("Tamil:"+T);
System.out.println("Maths:"+M);
System.out.println("Java programming"+J);
System.out.println("Analog and Electronics"+A);
}
}
Package1.java
import person.student;
class package1
{
public static void main(String agrs[])
{
student s=new student();
s.display();
}
}
OUTPUT:
Student Details
Age20
NameBhooshi
English:90
Tamil:80
Maths:85
Java programming96
Analog and Electronics80
14) Working with colors and fonts in Applet
Source Code:
fan.html
<html>
<body>
here is a output of fan applet program
<applet code="fan.class"width=600 height=600></applet>
</body>
</html>
fan.java
import java.awt.*;
import java.applet.Applet;
public class fan extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.white);
{
Font f1,f2;
f1=new Font("Arrival",Font.BOLD,18);
f2=new Font("Elephant",Font.ITALIC,28);
g.setFont(f1);
g.setColor(Color.blue);
g.drawString("Bhooshi",50,50);
g.setFont(f2);
g.setColor(Color.red);
g.drawString("Java Applet",30,90);
}
}
}
OUTPUT: