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

Lab Assignment 4

This document is a lab assignment submitted by Taha Yaseen for the subject Object Oriented Programming. It includes the creation of a Calculator class with methods like sum, multiplication, division, subtraction, modulus, sine, cosine, and tangent. The main method prompts the user to enter a number to perform the respective operation and passes the user input values to the Calculator class methods to return the result.

Uploaded by

taha butt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Lab Assignment 4

This document is a lab assignment submitted by Taha Yaseen for the subject Object Oriented Programming. It includes the creation of a Calculator class with methods like sum, multiplication, division, subtraction, modulus, sine, cosine, and tangent. The main method prompts the user to enter a number to perform the respective operation and passes the user input values to the Calculator class methods to return the result.

Uploaded by

taha butt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

COMSATS University Islamabad, Wah Campus

Electrical & Computer Engineering Department

Program: BEE Semester Section: 3A


Subject: OOP Instructor: Ma’am Samia Zafar

Lab Asssignment:01

Submitted by:
Name: Taha Yaseen Reg # FA20-BEE-018

Date of Submission:
13th-October-2021

Q1: Create a calculator class with sum , multiplication , division sin , cos ,tan as function.
Main:
public class Quiz2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.println("Enter\n 1 for Sum \n 2 for Multiplication \n 3 for Division \n 4 for


Subtraction \n 5 for Mod \n 6 for Sin \n 7 for Cos \n 8 for Tan");
int z= in.nextInt();
switch (z)
{case 1: // SUM
System.out.print("Enter first digit ");
double x= in.nextDouble();
System.out.print("Enter second digit ");
double y= in.nextDouble();
System.out.println(" Sum : "+Calculator.sum(x, y));
break;
case 2: //MULTIPLICATION
System.out.print("Enter first digit ");
x= in.nextDouble();
System.out.print("Enter second digit ");
y= in.nextDouble();
System.out.println("Prouct : "+Calculator.mul(x, y));
break;
case 3: //DIVISION
System.out.print("Enter first digit ");
x= in.nextDouble();
System.out.print("Enter second digit ");
y= in.nextDouble();
System.out.println( " = "+Calculator.div(x, y));
break;
case 4: //SUBTRACTION
System.out.print("Enter first digit ");
x= in.nextDouble();
System.out.print("Enter second digit ");
y= in.nextDouble();
System.out.println("Difference"+Calculator.diff(x, y));
break;
case 5: //MOD
System.out.print("Enter digit ");
x= in.nextDouble();
System.out.println("Mod : "+Calculator.mod(x));
break;
case 6: //SIN
System.out.print("Enter Degree ");
x= in.nextDouble();

System.out.println("Sin :"+Calculator.sin(x));
break;
case 7: //COS
System.out.print("Enter Degree ");
x= in.nextDouble();
System.out.println("Cos: "+Calculator.cos(x));
break;
case 8: //Tan
System.out.print("Enter Degree ");
x= in.nextDouble();
System.out.println("Tan :"+Calculator.tan(x));
break;
default :
System.out.println("Invalid");
}
}}

Class:
public class Calculator {
static double var1 ;
static double var2;
static double sum(double a , double b)
{
var1= a;
var2=b;
return a+b;
}
static double mul(double a , double b)
{
var1 = a;
var2 = b;
return a*b;
}
static double div(double a , double b)
{
var1 = a;
var2 = b;
return a/b;
}
static double diff(double a , double b)
{
var1 = a;
var2 = b;
return a-b;
}
static double mod(double a )
{
var1 = a;
if (a>=0)
return a;
else
return -a;
}
static double sin(double a )
{
var1 = a;
double p = Math.toRadians(a);
return Math.sin(p);
}
static double cos(double a )
{
var1 = a;
double p =Math.toRadians(a);

return Math.cos(p);
}
static double tan(double a )
{
var1 = a;
double p = Math.toRadians(a);
return Math.tan(p);
}

Result:

You might also like