0% found this document useful (0 votes)
119 views4 pages

OOP Lab 13 Tasks PDF

The document describes two tasks completed as part of an OOP lab assignment. Task 1 involves creating three threads - one to generate random integers every second, and two others to calculate and print the square if even or cube if odd of the number. Task 2 creates threads to concurrently calculate the sine, cosine and tangent of angles, then combines the results after ensuring all threads have completed.
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)
119 views4 pages

OOP Lab 13 Tasks PDF

The document describes two tasks completed as part of an OOP lab assignment. Task 1 involves creating three threads - one to generate random integers every second, and two others to calculate and print the square if even or cube if odd of the number. Task 2 creates threads to concurrently calculate the sine, cosine and tangent of angles, then combines the results after ensuring all threads have completed.
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/ 4

OOP Lab 13 Tasks

MUHAMMAD AQIB JAVED


18-SE-50
Task 1:
Write a java program that implements a multi-thread application
that has three threads. First thread generates random integer every
1 second and if the value is even, second thread computes the square
of the number and prints. If the value is odd, then the thread will
print the cube of the number.
Code:
package aqibprectice;
import java.util.Random;

class Square extends Thread


{
int x;
Square(int n) {
x = n;
}
public void run()
{
int sqr = x * x;
System.out.println("Square of " + x + " = " + sqr );
}
}
class Cube extends Thread
{
int x;
Cube(int n)
{
x = n;
}
public void run()
{
int cub = x * x * x;
System.out.println("Cube of " + x + " = " + cub );
}
}
class Number extends Thread {
public void run()
{
Random random = new Random();
for(int i =0; i<10; i++)
{
int randomInteger = random.nextInt(100);
System.out.println("Random Integer generated : " + randomInteger);
Square s = new Square(randomInteger);
s.start();
Cube c = new Cube(randomInteger);
c.start();
try {
Thread.sleep(1000);

} catch (InterruptedException ex) {


System.out.println(ex);
}
}
}
}
public class lab_13 {
public static void main(String[] args) {
Number n = new Number();
n.start();
}

}
Output:
Task 2:
Write a program named MathThreads.java that perform
concurrent mathematical operations like finding Sin, Cos and Tan
of an angle using multiple threads. Although the master thread can
continue its execution, in this case, it needs to make sure that all
operations are completed before combining individual results. z=
sin(45)+cos(60)+tan(30)
Code:
package aqibprectice;
import java.lang.Math;
class MathSin extends Thread
{
public double deg;
public double res;
public MathSin(int degree)
{ deg = degree;
}
public void run()
{
System.out.println("Executing sin of "+deg);
double Deg2Rad = Math.toRadians(deg);
res = Math.sin(Deg2Rad);
System.out.println("Exit from MathSin.Res = "+res);
}
}
class MathCos extends Thread
{
public double deg;
public double res;

public MathCos(int degree)


{ deg = degree;
}
public void run()
{
System.out.println("Executing cos of :"+deg);
double Deg2Rad = Math.toRadians(deg);
res = Math.cos(Deg2Rad);
System.out.println("Exit from MathCos.Res = "+res);
}
}
class MathTan extends Thread
{
public double deg;
public double res;
public MathTan(int degree)
{
deg = degree;
}
public void run()
{
System.out.println("Executing tan of :"+deg);
double Deg2Rad = Math.toRadians(deg);
res = Math.tan(Deg2Rad);
System.out.println("Exit from MathTan.Res = "+res); } }
public class lab_13_task2 {

public static void main(String[] args) {


// TODO Auto-generated method stub
MathSin st = new MathSin(45);
MathCos ct = new MathCos(60);
MathTan tt = new MathTan(30);
st.start();
ct.start();
tt.start();
try { // wait for completion of all thread and then sum
st.join();
ct.join();
//wait for completion of MathCos object
tt.join();
double z = st.res + ct.res + tt.res;
System.out.println("Sum of sin, cos, tan = "+z);
}
catch(InterruptedException IntExp) { }
}
}
Output:

You might also like