Java basicsDisplayForecast.javaIndentationGood whitespacingBad whitespacingAn aside: IOCCCSlide 7IdentifiersKeywordsCapitalizationDefining a methodEscape sequencesSlide 13Primitive variable assignmentSlide 15Slide 16Primitive variable typesPrimitive real (floating-point) typesPrimitive integer typesSlide 20Increment and decrement operatorsWhy you should get the extended warrantyPrimitive character typeSlide 24Primitive boolean typePrimitive void “type”Variable initializationConstantsExpressionsQuestion on expressionsSystem.out.println()System.outSelectionI/O streamsBeware!!!Example program: temperature conversionComputationProgram outline for BMI.javaBMI.java: define constantsBMI.java: personal characteristicsBMI.java: convert to metric equivalentsBMI.java: perform BMI calculationBMI.java: display resultSlide 44Pentium math error 1Pentium math error 2Common program elementsSlide 48Interactive programsUn-reliable computers…Slide 51Support for interactive console programsHow to make Java work with the Scanner classInteractive program for BMISlide 55Slide 56Scanner APIClass fields1Java basicsChapter 2CS 101-E2DisplayForecast.java// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console windowpublic class DisplayForecast { // 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.");}}// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console windowpublic class DisplayForecast { // 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.");}}Three statements make up the action of method main()Method main() is part of class DisplayForecast// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console windowpublic class DisplayForecast { // 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.");}}A method is a named piece of code that performs some action or implements a behavior// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console windowpublic class DisplayForecast { // 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.");}}An application program is required to have a public static void method named main().// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console windowpublic class DisplayForecast { // 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.");}}public, static, and void are keywords. They cannot be used as namespublic means the method is shareable// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console windowpublic class DisplayForecast { // 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.");}}We will discuss static and void later// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console windowpublic class DisplayForecast { // 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 statement to be made up of multiple lines of textSemicolons delimit one statement from the next// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console windowpublic class DisplayForecast { // 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.");}}A class defines an object form. An object can have methods and attributesKeyword class indicates a class definition follows// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console windowpublic class DisplayForecast { // 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.");}}A class like a method must have a name// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console windowpublic class DisplayForecast { // 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.");}}A class like a method must have a name// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console windowpublic class DisplayForecast { // 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.");}}Programs are read by people – make sure they are readable.Use whitespace, comments, and indentation to aid understanding// Authors: J. P. Cohoon and J. W. Davidson// Purpose: display a quotation in a console windowpublic class DisplayForecast { // 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,
View Full Document