0% found this document useful (0 votes)
125 views

(A) Write A Java Program To Multiply Two Given Matrices

The document contains two Java programs. The first program multiplies two matrices by iterating through the rows and columns of the first matrix and multiplying each element by the corresponding element in the second matrix, summing the results into an output matrix. The second program handles mouse and keyboard events in an applet by adding listeners, changing colors and text on events, and drawing to the screen. It demonstrates how to capture user input from these devices.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views

(A) Write A Java Program To Multiply Two Given Matrices

The document contains two Java programs. The first program multiplies two matrices by iterating through the rows and columns of the first matrix and multiplying each element by the corresponding element in the second matrix, summing the results into an output matrix. The second program handles mouse and keyboard events in an applet by adding listeners, changing colors and text on events, and drawing to the screen. It demonstrates how to capture user input from these devices.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.(a) Write a java program to multiply two given matrices.

class Array58 { public static void main(String[] args) { int i,j,k; int [][] a={{1,2,3},{4,5,6},{7,8,9}}; int [][] b={{9,8,7},{6,5,4},{3,2,1}}; int [][]c=new int[3][3]; for (i=0;i<a.length;i++) { for (j=0;j<a[i].length;j++) { System.out.print(a[i][j]+" "); } System.out.println(" "); } System.out.println(" "); for (i=0;i<b.length;i++ ) { for (j=0;j<b[i].length;j++) { System.out.print(b[i][j]+" "); } System.out.println(" "); } System.out.println(" "); for(i=0;i<a.length;i++) { for(j=0;j<b.length;j++) { c[i][j]=0; for(k=0;k<a.length;k++) { c[i][j]+=a[i][k]*b[k][j]; } System.out.print(c[i][j]+" "); } System.out.println(" "); } } }

1.(b)Write a java program to handle mouse events and keyboard events import java.io.*; import java.applet.Applet; import java.awt.*; import java.awt.event.*; /* */ public class Mouse extends Applet implements MouseListener,MouseMotionListener { String txt=""; int x=10,y=30; public void init() { addMouseListener(this); addMouseMotionListener(this); } public void mouseClicked(MouseEvent me) { txt="Mouse Clicked"; setForeground(Color.pink); repaint(); } public void mouseEntered(MouseEvent me) { txt="Mouse Entered"; repaint(); } public void mouseExited(MouseEvent me) { txt="Mouse Exited"; setForeground(Color.blue); repaint(); } public void mousePressed(MouseEvent me) { txt="Mouse Pressed"; setForeground(Color.blue); repaint(); } public void mouseMoved(MouseEvent me) { txt="Mouse Moved"; setForeground(Color.red); repaint(); } public void mouseDragged(MouseEvent me) { txt="Mouse Dragged"; setForeground(Color.green);

repaint(); } public void mouseReleased(MouseEvent me) { txt="Mouse Released"; setForeground(Color.yellow); repaint(); } public void paint(Graphics g) { g.drawString(txt,30,40); showStatus("Mouse events Handling"); } } 2.(a)Write a java program to write Applets to draw various polygons.

import java.applet.*; import java.awt.*; public class CircleLine extends Applet{ int x=300,y=100,r=50; public void paint(Graphics g){ g.drawLine(3,300,200,10); g.drawString("Line",100,100); g.drawOval(x-r,y-r,100,100); g.drawString("Circle",275,100); g.drawRect(400,50,200,100); g.drawString("Rectangel",450,100); } } Here is the HTML code of the program:
<HTML> <HEAD> </HEAD> <BODY>

<div align="center"> <APPLET CODE="CircleLine.class" WIDTH="800" HEIGHT="500"></APPLET> </div>


</BODY> </HTML>

2. (b)Write a java program to count number of words and characters in a text.

import java.io.File; import java.util.Scanner; public class FileWordVowelCounter { public static void main(String[] args) throws Exception { Scanner in = new Scanner(new File("read.txt")); int nbWords = 0; int nbVowels = 0; while(in.hasNext("\\S+")) { String word = in.next("\\S+"); nbVowels += getNbVowels(word); nbWords++; } System.out.println(nbWords); System.out.println(nbVowels); } public static int getNbVowels(String word) { int result = 0; char[] chars = word.toCharArray(); for(Character c : chars) { if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { result++; }} return result; } }

You might also like