Subject: Programming in Java (Theory Cum Tutorial) Subject Code: IT0206
Subject: Programming in Java (Theory Cum Tutorial) Subject Code: IT0206
%& ectation at end o' (ee$ !: Capable of understanding and installing JDK and
NetBeans IDE. Capable of understanding, writing and executing a basic java progra object orientation s"ills.
)evel": Basic #No extra s"ills needed$, Inter ediate% Extra boo" reference or networ"ing
#online&offline$ needed.
In"truction to "tudent": 'tudents can bring t!eir own laptops but a( be advised
strictl( to continue till t!e end of t!e course wit!out an( excuses. Bot! for t!eor( and tutorial classes. 'o t!e students can execute t!e codes we teac! at t!e end of t!e class # a(be we will provide )* in to access laptops. If (ou all agree.$
*ote: The order o' coverage could "uit the 'aculty "tyle++++
). Introduction to +bject +riented ,rogra ing. -. .ow is it different fro ,rocedural languages/ 0. ,roperties of ++, #1bstraction, Encapsulation, In!eritance, ,ol( orp!is $. 2evel% Basic 3. ,roperties of Java #Intro to all Bu44 words$. Installation re5uire ents #JDK, J6E, J78$. *. 9!at is a standalone application and an 1pplet wit! real exa ple insig!t/ 2evel% Basic :. Basic s"eleton of Java ,rogra wit! introduction to classes and et!ods. 9it! explanation of w!ole s"eleton wit! basic intro to arra( and co and line argu ents #if needed$. ;. 6ules of na ing Classes, et!ods, variables, constants#<inal$ in java, .ow to na e a <ile in java/#2i"e t!e na e of t!e public class is t!e na e of t!e file and also t!ere can be onl( one public class and a basic level of intro to w!at is public$. =. Introduction to Co pile ti e and 6unti e. 'econd e p!asis on B(te code and its relevance to J78 and co parison wit! .exe of ot!er languages. .ow to co pile and run #in C8D pro pt and IDE #wit! introduction to NetBeans IDE$$. =. 9!at is 'tandard Input, 'tandard +utput and 'tandard Error/ ;. 9!at are t!e basic wa(s of giving inputs #Co and line argu ents, and 'canner$. Note% 'canner just to introduce !ow to use.
Tutorial,(ee$!:
Program !: 1ppl( an +bject oriented paradig #.int% Classes$ using java to develop a
stand>alone application wit!out a et!od to print ?I a enjo(ing ( progra in J171 and I swear I will put all ( sincere efforts to grow eac! da( tec!nicall(? 2evel% Basic
Code%
Code: import !ava.util."; public class #ead$Print { public static void main(String[] args) { Scanner s % ne& Scanner(System.in); System.out.println("Please enter your input ' "); String l % s.ne(t)ine(); System.out.println(l);
Code: import !ava.util.Scanner; public class #ead { String #ead() { Scanner s % ne& Scanner(System.in); System.out.println("Please enter your input ' "); String l % s.ne(t)ine(); return l;
public class Print { public static void main(String[] a) { #ead r* % ne& #ead(); System.out.println(r*.#ead());
Code: import !ava(.s&ing.+,ptionPane; -"" " .umber/onversion is used to convert numbers 0rom one 0ormat to anot1er 2 to veri0y &1et1er t1e entered number is o0 a speci0ic type. " 3t 1as t1e met1ods number/onverter 2 number4eri0ier. " 5aut1or 6nag1 7umar 8aran&al$*9:*;*9**; " 5version *.< " 5since *.< "public class .umber/onversion { -"" " number/onverter is used to convert a number 0rom a given 0ormat to anot1er 0ormat speci0ied by t1e user. 3t 1as t1ree String arguments " &1ose uses are speci0ied under Parameters. " =(ample o0 >sage' " ?/,@=Anumber/onverter(BBCHe(a@ecimalC,ctal)?-/,@=A " converts BB &1ic1 is a He(adecimal number to D<< &1ic1 is an octal number 2 t1en prints t1e output. " 8e0ore conversion it 0irst c1ecEs &1et1er t1e input type 2 output type are same and i0 true t1en prints t1e same number " in a ?codeAW6#.3.F$G=SS6F=?-codeA dialog bo(. " 5param number ' String asEed 0rom user &1ic1 is to be converted to ot1er types. " 5param b ' Hype o0 t1e number entered. /an be "8inary"C ",ctal"C "@ecimal" 2 "He(a@ecimal". " 5param c ' Hype o0 t1e number &1ic1 is to be obtained as output. /an be "8inary"C
",ctal"C "@ecimal" 2 "He(a@ecimal". " 5see String " 5see 3nteger.parse3nt " 5see 3nteger.to,ctalString " 5see 3nteger.toHe(String " 5see 3nteger.to8inaryString " 5see +,ptionPane.s1o&Gessage@ialog " 5e(ception .umberBormat=(ception "public void number/onverter(String numberCString bC String c) { int decimal % 9; --H1e variable decimal is initialised 2 set to 9. String octal % nullC 1e(adecimal % nullC binary % null; --H1e variables octalC 1e(adecimal 2 binary are initialised 2 set to null. s&itc1(b) { case "8inary" ' decimal % 3nteger.parse3nt(numberC ;); octal % 3nteger.to,ctalString(decimal); 1e(adecimal % 3nteger.toHe(String(decimal); --/onverts into decimalC octal 2 1e(adecimal 0ormats using 3ntegerCparse3nt breaE; --2 3nteger.to,ctal(or He( or 8inary) String. case ",ctal" ' decimal % 3nteger.parse3nt(numberC :); binary % 3nteger.to8inaryString(decimal); 1e(adecimal % 3nteger.toHe(String(decimal); breaE; case "@ecimal" ' decimal % 3nteger.parse3nt(numberC *9); binary % 3nteger.to8inaryString(decimal); 1e(adecimal % 3nteger.toHe(String(decimal); octal % 3nteger.to,ctalString(decimal); breaE; case "He(a@ecimal" ' decimal % 3nteger.parse3nt(numberC *I); binary % 3nteger.to8inaryString(decimal); octal % 3nteger.to,ctalString(decimal); breaE; i0(b.eJuals(c)) { +,ptionPane.s1o&Gessage@ialog(nullC"/onverted .umber is "K number K"LnLnH1e
,utput type &as t1e " K "same as t1e 3nput type."C"Same 3nput 2 ,utput Hypes"C+,ptionPane.W6#.3.F$G=SS6F= ); else { s&itc1(c) { case "8inary" ' +,ptionPane.s1o&Gessage@ialog(nullC"H1e number "K number K" 1as been converted 0rom "K b K " to "K c K" and t1e resulting number is ' "K binary); breaE; case ",ctal" ' +,ptionPane.s1o&Gessage@ialog(nullC"H1e number "K number K" 1as been converted 0rom "K b K " to "K c K" and t1e resulting number is ' "K octal); breaE; case "@ecimal" ' +,ptionPane.s1o&Gessage@ialog(nullC"H1e number "K number K" 1as been converted 0rom "K b K " to "K c K" and t1e resulting number is ' "K decimal); breaE; case "He(a@ecimal" ' +,ptionPane.s1o&Gessage@ialog(nullC"H1e number "K number K" 1as been converted 0rom "K b K " to "K c K" and t1e resulting number is ' "K 1e(adecimal); breaE; --Prints t1e result.
-"" "number4eri0ier veri0ies t1e number entered &it1 t1e input type selected and returns a boolean value depending on t1e result. " >ses tryMcatc1 blocEs to identi0y number 2 type mismatc1. " 5param d ' .umber entered by t1e user. " 5param e ' 3nput type selected by t1e user. " 5see =(ception " 5see 3nteger.parse3nt " 5return 6 boolean value depending upon t1e result. "-
public boolean number4eri0ier(String dC String e) { boolean ret % 0alse; s&itc1(e) { case "8inary" ' { try { int t % 3nteger.parse3nt(dC ;); -- try to parse t1e string to an integerC using ; as radi( ret % true; -- parsing succeededC string is valid binary number catc1 (.umberBormat=(ception 0) { ret % 0alse; breaE; case ",ctal" ' { try { int t % 3nteger.parse3nt(dC :); -- try to parse t1e string to an integerC using : as radi( ret % true; -- parsing succeededC string is valid octal number catc1 (.umberBormat=(ception 0) { ret % 0alse; breaE; case "@ecimal" ' { try { int t % 3nteger.parse3nt(dC *9); -- try to parse t1e string to an integerC using *9 as radi( ret % true; -- parsing succeededC string is valid decimal number catc1 (.umberBormat=(ception 0) { ret % 0alse;
breaE; case "He(a@ecimal" ' { try { int t % 3nteger.parse3nt(dC *I); -- try to parse t1e string to an integerC using *I as radi( ret % true; -- parsing succeededC string is valid 1e( number catc1 (.umberBormat=(ception 0) { ret % 0alse; breaE; return (ret); -"" " H1is is t1e main met1od &1ic1 is used to instantiate t1e .umber/onversion class as &ell as asE t1e user 0or 1is input to pass " it onto t1e met1od reJuired i.e. number/onverter 2 number4eri0ier " depending upon t1e 0unction reJuired o0 t1e class as &ell as to s1o& output 2 e(it t1e program. " 3t veri0ies &1et1er t1e number 2 input$type matc1 and only t1en proceeds &it1 t1e rest o0 t1e program. " 5param a ' ?/odeAString?-codeA generally used 0or commandMline arguments. no 0unction 1ere. " 5see +,ptionPane " 5see ,b!ect " 5see String " 5see System " 5e(ception .umberBormat=(ception "public static void main(String[] a) { ,b!ect[] possibilities % {"8inary"C ",ctal"C "@ecimal"C "He(a@ecimal" ; String type % (String)+,ptionPane.s1o&3nput@ialog(nullC"Select t1e .umber 3nput type ' "C"3nput Hype @ialog"C+,ptionPane.N>=SH3,.$G=SS6F=CnullCpossibilitiesC"@ecimal"); String no % +,ptionPane.s1o&3nput@ialog(nullC"Please =nter t1e .umber ' "C"3nput
@ialog"C+,ptionPane.3.B,#G6H3,.$G=SS6F=); .umber/onversion n* % ne& .umber/onversion(); boolean r % n*.number4eri0ier(noC type); i0(r %% true) { String ne(t$type % (String)+,ptionPane.s1o&3nput@ialog(nullC"Select t1e .umber ,utput type ' "C",utput Hype @ialog"C+,ptionPane.N>=SH3,.$G=SS6F=CnullCpossibilitiesC"@ecimal"); n*.number/onverter(noC typeC ne(t$type); System.e(it(9); else i0(r %% 0alse) { +,ptionPane.s1o&Gessage@ialog(nullC"H1e number you entered does not matc1 t1e entered type. Please run t1e program again."C ".umber 2 Hype Gismatc1"C+,ptionPane.=##,#$G=SS6F=); System.e(it(*);