Total Pageviews

Monday, July 17, 2023

11. Write a Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List.

 11. Write a Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List.


max([H|T],R):-

 length(T,L),

 L>0 -> (  max(T,R1),  (   H > R1 ->     R is H    ;     R is R1  ) ) ; R is H.



OUTPUT:

max([6,9,7],X).

X = 9

max([6,9,70],X).

X = 70


10. Write a Prolog program to implement max(X,Y,Max) so that Max is the greater of two numbers X and Y.

 10. Write a Prolog program to implement max(X,Y,Max) so that Max is the greater of two numbers X and Y.


max(X,Y,R):-

 X>=Y ->

  R is X,

  write(R)

 ;

  R is Y,

  write(R).

 

OUTPUT:

max(2,6,X).

6

X = 6



12. Write a Prolog program to implement sumlist(List,Sum) so that Sum is the sum of a given list of numbers List.

  12. Write a Prolog program to implement sumlist(List,Sum) so that Sum is the sum of a given list of numbers List. 


sumlist([],0).

 

sumlist([H|T],R):-

sumlist(T,R1),

  R is H+R1.

OUTPUT:

sumlist([6,9,70],X).

X = 85


14. Write a Prolog program to implement reverse(List,ReversedList) that reverses lists.

 14. Write a Prolog program to implement reverse(List,ReversedList) that reverses lists. 


reverse([],[]).

reverse([X|T],REV):-reverse(T,R),append(R,[X],REV).


OUTPUT:


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

X = [2, 2, 1]


13. Write a Prolog program to implement two predicates evenlength(List) and oddlength(List) so that they are true if their argument is a list of even or odd length respectively.

 13. Write a Prolog program to implement two predicates evenlength(List) and oddlength(List) so that they are true if their argument is a list of even or odd length respectively. 

evenlength:-

write('true --> even').

oddlength:-

write('true --> odd').

 

oddeven([_|T]):- length(T,L), L>=0 -> (  L1 is L+1,  L2 is mod(L1,2),  L2=:=0 ->evenlength  ;   oddlength ).




OUTPUT:

 

oddeven([5,8,9]).

true --> odd

1true

oddeven([5,8,6,9]).

true --> even

1true

 



8. Write a Prolog program to implement append for two lists.

  8. Write a Prolog program to implement append for two lists.

app([],L,L).

app([X|M],N,[X|Q]):-

 app(M,N,Q).



OUTPUT:

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

X = [1, 2, 3, 4]

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

X = [1, 2]

 


7. Write a Prolog program, remove-nth(Before, After) that asserts the After list is the Before list with the removal of every n‘th item from every list at all levels.

 7. Write a Prolog program, remove-nth(Before, After) that asserts the After list is the Before list with the removal of every n‘th item from every list at all levels.


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

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

 P1 is P-1,

 del(P1,Y,R).



dalpos(P,L,R):-

 P1 is P-1,

 del(P1,L,R1),

 del(P,R1,R).


OUTPUT:


del(2,[10,20,30,40],X).

X = [10, 30, 40]

Next101001,000 Stop


9. Write a Prolog program to implement palindrome (List).

 9. Write a Prolog program to implement palindrome (List). 


pal([]):- write('palindrome').

pal([_]):- write('palindrome').

pal(L) :-

 append([H|T], [H], L),

 pal(T) ;write('Not a palindrome').


OUTPUT:

pal([1,2,1]).

palindrome

1true

Next101001,000 Stop

pal([1,2,2]).

Not a palindrome

1true


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


 

The TextEvent Class

 

The TextEvent Class Instances of this class describe text events. These are generated by text fields and text areas when characters are entered by a user or program. TextEvent defines the integer constant TEXT_VALUE_CHANGED. 

The one constructor for this class is shown here: TextEvent(Object src, int type) Here, src is a reference to the object that generated this event. The type of the event is specified by type.

The MouseEvent Class

 




Here, src is a reference to the component that generated the event.  The system time at which the mouse event occurred is passed in when. 

The modifiers argument indicates which modifiers were pressed when a mouse event occurred. The coordinates of the mouse are passed in x and y. The click count is passed in clicks. 

The triggersPopup flag indicates if this event causes a pop-up menu to appear on this platform.

 Two commonly used methods in this class are getX( ) and getY( ). 

These return the X and Y coordinates of the mouse within the component when the event occurred. 


Point getPoint( ) It returns a Point object that contains the X,Y coordinates in its integer members: x and y. 

The translatePoint( ) method changes the location of the event. Its form is shown here: void translatePoint(int x, int y) Here, the arguments x and y are added to the coordinates of the event. 

The getClickCount( ) method obtains the number of mouse clicks for this event. Its signature is shown here: int getClickCount( ) 

The isPopupTrigger( ) method tests if this event causes a pop-up menu to appear on this platform. Its form is shown here:  boolean isPopupTrigger( ) 

 int getButton( ) It returns a value that represents the button that caused the event. The return value will be one of these constants defined by MouseEvent: 

NOBUTTON 

BUTTON1

 BUTTON2 

BUTTON3 

The NOBUTTON value indicates that no button was pressed or released. Java SE 6 added three methods to MouseEvent that obtain the coordinates of the mouse relative to the screen rather than the component. 

They are shown here: 

Point getLocationOnScreen( ) 

int getXOnScreen( )

 int getYOnScreen( ) 

The getLocationOnScreen( ) method returns a Point object that contains both the X and Y coordinate. The other two methods return the indicated coordinate