Java Programming Language Introduction-Modern Programing Language-Lecture Handout, Exercises of Programming Languages

Modern Programing Language is about different languages of today era. It explains pros and cons of some new languages and their differences with old ones. Languages like java, c sharp, c plus plus, c, fotran are included in this course. This lecture handout is about: Expert, System, Prolog, Application, Domains, symptoms, Penicilline, Aggravates, Standardization

Typology: Exercises

2011/2012

Uploaded on 08/04/2012

dhanvin
dhanvin 🇮🇳

4.2

(14)

108 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PROLOG - PROgramming in LOGic(Lecture 22-26) VU
Expert system
One of the main application domains for Prolog has been the development of expert
systems. Following is an example of a simple medical expert system:
% clauses for relieves(Drug, Symptom). That is facts about drugs and relevant symptoms
relieves(aspirin, headache).
relieves(aspirin, moderate_pain).
relieves(aspirin, moderate_arthritis).
relieves(aspirin_codine_combination, severe_pain).
relieves(cough_cure, cough).
relieves(pain_gone, severe_pain).
relieves(anti_diarrhea, diarrhea).
relieves(de_congest, cough).
relieves(de_congest, nasal_congestion).
relieves(penicilline, pneumonia).
relieves(bis_cure, diarrhea).
relieves(bis_cure, nausea).
relieves(new_med, headache).
relieves(new_med, moderate_pain).
relieves(cong_plus, nasal_congestion).
% now we have clauses for side effects in the form of aggravates(Drug, Condition).
aggravates(aspirin, asthma).
aggravates(aspirin, peptic_ulcer).
aggravates(anti-diarrhea, fever).
aggravates(de_congest, high_blood_pressure).
aggravates(de_congest, heart_disease).
aggravates(de_congest, diabetes).
aggravates(de_congest, glaucoma).
aggravates(penicilline, asthma).
aggravates(de_congest, high_blood_pressure).
aggravates(bis_cure, diabetes).
aggravates(bis_cure, fever).
% now some rules for prescribing medicine using symptoms and side effects
should_take(Person, Drug) :-
complains_of(Person, Symptom),
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Java Programming Language Introduction-Modern Programing Language-Lecture Handout and more Exercises Programming Languages in PDF only on Docsity!

Expert system

One of the main application domains for Prolog has been the development of expert systems. Following is an example of a simple medical expert system:

% clauses for relieves(Drug, Symptom). That is facts about drugs and relevant symptoms

relieves(aspirin, headache). relieves(aspirin, moderate_pain). relieves(aspirin, moderate_arthritis). relieves(aspirin_codine_combination, severe_pain). relieves(cough_cure, cough). relieves(pain_gone, severe_pain). relieves(anti_diarrhea, diarrhea). relieves(de_congest, cough). relieves(de_congest, nasal_congestion). relieves(penicilline, pneumonia). relieves(bis_cure, diarrhea). relieves(bis_cure, nausea). relieves(new_med, headache). relieves(new_med, moderate_pain). relieves(cong_plus, nasal_congestion).

% now we have clauses for side effects in the form of aggravates(Drug, Condition).

aggravates(aspirin, asthma). aggravates(aspirin, peptic_ulcer). aggravates(anti-diarrhea, fever). aggravates(de_congest, high_blood_pressure). aggravates(de_congest, heart_disease). aggravates(de_congest, diabetes). aggravates(de_congest, glaucoma). aggravates(penicilline, asthma). aggravates(de_congest, high_blood_pressure). aggravates(bis_cure, diabetes). aggravates(bis_cure, fever).

% now some rules for prescribing medicine using symptoms and side effects should_take(Person, Drug) :- complains_of(Person, Symptom),

relieves(Drug, Symptom), not(unsuitable_for(Person, Drug)).

unsuitable_for(Person, Drug) :- aggravates(Drug, Condition), suffers_from(Person, Condition).

% we now have some specific facts about a patient named Ali complains_of(ali, headache). suffers_from(ali, peptic_ulcer).

% now the query about the proper medicine and the answer by the expert system ?- should_take(ali, Drug). Drug = new_med;

As mentioned earlier, Prolog has been used to write expert systems and expert system shells. Because of its inference mechanism and independence of rules and clauses it is relatively easy to achieve such task as compared to any imperative language.

Conclusions

Prolog is a declarative programming language where we only specify what we need rather than how to do it. It has a simple concise syntax with built-in tree formation and backtracking which generates readable code which is easy to maintain. It is relatively case and type insensitive and is suitable for problem solving / searching, expert systems / knowledge representation, language processing / parsing and NLP, and game playing. Efficiency is definitely is negative point. While writing programs one has to be aware of the left recursion, failing to do that may result in infinite loops. It has a steep learning curve and suffers from lack of standardization.

creates one ".class" file, where the file name is the same as the class name. When compiling a program, you type the full file name, including the ".java" extension; When running a program, you just type the name of the class whose main function you want to run. Java Types

Java has two "categories" of types: primitive types and reference types.

Primitive Types All the primitive types have specified sizes that are machine independent for portability. This includes:

boolean same as bool in C++ char holds one 16 bit unicode character byte 8-bit signed integer short 16-bit signed integer int 32-bit signed integer long 64-bit signed integer float floating-point number double double precision floating-point number

Reference Types arrays classes

There are no struct, union, enum, unsigned, typedef, or pointers types in Java.

C++ Arrays vs Java Arrays

In C++, when you declare an array, storage for the array is allocated. In Java, when you declare an array, you are really only declaring a pointer to an array; storage for the array itself is not allocated until you use "new". This difference is elaborated as shown below:

C++ int A[10]; // A is an array of length 10 A[0] = 5; // set the 1st element of array A

JAVA int [ ] A; // A is a reference to an array A = new int [10]; // now A points to an array of length 10 A[0] = 5; // set the 1st element of the array pointed to by A

In both C++ and Java you can initialize an array using values in curly braces. Here's example Java code:

int [ ] myArray = {13, 12, 11}; // myArray points to an array of length 3 // containing the values 13, 12, and 11 In Java, a default initial value is assigned to each element of a newly allocated array if no initial value is specified. The default value depends on the type of the array element as shown below:

Type Value boolean false char '\u0000' byte, int, short, long, float, double 0 any reference null

In Java, array bounds are checked and an out-of-bounds array index always causes a runtime error.

In Java, you can also determine the current length of an array (at runtime) using ".length" operator as shown below:

int [ ] A = new int[10]; ... A.length ... // this expression evaluates to 10 A = new int[20]; ... A.length ... // now it evaluates to 20

In Java, you can copy an array using the arraycopy function. Like the output function println , arraycopy is provided in java.lang.System, so you must use the name System.arraycopy. The function has five parameters:

src : the source array (the array from which to copy) srcPos : the starting position in the source array dst : the destination array (the array into which to copy) dstPos : the starting position in the destination array count : how many values to copy

Here is an example:

int [ ] A, B; A = new int[10];

// code to put values into A B = new int[5]; System.arraycopy(A, 0, B, 0, 5) // copies first 5 values from A to B System.arraycopy(A, 9, B, 4, 1) // copies last value from A into last // element of B

Note that the destination array must already exist (i.e., new must already have been used to allocate space for that array), and it must be large enough to hold all copied values (otherwise you get a runtime error). Furthermore, the source array must have enough values to copy (i.e., the length of the source array must be at least srcPos+count). Also, for arrays of primitive types, the types of the source and destination arrays must be the same. For arrays of non-primitive types, System.arraycopy(A, j, B, k, n) is OK if the assignment B[0] = A[0] would be OK.

The arraycopy function also works when the source and destination arrays are the same array; so for example, you can use it to "shift" the values in an array:

// has been called

m = new MyClass(); // now storage for an object of MyClass has been allocated // and the constructor function has been called. Note // that you must use parentheses even when you are not // passing any arguments to the constructor function

// Also note that there is a simple „.‟ (dot) operator used to // access members or send message. Java does not use // -> operator.

Whereas, as opposed to Java, in C++ use have the following:

MyClass m; // m is an object of type MyClass; // the constructor function is called to initialize M.

MyClass *pm; // pm is a pointer to an object of type MyClass // no object exists yet, no constructor function has // been called

pm = new MyClass; // now storage for an object of MyClass has been // allocated and the constructor function has been // called

Aliasing Problems in Java

The fact that arrays and classes are really pointers in Java can lead to some problems. Here is a simple assignment that causes aliasing :

int [] A = new int [4]; Int [] B = new int [2];

This is depicted as below:

Now, when we say: A[0] = 5;