1 / 14

Introduction to Java - Input, Program Control and Instantiation

Department of Computer and Information Science, School of Science, IUPUI. Introduction to Java - Input, Program Control and Instantiation. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. 2.5 Another Java Application: Adding Integers. Upcoming program

colum
Download Presentation

Introduction to Java - Input, Program Control and Instantiation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Department of Computer and Information Science,School of Science, IUPUI Introduction to Java- Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

  2. 2.5 Another Java Application: Adding Integers • Upcoming program • Use Scanner to read two integers from user • Use printf to display sum of the two values • Use packages

  3. import declaration imports class Scanner from package java.util. Declare and initialize variable input, which is a Scanner. Declare variables number1, number2 and sum. Read an integer from the user and assign it to number1. Outline • nextInt

  4. Two integers entered by the user. Outline Read an integer from the user and assign it to number2. Calculate the sum of the variables number1 and number2, assign result to sum. • Addition.java • (2 of 2) • 4. Addition • 5. printf Display the sum using formatted output.

  5. 2.8 Decision Making: Equality and Relational Operators • Condition • Expression can be either true or false • if statement • Simple version in this section, more detail later • If a condition is true, then the body of the if statement executed • Control always resumes after the if statement • Conditions in if statements can be formed using equality or relational operators (next slide)

  6. Fig. 2.14 | Equality and relational operators.

  7. Test for equality, display result using printf. Compares two numbers using relational operator <. Outline • Comparison.java • (1 of 2) • 1. Class Comparison1.1 main1.2 Declarations1.3 Input data (nextInt)1.4 Compare two inputs using if statements

  8. Compares two numbers using relational operators >, <= and >=. Outline • Comparison.java • (2 of 2) • Program output

  9. Declaring a Class with a Method and Instantiating an Object of a Class • Each class declaration that begins with keyword public must be stored in a file that has the same name as the class and ends with the .java file-name extension.

  10. Print line of text to output Outline • GradeBook.java

  11. Use class instance creation expression to create object of class GradeBook Call method displayMessage using GradeBook object Outline • GradeBookTest.java

  12. Compiling an Application with Multiple Classes • Compiling multiple classes • List each .java file in the compilation command and separate them with spaces • Compile with *.java to compile all .java files in that directory

  13. Notes on Import Declarations • java.lang is implicitly imported into every program • Default package • Contains classes compiled in the same directory • Implicitly imported into source code of other files in directory • Imports unnecessary if fully-qualified names are used

  14. Acknowledgements • http://java.sun.com/docs/books/tutorial/getStarted/TOC.html • Pearson Education, Lewis and Loftus. • Deitel, Java How to Program • http://www.cs.wustl.edu/~plezbert/contcom/thesis/node6.html • http://www.cs.usfca.edu/~parrt/course/652/lectures-Spring-2004/language.impl.overview.pdf • http://ei.cs.vt.edu/~history/Youmans.Java.html

More Related