![Скачать презентацию Java basics Chapter 2 CS 101 -E 1 Скачать презентацию Java basics Chapter 2 CS 101 -E 1](https://present5.com/wp-content/plugins/kama-clic-counter/icons/ppt.jpg)
8ffc412f50520f07faf81b28a8102ccf.ppt
- Количество слайдов: 58
Java basics Chapter 2 CS 101 -E 1
Display. Forecast. java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class Display. Forecast { Three comments // method main(): application entry point public static void main(String[] args) { System. out. print("I think there is a world market for"); System. out. println(" maybe five computers. "); System. out. println(" Thomas Watson, IBM, 1943. "); } } Java allows a named must be An name We applicationreadobjectto is – comment method public, static, and voidpeople a laterto have a Anindicates aa statement up keywords. sure they A method are method and void madethat ofcan are Three statements make isform. codeobjectperforms // class definesprogramlinerequired up Programs is reststatic piece the action They will like discuss an the are have a of by of make of have multiple lines cannot be used as attributes public action orand names named main(). some methodsofimplements a behavior main()static voidtext readable. method Comments are used to document authors, purpose, Semicolons delimit one class Display. Forecast 2 aid public means elementsofa is shareable Method main() indicates statement from the next and program is method class definition follows Use whitespace, part Keyword classthecomments, and indentation to understanding
Indentation // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class Display. Forecast { Method main() is part of Display. Forecast // method main(): application entry point public static void main(String[] args) { System. out. print("I think there is a world market for"); System. out. println(" maybe five computers. "); System. out. println(" Thomas Watson, IBM, 1943. "); } } Indentation indicates subcomponents Statements are part of method main() 3
Good whitespacing // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class Display. Forecast { Whitespace // method main(): application entry point public static void main(String[] args) { System. out. print("I think there is a world market for"); System. out. println(" maybe five computers. "); System. out. println(" Thomas Watson, IBM, 1943. "); } } Whitespace separates program elements Whitespace between program elements is ignored by Java 4
Bad whitespacing o The same program without any whitespacing or comments: public class Display. Forecast 2 { public static void main (String[] args) { System. out. print("I think there is a world market for"); System. out. println(" maybe five computers. "); System. out. println(" Thomas Watson, IBM, 1943. "); } } 5
An aside: IOCCC o o o The International Obfuscated C Code Contest n Online at http: //www. ioccc. org C has very terse syntax n So the contest tries to make it terser! One common method is by modifying the whitespace 6
An aside: IOCCC #define #define #define #define X X X X X X X X XX X X XXXXX X XXXX X XXXX X XX ainma(){ archa XX X XXXX X oink[9], *igpa, X XXXX X X XXXXXX atinla=etcharga(), iocccwa XXXXXX X X XXXX , apca='A', owla='a', umna=26 XXXX X X XXX ; orfa(; (atinla+1)&&(!((( XXX X X XX atinla-apca)*(apca+umna-atinla) XX X >=0)+((atinla-owla)*(owla+umna- X X X atinla)>=0))); utcharpa(atinla), X X X atinla=etcharga()); orfa(; atinla+1; X X ){ orfa( igpa=oink , iocccwa=( X X (atinla- XXX apca)*( XXX apca+umna- X X X atinla)>=0) XXX ; (((( X X atinla-apca XXXXXXX )*(apca+ X X umna-atinla XXXXXX )>=0) XXXXXX +((atinla- X X owla)*(owla+ XXXX umna. XXXX atinla)>=0)) X X &&"-Pig-" XX "Lat-in" XX "COb-fus" X X "ca-tion!!"[ X (((atinla. X apca)*(apca+ X X umna-atinla) X >=0)? atinla. X apca+owla: X X atinla)-owla X ]-'-')||((igpa== X oink)&&!(*( X X igpa++)='w') X )||! X (*( X igpa X ++)=owla); * X X (igpa++)=(( X ( XXX X atinla-apca X X )*(apca+ X umna XXX - XXX X atinla)>=0) X X ? atinla. X apca XXX + XXX owla X : atinla), X X atinla= X X etcharga()) X X ; orfa( X atinla=iocccwa? (( X (atinla. X X owla)*(owla+ X umna-atinla)>=0 X )? atinla. X X owla+apca: X atinla): X atinla; ((( X X atinla-apca)* X (apca+umna- X atinla)>=0)+( X X (atinla-owla)* X (owla+ X umna-atinla)>= X X 0)); utcharpa( XX XX atinla), atinla X X =etcharga()); XXXXXXX orfa(*igpa=0, X X igpa=oink; * igpa; utcharpa( X X *(igpa++))); orfa(; (atinla+1)&&(!((( X X atinla-apca )*(apca+ X X umna. XXXXX atinla)>=0 X X )+(( XXXXX atinla- X XX owla)*( owla+umna- XX XX atinla)>=0))); utcharpa XX XX (atinla), atinla= XX XX etcharga()); } XX XXXX } XXXXXXXXX a(X){/*/X=X XX -1; F; X=XXX -1; F; }/*/ XXXXX char*z[]={", "a(X){/*/X=-", "-1; F; }/*/", "99999 : -| ", XXXXXX "int XXXXXXX q, i, j, k, X, O=0, H; S(x)int*x; {X+=X; O+=O; *x+1? *x+2||X++: O++; *x=1; }L(n){for(*", orfa for "z[i=1]=n+97; i<4; i++)M(256), s(i), M(128), s(i), M(64), N; X*=8; O*=8; }s(R){char*r=z", XXXXX "[R]; for(q&&Q; *r; )P(*r++); q&&(Q, P(44)); }M(m){P(9); i-2||P(X&m? 88: O&m? 48: 32); P(", archa char ainma main "9); }y(A){for(j=8; j; )~A&w[--j]||(q=0); }e(W, Z){for(i-=i*q; i<9&&q; )y(W|(1<<i++&", etcharga getchar "~Z)); }R(){for(k=J[*J-48]-40; k; )e(w[k--], X|O); }main(u, v)char**v; {a(q=1); b(1); ", utcharpa putchar #define _ -F<00||--F-OO--; #include <stdio. h> int F=00, OO=00; main(){F_OO(); printf("%1. 3 fn", 4. *-F/OO/OO); }F_OO() #define Q r=R[*p++-'0']; while( { #define B ; break; case _-_-_-_ char*s="Qjou!s\311^-g\311^-n\311^-c\: : ^-q-ma%m. O 1 JBHm%BQ-a. P 1 J[O 1 HB%[Q<nbj _-_-_-_-_ o)*|gps)<<*txjudi)m*|a. Qdbtf!: : ; sfuvso<a. Qefgbvmu; a. Q<m, , a%CQ<csfbla%b. Q<a. N 2!Q _-_-_-_-_-_-_ ndbtf!a. P 2 Q; m>a. P 2 Q<a%!D 12 J!JGJHJOJQJFJSJJJMHS%HD 12 N 3!N 4n. JUJT%UQm>a. P 4 HC%T Qs\q, , ^>m, 2<m>a. P 4 HC%SD 12 N 1n. JNQm>s\. . q^a. HC%NHb%GN 1!D 32 P 3%RN 1 UP 1 D 12 JPQUa. P 1 H "c(1); *J=--u? O? *J: *v[1]: 53; X|=u<<57 -*v[u]; y(X); K=40+q; q? e(O, X), q&&(K='|'), e(X", _-_-_-_-_-_-_-_ ", O), R(), O|=1<<--i: J[*J-48+(X=O=0)]--; L(q=0); for(s(i=0); q=i<12; )s(i++), i>4&&N", R%PN 4n. Q<g\(a. P 3 Q(^>a. P 2 Q, 2<n\(a. P 3 Q(^>a. P 4 Hb%OD 12 N 2!N 3n. JVP 3 Q, , <jg)a. P 3 Q=>n _-_-_-_-_-_-_-_ "; s(q=12); P(48); P('}'); P(59); N; q=0; L(1); for(i=5; i<13; )s(i++), N; L(2); }", 0}; \(a. P 3 Q(^*m>g\(a. P 3 Q(^<fmtf!m, , a. HC%QN 1!N 1n. J#Qqsjoug)#&e]o#-a. P 1 Q*a. Hb%#Qqvut) _-_-_-_-_-_-_-_ b(X){/*/X=a. P 1 Q*a. Hb%FN 1n. Qm>: : a. HC%VP 3 Q>bupj)hfut)c**a. Hb%JD 12 JON 1!Qjg)a%LN 1 UP 1 D 12 JIQUa -1; F; X=_-_-_-_-_-_-_-_-_ P 1 HL%IQ*m>a. N 2!N 2n. P 2 Q<fmtf!m, , a. HC%MN 1!N 2>P 2 Q>a. N 2n. P 2 Hbdd!b/d"; k; char R[4][99] -1; F; }/*/ _-_-_-_-_-_-_-_-_ ; main(c, v)char**v; {char*p, *r, *q; for(q=s; *q; q++)*q>' '&&(*q)--; {FILE*i=fopen(v int q, i, j, k, X, O=0, H; S(x)int*x; {X+=X; O+=O; *x+1? *x+2||X++: O++; *x=1; }L(n){for(* _-_-_-_-_-_-_-_-_ [1], "r"), *o=fopen(q-3, "w"); for(p=s; ; p++)switch(*p++){B'M': Q(k=fgetc(i))!=EOF z[i=1]=n+97; i<4; i++)M(256), s(i), M(128), s(i), M(64), N; X*=8; O*=8; }s(R){char*r=z [R]; for(q&&Q; *r; )P(*r++); q&&(Q, P(44)); }M(m){P(9); i-2||P(X&m? 88: O&m? 48: 32); P( _-_-_-_-_-_-_-_-_ &&k!=*p)*r++=k; if(k==EOF){fputs("}}n", o); fclose(o); return system(q-6); }*r=0 9); }y(A){for(j=8; j; )~A&w[--j]||(q=0); }e(W, Z){for(i-=i*q; i<9&&q; )y(W|(1<<i++& B'P': while(*p!='`')fputc(*p++, o)B'O': Q*r)fputc(*r++, o); p--B'C': k=0; Q k<*p-'0' _-_-_-_-_-_-_-_ ~Z)); }R(){for(k=J[*J-48]-40; k; )e(w[k--], X|O); }main(u, v)char**v; {a(q=1); b(1); )(*r++=fgetc(i), k++); *r=0 B'I': k= _-_-_-_-_-_-_-_ *p; if(**R==k)goto G B'G': k= *p; G: p=s; while( c(1); *J=--u? O? *J: *v[1]: 53; X|=u<<57 -*v[u]; y(X); K=40+q; q? e(O, X), q&&(K='|'), e(X *p!='$'||p[1]!= k)p++; p++B'N': R[*p-'0'][0]++; }}} , O), R(), O|=1<<--i: J[*J-48+(X=O=0)]--; L(q=0); for(s(i=0); q=i<12; )s(i++), i>4&&N _-_-_-_-_-_-_-_ ; s(q=12); P(48); P('}'); P(59); N; q=0; L(1); for(i=5; i<13; )s(i++), N; L(2); } _-_-_-_-_-_-_ c(X){/*/X=_-_-_-_-_ -1; F; X=-1; F; }/*/ _-_-_-_ 7 }
Identifiers o o o Identifiers are names for variables, classes, etc. Good ones are compact, but inidicate what they stand for n radius, width, height, length Bad ones are either too long n the. Radius. Of. The. Circle n the. Width. Of. The. Box. That. Is. Being. Used n the_width_of_the_box_that_is_being_used Or too short n a, b, c, d, e Good identifiers will help the graders understand your program! 8
Keywords o Some words are reserved, and can’t be used as identifiers // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class Display. Forecast { } // method main(): application entry point public static void main(String[] args) { System. out. print("I think there is a world market for"); System. out. println(" maybe five computers. "); System. out. println(" Thomas Watson, IBM, 1943. "); } 9
Capitalization o o o Case matters! public ≠ PUBLIC n This is different that FORTRAN and BASIC n This is the same as C/C++ You can use Public as a identifier n Not recommended, though! 10
Defining a method o All methods have the following syntax: modifers type name ( parameters ) { statements } Properties of the method Type that it returns public static void A name for the method main Any number The body of (including zero) the method of parameters (can be empty) (String[] args) {. . . } 11
Escape sequences o Java provides escape sequences for printing special characters n b backspace n n newline n t tab n r carriage return n \ backslash n " double quote n ' single quote 12
Escape sequences o What do these statements output? System. out. println("Persont. Heightt. Shoe size"); System. out. println("============="); System. out. println("Hannaht 5‘ 1"t 7"); System. out. println("Jennat 5'10"t 9"); System. out. println("JJt 6'1"t 14"); o Output Person Height Shoe size ============= Hannah 5‘ 1" 7 Jenna 5'10" 9 JJ 6'1" 14 13
Primitive variable assignment o o Assignment operator = n Allows the memory location for a variable to be updated Consider int j = 11; j = 1985; 14
Primitive variable assignment o o o Consider int a = 1; int a. Squared = a * a; a = 5; a. Squared = a * a; Consider int i = 0; i = i + 1; Consider int asa. Rating; asa. Rating = 400; 15
Primitive variable assignment o Consider double x = 5. 12; double y = 19. 28; double remember. X = x; x = y; y = remember. X; 16
Primitive variable types o o Java has 8 (or so) primitive types: n float real numbers n double two values: true and falsea n boolean n char a single character n byte n short integer numbers n int n long Also the void “type” 17
Primitive real (floating-point) types o o o A float takes up 4 bytes of space n Has 6 decimal places of accuracy: 3. 14159 A double takes up 8 bytes of space n Has 15 decimal places of accuracy: 3. 14159265358979 Always use doubles n It will save you quite a headache! 18
Primitive integer types o Consider a byte: 0 o o 1 0 0 0 1 1 byte = 8 bits Each bit has two possibilities: 0 or 1 o 28 = 256 Thus, a byte can have any one of 256 values o A Java byte can have values from -128 to 127 o n o From -27 to 27 -1 C/C++ has unsigned versions; Java does not 19
Primitive integer types Type Bytes Minimum value Maximum value byte -27=-128 27 -1=127 short 2 -215= -32, 768 215 -1= 32, 767 int 4 -231=-2, 147, 483, 648 231 -1=2, 147, 483, 647 long 8 -263=-9, 223, 372, 036, 854, 775, 808 263 -1=9, 223, 372, 036, 854, 775, 807 1 20
Increment and decrement operators o ++ n o -n o Increments a number variable by 1 Decrements a numeric variable by 1 Consider int i = 4; ++i; System. out. println(i); System. out. print(++i); System. out. println(i++); System. out. println(i); // // // define increment display update then display then update display 21
Why you should get the extended warranty 22
Primitive character type o o All characters have a integer equivalent n ‘ 0’ = 48 n ‘ 1’ = 49 n ‘A’ = 65 n ‘a’ = 97 Thus, you can refer to ‘B’ as ‘A’+1 23
Primitive character type public class Lower. To. Upper { // main(): application entry point public static void main(String[] args) { // set lower case character of interest char lower. Case. Letter = 'c'; // convert to uppercase equivalent char upper. Case. Letter = 'A' + (lower. Case. Letter - 'a'); // display result System. out. println("Uppercase equivalent of"); System. out. println(" " + lower. Case. Letter); System. out. println("is"); System. out. println(" " + upper. Case. Letter); } } 24
Primitive boolean type o o o When is the following program valid in Java? n Assume a and b have been properly declared. . . if ( a && b ) { // do something interesting }. . . Answer: ONLY when a and b are boolean variables In C/C++, a and b would be ints (or int variants) n If you try making a and b ints in Java, you get the following: n operator && cannot be applied to int, int 25
Primitive void “type” o o o In Java, you can ONLY use void to specify that a method does not return a value You cannot use it to declare a void “variable”, as in C/C++: void *foo; You cannot use it to specify that there are no parameters to a method: . . . int foo (void) {. . . n This is different from C/C++ 26
Variable initialization o Consider the following code: int x; System. out. println(x); o o What happens? Error message: n variable x might not have been initialized 27
Constants o Consider the following: final int x = 5; o o The value of x can NEVER be changed! n The value assigned to it is “final” This is how Java defines constants 28
Expressions o o o What is the value used to initialize expression int expression = 4 + 2 * 5; What value is displayed System. out. println(5 / 2. 0); Java rules in a nutshell n Each operator has a precedence level and an associativity o Operators with higher precedence are done first n o n * and / have higher precedence than + and - Associativity indicates how to handle ties 29 When floating-point is used the result is floating point
Question on expressions o Does the following statement compute the average of double variables a, b, and c? Why double average = a + b + c / 3. 0; 30
System. out. println() public static void main(String[] args) { System. out. print("I think there is a world market for"); System. out. println(" maybe five computers. "); System. out. println(" Thomas Watson, IBM, 1943. "); } o o o Class System supplies objects that can print and read values System variable out references the standard printing object n Known as the standard output stream Variable out provides access to printing methods n print(): displays a value n println(): displays a value and moves cursor to the next line 31
System. out : Print. Stream - destination = -. . . + println(String s) : void + print(String s) : void +. . . Variable System. out gives access to an output stream of type Print. Stream The printing destination attribute for this Print. Stream object is the console window The behaviors of a Print. Stream object support a high-level view of printing 32
Selection The period indicates that we want to select an individual class member of System The period indicates that we want to select an individual class member of out The method we are calling System . out . print ( " string " ) Member out of System is an output Literal character string that is stream object automatically the parameter to print(). associated with the console window running the application Class System is defined in the standard Method member of out. The execution of member print() package java. lang causes its parameter to be displayed to the output stream 33
I/O streams o o o System. out n Prints to standard output n Equivalent to cout in C++, and print() in C System. err n Prints to standard error n Equivalent to cerr in C++, and fprintf(stderr) in C System. in n Reads from standard input n Equivalent to cin in C++, and scanf() in C 34
Beware!!! 35
Example program: temperature conversion // Purpose: Convert a Celsius temperature to Fahrenheit public class Celsius. To. Fahrenheit { // main(): application entry point public static void main(String[] args) { // set Celsius temperature of interest int celsius = 28; // convert to Fahrenheit equivalent int fahrenheit = 32 + ((9 * celsius) / 5); // display result System. out. println("Celsius temperature"); System. out. println(" " + celsius); System. out. println("equals Fahrenheit temperature"); System. out. println(" " + fahrenheit); } } 36
Computation o o o Programmers frequently write small programs for computing useful things Example – body mass index (BMI) n Measure of fitness o Ratio of person’s weight to the square of the person’s height n Weight in is kilograms, height is in meters o Person of interest is 4. 5 feet and weighs 75. 5 pounds Metric conversions n Kilograms per pound 0. 454 n Meters per foot 0. 3046 37
Program outline for BMI. java // Purpose: Compute BMI for given weight and height public class BMI { // main(): application entry point public static void main(String[] args) { // define constants // set up person's characteristics // convert to metric equivalents // perform bmi calculation // display result } } 38
BMI. java: define constants // define constants final double KILOGRAMS_PER_POUND = 0. 454; final double METERS_PER_FOOT = 0. 3046; 39
BMI. java: personal characteristics // set up person's characteristics double weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height 40
BMI. java: convert to metric equivalents // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; 41
BMI. java: perform BMI calculation // perform bmi calculation double bmi = metric. Weight / (metric. Height * metric. Height); 42
BMI. java: display result // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + Math. round(bmi)); Math. round(bmi) is 18 Operator evaluation depend upon its operands 43
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. 454; final double METERS_PER_FOOT = 0. 3046; // set up person's characteristics double weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = metric. Weight / (metric. Height * metric. Height); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + Math. round(bmi)); } 44
Pentium math error 1 o o o Intel’s Pentiums (60 Mhz – 100 Mhz) had a floating point error Graph of z = y/x Intel reluctantly agreed to replace them in 1994 Graph from http: //kuhttp. cc. ukans. edu/cwis/units/IPPBR/pentium_fdiv/pentgrph. html 45
Pentium math error 2 o Top 10 reasons to buy a Pentium: 10 Your old PC is too accurate 8. 9999163362 Provides a good alibi when the IRS calls 7. 9999414610 Attracted by Intel's new "You don't need to know what's inside" campaign 6. 9999831538 It redefines computing--and mathematics! 5. 9999835137 You've always wondered what it would be like to be a plaintiff 4. 9999999021 Current paperweight not big enough 3. 9998245917 Takes concept of "floating point" to a new level 2. 9991523619 You always round off to the nearest hundred anyway 1. 9999103517 Got a great deal from the Jet Propulsion Laboratory 0. 999998 It'll probably work!! 46
Common program elements o o o Type n Set of values along with operators that can manipulate and create values from the set Primitive types support numeric, character, logical values n double and float o Values with decimals n byte, short, int, long o Integers n char o Characters (considered numeric) n boolean o Logical values Basic operators n + addition n * multiplication - subtraction / division 47
Common program elements o o Constant n Symbolic name for memory location whose value does not change o KILOGRAMS_PER_POUND Variable n Symbolic name for memory location whose value can change o weight. In. Pounds 48
Interactive programs o o o Programs that interact with their users through statements performing input and output Temperature conversion n Not interactive – Celsius temperature is fixed BMI. java n Not interactive – weight and height are fixed 49
Un-reliable computers… 50
Interactive programs o o Programs that interact with their users through statements performing input and output BMI. java n Not interactive – weight and height are fixed 51
Support for interactive console programs o o Variable System. in n Associated with the standard input stream – the keyboard Class Scanner n Makes obtaining input from the keyboard easy Scanner stdin = Scanner. create(System. in); stdin : Scanner - source = -. . . + next. Double() : double +. . . Variable stdin gives Scanner access to an input stream Input source attribute for this Scanner is the keyboard Behaviors of a Scanner support 52 high-level view of inputting text
How to make Java work with the Scanner class o o In Java 1. 5, do a: import java. util. *; In Java 1. 4 (what we are using) n Copy the Scanner. class file to the classes subdirectory for the JCreator project 53
Interactive program for BMI o Program outline // Purpose: Compute BMI for user-specified // weight and height public class BMICalculator { // main(): application entry point public static void main(String[] args) { // // defining constants displaying legend set up input stream get person's characteristics convert to metric equivalents perform bmi calculation display result } } 54
public static void main(String[] args) { // define constants //. . . // displaying legend System. out. println ("BMI Calculatorn"); // set up input stream Scanner stdin = Scanner. create(System. in); // get person's characteristics System. out. print("Enter weight (lbs): "); double weight = stdin. next. Double(); System. out. print("Enter height (feet): "); double height = stdin. next. Double(); // convert to metric equivalents double metric. Weight = weight * KILOGRAMS_PER_POUND; double metric. Height = height * METERS_PER_FOOT; // perform bmi calculation double bmi = metric. Weight / (metric. Height * metric. Height); // display result //. . . } 55
class BMICalculator { public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. 454; final double METERS_PER_FOOT = 0. 3046; // displaying legend System. out. println ("BMI Calculatorn"); // set up input stream Scanner stdin = Scanner. create(System. in); // get person's characteristics System. out. print("Enter weight (lbs): "); double weight = stdin. next. Double(); System. out. print("Enter height (feet): "); double height = stdin. next. Double(); // convert to metric equivalents double metric. Weight = weight * KILOGRAMS_PER_POUND; double metric. Height = height * METERS_PER_FOOT; // perform bmi calculation double bmi = metric. Weight / (metric. Height * metric. Height); // display result System. out. println("A person with"); System. out. println(" weight " + weight + " lbs"); System. out. println(" height " + height + " feet"); System. out. println("has a BMI of " + Math. round(bmi)); } } 56
Scanner API public Scanner(Input. Stream in) public Scanner(File s) // Scanner(): convenience constructor for an // Input. Stream // Scanner(): convenience constructor for a filename public create(Input. Stream in) // create(): convenience construction from an // Input. Stream public static Scanner create(File s) // Scanner(): convenience construction from a filename public int next. Int() // next. Int(): next input value as an int public short next. Short() // next. Short(): next input value as a short public long next. Long() // next. Long(): next input value as a long public double next. Double() // next. Double(): next input value as a double public float next. Float() // next. Float(): next input value as a float public String next() // next(): get next whitespace-free string public String next. Line() // next. Line(): return contents of input line buffer public boolean has. Next() // has. Next(): is there a value to next 57
Class fields class BMICalculator { public static void // define constantsmain(String[] args) { finaldefine constants // static double KILOGRAMS_PER_POUND = 0. 454; final static double METERS_PER_FOOT==0. 454; final double KILOGRAMS_PER_POUND 0. 3046; final double METERS_PER_FOOT = 0. 3046; public static void main(String[] args) { // displaying legend System. out. println ("BMI Calculatorn"); //. . . } } 58
8ffc412f50520f07faf81b28a8102ccf.ppt