INTERNATIONAL UNIVERSITY OF GRAND BASSAM
IUGB School
Course ID# CSC 2301- Principles of Computer Programming
LAB Room 2 Grand-Bassam
Final Exam, April 24th 2023, Duration: 1 hour 30
Given by: Dr André Claude BAYOMOCK
Student’s Nama: Laknapin Mohamed LAMIZANA
CSC 2301- Final Exam
Problem 1
1-
/**
*
* @author HP
*/
public class CalculatorExam {
public CalculatorExam() {
public static double add(double x, double y) {
return x + y;
}
public static double sub(double x, double y) {
return x - y;
}
public static double mult(double x, double y) {
return x * y;
}
public static int div(int x, int y) {
return x / y;
}
public static int modulo(int x, int y) {
return x % y;
}
public static double power(double x, double y) {
return Math.pow(x, y);
}
public static void main(String[] args) {
double num1 = 10.5;
double num2 = 3.0;
double sum = add(num1, num2);
double difference = sub(num1, num2);
double product = mult(num1, num2);
int quotient = div(20, 3);
int remainder = modulo(20, 3);
double result = power(num1, num2);
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
System.out.println("Result: " + result);
}
}
2-
/**
*
* @author HP
*/
public class CalculatorGUIExam {
public CalculatorGUIExam() {
setTitle("Calculator");
setSize(250, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 3));
numButtons = new JButton[10];
for (int i = 0; i < 10; i++) {
numButtons[i] = new JButton("" + i);
numButtons[i].addActionListener(this);
panel.add(numButtons[i]);
}
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
resetButton = new JButton("C");
eqButton = new JButton("=");
addButton.addActionListener(this);
subButton.addActionListener(this);
mulButton.addActionListener(this);
divButton.addActionListener(this);
resetButton.addActionListener(this);
eqButton.addActionListener(this);
panel.add(addButton);
panel.add(subButton);
panel.add(mulButton);
panel.add(divButton);
panel.add(resetButton);
panel.add(eqButton);
panel.add(new JLabel());
panel.add(new JLabel());
panel.add(new JLabel());
panel.add(new JLabel());
resultLabel = new JLabel("0");
resultLabel.setHorizontalAlignment(JLabel.RIGHT);
resultLabel.setFont(new Font("Arial", Font.BOLD, 16));
add(panel, BorderLayout.CENTER);
add(resultLabel, BorderLayout.NORTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numButtons[i]) {
String num = resultLabel.getText();
if (num.equals("0")) {
resultLabel.setText("" + i);
} else {
resultLabel.setText(num + i);
}
}
}
if (e.getSource() == addButton) {
operator = '+';
num1 = Double.parseDouble(resultLabel.getText());
resultLabel.setText("0");
} else if (e.getSource() == subButton) {
operator = '-';
num1 = Double.parseDouble(resultLabel.getText());
resultLabel.setText("0");
} else if (e.getSource() == mulButton) {
operator = '*';
num1 = Double.parseDouble(resultLabel.getText());
resultLabel.setText("0");
} else if (e.getSource() == divButton) {
operator = '/';
num1 = Double.parseDouble(resultLabel.getText());
resultLabel.setText("0");
} else if (e.getSource() == resetButton) {
resultLabel.setText("0");
num1 = 0;
num2 = 0;
operator = ' ';
} else if (e.getSource() == eqButton) {
num2 = Double.parseDouble(resultLabel.getText());
double result = 0.0;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
resultLabel.setText("" + result);
}
}
public static void main(String[] args) {
new CalculatorGUIExam();
}
}
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Problem 2
/**
*
* @author HP
*/
public class TempConversion {
Label fL, cL;
TextField fTF, cTF;
public TempConversion(String title, int width, int height, int locX , int locY){
super(title);
setLayout(new GridLayout(2,2,10,10));
setLocation(locX, locY);
setSize(width,height);
fL= new Label("Forenheit");
cL= new Label("Celsius");
fTF= new TextField();
cTF= new TextField();
add(fL);
add(fTF);
add(cL);
add(cTF);
cTF.addTextListener(new TempConversion.WordListener());
fTF.addTextListener(new TempConversion.WordListener());
addWindowListener(new WindowCloser());
setVisible(true);
}
class WordListener implements TextListener{
public void textValueChanged(TextEvent evt){
if (evt.getSource()==fTF){
String fahrenheitTextVal=fTF.getText();
double curF=Double.parseDouble(fahrenheitTextVal);
double curC= (curF-32)*5/9;
curC=Math.round(curC);
cTF.setText("" + curC);
}
if (evt.getSource()==cTF){
String celsiusTextVal=cTF.getText();
double curC=Double.parseDouble(celsiusTextVal);
double curF= (curC*9/5)+32;
curF=Math.round(curF);
fTF.setText("" + curF);
}
}
}
class WindowCloser extends WindowAdapter{
public void windowClosing(WindowEvent evt){
System.exit(0);
}
}
public static void main(String[] args) {
TempConversion tc= new TempConversion("Temperature Conversion", 150,100,80,80);
System.out.println("Hello World!");
}
}