Tuesday, April 18, 2023

3. a) Develop an applet in Java that displays a simple message. b) Develop an applet in Java that receives an integer in one text field, and computes its factorial Value and returns it in another text field, when the button named “Compute” is clicked.

 a)      Develop an applet in Java that displays a simple message.

Program:

import java.awt.*;

import java.applet.Applet;

/* <applet code="SimpleApplet" width=300 height=50> </applet> */

public class SimpleApplet extends Applet

{

 public void paint(Graphics g)

 {

 g.drawString ("A Simple Applet",100, 100);

 }

}

 

Execution:

C:\Users\vits\Desktop\java> javac  SimpleApplet.java

 

C:\Users\vits\Desktop\java>appletviewer  SimpleApplet.java

 

Output:

 


 

b)      Develop an applet in Java that receives an integer in one text field, and computes its factorial Value and returns it in another text field, when the button named “Compute” is clicked.

Program:

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

/* <applet code="Fact" width=300 height=50> </applet> */

public class Fact extends Applet implements ActionListener

{

Label l1,l2; TextField t1,t2; Button b1;

public void init(){

l1=new Label("enter the value"); add(l1);

t1=new TextField(10); add(t1);

b1=new Button("Factorial"); add(b1); b1.addActionListener(this);

l2=new Label("Factorial of given no is"); add(l2);

t2=new TextField(10); add(t2);

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==b1)

{

int fact=fact(Integer.parseInt(t1.getText())); t2.setText(String.valueOf(fact));

}

}

int  fact(int f)

{

int  s=0;

if(f==0)

 return 1;

else

return  f*fact(f-1);

}

}

 

Execution:

C:\Users\vits\Desktop\java>javac   Fact.java

 

C:\Users\vits\Desktop\java>appletviewer   Fact.java

 

Output:

 


Monday, April 17, 2023

Experiment No: 4 Write a Java program that creates a user interface to perform integer divisions. The user enters two numbers in the text fields, Num1 and Num2. The division of Num1 and Num 2 is displayed in the Result field when the Divide button is clicked. If Num1 or Num2 were not an integer, the program would throw a Number Format Exception. If Num2 were Zero, the program would throw an Arithmetic Exception. Display the exception in a message dialog box.

 Program:

import java.awt.*;

import java.awt.event.*;


import javax.swing.*;

/*

&lt;applet code=&quot;DivApplet&quot; width=350 height=300&gt;

&lt;/applet&gt;

*/

public class DivApplet extends JApplet implements ActionListener{

JTextField number1,number2,result;

JButton divide;

public void init(){

try

{

SwingUtilities.invokeAndWait(

new Runnable() {

public void run() {

makeGUI();

}

});

}

catch (Exception exc)

{

System.out.println(&quot;Can&#39;t create because of &quot; + exc);

}

}

private void makeGUI()

{

setLayout(new FlowLayout());

Label number1p = new Label(&quot;Number1: &quot;,Label.RIGHT);

Label number2p = new Label(&quot;Number2: &quot;,Label.RIGHT);

number1= new JTextField(20);

number2 = new JTextField(20);

result = new JTextField(20);

divide = new JButton(&quot;Divide&quot;);

add(number1p);

add(number1);

add(number2p);


add(number2);

add(result);

add(divide);

divide.addActionListener(this);

}

public void actionPerformed(ActionEvent e){

String snumber1,snumber2;

snumber1 = number1.getText();

snumber2 = number2.getText();

try{

int number1 = Integer.parseInt(snumber1);

int number2 = Integer.parseInt(snumber2);

if(number2==0)

JOptionPane.showMessageDialog(null, &quot;Division by zero not defined.&quot;);

else{

double r = (double)number1/number2;

result.setText(((Double)r).toString());

}

}

catch(NumberFormatException ne)

{

JOptionPane.showMessageDialog(null,&quot;Enter a number&quot;);

}

}

}

Execution:

C:\Users\vits\Desktop\java&gt;javac DivApplet.java

C:\Users\vits\Desktop\java&gt;appletviewer DivApplet.java

Output:



Monday, April 10, 2023

Experiment No: 5 Aim: Write a Java program that implements a multi-thread application that has three threads. First thread generates random integer every 1 second and if the value is even, second thread computes the square of the number and prints. If the value is odd, the third thread will print the value of cube of the number.

 

Program:

 

import java.util.Random;

class Square extends Thread

{

 int x;

 Square(int n)

 {

 x = n;

 }

 public void run()

 {

 int sqr = x * x;

 System.out.println("Square of " + x + " = " + sqr );

 }

}

class Cube extends Thread

{

 int x;

 Cube(int n)

 {x = n;

 }

 public void run()

 {

 int cub = x * x * x;

 System.out.println("Cube of " + x + " = " + cub );

 }

}

class Number extends Thread

{

 public void run()

 {

 Random random = new Random();

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

 {

 int randomInteger = random.nextInt(100);

 

 System.out.println("Random Integer generated : " + randomInteger);

 Square s = new Square(randomInteger);

 s.start();

 Cube c = new Cube(randomInteger);

 c.start();

 try

{

 Thread.sleep(1000);

}

catch (InterruptedException ex)

{

 System.out.println(ex);

}

 }

 }

}

public class Thr

{

 public static void main(String args[])

 {

 Number n = new Number();

 n.start();

 }

}

Output:

 

C:\Users\vits\Desktop\java>javac Thr.java

 

C:\Users\vits\Desktop\java>java Thr

Random Integer generated : 61

Square of 61 = 3721

Cube of 61 = 226981

Random Integer generated : 19

Square of 19 = 361

Cube of 19 = 6859

Random Integer generated : 13

Square of 13 = 169

Cube of 13 = 2197

Random Integer generated : 5

Square of 5 = 25

Cube of 5 = 125

Random Integer generated : 94

Square of 94 = 8836

Cube of 94 = 830584

A simple Java program to find the inverse of a given matrix

  import java.util.Scanner; public class MatrixInverse { public static void main (String[] args) { Scanner scanner =...