CSE 110 - 2nd set of Notes
CSE 110 - 2nd set of Notes CSE 110
Popular in Prin of Programming with Java Lecture
Popular in ComputerScienence
This 5 page Class Notes was uploaded by RianMartins on Thursday October 15, 2015. The Class Notes belongs to CSE 110 at Arizona State University taught by Justin Selgrad in Fall 2015. Since its upload, it has received 94 views. For similar materials see Prin of Programming with Java Lecture in ComputerScienence at Arizona State University.
Reviews for CSE 110 - 2nd set of Notes
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 10/15/15
CSE 110 PRINCIPLES OF PROGRAMMING IN JAVA WEEKLY NOTES 2 Constants and Comments Constant use the reserved keyword final final lttypegt ltvaiabenamegt ltvauegt final double CANVOLUME O 3555 Comment line comment use The following text are ignored block comment use All lines between the and are ignored Common Errors Undeclared Variables You must declare a variable before you use it ie above in the code double canVolume 12 literPerOunce quot6 Wrong double literPerOunce 00296 Uninitialized Variables You must initialize ie set a variable s contents before you use it int bottles int bottleVolume bottles 2 Wrong InputOutput Step 1 Add the following line at the beginning to import the Scanner class library import javautilScanner Step 2 Create a variable of Scanner type in the main block Scanner in new Scanner Systemin Step 3 Get the value from the typed keys The nextlnt reads the integer input the nextDouble read the double real number and the next reads the String input int aaa innextInt double bbb innextDouble String 000 innext Arithmetic Operations Addition subtraction multiplication division I and remaindermodulus are arithmetic operations in Java The parenthesis is used to specify the order of calculations x y 5 and x y 5 return differentanswers Table 5 llntsgsi Division an Remainder lEii TESEl l i Wham n 1 1 Mg 1s Tallue C xmment n1 is ill 9 H s 141 is always the LIEit di it sin n E 1i HE This is eilwsys m tsquoti1 i ti1 it digit m ii 1le 2 9 Tl uzlssttssrs digits mi N n 339 ii i 1 E s l 3J3 is s Hosting pm int stumbles tl isimsti nsl part is notdisssrdsd n1 is l s Bsssussrzhst irsisrgumsm isnsgstiss His rsmsi dsr isslss Iisgsiisvs i1 2 1 n s 2 iin is sssn I sir 1 if n is tidlsii In Java a modulus operator is used to calculate the remainder of division eg 152 9 1 15 divide by 2 is 7 and the remainder is 1 Examples 84O 752 575 532 3842 9 639 1094 12O 10912415 10912421 1091246 CastingConverting Any arithmetic operation with int integer type variables return an integer value eg int a 100 int b 24 Systemoutprinta b94 You can convert the floatingpoint numbers to integers lttypegt ltvariabenamegt int ltdoubevariabegt eg double balance 12098 int dollars int balance Now the dollars has a value of 120 instead of 12098 Mathematical Methods Basics The mathematic methodsfunctions are defined in Math class which is under the javalang package Basic use is lttypegt ltvariabegt Math ltmethodgt ltinputsgt eg double squareRoot4 Mathsqrt4 double fouquuared Mathpow4 2 Taile Mathematical Methods Methnd Hhth5qrtfr Hathpuwri y Mathisintn Math we fit Hathitan r Hathtn adinnsi i Hathntn egrEEEix Mathenpir Enthiugit Hathqiegl ix Hathuroumdixj Mathiabeixj Hath manta y Hathnminfx F Returns Square innit111 CI r3 w ii nr U and J in J nr I r and j is an i nteg i Sine clef it in i eidint isi Engine til 1 Tangentnfx Convert degrees in radians inn returns if mquot 13H Clamerr 3 radans to degrees ital returns if 1 l i iin x E39 Natural log inle A T Ell Decimal log ilogmirj j Bil El i ii t integer til I as a long b lnlute value The larger ref 3 and y ETHESHHUETQFI de String is an object data type to store a set of characters Strings Data Type Declaration you can declare the variable of String type in the same way of primitives String vname Declaration and Initialization Need a new memory space for object with a keyword new such as String mynamel String myname2 String myname3 Concatenation empty value John regular value John We use this way but this is special only for String new String new String You can concatenate multiple String objects using operation eg String myname John Jones Methods The length charAtint substringint int are the methods of String length returns the length of characters It is used by calling the method to the objectvariable as String name Harry int num namelengthnumis charAtint returns a character primitive of char at the input position The position index starts from 0 String name char start Harryll namecharAtO a ti char last namecharAt4kmty substringint int returns a new String object with characters from the first input position to the second input position String greeting Hello String sub Worldl greetingsubstring0 5a Hs Hde