Thursday, 19 January 2023

Write a java program to display alternate character from a given string. Write a java program using Applet to implement a simple arithmetic calculator

 Slip4

 Write a java program to display alternate character from a given string.

 import java.util.Scanner;

public class Main

{

            public static void main(String str[]) {

                         Scanner sc = new Scanner(System.in);

                        System.out.println("Enter an string");

                        String str1 = sc.nextLine();

                        try{

                        for(int i = 0; i<=str1.length() ; i=i+2){

                           System.out.println(str1.charAt(i));

                        }

                        } catch(StringIndexOutOfBoundsException e) {

            System.out.println("String index out of bounds. String length: " + str1.length());

        }

            }

}

 Write a java program using Applet to implement a simple arithmetic calculator.

 Calculator.java

 import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class Calculator extends Applet implements ActionListener

{

String str = " ";

int v1, v2, result;

TextField t1;

Button b[] = new Button[10];

Button add, sub, mul, div, clear, mod, equals;

char choice;

public void init()

{

Color k = new Color(120, 89, 90);

setBackground(k);

t1 = new TextField(10);

GridLayout g1 = new GridLayout(4, 5);

setLayout(g1);

 

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

{

b[i] = new Button("" + i);

}

add = new Button("add");

sub = new Button("sub");

mul = new Button("mul");

div = new Button("div");

mod = new Button("mod");

clear = new Button("clear");

equals = new Button("equals");

t1.addActionListener(this);

add(t1);

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

{

add(b[i]);

}

add(add);

add(sub);

add(mul);

add(div);

add(mod);

add(clear);

add(equals);

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

{

b[i].addActionListener(this);

}

add.addActionListener(this);

sub.addActionListener(this);

mul.addActionListener(this);

div.addActionListener(this);

mod.addActionListener(this);

clear.addActionListener(this);

equals.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

{

String str = ae.getActionCommand();

char ch = str.charAt(0);

if (Character.isDigit(ch))

t1.setText(t1.getText() + str);

else

{

if (str.equals("add"))

{

v1 = Integer.parseInt(t1.getText());

choice = '+';

t1.setText("");

}

else if (str.equals("sub"))

{

v1 = Integer.parseInt(t1.getText());

choice = '-';

t1.setText("");

}

if (str.equals("mul"))

{

v1 = Integer.parseInt(t1.getText());

choice = '*';

t1.setText("");

}

if (str.equals("div"))

{

v1 = Integer.parseInt(t1.getText());

choice = '/';

t1.setText("");

}

if (str.equals("mod"))

{

v1 = Integer.parseInt(t1.getText());

choice = '%';

t1.setText("");

}

if (str.equals("clear"))

{

t1.setText("");

}

if (str.equals("equals"))

{

v2 = Integer.parseInt(t1.getText());

switch (choice)

{

case '+': result = v1 + v2;

break;

case '-': result = v1 - v2;

break;

case '*': result = v1 * v2;

break;

case '/': result = v1 / v2;

break;

case '%': result = v1 % v2;

break;

}

t1.setText("" + result);

}

}

}

}

 Calculator.html

 <applet code="Calculator.class" width=400 height=400></applet>

C:\Program Files\Java\jdk1.6.0\bin\Java Slips>javac Calculator.java

C:\Program Files\Java\jdk1.6.0\bin\Java Slips>appletviewer Calculator.html