Popular in Course
Popular in Computer Information Systems
This 36 page Class Notes was uploaded by Jacinto Simonis Jr. on Saturday October 3, 2015. The Class Notes belongs to CIS304 at California State Polytechnic University taught by ZhongmingMa in Fall. Since its upload, it has received 25 views. For similar materials see /class/218165/cis304-california-state-polytechnic-university in Computer Information Systems at California State Polytechnic University.
Reviews for IntermediateJavaProgrammingforBusiness
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 10/03/15
Java Review Chapter 2 Introduction to Java programming C18 304 Dr Zhongming Ma 32911 The basic operators for arithmetic expressions Operator Name Description Addition Adds two operands Subtraction Subtracts the right operand from the left Multiplication Multiplies the right and left operands Division Divides the right operand into the left If both are integers the result is an integer How to code arithmetic expressions 0 An arithmetic expression consists of one or more operands and arithmetic operators 0 When an expression mixes int and double variables Java automatically casts the int types to double types To keep the decimal places the variable that receives the result must be a double Statements that use simple arithmetic expressions integer arithmetic int x 14 int y 8 int resultl x y int result2 x y int result3 x y int result4 x y double arithmetic double a 85 double b 34 double result5 a b double result6 a b double result7 a b double result8 a b resultl result2 result3 result4 result5 result6 result7 result8 22 112 The syntax for declaring and initializing a string variable String variableName value How to declare and initialize a string String messagel String message2 String message3 String message4 quotInvalid data entryquot II II llll I null How to join strings String firstName quotBobquot firstName is Bob String lastName quotSmithquot lastName is Smith String name firstName quot quot lastName Bob Smith How tojoin a string and a number double price 1495 String priceString quotPrice quot price Price 1495 How to append one string to another with the operator firstName quotBobquot firstName is Bob lastName quotSmithquot lastName is Smith name firstName quot quot name is Bob followed by a space name name lastName name is Bob Smith How to append one string to another with the operator firstName quotBobquot firstName is Bob lastName quotSmithquot lastName is Smith name firstName quot quot name is Bob followed by a space name lastName name is Bob Smith Conunonescapesequences Sequence Character n New line t Tab r Return quot Quotation mark Backslash Desc p on 0 Within a string you can use escape sequences to include certain types of special characters Example 1 New line String Systemoutprintln Code JSPSnPrice 4950quot Result Code JSPS Price 4950 Using API do not reinvent wheels How to import Java classes The API for the JZSE provides a large library of classes that are organized into packages To use those classes you need an import statement To import one class om a package specify the package name followed by the class name To import all classes in a package specify the package name followed by an asterisk For example import javautil All classes stored in the javalang package are automatically available to all Java programs In other words you don t need to write this import import java lung The syntax of the import statement import packagenameClassName or import packagename Examples import javatextNumberFormat import javautilScanner import javautil import javaxswing How to use the Scanner class to create an object With an import statement Scanner so new ScannerSystemin Without an import statement the package name is required as a qualifier Scanner so new few m 1 l v 7 1r ll JT ScannerSystemin How to create an object from a class Syntax ClassName objectName new ClassNamearguments Examples creates a Scanner object named sc Scanner sc new ScannerSystemin How to call a method from an object Syntax obj ectName methodName arguments Examples double subtotal W w get a double entry from the console such as the command prompt The Scanner class javautilScanner How to create a Scanner object Scanner sc new ScannerSystemin Common methods of a Scanner object Method Description next Returns the next token stored in the scanner as a String object nextInt Returns the next token stored in the scanner as an int value nextDouble Returns the next token stored in the scanner as a double value create a Scanner object Scanner sc new ScannerSystemin read a string SystemoutprintquotEnter product code quot String productCode scnext read a double value SystemoutprintquotEnter price quot double price scnextDouble read an int value SystemoutprintquotEnter quantity quot int quantity scnextInt perform a calculation and display the result double total price quantity Systemoutprintln Systemoutprintlnquantity quot quot productCode quot quot price quot quot total Systemoutprintln nter price Enter quantity 2 product code cshp SB 2 cshp E 495 998 Press any key to continue Relational operators for comparison not for assignment Operator Name Equality Inequality Greater Than Less Than Greater Than Or Equal Less Than Or Equal Returns a true value if Both operands are equal The left and right operands are not equal The left operand is greater than the right operand The left operand is less than the right operand The left operand is greater than or equal to the right operand The left operand is less than or equal to the right operand Examples of conditional expressions discountPercent 23 equal to a numeric literal subtotal 0 not equal to a numeric literal years gt 0 greater than a numeric literal i lt m less than a variable greater than or equal to a numeric literal subtotal gt 500 less than or equal to a variable quantity lt reorderPoint Two methods of the String class Method equalsString Description Compares the value of the String object With a String argument and returns a true value if they are equal Comparison is case sensitive Works like the equals method but is not casesensitive equalsIgnoreCaseString Examples userEntryequalsquotYquot equal to a string literal userEntryequalsIgnoreCasequotYquot equal to a string literal lastNameequalsquotJonesquot not equal to a string literal equal to another string variable codeequalsIgnoreCaseproductCode The syntax of the ifelse statement if booleanExpression statements else if booleanExpression statements else statements How to code ifelse statements An z39felse statement or just if statement always contains an if clause It can also contain one or more else if clauses and a final else clause If a clause requires more than one statement you enclose the block of statements in braces Otherwise the braces are optional Any variables that are declared Within a block have black scope so they can only be used Within that block If statements without else if or else clauses With a single statement if subtotal gt 100 discountPercent 2 With a block of statements if subtotal gt 100 discountPercent 2 status quotBulk ratequot An if statement with an else clause if subtotal gt 100 discountPercent 2 else discountPercent 1 An if statement with else if and else clauses if customerTypeequalsquotTquot discountPercent 4 else if customerTypeequalsquotCquot discountPercent 2 else if subtotal gt 100 discountPercent 2 else discountPercent 1 What is the discountPercent when ocustomerType is C and subtotal is 200 osubtotal is 50 ocustomerType is A and subtotal is 200 A loop that continues while choice is y or Y String choice quotyquot while choiceequalsIgnoreCasequotYquot get the invoice subtotal from the user Scanner sc new ScannerSystemin SystemoutprintquotEnter subtotal quot double subtotal scnextDouble see if the user wants to continue SystemoutprintquotContinue yn quot what if you forget to reset the value for choice choice scnext Systemoutprintln 20 How to call a static method from a class Syntax ClassNamemethodNamearguments Examples convert a string to a double String sPrice quot105quot double price Nit now price is 10 e if elf wt ii V 5 j J 1 5 which is a num er double X 50 price right or wrong double y 50 sPrice right or wrong String 2 50 sPrice right or wrong static double EarseDnuhle String 3 Returns a newr double initialized to the value represented by the speci ed String as performed valueOf method of class Double 21 Java Review Chapter 1 How to get started with Java C18 304 Dr Zhongming Ma 32911 Download JDK 6 update 24 I http www oracle com technetworkjavajavasedownloadsindex html JDK java development kit This is essentially what you need when you develop java programs Install Java Run the exe file and respond to the resulting dialog boxes You download Java SE 6 Documentation from the save URL Download Eclipse such as 362 htt wwweci seor downloads 0 Eclipse one ofthe popular IDEs integrated development environments used in program development forJava 0 Install Eclipse Download the zip file and unzip it using a compression software such as WinRAR or WinZip If you an old version of Eclipse ver 31 installed you need to upgrade Compression software WinRAR ver 40 Find and download from httpdownloadcnetcom With WinRAR you can comparess a group of files into one file rar or zip format Downloadable files of the textbook 0 Make learning easier 0 httpwwwmurachcomdownloadsise6htm See Page 7 1 Java application CDncumenteTeaehing234 FBHSSignmentx 4nriginaljaua FutureUaluerp I 183 Enter monthly inueetment Enter yearly interest rate 3 Enter number of years Euture value 3714E Continue yn 4 l You learned how to develop such a Java application in CIS 234 1 Java GUI application Future Value 39Calau39latur Mentth Payment V Yearly Interest Rate l3 Number uf l39ears W Future u alue 3T14E Calculate Exit l You ll learn how to develop such a Java GUI application in CIS 304 2 Applet le Edlt Wew Favnrltes Tans He D aae v J E E 75 leearm memes Meda 9 m U am lWww merammmglaawnaeml The Future Value Calculator applet Mnmhw Payment mu 2 i e aulck omer 0 Internet You ll learn how to develop such a Java Applet in CIS 304 3 Servlet le an Wew Favnrmes Tans aaa v J E g 75 new mamas mm 9 31 mm www muram mmservletmuram w Fumrevalueservlet Help Mir U Publisher of Provessional Pragramming Books The Future Value Calculator Servlet Mummy Paymem mu Vezrly lnleresl Rate Nummym a qurevalue Calculate v 0 meme You ll learn how to develop such a Java Servlet in CIS 424 Classes bytecodes and JVMs You can use any text editor to create edit and save a source program j ava file The Java compiler translates Java source code into a platform independent format known as Java bytecodes Files that contain Java bytecodes have the class extension The Java interpreter executes Java bytecodes Java interpreters exist for all major operating systems A Java interpreter is an implementation of a Java virtual machine J VM A Java application consists of one or more classes i e class les 10 How Java compiles and interprets code Java virtual machine JVM Text editor eg textpad Java compiler Java interpreter notepad 7 0 Operating system bytecodes class files source code java les 11 The API documentation 0 The Application Programming Interface or API provides documentations for all the classes that are included as part of the J DK 0 You can View the API documentation from the Java web site or you can download and install it on your system How to navigate the API documentation You can use your web browser to browse through the API documentation on your own system if you ve downloaded and installed it or at the Java web site Related classes in the Java API are organized into packages which are listed in the upper left frame of the documentation page When you select a package all the classes for that package are listed in the lower left frame You can select a class from the lower left frame to display the documentation for that class in the right frame You can also select the name of the package at the top of the lower left frame to display information about the package and the classes it contains 13 In class exercise browsing the String class quotg Java 1 Platform Flle m we Faxantes 4 E g h T 5eam LNFavuntes Meaa V Mlcrosoft Internet Bplorer Tuuls Help x am an5 gt an m my much m mslmmenl Overvew Package Use Tree Degrecaled Index Help Viacuss Hzxvcuss FRAMES Hermes sulmmv N mm Dsz mm Jawm 2 Plathm Standard Eu 511 lavalang Class String 1a anaob r Lava1ang stung All Implemenled Inle aces Serlahzahle CharSeguence Cumgarablelt5mnggt mpmeaaa lmplemenled as mstances enms class UH quotabcquot are m Tlvea39lekmn smngs Because smng amen are lrnmulable We can be shared Fur example nquot T Scrmq at z abs g z 7 w l g Mycnmpmer Preparations at home not homework Download and install JDK 6 into your computer You download the Java SE 6 documentation and uncompressed the API documents under your local java directory such as cjava Then open your favorite Web browser and set the index file under the api directory eg cjavadocsapiindexhtml as one of your favorite links so that you can easily check the API documentation when you do coding Download and unzip Eclipse into your computer Download and install WinRAR in your computer 15
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'