0% found this document useful (0 votes)
104 views

Assignment 1 - Semester Fall 2019: COMSATS University Islamabad, Lahore Campus Department of Electrical Engineering

The document is an assignment sheet for an Object Oriented Programming course. It instructs students to rewrite a Java calculator program using object-oriented principles. Students are asked to extract the mathematical operations into separate class methods and add a method for user input. The original calculator program uses a switch statement to perform addition, subtraction, multiplication, division, or remainder operations on two numbers based on a operator selected by the user.
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)
104 views

Assignment 1 - Semester Fall 2019: COMSATS University Islamabad, Lahore Campus Department of Electrical Engineering

The document is an assignment sheet for an Object Oriented Programming course. It instructs students to rewrite a Java calculator program using object-oriented principles. Students are asked to extract the mathematical operations into separate class methods and add a method for user input. The original calculator program uses a switch statement to perform addition, subtraction, multiplication, division, or remainder operations on two numbers based on a operator selected by the user.
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/ 2

COMSATS University Islamabad, Lahore Campus

Department of Electrical Engineering

Assignment 1 – Semester Fall 2019

Course Title: Object Oriented Programing Course Code: CSC241 Credit 4(3,1)
Hours:
Course Instructor/s: Mughees Ahmad Program BEE
Name:
Semester: Batch: Section: Date
Student’s Name: Reg. No.
Important Instructions / Guidelines:
• Use front page and submit it in printed form.
• Do not copy code from internet else you will be awarded with a zero.
• Due Date: 27-09-2019

Q1: Rewrite the given code into object-oriented java code. Estimate mathematical operations and rewrite a
separate class method for each operation. Extend your code by adding a separate method for user input.

Data:

double firstNumber
double secondNumber
String operator

Methods:

Userinput
Sum
Difference
Multiplication
Division
Remainder

Classes:

Calculator
Main

import java.util.Scanner;
import java.io.*;

public class Calculator


{

public static void main(String[] args) {


System.out.println(" Welcome to Java Calculator \n");
Scanner input = new Scanner(System.in);

System.out.println("\n Please enter two numbers");


Page 1 of 2
System.out.print("\n First number: ");
double firstNumber = input.nextInt();
System.out.print("\n Second number: ");
double secondNumber = input.nextInt();
System.out.println("\n Select between (*,/,+,-,%)\n Type out the character in a single letter: ");
String operator = input.next();

switch (operator) {
case "*":
System.out.println("Multiplication =: "+ ( firstNumber * secondNumber ));
break;
case "/":
System.out.println("Division =: "+ ( firstNumber / secondNumber ));
break;
case "+":
System.out.println("Addition =: "+ ( firstNumber + secondNumber ));
break;
case "-":
System.out.println("Difference =: "+ ( firstNumber - secondNumber ));
break;
case "%":
System.out.println("Remainder =: "+ ( firstNumber % secondNumber ));
break;

default: System.out.println("\n Please select a valid character");


}

System.out.println(" Closing Application ");


}
}

Page 2 of 2

You might also like