Practicle File of

Programming in JAVA
Submitted for the Partial fulfillment of the Requirement for the Award of Degree of MCA

N.C. INSTITUE OF COMPUTER SCIENCE ISRANA,PANIPAT

SUBMITTED TO:Mr.Vikas Raheja

SUBMITTED BY:Anil Kumar 150487 4TH SEMESTER

Program No:-……….

Date:-…………

WRITE A PROGRAM FOR MATRIX OPERATION.
class MatrixMulti { public static void main(String args[]) { int a[][]={{1,2},{3,4}}; int b[][]={{1,2},{3,4}}; int c[][]=new int[2][2]; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { for(int k=0;k<2;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { System.out.println(c[i][j]); } } } }

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO MAINTAIN SAVING AND CURRENT ACCOUNT
class Savings { double bal; int accno; String cname; Savings(String name, int no, double b) { cname=name; accno=no; bal=b; } void show() { System.out.println(" "); System.out.println("CUSTOMER DETAIL"); System.out.println(" CUSTOMER'S NAME = " + cname); System.out.println(" ACCOUNT NUMBER = " + accno); System.out.println(" CURRENT BALANCE = " + bal); System.out.println(" "); } void debit(String name, int num,double db) { bal=bal-db; System.out.println("CUSTOMER DETAIL AFTER DEBIT OPERATION:"); System.out.println(" CUSTOMER'S NAME = " + cname); System.out.println(" ACCOUNT NUMBER = " + accno); System.out.println(" CURRENT BALANCE = " + bal); } void credit(String name,int num, double cdt) { bal=bal+cdt; System.out.println("CUSTOMER DETAIL AFTER CREDIT OPERATION:"); System.out.println(" CUSTOMER'S NAME = " + cname); System.out.println(" ACCOUNT NUMBER = " + accno);

System.out.println(" CURRENT BALANCE = " + bal); } } class S1 { public static void main(String args[]) { Savings a = new Savings("ANIL",1000348,10000); a.show(); a.debit("ANIL",1000348,2000); a.credit("ANIL",1000348,3000); a.show(); Savings s1=new Savings("SUNIL",1245677,1000); s1.show(); s1.credit("SUNIL",1245677,500); s1.debit("SUNIL",1245677,200); } }

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO IMLEMENT STACK OPERATION
class Stack { int top; int stk[]=new int[10]; Stack() { top=-1; } void push(int x) { stk[++top]=x; } void pop() { System.out.println(stk[top--]); } } class Stackmain { public static void main(String args[]) { Stack s1= new Stack(); s1.push(20); s1.push(30); s1.push(40); s1.push(50); s1.pop(); s1.pop(); } }

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM THAT WILL READ A STRING AND REWRITE IT IN ALPHABETICAL ORDER
import java.io.*; class Alpha { public static void main(String args[]) { try { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); DataInputStream in=new DataInputStream(System.in); int i,j,n; char temp; char s[] = new char[50]; String str; System.out.println("Enter lines of text."); str=br.readLine(); n=str.length(); for(i=0;i<n;i++) { s[i]=str.charAt(i); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(s[i]>s[j]) { temp=s[i]; s[i]=s[j]; s[j]=temp; } } } System.out.println("THE ALPHABETICAL ORDERING IS:" ); System.out.println();

for( i=0;i<n;i++) System.out.print(s[i]); } catch(Exception e){} } }

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO PASS OBJECT AS AN ARGUMENT
class Test { int a,b; Test(int i,int j) { a=i;b=j; } boolean equals(Test o) { if(o.a==a && o.b==b) return true; else return false; } } class Passob { public static void main(String args[]) { Test ob1=new Test(100,22); Test ob2=new Test(100,22); Test ob3=new Test(10,20); System.out.println("ob1 == ob2: " + ob1.equals(ob2)); System.out.println("ob1 == ob3: " + ob1.equals(ob3)); } }

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO CREATE A CLASS IN JAVA FOR COMMAND LINE ARGUMENTS
class Comline { public static void main(String args[]) { int count,i=0; String string; count=args.length; System.out.println("number of arguments="+count); while(i<count) { string=args[i]; i=i+1; System.out.println(i+":"+"Java is "+string+"!"); } } }

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO CALCULATE THE VOLUME & AREA OF BOX.
class Box { int length,breadth; Box(int x,int y) { length=x; breadth=y; } int area() { return(length*breadth); } } class Box1 extends Box { int height; Box1(int x,int y,int z) { super(x,y);height=z; } int volume() { return(length*breadth*height); } } class Test { public static void main(String args[]) { Box1 b1=new Box1(11,12,13); int area=b1.area(); int volume=b1.volume(); System.out.println("area="+ area); System.out.println("volume="+ volume); } }

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO GENERATE EXCEPTIONS.
class MultiCatch { public static void main(String args[]) { try { int a = args.length; System.out.println("a = " + a); int b = 42 / a; int c[] = { 1 }; c[42] = 99; } catch(ArithmeticException e) { System.out.println("Divide by 0: " + e); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array index oob: " + e); } System.out.println("After try/catch blocks."); } }

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO CREATE AN APPLET TO SHOW A BORDER LAYOUT.
import java.awt.*; import java.applet.*; import java.util.*; public class BorderLayoutDemo extends Applet { public void init() { setLayout(new BorderLayout()); add(new Button("This is across the top."),BorderLayout.NORTH); add(new Label("The footer message might go here."),BorderLayout.SOUTH); add(new Button("Right"), BorderLayout.EAST); add(new Button("Left"), BorderLayout.WEST); String msg = "The reasonable man adapts " +"himself to the world;\n" +"the unreasonable one persists in " +"trying to adapt the world to himself.\n" +"Therefore all progress depends " +"on the unreasonable man.\n\n" +" - George Bernard Shaw\n\n"; add(new TextArea(msg), BorderLayout.CENTER); } } BorderLayoutDemo.html file <applet code="BorderLayoutDemo.class" width=400 height=200> </applet>

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO IMPLEMENT GRID LAYOUT
import java.awt.*; import java.applet.*; public class GridLayoutDemo extends Applet { static final int n = 4; public void init() { setLayout(new GridLayout(n, n)); setFont(new Font("SansSerif", Font.BOLD, 24)); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { int k = i * n + j; if(k > 0) add(new Button("" + k)); } } } } HTML file <applet code="GridLayoutDemo.class" width=300 height=200> </applet>

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO IMPLEMENT BUTTON EVENTS
import java.applet.*; import java.awt.*; import java.awt.event.*; public class ButtonDemo extends Applet implements ActionListener { Button Yes,No,Maybe; String msg=" "; public void init() { Yes=new Button("Yes"); No=new Button("No"); Maybe=new Button("MayBe"); add(Yes); add(No); add(Maybe); Yes.addActionListener(this); No.addActionListener(this); Maybe.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); if(str.equals("Yes")) { msg="You Pressed Yes"; } else if(str.equals("No")) { msg="You Pressed No"; } else if(str.equals("Maybe")) { msg="You Pressed Maybe"; }

repaint(); } public void paint(Graphics g) { g.drawString(msg,60,100); } } ButtonDemo.html FILE <HTML> <HEAD> <TITLE>BUTTON DEMO</TITLE> </HEAD> <BODY> <APPLET CODE=ButtonDemo.class WIDTH=400 HEIGHT=400> </APPLET> </BODY> </HTML>

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO MAKE COLOR USING RGB COLOR.
import java.awt.*; import java.applet.*; public class ColorDemo extends Applet { public void paint(Graphics g) { Color c1 = new Color(255, 100, 100); Color c2 = new Color(100, 255, 100); Color c3 = new Color(100, 100, 255); g.setColor(c1); g.drawLine(0, 0, 100, 100); g.drawLine(0, 100, 100, 0); g.setColor(c2); g.drawLine(40, 25, 250, 180); g.drawLine(75, 90, 400, 400); g.setColor(c3); g.drawLine(20, 150, 400, 40); g.drawLine(5, 290, 80, 19); g.setColor(Color.red); g.drawOval(10, 10, 50, 50); g.fillOval(70, 90, 140, 100); g.setColor(Color.blue); g.drawOval(190, 10, 90, 30); g.drawRect(10, 10, 60, 50); g.setColor(Color.cyan); g.fillRect(100, 10, 60, 50); g.drawRoundRect(190, 10, 60, 50, 15, 15); } }

HTML <html> <head> <title>ColorDemo</title> </head> <body> <applet code="ColorDemo" width=300 height=200> </applet> </body>

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO REATE AN APPLET TO MAKE DIFFERENT TYPE OF TEXT BOXES
import java.awt.*; import java.awt.event.*; import java.applet.*; public class TextFieldDemo extends Applet implements ActionListener { TextField name, pass; public void init() { Label namep = new Label("Name: ", Label.RIGHT); Label passp = new Label("Password: ", Label.RIGHT); String val = "There are two ways of constructing "+"a software design.\n" +"One way is to make it so simple\n"+"that there are obviously no deficiencies.\n" +"And the other way is to make it so complicated\n"+"that there are no obvious deficiencies.\n\n"+" -C.A.R. Hoare\n\n"+"There's an old story about the person who wished\n" +"his computer were as easy to use as his telephone.\n"+"That wish has come true,\n"+"since I no longer know how to use my telephone.\n\n"+" -Bjarne Stroustrup, AT&T, (inventor of C++)"; name = new TextField(12); pass = new TextField(8); pass.setEchoChar('?'); TextArea text = new TextArea(val, 10, 30); add(namep); add(name); add(passp); add(pass); add(text); name.addActionListener(this); pass.addActionListener(this); } // User pressed Enter. public void actionPerformed(ActionEvent ae) { repaint(); } public void paint(Graphics g) {

g.drawString("Name: " + name.getText(), 6, 60); g.drawString("Selected text in name: " + name.getSelectedText(), 6, 80); g.drawString("Password: " + pass.getText(), 6, 100); } } HTML FILE(TextFieldDemo.HTML) <applet code="TextFieldDemo.class" width=380 height=150> </applet>

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO HANDLE DIFFERENT MOUSE EVENTS.
import java.awt.*; import java.awt.event.*; import java.applet.*; public class MouseEvents extends Applet implements MouseListener, MouseMotionListener { String msg = ""; int mouseX = 0, mouseY = 0; // coordinates of mouse public void init() { addMouseListener(this); addMouseMotionListener(this); } // Handle mouse clicked. public void mouseClicked(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse clicked."; repaint(); } // Handle mouse entered. public void mouseEntered(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse entered."; repaint(); } // Handle mouse exited. public void mouseExited(MouseEvent me) {

// save coordinates mouseX = 0; mouseY = 10; msg = "Mouse exited."; repaint(); } // Handle button pressed. public void mousePressed(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Down"; repaint(); } // Handle button released. public void mouseReleased(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Up"; repaint(); } // Handle mouse dragged. public void mouseDragged(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "*"; showStatus("Dragging mouse at " + mouseX + ", " + mouseY); repaint(); } // Handle mouse moved. public void mouseMoved(MouseEvent me) { // show status showStatus("Moving mouse at " + me.getX() + ", " + me.getY()); }

// Display msg in applet window at current X,Y location. public void paint(Graphics g) { g.drawString(msg, mouseX, mouseY); } } HTML FILE(MouseEvents.HTML) <applet code="MouseEvents.class" width=300 height=100> </applet>

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO HANDLE KEY EVENTS.
import java.awt.*; import java.awt.event.*; import java.applet.*; public class KeyEvents extends Applet implements KeyListener { String msg = ""; int X = 10, Y = 20; // output coordinates public void init() { addKeyListener(this); requestFocus(); // request input focus } public void keyPressed(KeyEvent ke) { showStatus("Key Down"); int key = ke.getKeyCode(); switch(key) { case KeyEvent.VK_F1: msg += "<F1>"; break; case KeyEvent.VK_F2: msg += "<F2>"; break; case KeyEvent.VK_F3: msg += "<F3>"; break; case KeyEvent.VK_PAGE_DOWN: msg += "<PgDn>"; break; case KeyEvent.VK_PAGE_UP: msg += "<PgUp>"; break; case KeyEvent.VK_LEFT: msg += "<Left Arrow>";

break; case KeyEvent.VK_RIGHT: msg += "<Right Arrow>"; break; } repaint(); } public void keyReleased(KeyEvent ke) { showStatus("Key Up"); } public void keyTyped(KeyEvent ke) { msg += ke.getKeyChar(); repaint(); } // Display keystrokes. public void paint(Graphics g) { g.drawString(msg, X, Y); } } HTML FILE(KeyEvents.HTML) <applet code="KeyEvents.class" width=300 height=100> </applet>

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO EXTRACT THE PORTION OF A STRING AND PRINT THE EXTRACTED PORTION.
class getCharsDemo { public static void main(String args[]) { String s = "This is a demo of the getChars method."; int start = 10; int end = 14; char buf[] = new char[end - start]; s.getChars(start, end, buf, 0); System.out.println(buf); } }

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO IMPLEMENT FLOYD’S TRIANGLE.
class Floyd { public static void main (String args[]) { LOOP1:for(int i=1;i<100;i++) { System.out.println(" "); if(i>=10) break; for(int j=1;j<100;j++) { System.out.print(i); if(j==i) continue LOOP1; } } } }

OUTPUT:-

Program No:-……….

Date:-…………

WRITE A PROGRAM TO MANIPULATE STRINGS(FINDING SUBSTRING,COMPARING STRING,PATTERN MATCHING)
class StringDemo { public static void main(String args[]) { String s1 = "Hello"; String s2 = "Hello"; String s3 = "Good-bye"; String s4 = "HELLO"; System.out.println("Comparing strings"); System.out.println(); System.out.println(s1 + " equals " + s2 + " -> " +s1.equals(s2)); System.out.println(s1 + " equals " + s3 + " -> " +s1.equals(s3)); System.out.println(s1 + " equals " + s4 + " -> " +s1.equals(s4)); System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " +s1.equalsIgnoreCase(s4)); String s5 = new String(s1); System.out.println(s1 + " equals " + s5 + " -> " +s1.equals(s5)); System.out.println(s1 + " == " + s5 + " -> " + (s1 == s5)); System.out.println(); System.out.println("Finding Substring"); System.out.println(); String s6="anil agarwal"; String s7; System.out.println("string is :" +s6);s7=s6.substring(3,8); System.out.println("substring is:"+s7); } }

OUTPUT:-

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.