import java.io.*;
public class echoclnt
{
public static void main(String argv[])
{
Socket es=null;
PrintStream os=null;
DataInputStream is=null;
DataInputStream stdin=new DataInputStream(System.in);
try
{
es=new Socket("127.0.0.1",17);
is=new DataInputStream(es.getInputStream());
os=new PrintStream(es.getOutputStream());
}
catch (Exception e)
{
System.err.println(e);
}
if(es!=null && os!=null && is!=null)
{
try
{
String userip;
while((userip=stdin.readLine())!=null)
{
os.print(userip);
String serverip =is.readLine();
System.out.println("echo: " + serverip);
}
os.close();
is.close();
es.close();
}
catch(Exception e)
{
System.err.println(e);
}
}
}
}
import java.io.*;
import java.net.*;
public class echoser
{
public static void main(String args[])
{
ServerSocket ss=null;
try
{
System.out.println("Creating server socket");
ss=new ServerSocket(17);
System.out.println("Done");
}
catch(Exception e)
{
System.out.println(e);
}
while(true)
{
try
{
System.out.println("Waiting for a connection");
Socket s=ss.accept();
PrintStream ps=new PrintStream(s.getOutputStream());
System.out.println("Passing the quote..");
InputStream is=s.getInputStream();
int ch;
while((ch=is.read())!=-1)
{
ps.print((char)ch);
System.out.print((char)ch);
}
ps.close();
is.close();
}
catch(Exception e)
{
System.out.println(e);
System.exit(0);
}
}
}
}
/*
- Explains the use of Enumerators or 'enum'
*/
import java.io.*;
class enumerators
{
public static void main(String[] args)
{
enum size {Small, Medium, Large, XLarge};
size s1 = size.Small;
}
}
import java.io.*;
class ex{
public static void main(String args[]) throws IOException
{
int i,j,k;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter two numbers");
i=Integer.parseInt(br.readLine());
j=Integer.parseInt(br.readLine());
k=i+j;
System.out.println ("k" + k);}
}
import java.io.*;
class exam{
public static void main (String args[]) throws IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
String str;
System.out.println ("enter lines of text");
System.out.println ("enter stop to quit");
do{
str=br.readLine();
System.out.println ("u entered" +str);
}while (!str.equals("stop"));
}
}import java.io.*;
import java.util.Scanner;
class examp{
public static void main (String args[]) throws IOException
{
Scanner br = new Scanner (System.in);
String str;
System.out.println ("enter lines of text");
System.out.println ("enter stop to quit");
do{
str=br.next();
System.out.println ("u entered" +str);
}while (!str.equals("stop"));
}
}import javax.swing.JOptionPane;
public class Exercise1
{
public static void main(String[] args)
{
String name="";
name=JOptionPane.showInputDialog("Please enter your name ");
String msg="Hello "+name+" !";
JOptionPane.showMessageDialog(null,msg);
}
}
import java.io.*;
public class Exercise10
{
public static void main(String[] args) throws IOException
{
int a,b,c,d,x,y;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter First Number: ");
a=Integer.parseInt(br.readLine());
System.out.println("Enter Second Number: ");
b=Integer.parseInt(br.readLine());
System.out.println("Enter Number of Sequence: ");
y=Integer.parseInt(br.readLine());
System.out.print("The Numbers are: "+a+" "+b);
x=1;
while (x<=y)
{
c=a+b;
System.out.print(" "+c);
a=b;
b=c;
x++;
}
}
}
import javax.swing.JOptionPane;
public class Exercise12
{
public static void main(String[] args)
{
String word;
word=JOptionPane.showInputDialog("Enter a Word: ");
StringBuffer Name=new StringBuffer (word);
System.out.print(Name);
System.out.print(Name.reverse());
}
}
import javax.swing.JOptionPane;
public class Exercise2
{
public static void main(String[] args)
{
String word,word1,word2;
word= JOptionPane.showInputDialog("Input First Word: ");
word1= JOptionPane.showInputDialog("Input Second Word: ");
word2= JOptionPane.showInputDialog("Input Third Word: ");
//JOptionPane.showMessageDialog(null,"Input words are: "+" "+word+" "+word1+" "+word2);
JOptionPane.showMessageDialog(null,"Input words are: "+word+word1+word2);
}
}
import javax.swing.JOptionPane;
public class Exercise6
{
public static void main(String[] args)
{
double Average;
double sMath,sJava,sSociology,sHumanities,sPhysics;
sMath=Double.parseDouble(JOptionPane.showInputDialog("Input Grade for Math "));
sJava=Double.parseDouble(JOptionPane.showInputDialog("Input Grade for Java "));
sSociology=Double.parseDouble(JOptionPane.showInputDialog("Input Grade for Sociology "));
sHumanities=Double.parseDouble(JOptionPane.showInputDialog("Input Grade for Humanities "));
sPhysics=Double.parseDouble(JOptionPane.showInputDialog("Input Grade for Physics "));
Average=((sMath+sJava+sSociology+sHumanities+sPhysics)/5);
JOptionPane.showMessageDialog(null,"The Average is "+Average);
if (Average >= 75.0)
{
JOptionPane.showMessageDialog(null,"Passed");
}
else
{
JOptionPane.showMessageDialog(null,"Failed");
}
}
}
import javax.swing.JOptionPane;
public class Exercise7
{
public static void main(String[] args)
{
Double Activity,Quiz,Attendance,Assignment,Exam,Grade;
Activity=Double.parseDouble(JOptionPane.showInputDialog("Enter partial Grade for Activity "));
Quiz=Double.parseDouble(JOptionPane.showInputDialog("Enter partial Grade for Quiz "));
Attendance=Double.parseDouble(JOptionPane.showInputDialog("Enter partial Grade for Attendance "));
Assignment=Double.parseDouble(JOptionPane.showInputDialog("Enter partial Grade for Assignment "));
Exam=Double.parseDouble(JOptionPane.showInputDialog("Enter partial Grade for Exam "));
Grade=((Activity*0.15)+(Quiz*0.15)+(Attendance*0.10)+(Assignment*0.10)+(Exam*0.5));
JOptionPane.showMessageDialog(null,"Your grade for this subject is "+Grade);
if(Grade >= 75.0)
{
JOptionPane.showMessageDialog(null,"Passed");
}
else
{
JOptionPane.showMessageDialog(null,"Failed");
}
}
}
import javax.swing.JOptionPane;
public class Exercise8 {
public static void main(String[] args)
{
int num1,num2,choice,result;
num1=Integer.parseInt(JOptionPane.showInputDialog("Enter First Number"));
num2=Integer.parseInt(JOptionPane.showInputDialog("Enter Second Number"));
choice=Integer.parseInt(JOptionPane.showInputDialog("Select Operation:\n1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division"));
if(choice==1)
{
result=num1+num2;
JOptionPane.showMessageDialog(null,"The result is: "+result);
}
else if(choice==2)
{
result=num1-num2;
JOptionPane.showMessageDialog(null,"The result is: "+result);
}
else if(choice==3)
{
result=num1*num2;
JOptionPane.showMessageDialog(null,"The result is: "+result);
}
else if(choice==4)
{
result=num1/num2;
JOptionPane.showMessageDialog(null,"The result is: "+result);
}
else
{
JOptionPane.showMessageDialog(null,"Illegal Operation");
}
}
}
import javax.swing.JOptionPane;
public class Exercise9
{
public static void main(String[] args)
{
int num1;
num1=Integer.parseInt(JOptionPane.showInputDialog("Enter number:"));
if(num1==1)
{
JOptionPane.showMessageDialog(null,"Red");
}
else if(num1==2)
{
JOptionPane.showMessageDialog(null,"White");
}
else if(num1==3)
{
JOptionPane.showMessageDialog(null,"Blue");
}
else if(num1==4)
{
JOptionPane.showMessageDialog(null,"Orange");
}
else if(num1==5)
{
JOptionPane.showMessageDialog(null,"Yellow");
}
else if(num1==6)
{
JOptionPane.showMessageDialog(null,"Green");
}
else if(num1==7)
{
JOptionPane.showMessageDialog(null,"Brown");
}
else if(num1==8)
{
JOptionPane.showMessageDialog(null,"Black");
}
else if(num1==9)
{
JOptionPane.showMessageDialog(null,"Gold");
}
else
{
JOptionPane.showMessageDialog(null,"Gray");
}
}
}
import java.util.Scanner;
import javax.swing.JOptionPane;
public class ExperimentOne
{
public static void main ( String [] x)
{
double max = 0;
Scanner sc= new Scanner (System.in);
//Scanner num2= new Scanner (System.in);
//Scanner num3= new Scanner (System.in);
// Method 1
int num1= Integer.parseInt (JOptionPane.showInputDialog("Enter num1: "));
int num2= Integer.parseInt (JOptionPane.showInputDialog("Enter num2: "));
int num3= Integer.parseInt (JOptionPane.showInputDialog("Enter num3: "));
//Method 2
/*
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int num3 = sc.nextInt();
*/
if (num1 > num2 && num1>num3) max=num1;
if (num2 > num1 && num2>num3) max=num2;
if (num3 > num2 && num3>num1) max=num3;
System.out.println ("The Max. Number is: "+ max);
}
}/*
- This program would calculate the no.s exponent value
- Eg: 2 cube = 8
*/
import java.io.*;
class exponent
{
int performExponent(int b, int e)
{
int ans = 0;
int temp = b;
for(int i=1; i
temp = temp*b;
ans = temp;
}
return ans;
}
public static void main(String[] args)
{
String sbase;
String sexp;
int base = 0;
int exp = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.print("Enter the base value: ");
sbase = br.readLine();
base = Integer.parseInt(sbase);
System.out.print("Enter the base value: ");
sexp = br.readLine();
exp = Integer.parseInt(sexp);
}
catch(IOException ioe)
{
System.out.println("Error!!!");
System.exit(1);
}
exponent e1 = new exponent();
int answer = e1.performExponent(base, exp);
System.out.println(base + "^" + exp + " = " + answer);
}
}public class fact
{
public static void main(String as[])
{
int no=5;
int f=1;
while(no>0)
{
f=f*no;
no--;
}
System.out.println("factorial is "+f);
}
}
/*
- This program calculates the factorial for a given number.
*/
import java.io.*;
class factorial
{
int calculateFactorial(int n)
{
int ans = 1;
for(int i=1; i<=n; i++)
{
ans = ans*i;
}
return ans;
}
public static void main(String[] args)
{
String number;
int num = 0;
int answer = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Number: ");
try
{
number = br.readLine();
num = Integer.parseInt(number);
}
catch(IOException ioe)
{
System.out.println("Error!!!");
System.exit(1);
}
factorial f = new factorial();
answer = f.calculateFactorial(num);
System.out.println(num + "! = " + answer);
}
}
//importing io classes
import java.io.*;
class Fibonacci
{
public static void main(String[] args) throws IOException
{
DataInputStream in=new DataInputStream(System.in);
String s;
int a=1,b=1,c=a+b;
int n;
System.out.print("Enter nth value?");
s=in.readLine();
n=Integer.parseInt(s);
System.out.print(a+","+b);
while(c<=n)
{
System.out.print(c+",");
a=b;
b=c;
c=a+b;
}
}
}
//importing io classes
import java.io.*;
class Fibonacci_rec
{
public int fibo(int n)
{
if (n == 1)
return 1;
else if (n == 2)
return 1;
else
return fibo(n-1) + fibo(n-2);
}
/*
This method will do exactly that, it will invoke the two previous versions of itself, and those invoked will invoke their previous selves and so on until one of them reaches f(2) or f(1).
It's easy to use this method in a loop to print out many Fibonacci numbers:
*/
public static void main(String[] args)
{
Fibonacci_rec f=new Fibonacci_rec();
for (int i=1; i<10 i="" nbsp="" p=""> System.out.println(f.fibo(i));
}
}
import java.io.*;
import java.util.*;
class FileDemo
{
public static void main(String s[])
{
File parent=new File("d:/Rahul");
File f=new File(parent , "curser.txt");
System.out.println("File is "+f);
System.out.println("File parent "+f.getParent());
System.out.println("File name "+f.getName());
System.out.println("File length= "+f.length());
System.out.println("Path= "+f.getPath());
System.out.println("Absolute path= "+f.getAbsolutePath());
System.out.println(f.exists()?"File is exist":"File is not exist");
System.out.println(f.canRead() ?"File is Readable" : "file is not readable");
System.out.println(f.canWrite() ?"File is writeable" : "file is not writeable");
System.out.println(f.isFile() ?"File is simple file" : "file is not simple file");
System.out.println(f.isDirectory ()?"is directory" : "not a directory");
System.out.println("Last modified at :"+new Date(f.lastModified()));
}
}import java.io.*;
import java.awt.*;
import java.awt.event.*;
/*
program compiled by RVS@CSE to open a file dialog box using menu item clicks
*/
class filedialog extends WindowAdapter implements ActionListener
{
Frame f;
public void actionPerformed(ActionEvent e)
{
FileDialog fd;
String s=e.getActionCommand();
//opening dialog box
if (s.equals("OPEN"))
{
fd=new FileDialog(f,"RVS@Open Dialog",FileDialog.LOAD);
fd.show();
}
else
if (s.equals("SAVE"))
{
fd=new FileDialog(f,"RVS@Save Dialog",FileDialog.SAVE);
fd.show();
}
}
public void windowClosing(WindowEvent e)
{
Frame f;
f=(Frame)e.getSource();
f.dispose();
}
public filedialog()
{
f=new Frame("Naveen....");
Panel p=new Panel();
MenuBar m=new MenuBar();
Menu file=new Menu("FILE");
Menu edit=new Menu("EDIT");
Menu view=new Menu("VIEW");
Menu tool=new Menu("TOOL");
MenuItem New=new MenuItem("NEW");
MenuItem open=new MenuItem("OPEN");
//adding action listener
open.addActionListener(this);
MenuItem save=new MenuItem("SAVE");
//adding action listener
save.addActionListener(this);
MenuItem advanceopt=new MenuItem("ADVANCEOPT");
MenuItem copy=new MenuItem("COPY");
MenuItem paste=new MenuItem("PASTE");
MenuItem toolbars=new MenuItem("TOOLBARS");
file.add(New);
file.add(open);
file.add(save);
tool.add(advanceopt);
edit.add(copy);
edit.add(paste);
view.add(toolbars);
m.add(file);
m.add(edit);
m.add(view);
m.add(tool);
f.setMenuBar(m);
f.addWindowListener(this);
f.setSize(300,400);
f.setVisible(true);
}
public static void main(String args[])
{
new filedialog();
}
}
/*
class Filedialog
{
Frame f=new Frame();
}
*///fd.LOAD;
import java.io.*;
import java.util.*;
class FileListDemo
{
public static void main(String s[])
{
//File f1=new File("d:/Rahul");
File f1=new File("F:/studies");
String str[]=f1.list();
for(int i=0;i
File f2=new File(f1,str[i]);
if(f2.isFile())
{
System.out.println(f2+" is file");
}
else if(f2.isDirectory())
{
System.out.println(f2+" is Directory");
}
}
}
}
import java.io.*;
class fileread
{
public static void main(String args[]) throws IOException
{
int ch,ctr=1;
String fname;
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter a file name?");
fname=in.readLine();
//opening file
FileInputStream f=new FileInputStream(fname);
System.out.print(ctr+" ");
while((ch=f.read())!=-1)
{
System.out.print((char)ch);
if(ch=='\n')
{
ctr++;
System.out.print(ctr+" ");
}
}
}
}
import java.io.*;
class filestat
{
public static void main(String args[]) throws IOException
{
int pre=' ' , ch , ctr=0 , L=0 , w=1;
String fname;
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter a file name?");
fname=in.readLine();
//opening file
FileInputStream f=new FileInputStream(fname);
while((ch=f.read())!=-1)
{
//char count
if(ch!=' ' && ch!='\n')
ctr++;
//line count
if(ch=='\n')
L++;
//word count
if(ch==' ' && pre!=' ')
w++;
pre=ch;
}
System.out.println("Char count="+ctr);
System.out.println("Word count="+(w+(L-1)));
System.out.println("Line count="+L);
}
}
import java.io.*;
class filetest
{
public static void main(String args[]) throws IOException
{
String fname;
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter a file name?");
fname=in.readLine();
//opening file
File f=new File(fname);
if(f.exists())
System.out.println("File exists..");
else
System.out.println("File not exists..");
if(f.canRead())
System.out.println("File is readable..");
else
System.out.println("File is not readable..");
if(f.canWrite())
System.out.println("File is writable..");
else
System.out.println("File is not Writable..");
System.out.println("File length :"+f.length());
}
}
/* The following code will NOT compile since we have tried to inherit a 'final' class.
A class declared as 'final' can not be inherited. */
final class Final_Base_Class
{
int fc;
}
class Final_Class extends Final_Base_Class
{
public static void main(String args[])
{
System.out.println("\n A final class can't be inherited...");
}
} /* Aim : program to create an interface to perform division operations
when user enters the two integers. The division of two values must displayed
in the third text box when user clicks the Cal button . If num1 and num2 are not integers
then the program to throw NumberFormatException.when num1 divide with zero it has to throw
ArithmeticException in exception Message Label.
*/
/* framediv.java */
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class framediv extends JFrame implements ActionListener
{
JPanel p;
JTextField t1,t2,t3;
JButton b;
JLabel msg;
public void actionPerformed(ActionEvent e)
{
int n1,n2,n3;
JButton bt;
bt=(JButton)e.getSource();
if(bt.getLabel().equals("Cal"))
{
try
{
n1=Integer.parseInt(t1.getText());
n2=Integer.parseInt(t2.getText());
n3=n1/n2;
t3.setText(String.valueOf(n3));
msg.setText("Calculation performed......");
}
catch(NumberFormatException ex)
{
msg.setText("Wrong Data Values are enetered...");
}
catch(ArithmeticException ex)
{
msg.setText("Zero divide canot possible");
}
}
}
public framediv()
{
setTitle("Frame for division..");
p=new JPanel();
t1=new JTextField("10000");
t2=new JTextField("1000");
t3=new JTextField("Result Here");
b=new JButton("Cal");
//adding action listener
b.addActionListener(this);
p.add(t1);
p.add(new JLabel(" / "));
p.add(t2);
p.add(new JLabel(" = "));
p.add(t3);
p.add(b);
msg=new JLabel("Click Cal Button to perform cal...");
p.add(msg);
add(p);
setSize(300,400);
setVisible(true);
}
public static void main(String args[])
{
new framediv();
}
}
import java.awt.*;
import java.awt.event.*;
class framelights implements ItemListener
{
Frame f;
Label l;
Checkbox b1,b2,b3;
CheckboxGroup g;
Panel p;
public void itemStateChanged(ItemEvent e)
{
Checkbox c=(Checkbox)e.getSource();
if(c.getLabel().equals("Red"))
{
l.setText("Red color...");
}
else
if(c.getLabel().equals("Yellow"))
{
l.setText("Yellow color...");
}
else
if(c.getLabel().equals("Green"))
{
l.setText("Green color...");
}
}
public framelights()
{
f=new Frame("Frame with Lights");
l=new Label("COLOR BOX.....");
g=new CheckboxGroup();
b1=new Checkbox("Red",g,false);
b2=new Checkbox("Yellow",g,false);
b3=new Checkbox("Green",g,false);
b1.addItemListener(this);
b2.addItemListener(this);
b3.addItemListener(this);
p=new Panel();
p.add(l);
p.add(b1);
p.add(b2);
p.add(b3);
f.add(p);
f.setSize(300,400);
f.setVisible(true);
}
public static void main(String[] args)
{
new framelights();
}
}
import javax.swing.JOptionPane;
public class GradePerSubjects
{
public static void main(String[] args)
{
Double Activity,Quiz,Attendance,Assignment,Exam,Grade;
Activity=Double.parseDouble(JOptionPane.showInputDialog("Enter partial Grade for Activity "));
Quiz=Double.parseDouble(JOptionPane.showInputDialog("Enter partial Grade for Quiz "));
Attendance=Double.parseDouble(JOptionPane.showInputDialog("Enter partial Grade for Attendance "));
Assignment=Double.parseDouble(JOptionPane.showInputDialog("Enter partial Grade for Assignment "));
Exam=Double.parseDouble(JOptionPane.showInputDialog("Enter partial Grade for Exam "));
Grade=((Activity*0.15)+(Quiz*0.15)+(Attendance*0.10)+(Assignment*0.10)+(Exam*0.5));
JOptionPane.showMessageDialog(null,"Your grade for this subject is "+Grade);
}
}
import java.awt.* ;
import java.applet.*;
public class hell extends Applet {
public void paint(Graphics g) {
g.drawString("Welcome to Java Applets",20,20); } }
//simple use of static initialization
/* A static initializer block is defined using the keyword static.
The code in a static initializer block is executed once by the virtual machine when the class is loaded.
A static initializer block cannot contain a return statement. Therefore, no need to specify a return type.
A static initializer block doesn’t have an argument list.
It can initialize only static data members of the class.
Because the static initializer block is executed when the class is first loaded, we can print out “Hello World” without writing a main method. The execution is stopped using “System.exit()” command.
*/
class hello
{
static
{
System.out.println("Hello World");
System.exit(0);//prevents main method not found error
}
}class Hello {
public static void main(String args[]) {
System.out.println("\n " + args[0] + "\n " + args[1]);
}
}
import java.applet.*;
import java.awt.*;
/* */
public class ImgApplet extends Applet
{
Image img;
public void init()
{
img=getImage(getCodeBase(),"Sunset.jpg");
}
public void paint(Graphics g)
{
g.drawImage(img,50,50,20,20,this);
}
}
class IncrementDecrement{
public static void main (String[] args){
int i = 1;
System.out.println("i : " + i);
System.out.println("++i : " + ++i);
System.out.println("i++ : " + i++);
System.out.println("i : " + i);
System.out.println("--i : " + --i);
System.out.println("i-- : " + i--);
System.out.println("i : " + i);
}
}interface InterfaceDemo
{
int x=10;
void Square(int n);
}
class InterfaceImpl implements InterfaceDemo
{
public void Square(int n)
{
int s=n*n;
System.out.println("Sqaure of"+n+"is="+s);
}
public static void main(String args[])
{
InterfaceImpl j=new InterfaceImpl();
j.Square(x);
}
}
interface InterfaceDemo
{
int x=10;
void Square(int n);
}
class InterfaceImpl1 implements InterfaceDemo
{
public void Square(int n)
{
int s=n*n;
System.out.println("Sqaure of"+n+"is="+s);
}
public void cube(int n)
{
int c=n*n*n;
System.out.println("Cube of"+n+"is="+c);
}
public static void main(String args[])
{
InterfaceImpl1 j=new InterfaceImpl1();
j.Square(x);
j.cube(5);
}
}
interface InterfaceDemo
{
int x=10;
void Square(int n);
}
interface InterfaceCube
{
void cube(int n);
}
class InterfaceImpl2 implements InterfaceDemo,InterfaceCube
{
public void Square(int n)
{
int s=n*n;
System.out.println("Sqaure of"+n+"is="+s);
}
public void cube(int n)
{
int c=n*n*n;
System.out.println("Cube of"+n+"is="+c);
}
public static void main(String args[])
{
InterfaceImpl2 j=new InterfaceImpl2();
j.Square(x);
j.cube(x);
}
}
class InterfaceImpl3 implements InterfaceSub
{
public void Add(int a,int b)
{
int s=a+b;
System.out.println("Addtion of a & b is="+s);
}
public void Sub(int a,int b)
{
int c=a-b;
System.out.println("Substration of a & b is="+c);
}
public static void main(String args[])
{
InterfaceImpl3 j=new InterfaceImpl3();
j.Add(10,20);
j.Sub(30,20);
}
}
import java.io.*;
class item
{
int icode,qty,up,total;
String iname;
void getdata()throws IOException
{
DataInputStream in = new DataInputStream(System.in);
System.out.print("enter the item code\t:");
icode = Integer.parseInt(in.readLine());
System.out.print("enter the item name\t:");
iname = in.readLine();
System.out.print("enter the qty\t:");
qty = Integer.parseInt(in.readLine());
System.out.print("enter the unit price\t:");
up = Integer.parseInt(in.readLine());
}
void cal()
{
total = qty*up;
System.out.println("Total amount is\t:"+total);
}
}
class item1
{
public static void main(String args[])throws IOException
{
item I = new item();
I.getdata();
I.cal();
}
}import java.io.*;
import java.lang.*;
class item
{
int icode,total;
void getdata()
{
try
{
DataInputStream in = new DataInputStream(System.in);
System.out.print("enter the item code\t:");
icode = Integer.parseInt(in.readLine());
System.out.print("enter the total\t:");
total = Integer.parseInt(in.readLine());
}
catch(IOException e)
{
System.out.println("input Failure");
}
catch(NumberFormatException e)
{
System.out.println("THE DATA IS WRONG");
}
finally
{
System.out.println("PROGRAM END");
}
}
}
class item11
{
public static void main(String args[])
{
try
{
item I = new item();
I.getdata();
}
catch(NullPointerException e)
{
System.out.println("array of object to pass the constructor");
}
}
}import javax.swing.*;
class jtbl
{
public static void main(String[] args)
{
JFrame f=new JFrame("MyFrame");
String h[]={"Sno","Sname"};
Object data[][]={{"101","Raj"},{"102","Ram"}};
JTable t=new JTable(data,h);
JPanel p=new JPanel();
p.add(t);
f.getContentPane().add(p);
f.setSize(300,400);
f.show();
}
}
import java.util.Scanner;
import java.io.*;
public class key1
{
public static void main(String args[]) throws IOException
{
Scanner s=new Scanner(System.in);
System.out.println(s.next());
String name = s.next();
System.out.println(name);
DataInputStream in = new DataInputStream(System.in);
name=in.readLine();
System.out.println(name);
InputStreamReader il= new InputStreamReader (System.in);
BufferedReader br = new BufferedReader(il);
name=br.readLine();
System.out.println(name);
}}import console.*;
public class KeyBoardInputDemo {
public static void main(String args[ ]) {
double x = Console.readDouble("Enter a real number: ");
System.out.println(x);
int y = Console.readInt("Enter an integer: ");
System.out.println(y);
String str = Console.readLine("Please enter a line of text: ");
System.out.println(str);
}
}import java.io.*;
public class KeyboardString {
public static void main(String args[ ]){
char c;
StringBuffer sb = new StringBuffer(255);
System.out.print("Enter a string: ");
try {
while((c = (char) System.in.read()) != '\r')
sb.append(c);
}
catch(IOException e) {
System.out.println(e.toString());
}
System.out.println(sb);
}
}import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
*/
public class la extends Applet
{
public void init()
{
Label one = new Label("one");
Label two = new Label("two");
Label three = new Label("three");
add(one);
add(two);
add(three);
}
}// Labeled break & continue statement
class LabeledStatement
{
public static void main(String args[])
{
int i,j;
int a[][] = {{1,2,3},{4,5,6},{7,8,9}};
System.out.println("A for loop without labeled break statement...");
for(i=0;i<3 i="" p=""> {
for(j=0;j<3 j="" p=""> {
System.out.println(i+"\t"+j);
if(a[i][j]==5)
break;
}
}
System.out.println("\n\nA for loop with labeled break statement...");
TEST:
for(i=0;i<3 i="" p=""> {
for(j=0;j<3 j="" p=""> {
System.out.println(i+"\t"+j);
if(a[i][j]==5)
break TEST;
}
}
System.out.println("\n\nA for loop without labeled continue statement...");
for(i=0;i<3 i="" p=""> {
for(j=0;j<3 j="" p=""> {
System.out.println(i+"\t"+j);
if(a[i][j]==5)
continue;
}
System.out.println("********");
}
System.out.println("\n\nA for loop with labeled continue statement...");
TESTCONT:
for(i=0;i<3 i="" p=""> {
for(j=0;j<3 j="" p=""> {
System.out.println(i+"\t"+j);
if(a[i][j]==5)
continue TESTCONT;
}
System.out.println("********");
}
System.out.print("\n\n\n\n");
}
}// A sigley linked list program
class Node
{
private int data;
private Node next;
public Node() { data=0; next=null; }
public Node(int n)
{
data=n;
next=null;
}
public void setNext(Node n) { next=n; }
public Node getNext() { return next; }
public int getData() { return data; }
}
public class LinkedList
{
public Node start;
int count;
public LinkedList() { count=0; start=null; }
public void insert(int data)
{
Node temp = start;
Node newnode = new Node(data);
if(temp==null)
{
temp=newnode;
start=newnode;
}
else
{
while(temp.getNext() != null)
temp = temp.getNext();
temp.setNext(newnode);
}
count++;
}
public void display()
{
Node temp =start;
while(temp != null)
{
System.out.println(temp.getData());
temp = temp.getNext();
}
}
public static void main(String args[])
{
LinkedList l = new LinkedList();
l.insert(10);
l.insert(15);
l.display();
}
}
class Loading
{
int x=2,y=4,z;
void add()
{
z=x+y;
System.out.println("The value of Z1:"+z);
}
void add(int x1)
{
x=x1;
z=x+y;
System.out.println("The value of Z2:"+z);
}
void add(int x1,int y1)
{
x=x1;
y=y1;
z=x+y;
System.out.println("The value of Z3:"+z);
}
public static void main(String arg[])
{
Loading t1=new Loading();
t1.add();
t1.add(10);
t1.add(10,20);
}
}
/* A class defined inside a method is called a local nested class or local
inner class.
*/
class LocalInnerClass
{
void test()
{
// int v1 = 1; ERROR - Must be declared final
final int v1 = 1;
class Inner
{
Inner()
{
System.out.println("\n Object of Inner class created...");
System.out.println("\n v1 = "+v1);
}
}
Inner i1 = new Inner();
}
protected void finalise()
{
System.out.println("finalise() called...");
}
public static void main(String args[])
{
LocalInnerClass obj = new LocalInnerClass();
obj.test();
}
}
class logik{
public static void main(String[] args) {
boolean Benar = true;
boolean Salah = false;
System.out.println("Hubungan OR (||)");
System.out.println("Benar || Benar : " +(Benar||Benar));
System.out.println("Benar || Salah : " +(Benar||Salah));
System.out.println("Salah || Benar : " +(Salah||Benar));
System.out.println("Salah || Salah : " +(Salah||Salah));
System.out.println("Hubungan AND (&&)");
System.out.println("Benar && Benar : " +(Benar&&Benar));
System.out.println("Benar && Salah : " +(Benar&&Salah));
System.out.println("Salah && Benar : " +(Salah&&Benar));
System.out.println("Salah && Salah : " +(Salah&&Salah));
System.out.println("Hubungan NOT (!)");
System.out.println("Kebalikan (NOT) dari Benar adalah: " +!Benar);
System.out.println("Kebalikan (NOT) dari Salah adalah: " +!Salah);
}
}/*
- This is a random number generation Program where it compares the number you entered and the number the program generats. If it matches, you WIN..
*/
import java.io.*;
import java.util.*;
class lottery
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter your lucky number: ");
int n = input.nextInt();
//Random Generator
Random lotteryNumber = new Random();
int ln;
do
{
ln = lotteryNumber.nextInt();
}
while(lotteryNumber.nextInt() <= 100 && lotteryNumber.nextInt() > 0);
if(ln == n)
{
System.out.println("********************* YOU WIN *********************");
}
else
{
System.out.println("Your number did not match " + ln);
System.out.println("Try Again");
System.exit(1);
}
}
}import java.io.*;
interface weightage
{
int sports = 5;
}
class student
{
int rollno;
}
class test extends student
{
int sub1;
int sub2;
}
class result extends test implements weightage
{
int total;
void gd()throws IOException
{
DataInputStream in = new DataInputStream(System.in);
rollno = Integer.parseInt(in.readLine());
sub1 = Integer.parseInt(in.readLine());
sub2 = Integer.parseInt(in.readLine());
}
void display()
{
total=sub1+sub2+sports;
System.out.println("rollno"+rollno+"sub1"+sub1+"sub2"+sub2);
System.out.println("total"+total);
}
}
class mainresult
{
public static void main(String args[])throws IOException
{
result res=new result();
res.gd();
res.display();
}
}
//import java.lang.Thread.*;
class MainThreadDemo
{
public static void main(String arg[])
{
try
{
Thread th= Thread.currentThread();
System.out.println("Thread="+th);
th.sleep(1000);
System.out.println("Thread name="+th.getName());
th.sleep(1000);
System.out.println("Thread priority="+th.getPriority());
th.setName("MyMainThread");
th.setPriority(th.MAX_PRIORITY);
Thread th1=Thread.currentThread();
System.out.println("Thread="+th1);
System.out.println("Active count Thread="+th1.activeCount());
System.out.println("Alive Thread="+th1.isAlive());
}catch(InterruptedException ie)
{
System.out.println("Error is"+ie);
}
}
}import java.io.*;
class area
{
int x,y;
double z;
void cal(double a,int b)
{
z = a*b*b;
System.out.println("circle area :"+z);
}
void cal(int a)
{
x = a*a;
System.out.println("squre :"+x);
}
void cal(int a,int b)
{
y = a*b;
System.out.println("rectangle :"+y);
}
}
class marea
{
public static void main(String args[])throws IOException
{
System.out.println("1.circle 2.rectangle 3.squre");
DataInputStream in = new DataInputStream(System.in);
int a = Integer.parseInt(in.readLine());
area A = new area();
if(a == 1)
{
System.out.println("enter the radius");
int b = Integer.parseInt(in.readLine());
A.cal(3.14,b);
}
else if(a == 2)
{
System.out.println("enter the length and breath");
int c = Integer.parseInt(in.readLine());
int d = Integer.parseInt(in.readLine());
A.cal(c,d);
}
else
{
System.out.println("enter the squre");
int e = Integer.parseInt(in.readLine());
A.cal(e);
}
}
}
class MathDemo
{
public static void main(String arg[])
{
System.out.println("Square of 2 ="+Math.sqrt(2));
System.out.println("Round value for 1.49 ="+Math.round(1.49));
System.out.println("ceiling value for 1.0001 ="+Math.ceil(1.0001));
System.out.println("Floor value for 1.9999 ="+Math.floor(1.9999));
System.out.println("Max value for 10,20 ="+Math.max(10,20));
System.out.println("Min value for 10,20 ="+Math.min(10,20));
System.out.println("A random num ="+Math.random());
System.out.println("Absolute value of -15 ="+Math.abs(-15));
System.out.println("IEEE value of 1.5,3.0 ="+Math.IEEEremainder(1.5,3.0));
}
}import java.io.*;
class matmul
{
public static void main(String[] args) throws IOException
{
int mat1[][]={{1,2,3},{4,5,6},{7,8,9}};
int mat2[][]={{1,2,3},{4,5,6},{7,8,9}};
int mat3[][],i,j,k;
mat3=new int[3][3];
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
for(k=0;k<=2;k++)
{
mat3[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
System.out.println("After Matrix Multiplicatin......");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(mat3[i][j]+"\t");
}
System.out.print("\n");
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
3>3>3>3>3>3>3>3>
*/
class MouseLstn implements MouseMotionListener
{
public void mouseDragged(MouseEvent evt)
{
}
public void mouseMoved(MouseEvent evt)
{
int x,y;
x=evt.getX();
y=evt.getY();
mouseapplt.t1.setText(String.valueOf(x)+" , "+String.valueOf(y));
}
}
public class mouseapplt extends Applet
{
static TextField t1=new TextField("......");
public void init()
{
Panel p=new Panel();
p.add(t1);
add(p);
setVisible(true);
setSize(300,400);
addMouseMotionListener(new MouseLstn());
}
}
import java.applet.Applet;
import java.awt.TextField;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.*;
class mmlstn implements MouseMotionListener
{
public void mouseDragged(MouseEvent m)
{
}
public void mouseMoved(MouseEvent m)
{
int x,y;
x=m.getX();
y=m.getY();
mousecoord.t.setText(String.valueOf(x)+" , "+String.valueOf(y));
}
}
public class mousecoord extends Applet
{
static TextField t;
public void init()
{
t=new TextField("RNEC.....");
Panel p=new Panel(); // to manage the components
p.add(t);
add(p);
setSize(300,400);
setVisible(true);
addMouseMotionListener(new mmlstn());
}
public void paint(Graphics g)
{
g.drawRect(20,70,120,150);
}
}
/*
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
/*
observe the code ( when submit clicks it deletes the textfields data
, help clicked shows the count in out put....
compiled by rvs@cse RNEC
*/
class mouselstn extends MouseAdapter implements ActionListener
{
Frame f;
Label l1,l2;
static TextField t1,t2;
Button b1,b2;
static int clicks;
Panel p;
public void actionPerformed(ActionEvent e)
{
String txt;
Button bt;
bt=(Button)e.getSource();
txt=bt.getLabel();
if(txt.equals("Submit"))
{
mouselstn.t1.setText(" ");
mouselstn.t2.setText(" ");
System.out.println("Your page cannot redirect? then press HELP");
}
else if(txt.equals("HELP"))
{
System.out.println("You Don'y have any Help...Because you are not Naveen������");
}
}
public void mouseClicked(MouseEvent m)
{
System.out.println("no of times mouse clicked........."+clicks++);
}
public mouselstn()
{
p=new Panel();
Label l1=new Label("User Name�");
Label l2=new Label("Password�");
t1=new TextField("000000");
t2=new TextField("000000");
b1=new Button("Submit");
b2=new Button("HELP");
f=new Frame("My Frame");
b1.addActionListener(this);
b2.addActionListener(this);
b2.addMouseListener(this);
p.setLayout(new GridLayout(3,3));
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(b1);
p.add(b2);
f.add(p);
f.setSize(300,400);
f.setVisible(true);
}
public static void main(String args[])
{
new mouselstn();
}
}
/*Dynamic Dispacthing Of Method*/
class Test11
{
void Demo()
{
System.out.println("This in method of Test11 class");
}
}
class Mov extends Test11
{
void Demo()
{
System.out.println("This in method of Mov Subclass");
}
public static void main(String arg[])
{
Test11 t=new Test11();
Mov m=new Mov();
Test11 obj=t;
obj.Demo();
obj=m;
obj.Demo();
}
}
class Test11
{
final int x=10;
void Demo()
{
System.out.println("This in method of Test11 class");
}
}
class Mov1 extends Test11
{
final void Demo()
{
System.out.println("This in method of Mov Subclass="+x);
}
public static void main(String arg[])
{
Test11 t=new Test11();
t.Demo();
Mov1 m=new Mov1();
m.Demo();
Test11 obj=t;
obj.Demo();
obj=m;
obj.Demo();
}
}
import java.lang.Thread;
class threada extends Thread
{
public threada(String msg)
{
super(msg);
}
public void run()
{
int i;
try
{
System.out.println(getName()+" Started....");
for(i=1;i<=10;i++)
{
System.out.println(" i = "+i);
//sleep(100);
}
System.out.println(getName()+" Exited....");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class threadb extends Thread
{
public threadb(String msg)
{
super(msg);
}
public void run()
{
int j;
try
{
System.out.println(getName()+" Started....");
for(j=1;j<=10;j++)
{
System.out.println(" j = "+j);
}
System.out.println(getName()+" Exited....");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class threadc extends Thread
{
public threadc(String msg)
{
super(msg);
}
public void run()
{
int k;
try
{
System.out.println(getName()+" Started....");
for(k=1;k<=10;k++)
{
System.out.println(" k = "+k);
//sleep(200);
}
System.out.println(getName()+" Exited....");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class multhread
{
public static void main(String arg[])
{
//creating new threads
threada t1=new threada("Thread A");
threadb t2=new threadb("Thread B");
threadc t3=new threadc("Thread C");
//setting priority
//t2.setPriority(Thread.MAX_PRIORITY);
///t1.setPriority(Thread.MAX_PRIORITY);
//t3.setPriority(Thread.MIN_PRIORITY);
//starting new threads
t1.start();
t2.start();
t3.start();
}
}
import java.lang.Thread;
/*
Java program that creates three threads first thread to display
"GOOD MORNING" every 1 sec , second thread to display HELLO for every 2 sec and
third thread to display WELCOME for every 3 sec
*/
class threada extends Thread
{
public void run()
{
try
{
for(;;)
{
System.out.println("GOOD MORNING");
sleep(1000);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class threadb extends Thread
{
public void run()
{
try
{
for(;;)
{
System.out.println("HELLO");
sleep(2000);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class threadc extends Thread
{
public void run()
{
try
{
for(;;)
{
System.out.println("WELCOME");
sleep(3000);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class multhreads
{
public static void main(String arg[])
{
//creating new threads
threada t1=new threada();
threadb t2=new threadb();
threadc t3=new threadc();
//starting new threads
t1.start();
t2.start();
t3.start();
}
}
class Test1
{
int x,y;
void getData(int x1,int y1)
{
x=x1;
y=y1;
}
void add()
{
int z=x+y;
System.out.println("Addtion of "+x+"and"+y+"is="+z);
}
}
class SingleInherit extends Test1
{
void sub()
{
int z=x-y;
System.out.println("Substraction of "+x+"and"+y+"is="+z);
}
}
class Multi extends SingleInherit
{
void div()
{
float z=x/y;
System.out.println("Division of "+x+"and"+y+"is="+z);
}
public static void main(String arg[])
{
Multi s=new Multi();
s.getData(5,10);
s.add();
s.sub();
s.div();
}
}
class Test2
{
int x,y;
void getData(int x1,int y1)
{
x=x1;
y=y1;
}
}
class SingleInherit1 extends Test1
{
void add()
{
int z=x+y;
System.out.println("Addtion of "+x+"and"+y+"is="+z);
}
}
class SingleInherit extends Test1
{
void sub()
{
int z=x-y;
System.out.println("Substraction of "+x+"and"+y+"is="+z);
}
}
class Multi1
{
public static void main(String arg[])
{
SingleInherit1 s1 =new SingleInherit1 ();
SingleInherit s2 =new SingleInherit ();
s1.getData(20,10);
s2.getData(20,10);
s1.add();
s2.sub();
}
}
class MultiCatch
{
public static void main(String s[])
{
Demo d=new Demo();
d.Sq(-5);
/* try
{
int arr[]=new int[4];
System.out.println("Array elements at 4th pointer="+arr[4]);
}
catch(ArithmeticException e)
{
System.out.println("NullPointer Exception "+e);
e.printStackTrace();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array inde out of bound"+e);
}
finally
{
System.out.println("Hello Deepak");
}*/
try
{
int a=10;
int b=0;
System.out.println("a="+a+"\n b="+b);
int c=a/b;
System.out.println("result is "+c);
}
catch(ArithmeticException e)
{
System.out.println("NullPointer Exception "+e);
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("Exception is "+e);
}
finally
{
System.out.println("Hello Rahul");
}
System.out.println("hello ");
}
}
class Demo
{
void Sq(int n)
{
/*try
{*/
if(n<0 p=""> {
throw new ArithmeticException();
}
else
{
int s=n*n;
System.out.println("Square of "+n+" is "+s);
}
/*}
catch(ArithmeticException e)
{
System.out.println("Caught inside method itself "+e);
}*/
}
}class ThreadBaru extends Thread {
public ThreadBaru(String id) {
super(id);
start(); //Mulai eksekusi thread baru
}
public void run() {
for(int i=0;i<5 i="" p=""> try{
Thread.sleep(100);
}catch(InterruptedException e) {}
}
}
}
class DemoThread {
public static void main(String[] args) {
ThreadBaru thread1 = new ThreadBaru("Thread1");
ThreadBaru thread2 = new ThreadBaru("Thread2");
ThreadBaru thread3 = new ThreadBaru("Thread3");
System.out.println("Thread1 masih dieksekusi : " + thread1.isAlive());
System.out.println("Thread2 masih dieksekusi : " + thread2.isAlive());
System.out.println("Thread3 masih dieksekusi : " + thread3.isAlive());
//tunggu hingga semua child thread selesai dieksekusi
try{
thread1.join();
System.out.println("Thread1 selesai dieksekusi");
thread2.join();
System.out.println("Thread2 selesai dieksekusi");
thread3.join();
System.out.println("Thread3 selesai dieksekusi");
}catch(InterruptedException e) {
System.out.println("Thread utama diinterupsi " + e);
}
System.out.println("Thread utama selesai dieksekusi");
}
}import java.applet.*;
import java.awt.*;
public class myapplet extends Applet{
String str;
public void init(){
str = "This is my first applet";
}
public void paint(Graphics g){
g.drawString(str, 50,50);
}
}import java.applet.*;
import java.awt.*;
/* */
public class MyApplet_2 extends Applet
{
public void init()
{
System.out.println("Applet initialized");
}
public void start()
{
System.out.println("Applet starts");
}
public void stop()
{
System.out.println("Applet stopped");
}
public void destroy()
{
System.out.println("Applet destroyed");
}
public void paint(Graphics g)
{
showStatus("this is status");
g.drawString("Hello print",50,70);
g.drawString("This is for Applet programming",50,90);
System.out.println("Applet painted");
}
}import java.awt.*;
class mycanvas
{
public static void main(String[] args)
{
Frame f=new Frame("My Frame");
Canvas c1=new Canvas();
Label l1=new Label("Enter Ur name?");
c1.setBackground(Color.red);
c1.setSize(100,100);
Canvas c2=new Canvas();
Label l2=new Label("Enter Age?");
c2.setBackground(Color.green);
c2.setSize(100,100);
Canvas c3=new Canvas();
Label l3=new Label("Enter Age?");
c3.setBackground(Color.blue);
c3.setSize(100,100);
f.add(l1);
f.add(c1);
f.add(l2);
f.add(c2);
f.add(l3);
f.add(c3);
f.setLayout(new GridLayout(3,2));
f.setSize(300,400);
f.setVisible(true);
}
}
import java.io.*;
public class MyExceptionDemo {
void a() throws MyException {
System.out.println("In use a().");
throw new MyException("I am MyException.");
}
void b() {
try {
a();
}
catch(MyException e) {
System.out.println("\nIn use b().");
System.err.println(e.toString());
System.out.println("I was transfered from a() and catched in b().");
}
}
void c() throws IOException {
System.out.println("\nIn use c().");
throw new IOException("Hi. I am born. My name is IOException. \nI was transfered from b() to main().");
}
public static void main(String args[ ]) {
MyExceptionDemo med = new MyExceptionDemo();
med.b();
try {
med.c();
}
catch(IOException e) {
System.err.println(e.toString());
}
}
}
class MyException extends Exception {
public MyException(String msg) {
super(msg);
System.out.println("Hi.I am born. My name is MyException.");
}
}
class MyThread implements Runnable
{
Thread th;
MyThread(String nm,int p)
{
th=new Thread(this,nm);
th.start();
System.out.println(th.getName()+"Thread Started Here");
th.setPriority(p);
}
public void run()
{
for(int i=0;i<4 i="" p=""> {
System.out.println("Inside"+th.getName()+"i="+i);
try{
th.sleep(1000);
}catch(InterruptedException e)
{
System.out.println("Child Thread Interruptde");
}
}
System.out.println(th.getName()+" Thread Over Here");
}
public static void main(String arg[])
{
System.out.println("Main Thread Started Here");
MyThread obj1=new MyThread(" One",3);
MyThread obj2=new MyThread(" Two",5);
MyThread obj3=new MyThread(" Three",7);
//MyThread rd=new MyThread();
for(int i=0;i<4 i="" p=""> {
System.out.println("Inside Main Thread i="+i);
try{
Thread.sleep(1000);
}catch(InterruptedException e)
{
System.out.println("Main Thread Interruptde");
}
}
// System.out.println("Main Thread Over Here");
}
}
class ObjArg
{
int x;
ObjArg(int x1)
{
x=x1;
}
void increase(ObjArg o)
{
System.out.println("original Value of x="+o.x);
x=o.x+10;
System.out.println("Udate Value="+x);
}
public static void main(String arg[])
{
ObjArg Obj1=new ObjArg(7);
Obj1.increase(Obj1);
}
}
/*CLASS CAN BE USED AS ARRGUMENT'S & RETURN TYPE*/
class ObjReturn
{
int x;
ObjReturn(int x1)
{
x=x1;
}
ObjReturn increase() //RETURN TYPE
{
ObjReturn ob=new ObjReturn(x+10);
return ob;
}
public static void main(String arg[])
{
ObjReturn Obj1=new ObjReturn(6); //PARAMETERISED CONSTRUCTOR
System.out.println("original Value of x="+Obj1.x);
ObjReturn Obj2=Obj1.increase(); //REFERNCE VARIABLE
System.out.println("New Value of x="+Obj2.x);
Obj2=Obj2.increase(); //VARIABLE INSTALIZATION
System.out.println("Again Value of x="+Obj2.x);
}
}
import java.io.*;
class OutputStream {
public static void main(String[] args) throws IOException {
byte[] data = {'a','b','c','d','e','f','g'};
System.out.write(data,3,4);
//pindah baris
System.out.write('\n');
//tulis semua isi array data
System.out.write(data);
}
}import mypackage.*;
import mypackage.subpackage.*;
class PackageImpl
{
public static void main(String args[])
{
Test.demo();
TestSub ts=new TestSub();
ts.cube(5);
}
}import mypackage.*;
import mypackage.subpackage.*;
class PackageImpl
{
public static void main(String args[])
{
Test.demo();
TestSub ts=new TestSub();
ts.cube(5);
}
}
import java.io.*;
class pal
{
public static void main(String arg[]) throws IOException
{
int i,j,len;
String s;
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter a string ?");
s=in.readLine();
len=s.length();
for(i=0,j=len-1;i
if(s.charAt(i)!=s.charAt(j))
break;
}
if(i==j)
System.out.println("Pal string");
else
System.out.println("Not a Pal string");
}
}
import java.io.*;
import java.lang.Math;
class pfixevl
{
int top,j,i;
String str;
char t,postfix[];
int s[];
DataInputStream in;
public pfixevl()
{
top=-1;i=0;
postfix=new char[30];
s=new int[30];
in=new DataInputStream(System.in);
}
public void push(int val)
{
top++;
s[top]=val;
}
public int pop()
{
int val;
val=s[top];
top--;
return val;
}
public void evaluate(String str) throws IOException
{
int op1,op2,value=0;
while(i
t=str.charAt(i);
if( (t>='a' && t<='z') || (t>='A' && t<='Z'))
{
System.out.print("Enter value for '"+t+"' ?");
value=Integer.parseInt(in.readLine());
push(value);
}
else
{
switch(t)
{
case '+':
op2=pop();
op1=pop();
value=op1+op2;
push(value);
break;
case '-':
op2=pop();
op1=pop();
value=op1-op2;
push(value);
break;
case '*':
op2=pop();
op1=pop();
value=op1*op2;
push(value);
break;
case '/':
op2=pop();
op1=pop();
value=op1*op2;
push(value);
break;
case '^':
op2=pop();
op1=pop();
value=(int)Math.pow((float)op1,(float)op2);
push(value);
break;
}
}
i++;
}
System.out.println("postfix evaluated val = "+value);
}
public static void main(String args[]) throws IOException
{
String str;
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter a string?");
str=in.readLine();
(new pfixevl()).evaluate(str);
}
}
import java.io.*;
import java.io.*;
class prime
{
public static void main(String[] args) throws IOException
{
String s;
int no,i,j;
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter a value?");
s=in.readLine();
no=Integer.parseInt(s);
for(i=no;i>=1;i--)
{
for(j=2;j {
if(i%j==0)
break;
}
if(i==j)
System.out.println(i+" is a prime no..");
}
}
}
import java.io.*;
class Consumer extends Thread
{
private CubbyHole cubbyhole;
private int number;
public Consumer(CubbyHole c,int number)
{
cubbyhole=c;
this.number=number;
}
public void run()
{
int value=0;
for(int i=0;i<10 i="" p=""> {
value=cubbyhole.get();
System.out.println("Consumer #" + this.number +"Got:" +value);
}
}
}
class Producer extends Thread
{
private CubbyHole cubbyhole;
private int number;
public Producer(CubbyHole c, int number)
{
cubbyhole=c;
this.number=number;
}
public void run()
{
for(int i=10;i<20 i="" p=""> {
cubbyhole.put(i);
try
{
sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Consumer #"+this.number+"put:" +i);
}
}
}
class CubbyHole
{
private int contents;
private boolean available=false;
public synchronized int get()
{
while(available==false)
{
try
{
wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
available=false;
notifyAll();
return contents;
}
public synchronized void put(int value)
{
while(available==true)
{
try
{
wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
available=true;
contents=value;
notifyAll();
}
}
public class ProducerConsumer
{
public static void main(String arg[])
{
CubbyHole c=new CubbyHole();
Producer p1= new Producer(c,2);
Consumer c1=new Consumer(c,1);
p1.start();
c1.start();
}
}
import java.io.*;
class Pwsdemo
{
public static void main(String args[])
{
PrintWriter p=new PrintWriter(System.out);
int i=10;
p.println("Hello!"+ i);
p.print(i);
p.print(i);
p.println(i);
p.println(i);
p.flush();
}
}//importing io classes
import java.io.*;
//importing Math class
import java.lang.Math;
class qd_eq
{
public static void main(String[] args) throws IOException
static
{
String s;
int a,b,c,d;
double r1,r2;
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter a value?");
s=in.readLine();
a=Integer.parseInt(s);
System.out.print("Enter b value?");
s=in.readLine();
b=Integer.parseInt(s);
System.out.print("Enter c value?");
s=in.readLine();
c=Integer.parseInt(s);
d=((b*b)-(4*a*c));
if(d<0 p=""> {
System.err.println("No real solution...");
}
else
{
r1=((-b)+Math.sqrt(d))/(2*a);
r2=((-b)-Math.sqrt(d))/(2*a);
System.out.println("Root 1 = "+r1);
System.out.println("Root 2 = "+r2);
}
//System.exit(0);
}
}
//reading file data line by line...
import java.io.*;
class readfile
{
public static void main(String args[])
{
char ch;
StringBuffer s=new StringBuffer();
String fname=null;
int ctr=1;
try
{
System.out.println("Enter a filoe name?");
while((ch=(char)System.in.read())!='\n')
{
s.append(ch);
}
fname=s.toString().trim();
//opening Random Access file
RandomAccessFile r=new RandomAccessFile("readfile.java","rw");
while(r.getFilePointer()
System.out.print(ctr+"\t");
System.out.println(r.readLine());
}
r.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
class RunnableDemo implements Runnable
{
Thread th;
RunnableDemo()
{
th=new Thread(this,"ChildThread");
th=new Thread(this,nm);
th.start();
th.setPriority(p);
System.out.println("Child Thread Started Here");
}
public void run()
{
for(int i=0;i<4 i="" p=""> {
System.out.println("Inside Child Thread i="+i);
try{
th.sleep(1000);
}catch(InterruptedException e)
{
System.out.println("Child Thread Interruptde");
}
}
System.out.println(th.getName()+" Thread Over Here");
}
public static void main(String arg[])
{
System.out.println("Main Thread Started Here");
MyThread obj1=new MyThread(" One",3);
MyThread obj2=new MyThread(" Two",5);
MyThread obj3=new MyThread(" Yhree",3);
RunnableDemo rd=new RunnableDemo();
for(int i=0;i<4 i="" p=""> {
System.out.println("Inside Main Thread i="+i);
try{
Thread.sleep(1000);
}catch(InterruptedException e)
{
System.out.println("Main Thread Interruptde");
}
}
System.out.println("Main Thread Over Here");
}
}
class RunnableDemo1 extends Thread
{
Thread th;
RunnableDemo1()
{
super("Child Thread");
start();
System.out.println("Child Thread Started Here");
}
public void run()
{
for(int i=0;i<4 i="" p=""> {
System.out.println("Inside Child Thread i="+i);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Child Thread Interruptde");
}
}
System.out.println("Child Thread Over Here");
}
public static void main(String arg[])
{
System.out.println("Main Thread Started Here");
RunnableDemo1 rd=new RunnableDemo1();
for(int i=0;i<4 i="" p=""> {
System.out.println("Inside Main Thread i="+i);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Main Thread Interruptde");
}
}
System.out.println("Main Thread Over Here");
}
}
class RunnableDemo2 extends Thread
{
Thread th;
RunnableDemo2(String nm,int p)
{
super(nm);
start();
System.out.println(getName()+"Thread Started Here");
setPriority(p);
}
public void run()
{
for(int i=0;i<4 i="" p=""> {
System.out.println("Inside"+getName()+" i="+i);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Child Thread Interrupted");
}
}
System.out.println(getName()+"Thread Over Here");
}
public static void main(String arg[])
{
System.out.println("Main Thread Started Here");
RunnableDemo2 obj1=new RunnableDemo2("One",3);
RunnableDemo2 obj2=new RunnableDemo2("Two",5);
RunnableDemo2 obj3=new RunnableDemo2("Three",7);
//RunnableDemo2 rd=new RunnableDemo2();
for(int i=0;i<4 i="" p=""> {
System.out.println("Inside Main Thread i="+i);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Main Thread Interruptde");
}
}
System.out.println("Main Thread Over Here");
}
}
/*
- This program illustrates the use of the Scanner class.
*/
import java.io.*;
import java.util.*;
import javax.swing.*;
class scanners
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a statement: ");
input.nextLine();
System.out.print("Enter your name: ");
input.next();
System.out.print("Enter your age: ");
input.nextInt();
String temp = JOptionPane.showInputDialog("What is your name?");
System.out.print("Thank you for using Scanner Class.");
}
}class scope
{
static int no=100;
public static void main(String[] args)
{
System.out.println("no : "+no);
System.out.println("no : "+scope.no);
}
}
/*
- This program will take a set of strings from the user and store it in an array.
- I have also written a search algorithm to search from the list that the user has entered
*/
import java.io.*;
class search1
{
public String sTotalInputs;
public int iTotalInputs = 0;
public String[] sInput = new String[50];
public String searchElement;
public BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public void takeInput()
{
try
{
System.out.print("Enter the total no. of elements you want: ");
sTotalInputs = br.readLine();
iTotalInputs = Integer.parseInt(sTotalInputs);
for(int i=1; i<=iTotalInputs; i++)
{
sInput[i] = br.readLine();
}
}
catch(IOException ioe)
{
System.out.println("ERROR!!!");
System.exit(1);
}
}
public void giveOutput()
{
for(int i=1; i<=iTotalInputs; i++)
{
System.out.println(i + ". " + sInput[i]);
}
}
public void search()
{
System.out.print("Enter the element you want to search: ");
try
{
searchElement = br.readLine();
System.out.println("Please wait. Searching.....");
for(int i=1; i<=iTotalInputs; i++)
{
if(sInput[i].equals(searchElement))
{
System.out.println("The element id you were looking for is " + i);
}
}
}
catch(IOException ioe)
{
System.out.println("ERROR!!!");
System.exit(1);
}
}
public static void main(String[] args)
{
search s1 = new search();
s1.takeInput();
System.out.println("\n The elements you entered are:");
s1.giveOutput();
s1.search();
}
}/*
- This program will take a set of strings from the user and store it in an array.
- I have also written a search algorithm to search from the list that the user has entered
*/
import java.io.*;
class search
{
public String sTotalInputs;
public int iTotalInputs = 0;
public String[] sInput = new String[50];
public String searchElement;
public BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public void takeInput()
{
try
{
System.out.print("Enter the total no. of elements you want: ");
sTotalInputs = br.readLine();
iTotalInputs = Integer.parseInt(sTotalInputs);
for(int i=1; i<=iTotalInputs; i++)
{
sInput[i] = br.readLine();
}
}
catch(IOException ioe)
{
System.out.println("ERROR!!!");
System.exit(1);
}
}
public void giveOutput()
{
for(int i=1; i<=iTotalInputs; i++)
{
System.out.println(i + ". " + sInput[i]);
}
}
public void search()
{
System.out.print("Enter the element you want to search: ");
try
{
searchElement = br.readLine();
System.out.println("Please wait. Searching.....");
for(int i=1; i<=iTotalInputs; i++)
{
if(sInput[i].equals(searchElement))
{
System.out.println("The element id you were looking for is " + i);
}
}
}
catch(IOException ioe)
{
System.out.println("ERROR!!!");
System.exit(1);
}
}
public static void main(String[] args)
{
search s1 = new search();
s1.takeInput();
System.out.println("\n The elements you entered are:");
s1.giveOutput();
s1.search();
}
}import java.io.*;
interface display1
{
String mark[]={"first","second","third"};
String ph[] = {"132","342","323"};
}
class shoe implements display1
{
void gd(int x)
{
System.out.println(mark[x]);
System.out.println(ph[x]);
}
}
class store extends shoe
{
void pd(int y)
{
gd(y);
}
}
class sh
{
public static void main(String args[])throws IOException
{
DataInputStream in = new DataInputStream(System.in);
store S = new store();
int z = Integer.parseInt(in.readLine());
S.pd(z);
}}class Test1
{
int x,y;
void getData(int x1,int y1)
{
x=x1;
y=y1;
}
void add()
{
int z=x+y;
System.out.println("Addtion of "+x+"and"+y+"is="+z);
}
}
class SingleInherit extends Test1
{
void sub()
{
int z=x-y;
System.out.println("Substraction of "+x+"and"+y+"is="+z);
}
}
class Multi extends SingleInherit
{
void div()
{
int z=x/y;
System.out.println("Division of "+x+"and"+y+"is="+z);
}
public static void main(String arg[])
{
Multi s=new Multi();
s.getData(10,20);
s.add();
s.sub();
s.div();
}
}
/*
- This program will sort all the list of numbers that you have entered in either ascending or descending order
- The following are the steps for sorting a list in ascending or descending order
*/
import java.io.*;
class sortInSequence
{
public String sTotalInputs;
public int iTotalInputs = 0;
public int[] inputElement = new int[50];
public BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public void takeInput()
{
try
{
System.out.print("Enter the total no. of elements you want: ");
sTotalInputs = br.readLine();
iTotalInputs = Integer.parseInt(sTotalInputs);
for(int i=1; i<=iTotalInputs; i++)
{
inputElement[i] = Integer.parseInt(br.readLine());
}
}
catch(IOException ioe)
{
System.out.println("ERROR!!!");
System.exit(1);
}
}
public void giveOutput()
{
for(int i=1; i<=iTotalInputs; i++)
{
System.out.println(i + ". " + inputElement[i]);
}
}
void ascending()
{
int newArray[]= new int[iTotalInputs];
for(int i=1; i<=iTotalInputs; i++)
{
int position = i;
for(int j=1; j<=iTotalInputs; j++)
{
if(inputElement[i] > inputElement[j])
{
position++;
}
else
{
j++;
newArray[position] = inputElement[i];
}
}
}
}
void descending(int[] n)
{
}
public static void main(String[] args)
{
sortInSequence s1 = new sortInSequence();
s1.takeInput();
s1.giveOutput();
}
}import java.io.*;
import java.lang.*;
import testclass.*;
/*interface student
{
int sports = 5;
}*/
class result extends test
{
int total;
void get()throws IOException
{
DataInputStream in = new DataInputStream(System.in);
sub1 = Integer.parseInt(in.readLine());
sub2 = Integer.parseInt(in.readLine());
}
void cal()
{
total = sub1+sub2;
//+sports;
System.out.println("total :"+total);
}
}
class sstudent
{
public static void main(String args[])throws IOException
{
result R = new result();
R.get();
R.cal();
}
}
import java.io.*;
//stack capable to store 4 elements
class Stack
{
int max,top;
int no[];
public Stack()
{
no=new int[5];
top=-1;
max=5;
}
public void push(int n)
{
if(top
no[++top]=n;
}
else
System.out.println("Stack full..");
}
public int pop()
{
int val;
if(top==-1)
{
System.out.println("Stack is Empty...");
return -1;
}
else
{
val=no[top--];
}
return val;
}
public void print()
{
System.out.println("Stack elements are..");
for(int i=0;i
}
}
class StackTest
{
public static void main(String arg[]) throws IOException
{
Stack s=new Stack();
s.push(11);
s.push(12);
s.push(13);
s.push(14);
s.push(15);
s.push(16);
s.print();
System.out.println("Pop ival="+s.pop());
s.print();
System.out.println("Pop ival="+s.pop());
s.print();
}
}
/*
This class describes the use of static variables
*/
import java.io.*;
public class staticVariable
{
//Delcare a static variable
static int instanceNo = 0;
public staticVariable()
{
instanceNo++;
}
public static void main(String[] args)
{
//Create 1st object
staticVariable sv1 = new staticVariable();
System.out.println("Instance for Sv1: " + sv1.instanceNo);
//Create 2nd object
staticVariable sv2 = new staticVariable();
System.out.println("Instance for sv1: " + sv1.instanceNo);
System.out.println("Instance for sv2: " + sv2.instanceNo);
//Note: Here sv1.instanceNo will be equal to sv2.instanceNo but both would have their own copy
staticVariable sv3 = new staticVariable();
System.out.println("Instance for sv1: " + sv1.instanceNo);
System.out.println("Instance for sv2: " + sv2.instanceNo);
System.out.println("Instance for sv3: " + sv3.instanceNo);
}
}class StrBuffDemo
{
public static void main(String s[])
{
StringBuffer sb=new StringBuffer();
System.out.println("sb="+sb);
System.out.println("lngth="+sb.length());
System.out.println("capacity="+sb.capacity());
sb.append("hello friend");
sb.append(145);
System.out.println("now sb="+sb);
System.out.println("capacity="+sb.capacity());
System.out.println("lenth ="+sb.length());
System.out.println("sb.substring="+sb.substring(5));
sb.insert(5,"nice");
System.out.println("sb="+sb);
System.out.println("sb in reverse "+sb.reverse());
}
}
class StrConstructor
{
public static void main(String arg[])
{
String s1=new String();
System.out.println("S1 ="+s1);
byte []b={65,66,67,68,55,56,57};
char []ch={'a','b','c','d','e','2'};
String s2=new String(b);
System.out.println("S2="+s2);
String s3=new String(b,0,3);
System.out.println("S3="+s3);
String s4=new String(ch);
System.out.println("S4="+s4);
String s5=new String(ch,2,2);
System.out.println("S5="+s5);
String s6=new String(s4);
System.out.println("S6="+s6);
}
}class StrContruction
{
public static void main(String s[])
{
String s1=new String();
System.out.println("S1="+s1);
byte b[]={97,98,99,100,101,102};
char ch[]={'r','a','h','u','l'};
String s2=new String(b);
System.out.println("S2="+s2);
String s3=new String(b,1,5);
System.out.println("S3="+s3);
String s4=new String(ch);
System.out.println("S4="+s4);
String s5=new String(ch,2,3);
System.out.println("S5="+s5);
String s6=new String(s2);
System.out.println("S6="+s6);
String s7="Hell0 Deepak";
System.out.println("S7="+s7);
}
}class StringBufferku{
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
String kata="ILMU KOMPUTER";
sb.append("PRODI ").append(kata).append(" UGM");
System.out.println(sb.toString());
System.out.println(sb);
}
}class MembandingkanString {
public static void main(String[] args) {
String str1 = "Ilmu";
String str2 = "Komputer";
String str3 = "IlmuKomputer";
System.out.println("String 1:"+str1);
System.out.println("String 2:"+str2);
System.out.println("String 3:"+str3);
System.out.println("String 1 = String 3 ==> "+str1.equals(str3));
System.out.println("String 2 = String 3 ==> "+str2.equals(str3));
System.out.println("String 1 + String 2 = String 3 ==> "+(str1+str2).equals(str3));
String str4 = "Universitas Gadjah Mada";
String str5 = "universitas gadjah mada";
System.out.println("String 4:"+str4);
System.out.println("String 5:"+str5);
System.out.println("Perintah Pembandingan dibawah mengabaikan antara huruf besar dengan huruf kecil");
System.out.println("String 4 = String 5 ==> " +str4.equalsIgnoreCase(str5));
System.out.println("String 4 + String 5 = String 5 + String 4 ==> " +(str4+str5).equalsIgnoreCase(str5+str4));
String str6 = "MIPA";
String str7 = new String(str6);
String str8 = "mipa";
String str9 = "MIPA";
System.out.println("String 6:"+str6);
System.out.println("String 7:"+str7);
System.out.println("String 8:"+str8);
System.out.println("String 9:"+str9);
System.out.println("Perintah Pembandingan dengan ==");
System.out.println("String 6 = String 7 ==> "+ (str6==str7));
System.out.println("String 6 = String 8 ==> "+ (str6==str8));
System.out.println("String 6 = String 9 ==> "+ (str6==str9));
}
}class PosisiKarakter {
public static void main(String[] args) {
String str1 = "Ilmu Komputer Matematika dan Ilmu Pengetahuan Alam Universitas Gadjah Mada";
System.out.println(str1.indexOf("Komputer"));
System.out.println(str1.indexOf("Komputer",10));
System.out.println(str1.lastIndexOf("Matematika"));
System.out.println(str1.lastIndexOf("Matematika",10));
System.out.println(str1.lastIndexOf("Matematika",20));
System.out.println(str1.indexOf(97));
System.out.println(str1.indexOf(97,7));
System.out.println(str1.lastIndexOf(97));
System.out.println(str1.lastIndexOf(97,7));
}
}/*
- This program explains everything about Strings
*/
import java.io.*;
class strings
{
public static void main(String[] args)
{
String t = null;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter text: ");
t = br.readLine();
while(t.length() < 3)
{
t=br.readLine();
}
System.out.println("This is a string " + t);
System.out.println("Its Length = " + t.length());
System.out.println("Substring is = " + t.substring(0, 3));
//Display the true length, i.e. the code length
System.out.println("Code Length = " + t.codePointCount(0, t.length()));
//Display the character at a particular position
System.out.println("Character at 3 = " + t.charAt(2));
//Concatinates 2 strings
String temp = "Hi" + t;
System.out.println("Concatination of 2 strings = " + temp);
}
catch(IOException ioe)
{
System.out.print("Error!!!");
System.exit(1);
}
}
}public class StringsDemo {
public static void main(String args[]) {
String str = "Varna";
int len = str.length();
StringBuffer dest = new StringBuffer(len);
for (int i = (len - 1); i >= 0; i--) {
dest.append(str.charAt(i));
}
System.out.println(dest.toString());
}
}class StrMethod
{
static String str="abc";
public static void main(String s[])
{
String str1="hello friend";
String str2="hello friend";
str= str.toUpperCase();
System.out.println("str="+str);
System.out.println("str1"+str1);
System.out.println("str2"+str2);
System.out.println("Str1 in uppercase="+str1.toUpperCase());
System.out.println("Str1 in lowercase="+str1.toLowerCase());
System.out.println("Str1.equals(str2)="+str1.equals(str2));
System.out.println("Str1.equalsIgnoreCase"+str1.equalsIgnoreCase(str2));
System.out.println("Length of str1"+str1.length());
System.out.println("CharAt(3)="+str1.charAt(3));
System.out.println("str1.substring(2)="+str1.substring(2));
System.out.println("str1.substring(0,3)"+str1.substring(0,3));
System.out.println(str1.startsWith("He")?"str1 start with He":"str1 not start with He");
}
}
import javax.swing.JOptionPane;
public class SumOfAll10Numbers
{
public static void main(String[] args)
{
int num1,num2,num3,num4,num5,num6,num7,num8,num9,num10,total;
num1=Integer.parseInt(JOptionPane.showInputDialog("Enter 1st Number "));
num2=Integer.parseInt(JOptionPane.showInputDialog("Enter 2nd Number "));
num3=Integer.parseInt(JOptionPane.showInputDialog("Enter 3rd Number "));
num4=Integer.parseInt(JOptionPane.showInputDialog("Enter 4th Number "));
num5=Integer.parseInt(JOptionPane.showInputDialog("Enter 5th Number "));
num6=Integer.parseInt(JOptionPane.showInputDialog("Enter 6th Number "));
num7=Integer.parseInt(JOptionPane.showInputDialog("Enter 7th Number "));
num8=Integer.parseInt(JOptionPane.showInputDialog("Enter 8th Number "));
num9=Integer.parseInt(JOptionPane.showInputDialog("Enter 9th Number "));
num10=Integer.parseInt(JOptionPane.showInputDialog("Enter 10th Number "));
total= num1+num2+num3+num4+num5+num6+num7+num8+num9+num10;
JOptionPane.showMessageDialog(null,"The Sum of the 10 Numbers is "+total);
}
}
class VarTest
{
void d()
{
System.out.println("Addtion of and");
}
}
class SuperMethod extends VarTest
{
void d()
{
System.out.println("Addtion of ");
super.d();
}
public static void main(String arg[])
{
SuperMethod sv =new SuperMethod ();
sv.d();
}
}class VarTest
{
void d()
{
System.out.println("Addtion of and");
}
}
class SuperMethod extends VarTest
{
void d()
{
System.out.println("Addtion of ");
super.d();
}
public static void main(String arg[])
{
SuperMethod sv =new SuperMethod ();
sv.d();
}
}class SuperDemo
{
int x;
SuperDemo()
{
System.out.println("Default constructor of super class x="+x);
}
SuperDemo(int x1)
{
x=x1;
System.out.println("parameterized constructor of super class x="+x);
}
}
class SuperTest extends SuperDemo
{
SuperTest()
{
super();
System.out.println("default constructor of super class x="+x);
}
SuperTest(int x1)
{
super();
x=x1;
System.out.println("parameterized constructor of super class x="+x);
}
public static void main(String arg[])
{
SuperTest s1=new SuperTest();
SuperTest s2=new SuperTest(20);
}
}class VarTest
{
int x=10;
}
class SuperVar extends VarTest
{
int x=20;
void add()
{
int z=x+super.x;
System.out.println("Addtion of "+x+"and"+ super.x +"is="+z);
}
public static void main(String arg[])
{
SuperVar sv =new SuperVar ();
sv.add();
}
}/*
- This program would take inputs from user and display it.
*/
import java.io.*;
class takingInputs
{
public static void main(String[] args)
{
System.out.print("Enter a text: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String text = null;
try
{
text = br.readLine();
}
catch(IOException ioe)
{
System.out.println("Error in text");
System.exit(1);
}
System.out.println("You entered: " + text);
}
}
//simple command line calculator
//import.java.io.*;
//import.java.lang.*;
import java.lang.*;
class calci
{
public static void main(String args[])
{
int n1,n2,n3;
char op;
n1= Integer.parseInt (args[0]);
op= args[1].charAt(0);
n2=Integer.parseInt (args[2]);
switch (op)
{
case '+':
n3=n1+n2;
System.out.println(n1 + "+" +n2 + " = " + n3 );
break;
case '-':
n3=n1-n2;
System.out.println(n1 + "-" +n2 + " = " + n3 );
break;
case '/':
n3=n1/n2;
System.out.println(n1 + "/" +n2 + " = " + n3 );
break;
case '*':
n3=n1-n2;
System.out.println(n1 + "*" +n2 + " = " + n3 );
break;
default:
System.out.println("enter correct choice");
}
//System.out.println (op);
}
}
class Test2
{
int x,y;
void getData(int x1,int y1)
{
x=x1;
y=y1;
}
}
class SingleInherit1 extends Test1
{
void add()
{
int z=x+y;
System.out.println("Addtion of "+x+"and"+y+"is="+z);
}
}
class SingleInherit extends Test1
{
void sub()
{
int z=x-y;
System.out.println("Substraction of "+x+"and"+y+"is="+z);
}
}
class Multi
{
public static void main(String arg[]);
{
SingleInherit1 s1 =new SingleInherit1 ();
SingleInherit s2 =new SingleInherit ();
s1.getData(20,10);
s1.add();
s2.sub();
}
}
import java.io.*;
class TestException
{
void Demo()
{
throw new ArithmeticException();
}
public static void main(String s[])
{
TestException obj=new TestException();
try
{
obj.Demo();
System.out.println("A");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("b");
}
finally
{
System.out.println("c");
}
System.out.println("D");
}
}
class TestSinkronisasi {
private java.util.Random random = new java.util.Random();
public void callMe(String data) {
System.out.print("[");
try{
Thread.sleep(random.nextInt(200));
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.print(data);
try{
Thread.sleep(random.nextInt(200));
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("]");
}
}
class ThreadBaru extends Thread {
private String data;
private TestSinkronisasi obj;
public ThreadBaru(TestSinkronisasi obj,String data) {
this.obj = obj;
this.data = data;
start();
}
public void run() {
obj.callMe(data);
}
}
class DemoThread {
public static void main(String[] args) {
TestSinkronisasi obj = new TestSinkronisasi();
ThreadBaru thread1 = new ThreadBaru(obj,"Superman");
ThreadBaru thread2 = new ThreadBaru(obj,"Batman");
ThreadBaru thread3 = new ThreadBaru(obj,"Spiderman");
//tunggu hingga semua child thread selesai dieksekusi
try{
thread1.join();
thread2.join();
thread3.join();
}catch(InterruptedException e) {
System.out.println("Thread utama diinterupsi " + e);
}
}
}class throwsdemo
{
void sqr(int num)throws Exception
{
if(num<0 p=""> {
throw new NullPointerException();
}
else
{
int s=num*num;
System.out.println("Square of num="+s);
}
}
public static void main(String arg[])
{
try{
throwsdemo td=new throwsdemo();
td.sqr(9);
td.sqr(-6);
}catch(Exception e)
{
System.out.println("Error is=== "+e);
e.printStackTrace();
}
}
}
class ThrowsDemo
{
public static void main(String s[])
{
Demo d=new Demo();
try
{d.Sq(-5);
int a=10;
int b=0;
System.out.println("a="+a+"\n b="+b);
int c=a/b;
System.out.println("result is "+c);
}
catch(ArithmeticException e)
{
System.out.println("inside main"+e);
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("Exception is "+e);
}
finally
{
System.out.println("Hello Rahul");
}
System.out.println("hello ");
}
}
class Demo
{
void Sq(int n) throws ArithmeticException
{
try
{
if(n<0 p=""> {
throw new ArithmeticException();
}
else
{
int s=n*n;
System.out.println("Square of "+n+" is "+s);
}
}
catch(ArithmeticException e)
{
System.out.println("Caught inside method itself "+e);
throw e;
}
}
}import java.io.*;
import java.lang.*;
class one extends Thread
{
public void run()
{
for(int i=0;i<6 i="" p="">{
System.out.println("\t class :i"+i);
}
}
}
class second extends Thread
{
public void run()
{
for(int j=0;j<6 j="" p=""> {
System.out.println("\t class :j"+j);
if(j==2)
{
try
{
sleep(1);
}
catch(Exception e){}
}
}
}
}
class tstart
{
public static void main(String args[])
{
one X = new one();
second Y = new second();
X.start();
Y.start();
/*for(int i=0;i<5 i="" p="">{
if (i==2)
Y.suspend();
if(i==4)
Y.resume();
}
Y.suspend();
X.suspend();
for(int i=0;i<5 i="" p="">System.out.println(i);
Y.resume();
Y.stop();*/
}
}
class UserDefineException
{
void cube(int c) throws MyException
{
if(c<0 p=""> {
throw new MyException(c);
}
else
{
int r=c*c*c;
System.out.println("Cube of "+c+" is "+r);
}
}
public static void main(String s[]) throws MyException
{
UserDefineException e=new UserDefineException();
e.cube(3);
e.cube(-3);
}
}/*Using With 'this' Keyword*/
class Withoutthis
{
int x;
Withoutthis(int x)
{
this.x=x;
}
void Square()
{
int z=x*x;
System.out.println("Square of "+x+"is"+z);
}
public static void main(String arg[])
{
Withoutthis obj1=new Withoutthis(10);
obj1.Square();
}
}
import java.io.*;
public class WordFromFile {
private FileReader in;
public WordFromFile(String filename) {
in = new FileReader(filename);
}
public String getWord() {
int c;
StringBuffer buf = new StringBuffer();
do {
c = in.read();
if (Character.isWhitespace((char)c))
return buf.toString();
else
buf.append((char)c);
} while (c != -1);
return buf.toString();
}
public static void main(String args[ ]) {
WordFromFile file = new WordFromFile("c:\\proba.txt");
System.out.println(file.getWord());
}
}
import java.io.*;
public class WordFromFile1 {
FileReader in;
public WordFromFile1(String filename) {
try {
in = new FileReader(filename);
}
catch(FileNotFoundException e) {
System.err.println(e.toString());
}
}
public String getWord(){
StringBuffer buf = new StringBuffer();
int c;
try {
do {
c = in.read();
if (Character.isWhitespace((char) c)) return buf.toString();
else buf.append((char) c);
} while (c != -1);
}
catch(IOException e) {
System.err.println(e.toString());
}
return buf.toString();
}
public static void main(String args[ ]) {
WordFromFile1 file = new WordFromFile1("c:\\w\\proba.txt");
System.out.println(file.getWord());
}
}
import java.io.*;
public class WordFromFile2 {
FileReader in;
public WordFromFile2(String filename) throws FileNotFoundException {
in = new FileReader(filename);
}
public String getWord() throws IOException {
int c;
StringBuffer buf = new StringBuffer();
do {
c = in.read();
if (Character.isWhitespace((char)c))
return buf.toString();
else
buf.append((char) c);
} while (c != -1);
return buf.toString();
}
public static void main(String args[ ]) {
try {
WordFromFile2 file = new WordFromFile2("c:\\w\\proba.txt");
System.out.println(file.getWord());
}
catch(IOException e) {
System.err.println(e.toString());
}
}
}
/*Using Without 'this' Keyword*/
class Wot
{
int x;
Wot(int x)
{
x=x;
}
void Square()
{
int z=x*x;
System.out.println("Square of "+x+"is"+z);
}
public static void main(String arg[])
{
Wot obj1=new Wot(10);
obj1.Square();
}
}
No comments:
Post a Comment