About
JAVA: class TicketBookingSystem { private int ticket = 10;
synchronized public void bookticket(int requested_ticket,String name)
{
if(requested_ticket > ticket)
{
System.out.println("Not enough tickets for "+name);
}
else{
ticket -= requested_ticket;
System.out.println(requested_ticket+" no of tickets booked for "+name);
}
}
}
class Customer extends Thread { public String name; public int ticket; public TicketBookingSystem tbs;
public Customer(String name,int t,TicketBookingSystem tb)
{
ticket = t;
tbs = tb;
this.name = name;
}
public void run()
{
tbs.bookticket(ticket,name);
}
}
public class aadith { public static void main(String args[]) { TicketBookingSystem tbs = new TicketBookingSystem();
Customer c1 = new Customer("pradeep",5,tbs);
Customer c2 = new Customer("karthik",5,tbs);
Customer c3 = new Customer("rajesh",5,tbs);
c1.start();
c2.start();
c3.start();
}
} class commonsum { private int sum = 0;
public void addtosum(int value)
{
sum += value;
}
public int getsum()
{
return sum;
}
}
class Calculator extends Thread { private commonsum csum; private int lower = 0; private int upper = 0;
public Calculator(int lower,int upper,commonsum c)
{
csum = c;
this.lower = lower;
this.upper = upper;
}
public void run()
{
int sum = 0;
for(int num=lower;num<upper;num++)
{
int count = 0;
for(int i=1;i<=num;i++)
{
if(num%i == 0)
{
count++;
}
}
if(count == 2)
{
sum+=num;
}
}
System.out.println("Sum between "+lower+" to "+upper+"is: "+sum);
csum.addtosum(sum);
}
}
public class aadith1 { public static void main(String args[]) { commonsum csum = new commonsum();
Calculator c1 = new Calculator(0,250,csum);
Calculator c2 = new Calculator(250,500,csum);
Calculator c3 = new Calculator(500,750,csum);
Calculator c4 = new Calculator(750,1000,csum);
c1.start();
c2.start();
c3.start();
c4.start();
try {
c1.join(); // Wait until the thread finishes
c2.join();
c3.join();
c4.join();
System.out.println("Total sum is: "+csum.getsum());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} Scala : import scala.io.StdIn
object sample {
def add2(x:Int):Int = {
x+2
}
def mymapfn(arr:Array[Int],fn:(Int)=>Int):Array[Int] = {
var newarr = new Array[Int](arr.length)
for(i<-0 to arr.length-1)
{
newarr(i) = fn(arr(i))
}
newarr
}
def main(args:Array[String])
{
println("Enter the length of the array: ")
var length = StdIn.readInt()
var arr: Array[Int] = new Array[Int](length)
println("Enter the elements of the array:")
for(i<-0 to length-1)
{
var value = StdIn.readInt()
arr(i) = value
}
println("The entered array is:")
for(i<-0 to arr.length-1)
{
print(arr(i)+" ")
}
println()
var array = mymapfn(arr,add2)
println("The mapped array is: ")
for(i<-0 to array.length-1)
{
print(array(i)+" ")
}
println()
}
}
Implementation of map function