Professional Documents
Culture Documents
Assignment b42
Assignment b42
A"A #rogr$mming Subject Co%e BSIT & '( Assignment TA (Compu)sory) 1) Explain basic features of Java. 2) Explain how Java differs from C and C++. 3) What is the difference between ob ect oriented and procedure oriented pro!rammin! " #) Explain the followin! $$%s concept. a) ob ect and classes. b) &bstraction. c) Encapsulation. d) 'nheritance. e) %ol(morphism. f) )essa!e passin!. *) What are the advanta!es of ob ect oriented pro!rammin! " +) Explain the structure of a Java pro!ram. ,) With an example explain Java to-ens. .) With an example explain different data t(pes in Java. /) With an example explain all the operators in Java. 10) With an example explain the followin! statements in Java. a) simple if b) if..else c) nested if d) else..if ladder 11) With an example explain the switch statement in Java. 12) Compare while and do..while statements in Java. 13) With an example explain for statement in Java. 1#) With the help of an example pro!ram explain brea- and continue statements in Java.
Assignment TB (Compu)sory)
Java is a platform independent language.After compiling the ".java" file ,that will be converting into the ".class" file,which is a byte code having the capability run on any OS.Basing on the concept byte code java achieving the platform independent,it leads to " rite onece run anyware".
2. List any three features of JAVA.
The Java Virtual Machine - Garbage Collection - Code Security Java Features: Java Virtual Machine (JVM) - an imaginary machine that is implemented by emulating so t!are on a real machine - provides the hard!are plat orm speci ications to !hich you compile all Java technology code Bytecode - a special machine language that can be understood by the Java Virtual Machine "JVM# - independent o any particular computer hard!are$ so any computer !ith a Java interpreter can e%ecute the compiled Java program$ no matter !hat type o computer the program !as compiled on& Garbage collection thread - responsible or reeing any memory that can be reed& This happens automatically during the li etime o the Java program& - programmer is reed rom the burden o having to deallocate that memory themselves Code security is attained in Java through the implementation o its Java 'untime (nvironment "J'(#&
)avac -options. -source iles. Javadoc is a documentation generator rom Sun Microsystems or generating ,/0 documentation in 1TM2 ormat rom Java source code& (Java DataBase Connectivity) A programming interface that lets Java applications access a database via the SQL language. Since Java interpreters (Java Virtual Machines) are available for all ma or client platforms! this allo"s a platform# independent database application to be "ritten
&.
o o o o o
@&
0n this case it only bootstraps the logger and uses the constuctor method o GC0"the graphical user inter ace o the program#
PART - B 1. a2 What is inheritance and how one can achieve code reusa"ility? 34plain with an e4ample.
0nheritance in Java re ers to the eature !herein the codeE unctionality o one class can be used in another class that e%tends this class& (%ample: public class /arent A &&& && D
public class Child e%tends /arent A &&& && & D 1ere the class Child e%tends the class /arent and hence the methods o /arent are available or Child to use& This !ay !e are re-using the code in the parent class in the child class instead o re-!riting the !hole thing again&
"2 What is a class? How does it accomplish data hidin*? 34plain with e4ample.
, class is a collection o data and methods to !or* !ith that$ and other$ data& ,n ob)ect is a speci ic instance o a class& For e%ample$ F,rray2istF is a class that contains methods such as FaddF and FremoveF or changing the items in the list& The instances "ob)ects# o ,rray2ist can use these methods to per orm operations& 0t+s easier to sho! the structure o things !ith a custom class& public class MyClass A EEGata E FFieldsF: private int numB EEConstructors: public MyClass"# A D public MyClass"int initial9umValue# A num ; initial9umValueB D EEFGettersF and FSettersF: public int get9um"# A return numB D public void set9um"int ne!9umValue# A num ; ne!9umValueB D D
2. a2 ompare in terms of their functions and semantics the followin* pairs of statements5 i2 do while and while
7hile : 0n 7hile loop the condition is tested irst and then the statements are e%ecuted i the condition turns out to be true&
Go-7hile : 0n do !hile the statements are e%ecuted or the irst time and then the conditions are tested$ i the condition turns out to be true then the statements are e%ecuted again&
ii2 while and for iii2 nested if and switch iv2 "rea( and continue. "2 Write a pro*ram to find sum of the followin* harmonic series for a *iven value of n 16 172 6 173 6 . . . .6 17n.
There is no correct simple general ormula or sum to n terms o the series 3H3E4H3E5H3E>H &&&&&&&&&&&&& H 3En The ollo!ing e%pression is relatively a very good appro%imation& S ; ln"n H I&J# H I&JKK4 H I&I5KJLE"nMn H 3&3K3# Geviation rom the actual value luctuates but remains relatively lo!&
boolean: The boolean data type has only t!o possible values: true and false& Cse this data type or simple lags that trac* trueE alse conditions& This data type represents one bit o in ormation$ but its Fsi6eF isn+t something that+s precisely de ined& char: The char data type is a single 3@-bit Cnicode character& 0t has a minimum value o "#u$$$$" "or I# and a ma%imum value o "#uffff" "or @J$J5J inclusive#&
#. a2 What is JV9 ? How does it help to achieve platform independence? :f JV9 is availa"le for windows then can we run pro*ram written on ;ni4 platform ? omment.
, Java Virtual Machine "JVM# is a set o computer so t!are programs that use a virtual machine model or the e%ecution o Java computer programs and scripts& The JVM accepts data in a orm commonly re erred to as Java bytecode& This language conceptually represents the instruction set o a stac*-oriented$ capability architecture&
)avac - it is compiler !hich converst )ava source code to byte code ie&class ile& This byte code is standard or all plat orms$ machines or operating systems& )ava - this is interpreter& this interprets the &class ile based on a particular plat orm and e%cutes them&e )vm -O )ava virtual machine comes into play& )vm or !indo!s !ill be di erent rom )vm or solarais or linu% & but all the )vm ta*e the same byte code and e%ecutes them in that plat orm& Source code -O )avac -OCniversal byte code Cniversal byte -O)vmE)ava -O e%ecute them on a particular machine& There can be any number o )vm+s but they all understand the one common language called byte code and translates them into the binary e%ecutable o a particular plat orm or !hich they are developed& There is separate development section in sun microsystems is to develop the )vm or a particular plat orm& or each upgrade o the )d* $ the )vm or each plat orm is updated&
"2 How are data and method or*ani<ed in an o"=ect oriented pro*ram ? :llustrate the same for car o"=ect.
There are 4 !ays: Fprivate or protectedF and FpublicF& , programmer has the choice o ma*ing the ob)ect+s data either protected or public& /rotected means the data associated !ith that ob)ect can be only accessed by the ob)ect+s o!n methods& 0t can+t be globally accessed by simply calling the variable& /ublic means it can be accessed rom any unction& (&g: Class /erson A /rotected: Char nameB Char hair-colourB change haircolour change name The above class can have an ob)ect o person !ith his name and hair-colour& ,s you can see it is protected and no one else can change them e%cept your o!n unctions o change hair-colour and change name& That+s ho! data and methods are organi6ed& 0t is a security eature$ called data encapsulation&
&. a2 8istin*uish "etween the followin* terms5 i2 8ynamic "indin* and messa*e passin*. ii2 :nheritance and polymorphism. "2 What are the difference "etween and Java ?How Java and 66 are similar?
Java is an pure ob)ect oriented programming language$ it uses the concepts o Classes$ ?b)ects$ 0nheritance$ /olymorphism& ,nd the e%ecution o a program is non-linear& 0t is so called because you can+t !rite a program !ith out using classes P ob)ects& C uses concept o structures "not ob)ect oriented#& 0n C !e use the concept o pointers !hereas there are no pointers used in J,V, 0n C the programmer needs to manage memory manually& Fmalloc"#F and F ree"#F are the undamental memory allocation library calls& Java !as created or the purpose o ma*ing a language that could be implemented on many di erent types o computers "cell phone$ mac$ /C$ linu%$ etc&# C on the other hand can only be run on a computer o the same type as the one that compiled the program& Java is also ob)ect-oriented$ !hereas C is not& Java allo!s a user to create classes that contain data and methods& C is not capable o doing that&
& progr$mming )$ngu$ge is an artificial lan!ua!e desi!ned to express computations that can be performed b( a machine: particularl( a computer. %ro!rammin! lan!ua!es can be used to create pro!rams that control the behavior of a machine: to express al!orithms precisel(: or as a mode of human communication.
he bitwise operators
P - and < - or Q - Ror S - not :: - le t shi t OO - right shi t OOO - right shi t (%amples: 5 P J ; 3 "3 i both bits are 3&# 5 < J ; K "3 i either bits are 3# 5QJ ; @ "3 i both bits are di erent# S5 ; -> "0nverts the bits# 5 :: 4 ; 34 "Shi ts the bits o n le t p positions& Tero bits are shi ted into the lo!-order positions&# J OO 4 ; 3 "Shi ts the bits o n right p positions& 0 n is a 4+s complement signed number$ the sign bit is shi ted into the high-order positions&# -> OOO 4N ; 3J "Shi ts the bits o n right p positions& Teros are shi ted into the high-order positions&#
"2 What is polymorphism? 34plain method overridin* with e4ample and prove that it is a polymorphism.
polymorphism means the ability to ta*e more than one orm& This is something similar to a !ord having several di erent meanings depending on the conte%t& /olymorphism in Java is implemented using Method ?verloading and Method ?verriding& ?verloading - More than one method in the same class !ith the same name but di erent signature ?verriding - Method !ith the same name in child class mas*ingEhiding the method in the parent class