About
object sample { def adder(x:Int,y:Int):Int = { return x+y }
def higherorder(x:Int,y:Int,fn:(Int,Int)=>Int):Int = {
fn(x,y)
}
def main(Args:Array[String])
{
print(higherorder(3,4,adder))
}
}
Simple higher order function
object sample {
def factorial(n:Int):Int = {
if(n == 0)
{
return 0
}
else if(n == 1)
{
return 1
}
else{
return n*factorial(n-1)
}
}
def main(Args:Array[String])
{
println(factorial(5))
}
}
Simple recursion question
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
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
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();
}
}
}
////////////////////////////////////////////
import java.util.ArrayList; import java.util.List;
public class MultithreadingOperation{ public static void main(String args[]){
Thread table3 = new Thread (new Table03());
Thread table4 = new Thread (new Table04());
Thread table5 = new Thread (new Table05());
table3.start();
table4.start();
table5.start();
try{
table3.join();
table4.join();
table5.join();
}
catch(InterruptedException e){
}
}
static class Table03 implements Runnable{
private static final Object lock = new Object();
public Table03(){
}
@Override
public void run(){
try{
Thread.sleep(100);
synchronized (MultithreadingOperation.class){
System.out.println("Table 3:");
for (int i=1;i<10;i++){
System.out.println(i+" X 3 = "+(i*3));
}
}
}
catch(InterruptedException e){
}
}
}
static class Table04 implements Runnable{
private static final Object lock = new Object();
public Table04(){
}
@Override
public void run(){
try{
Thread.sleep(100);
synchronized (MultithreadingOperation.class){
System.out.println("Table 4:");
for (int i=1;i<10;i++){
System.out.println(i+" X 4 = "+(i*4));
}
}
}
catch(InterruptedException e){
}
}
}
static class Table05 implements Runnable{
private static final Object lock = new Object();
public Table05(){
}
@Override
public void run(){
try{
Thread.sleep(100);
synchronized (MultithreadingOperation.class){
System.out.println("Table 5:");
for (int i=1;i<10;i++){
System.out.println(i+" X 5 = "+(i*5));
}
}
}
catch(InterruptedException e){
}
}
}
}
class BankAccount { private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public synchronized void deposit(double amount) {
balance += amount;
}
public synchronized double inquire() {
return balance;
}
}
class Customer implements Runnable { private String name; private double amount; private BankAccount account;
public Customer(String name, double amount, BankAccount account) {
this.name = name;
this.amount = amount;
this.account = account;
}
public void run() {
account.deposit(amount);
System.out.println(name + " deposited $" + amount);
System.out.println(name + "'s balance: $" + account.inquire());
}
}
public class Main { public static void main(String[] args) { BankAccount account = new BankAccount(1000);
// Create three customer threads
Customer customer1 = new Customer("Customer 1", 500, account);
Customer customer2 = new Customer("Customer 2", 300, account);
Customer customer3 = new Customer("Customer 3", 200, account);
Thread thread1 = new Thread(customer1);
Thread thread2 = new Thread(customer2);
Thread thread3 = new Thread(customer3);
// Start all customer threads
thread1.start();
thread2.start();
thread3.start();
}
}