java prac

10
1. I/P two matrix & multiplication in third matrix ( MatrixMul.java) Solution:- //""""""""""""""""""""""""""Practical No :- 1 """"""""""""""""""""""""" //Object:- I/P two matr ix & multiplication in t hird matrix import java.io.*; class MatrixMul { static int r[]=new int[2]; static int c[]=new int[2]; int m1[][];//=new int[r[0]][c[0]]; int m2[][];//=new int[r[1]][c[1]]; int m3[][];//=new int[r[0]][c[1]] ; MatrixMul(BufferedReader br) {  for(int i=0;i<2;i++) { int sr=1; System.out.print("enter row and colmn for matrix"+sr); try { r[i]=Integer.parseInt(br.readLine()); c[i]=Integer.parseInt(br.readLine()); } catch(IOException e) {} sr++; } m1=new int[r[0]][c[0]] ; m2=new int[r[1]][c[1]]; m3=new int[r[0]][c[1]] ;  } void InputMatrix(BufferedReader br) { System.out.print("enter first matrix"+r[0]+","+c[0]); for(int i=0;i<r[0];i++) { for(int j=0;j<c[0];j++) { try { m1[i][j]=Integer.parseInt(br.readLine()); } catch(IOException e) {}

Transcript of java prac

Page 1: java prac

8/4/2019 java prac

http://slidepdf.com/reader/full/java-prac 1/10

1. I/P two matrix & multiplication in third matrix (MatrixMul.java)

Solution:-

//""""""""""""""""""""""""""Practical No :- 1 """""""""""""""""""""""""//Object:- I/P two matrix & multiplication in third matrix

import java.io.*;

class MatrixMul{

static int r[]=new int[2];

static int c[]=new int[2];int m1[][];//=new int[r[0]][c[0]];

int m2[][];//=new int[r[1]][c[1]];

int m3[][];//=new int[r[0]][c[1]];

MatrixMul(BufferedReader br){

 

for(int i=0;i<2;i++)

{int sr=1;

System.out.print("enter row and colmn for matrix"+sr);try

{

r[i]=Integer.parseInt(br.readLine());

c[i]=Integer.parseInt(br.readLine());}

catch(IOException e)

{}sr++;

} m1=new int[r[0]][c[0]];m2=new int[r[1]][c[1]];

m3=new int[r[0]][c[1]];

 }

void InputMatrix(BufferedReader br)

{

System.out.print("enter first matrix"+r[0]+","+c[0]);for(int i=0;i<r[0];i++)

{

for(int j=0;j<c[0];j++){

try

{m1[i][j]=Integer.parseInt(br.readLine());

}

catch(IOException e)

{}

Page 2: java prac

8/4/2019 java prac

http://slidepdf.com/reader/full/java-prac 2/10

}

}

System.out.print("enter second matrix"+r[1]+","+c[1]);for(int i=0;i<r[1];i++)

{

for(int j=0;j<c[1];j++){

try

{m2[i][j]=Integer.parseInt(br.readLine());

}

catch(IOException e)

{}}

}

 

}

 

void multiplication()

{

 for(int i=0;i<r[0];i++)

{

for(int j=0;j<c[1];j++){

for(int k=0;k<c[0];k++){

m3[i][j]=m1[i][k]*m2[k][j]+m3[i][j];

}}

}

}

void showmatrix( ){

for(int i=0;i<m3.length;i++)

{for(int j=0;j<m3[i].length;j++)

{

System.out.print(" "+m3[i][j]);}

System.out.println();

}

 

Page 3: java prac

8/4/2019 java prac

http://slidepdf.com/reader/full/java-prac 3/10

}

public static void main(String args[])

{BufferedReader br;

br=new BufferedReader(new InputStreamReader(System.in));

MatrixMul matrix=new MatrixMul(br); 

matrix.InputMatrix(br);

matrix.multiplication();

matrix.showmatrix();}

 

}/*

output:-

E:\sourabh\java program>javac MatrixMul.java

E:\sourabh\java program>java MatrixMul

enter row and colmn for matrix12

2enter row and colmn for matrix12

4

enter first matrix2,211

1

1enter second matrix2,42

2

2

22

2

22

4 4 4 4

4 4 4 4*/

2. Drive a person class &inherit employee & student classes from it .A person

class contains its name ,address phone no ,emp contains additional features

Page 4: java prac

8/4/2019 java prac

http://slidepdf.com/reader/full/java-prac 4/10

like emp id , salary & student contains roll no, branch ,sem ,information also

.define getter setter method in class .(Person.java)

Solution:

//""""""""""""""""""""""""practical no 2""""""""""""""""

/* OBject:- *Drive a person class &inherit employee & student classes fromit .A person

class contains its name ,address phone no ,emp contains additional features like

emp id , salary & student contains roll no, branch ,sem ,information also .definegetter setter method in class .(Person.java)*/

import javax.swing.*;

class PersonClass{

String name,phone_no,address;

PersonClass()

{

}void input()

{

name=JOptionPane.showInputDialog(null,"NAME");

 phone_no=JOptionPane.showInputDialog(null,"PHONE NO.");address=JOptionPane.showInputDialog(null,"ADDRESS");

}

void show(){

System.out.print("\n NAME :"+name);

System.out.print("\n PHONE N :"+phone_no);System.out.print("\n ADDRESS :"+address);

}

}class Student extends Person

{

Student()

{//constructor 

}

String roll_no,college_name,sem,branch;void inputstu()

{

roll_no= JOptionPane.showInputDialog(null,"ROLL NO."); branch= JOptionPane.showInputDialog(null,"BRANCH");

sem= JOptionPane.showInputDialog(null,"SEMISTER");

college_name= JOptionPane.showInputDialog(null,"COLLEGE NAME");

}

Page 5: java prac

8/4/2019 java prac

http://slidepdf.com/reader/full/java-prac 5/10

void showstu()

System.out.print("\n COLLEGE NAME :"+college_name);

System.out.print("\n ROLL NO :"+roll_no);System.out.print("\n BRANCH :"+branch);

System.out.print("\n SEMESTER :"+sem);

}

}

class Employee extends Person

{String e_id,Salary,company_name;

Employee()

{

//constructor }

void inputemp(){

e_id= JOptionPane.showInputDialog(null,"EMPLOYEE ID");

company_name= JOptionPane.showInputDialog(null,"COMPANY

 NAME");Salary= JOptionPane.showInputDialog(null,"SALARY");

}

void showemp(){

System.out.print("\n EMPLOYEE ID :"+e_id);

System.out.print("\n COMPANY NAME :"+company_name);System.out.print("\n SALARY :"+Salary);

}

}class Person_info

{

public static void main(String args[])

Student s1=new Student();s1.input();

s1.inputstu();

s1.show();s1.showstu();

Employee e1=new Employee();

e1.input();

Page 6: java prac

8/4/2019 java prac

http://slidepdf.com/reader/full/java-prac 6/10

e1.inputemp();

e1.show();

e1.showemp();}

}

3. Declare a vehical interface that contain abstract method like drive(),start(),stop()

and in bike and scooter classes with additional features .Solution:-

interface Vehicle

{

void drive();void stop();

void start();

}

class Car implements Vehicle{

public void drive(){

System.out.println("driving a car");

}

public void stop(){

System.out.println("stop a car");

}public void start()

{

System.out.println("start a car");}

 

}class Scooter implements Vehicle

{

public void drive()

{System.out.println("driving a scooter");

}

public void stop(){

System.out.println("stop a scooter");

} public void start()

{

System.out.println("start a scooter");

}

Page 7: java prac

8/4/2019 java prac

http://slidepdf.com/reader/full/java-prac 7/10

}

class Bike implements Vehicle

{ public void drive()

{

System.out.println("driving a bike");}

 public void stop()

{System.out.println("stop a bike");

}

 public void start()

{System.out.println("start a bike");

}

}

class Implements{

public static void main(String args[]){

Vehicle v;

Car c=new Car();Bike b=new Bike();

Scooter s=new Scooter();

v=c;v.drive();

v.stop();

v.start();v=b;

 b.drive();

 b.stop(); b.start();

v=s;

s.drive();

s.stop();s.start();

}}

4. Develop a program to i/p and print following format also handle expression if 

raised in program (StringFormat.java)

INDIA

Page 8: java prac

8/4/2019 java prac

http://slidepdf.com/reader/full/java-prac 8/10

INDI

IND

INI

Solution:-

//''''''''''''''''practical 4'''''''''''/*object:-4. Develop a program to i/p and print following format also handle

expression if raised in program

INDIAINDI

IND

IN

I*/

class StringFormat

{

public static void main(String args[]){

String s="india";try

{

for(int i=5;i<=5;i--)

System.out.println(s.substring(0,i));}

catch(Exception e)

{}}

}

5. develop an application that take 2 file name frome cmd line arg.and copy thecontents of sourse file into target file .( CopiesFile.java)

solution:-

import java.io.*;

class CopiesFile

{

public static void main(String[] args){

FileInputStream fin=null;

FileOutputStream fout=null;try

{

fin=new FileInputStream(args[0]);fout=new FileOutputStream(args[1]);

do

{

fout.write(fin.read());

Page 9: java prac

8/4/2019 java prac

http://slidepdf.com/reader/full/java-prac 9/10

}while(fin.available()>0);

fin.close();fout.close();

System.out.println("file copied successfully");

}

catch(Exception e)

{System.out.print(""+e);

}

}

}6. develop a thread based app. Then executes two method concurrently.

7.design an applet to draw a car object and move it horizontly.

Solution:-/*<applet code="CarApp.class"

width=800height=800></applet>

*/import java.awt.*;

import java.applet.*; public class Car1 extends Applet

{

int x[]={45,485,485,385,350,190,140,80,45,392,378,173,158,158,378};int y[]={325,325,240,240,175,175,225,225,275,318,307,318,307,307,307};

public void init()

{setBackground(Color.blue);

}

public void paint(Graphics g){

g.setColor(Color.black);

g.setColor(Color.black);

g.fillOval(x[12],y[12],50,50);

g.setColor(Color.white);

g.drawOval(x[11],y[11],20,20);

g.setColor(Color.black);

g.fillOval(x[10],y[10],50,50);

g.setColor(Color.white);

Page 10: java prac

8/4/2019 java prac

http://slidepdf.com/reader/full/java-prac 10/10

g.drawOval(x[9],y[9],20,20);

g.setColor(Color.black);

g.fillArc(x[13],y[13],50,50,0,180);

g.fillArc(x[14],y[14],50,50,0,180);

g.setColor(Color.red);g.fillPolygon(x,y,9);

for(int i=0;i<x.length;i++)x[i]=x[i]+2;

 

try{

Thread.sleep(200);}

catch(Exception e)

{}

repaint();}

}