Monday, July 17, 2023

6. Write a Prolog program to remove the Nth item from a list.

 6. Write a Prolog program to remove the Nth item from a list. 

del(1,[_|T],T).

del(P,[X|Y],[X|R]):-

 P1 is P-1,

 del(P1,Y,R).


OUTPUT:

del(2,[1,2],X).

X = [1]

Next101001,000 Stop

del(3,[1,2,3,4],X).

X = [1, 2, 4]

Next101001,000 Stop

del(5,[1,2,3,4],X).

false


5. Write a prolog program, insert_nth(item, n, into_list, result) that asserts that result is the list into_list with item inserted as the n‘th element into every list at all levels.

 5. Write a prolog program, insert_nth(item, n, into_list, result) that asserts that result is the list into_list with item inserted as the n‘th element into every list at all levels. 


insert_nth(Item,1,List,[Item|List]).

insert_nth(Item,Pos,[H|List],[H|Result]):-Pos1 is Pos-1,insert_nth(Item,Pos1,List,Result).





OUTPUT:

insert_nth(5,1,[2],X).
X = 2


insert_nth(5,1,[9,8,7,6],Y).
Y = [59876]

4. Write a prolog program to calculate the nth Fibonacci number.

 4. Write a prolog program to calculate the nth Fibonacci number. 


fib(0, 1) :- !.

fib(1, 1) :- !.

fib(N, F) :-        N > 1,        N1 is N-1,        N2 is N-2,        fib(N1, F1),        fib(N2, F2),        F is F1+F2.



OUTPUT:

fib(5N.
N = 8

3. Write a prolog program to calculate the factorial of a given number.

 3. Write a prolog program to calculate the factorial of a given number. 


fact(0,1).

fact(N,F):-(  N>0,  N1 is N-1, fact(N1,F1),  F is N*F1).



OUTPUT:

fact(5,N).

N = 120

Next101001,000 Stop


2. Write a prolog program to find the maximum of two numbers.

 2. Write a prolog program to find the maximum of two numbers.


max(X,Y):-(   X=Y ->write('both are equal') ;

 X>Y ->  (  Z is X,   write(Z), write('  is max')  )

  ;

  (  Z is Y,   write(Z),write(' is max')  ) ).


OUTPUT:

max(50,50).

both are equal

1true

max(50,5).

50is max

1true


1. Write a prolog program to calculate the sum of two numbers.

1. Write a prolog program to calculate the sum of two numbers. 

sum(X,Y):-S is X+Y,write(S).


OUTPUT:

Sunday, July 16, 2023

Container class


 

AWT Classes

 The AWT classes are contained in the java.awt package. It is one of Java’s largest packages. Fortunately, because it is logically organized in a top-down, hierarchical fashion.









Event Listener Interfaces

 Event Listener Interfaces

 event model has two parts: sources and listeners. 

Listeners are created by implementing one or more of the interfaces defined by the java.awt.event package. When an event occurs, the event source invokes the appropriate method defined by the listener and provides an event object as its argument. 




Sources of Events