0% found this document useful (0 votes)
12 views6 pages

OOP PracticalFinal (19-SE-78)

The document contains instructions for a final term exam in Object Oriented Programming. It includes two questions - the first asks to write a program to count and print digits in a string, and determine if a substring is present. The second question asks to create an AWT program to convert currencies between US dollars, Euros, Yen, Canadian dollars and Rupees based on provided exchange rates. The code provided implements both questions by taking user input, performing the requested tasks, and displaying outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

OOP PracticalFinal (19-SE-78)

The document contains instructions for a final term exam in Object Oriented Programming. It includes two questions - the first asks to write a program to count and print digits in a string, and determine if a substring is present. The second question asks to create an AWT program to convert currencies between US dollars, Euros, Yen, Canadian dollars and Rupees based on provided exchange rates. The code provided implements both questions by taking user input, performing the requested tasks, and displaying outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

FINAL TERM EXAM

OBJECT ORIENTED PROGRAMMING |LAB

Submitted To: MA’AM SIDRA SHAFI


Submitted By: HAMZA QAISAR
Registration Number: 19-SE-78
DEPARTMENT OF SOFTWARE ENGINEERING
SUBMISSION DATE: February 3rd, 2021
Object Oriented Programming | Lab | Final Term Exam

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;

public class Q1_a {


public static void main(String[] args) {
Scanner src=new Scanner(System.in);
System.out.println("Enter the string: ");
String s=src.next();
int digits=0;
for(int i=0;i<s.length();i++){
if(Character.isDigit(s.charAt(i))){
digits++;
}
}
System.out.println("Total Number of Digits in
String = "+digits);
System.out.println("Digits in String =
"+s.replaceAll("[^\\d]", ""));
}
}

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

public class Q1_b {


public static void main(String[] args) {
String str_Determine = "Kill two birds with one stone";
//Check if String contains a Sequence
System.out.println("String 'Kill two birds with one stone '
contains substring 'birds': " + str_Determine.contains("birds"));
}
}

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);

label_1 = new Label("Convert: ");


label_1.setBounds(50,50,80,30);
label_1.setFont(font);
frame.add(label_1);

input = new TextField();


input.setBounds(140,55,100,25);
input.setFont(font);
frame.add(input);

label_2 = new Label("U.S. dollars to ");


label_2.setBounds(260,50,120,30);
label_2.setFont(font);
frame.add(label_2);

currency = new Choice();


currency.setBounds(400,50, 100, 25);
currency.setFont(font);
currency.add("Euros");
currency.add("Japanese Yen");
currency.add("Canadian Dollar");
currency.add("Rupees");
frame.add(currency);
ok = new Button("ok");
ok.setBounds(60,95, 40,30);
ok.setFont(font);
frame.add(ok);
ok.addActionListener(this);

label_2 = new Label("00");


label_2.setBounds(30,180, 550, 30);
label_2.setFont(font);
frame.add(label_2);

}
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

You might also like