OOP PracticalFinal (19-SE-78)
OOP PracticalFinal (19-SE-78)
Question # 1
(a) Write a Java program to input a string and find the total number of digits in a string and
print all digits present in the string.
Code
import java.util.Scanner;
Output
(b) Write a Java program to determine whether the String “birds” is a substring of an idiom
“Kill two birds with one stone”.
Code
Output
Question # 2
Write a Java program using AWT which converts one currency into another currency.
Euros=0.83 * US$
Japanese Yen= 105.12 * US$
Canadian Dollars= 1.28 * US$
Rupees=160.35 * US$
Code
import java.awt.*;
import java.awt.event.*;
public class currency_Converter extends WindowAdapter implements
ActionListener{
Frame frame;
TextField input;
Label label_1,label_2,output;
Choice currency;
Button ok;
public static void main(String[] args) {
new Converter();
}
public currency_Converter () {
frame = new Frame("CurrencyConverter");
frame.setSize(650,300);
frame.setLayout(null);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.addWindowListener(this);
Font font =new Font(Font.SERIF,Font.PLAIN,18);
}
public void windowClosing(WindowEvent event) {
System.exit(0
);}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(ok)) {
char[] ch = input.getText().toCharArray();
for(int i=0; i<ch.length; i++) {
if(ch[i]==46)continue;
if(Character.isDigit(ch[i])) {
if(i==ch.length-1) {
convert(Double.parseDouble(String.valueOf(ch)),
currency.getSelectedItem());
}
}else {
label_2.setText("The number you typed in was not
in the correct format. Use only numbers.");
}
}
}
}
private void convert(double nums, String currency) {
if(currency.equals("Euros")) {
label_2.setText(nums+" U.S. Dollars = "+ (nums*0.83)+"
Euros.");
}else if(currency.equals("Japanese")) {
label_2.setText(nums+" U.S. Dollars = "+
(nums*105.12)+" Japanese Yen.");
}else if(currency.equals("Canadian")) {
label_2.setText(nums+" U.S. Dollars = "+ (nums*1.28)+"
Canadian Dollars.");
}else if(currency.equals("Rupees")) {
label_2.setText(nums+" U.S. Dollars = "+
(nums*160.35)+"Rupees.");
}else {
}
}
Output