Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.

Similar presentations


Presentation on theme: "CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing."— Presentation transcript:

1 CIS 260: App Dev I

2 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing a program n Programming  The process of implementing a program design n Application program  A stand-alone computer program that is applied to a real-world problem

3 3 The Java Programming Language n Programming language  The ________ used to create valid program statements n Syntax  The symbols, words, and rules of a programming language n A simple Java program public class Welcome { public static void main( String[] args ) { System.out.println( "Welcome to Java Programming“ ); }

4 4 The Java Syntax n Tokens  Special symbols, word symbols, and __________ of a language n Special symbol  One or more characters w/ special meaning  Examples: +, -, *, /, <, … n Word symbols  Reserved word (___________)  Examples: int, static, return, true, … n Identifier  Predefined or user-defined names of things  Examples: print, totalCost, …

5 5 Data Types n Data type  A classification of data according to legal values and legal operations on those values n Primitive data types in Java primitive characternumericlogical integralfloating-point charbyteshortintlongfloatdoubleboolean

6 6 Details on Selected Data Types n char  Examples: ‘A’, ‘a’, ‘$’, ‘&’, ‘ ’, …  Unicode 65 is ‘A’ and Unicode 43 is ‘+’ n int  Non-decimal (whole number) values  Range of values: -2147483648 to 2147483647  Examples: 24, -117, 34082, 0, … n double  Decimal values with up to 15 decimal places (double precision)  Range of values: -1.7 x 10 308 to 1.7 x 10 308  Examples: 14.75, -.00053, -289038432.8993, -5.3E-4 n boolean  Logical values  Examples: true, ___________

7 7 Arithmetic Operators n Possible arithmetic operators for integral and floating- point data types:  +, -, *, /, % (remainder upon division) n Examples  8+7 yields 15  6-15 yields -9  6*8 yields 48  6*8.0 yields 48.0  15/4 yields _____  15/4.0 yields 3.75  15%7 yields 1  15.2%7 yields _____

8 8 Order of Precedence *, /, and % have the same precedence + and – have the same precedence, but lower than *, /, and % n Operations with the same precedence are performed from left to right () ’s can be used to override normal precedence n Examples  4 + 8 / 2 % 3 yields ____  (4 + 8 / 2) % 3 yields ____

9 9 Expressions n Integral expressions  All operands are integers or integer types  Example: (apples + oranges) * 2 n Floating-point expressions  All operands are floating-points or floating-point types  Example: totalCost *.05 n Mixed expressions  Operands are of different types  Examples: 2*5/mpg yields ______ if mpg is 4.0 ( double ) cost/2+(7-10.0) yields _______ if cost is 3 ( int )

10 10 Type Casting n Implicit type coercion  Occurs automatically with mixed expressions  15/4.0 automatically becomes 15.0/4.0 n Explicit type conversion  Also called type _________  Converts a result to a desired data type  Examples (double) 15/3 yields ______ (int) (16/3.0)+2*8%5 yields ______ ( int) 16/number + 7 yields _____ if number is 2.0 (char) 65 yields ______

11 11 The class String A string is a sequence of 0 or more characters enclosed in double ________ (e.g., “Joe” ) In Java, a String is not a primitive data type A String with no characters is called a _____ string ( “” ) n The length of a String is its number of ___________ The position of a character in a String starts with 0 for the first, 1 for the second, …

12 12 Parsing Numeric Strings n In Java, input can only be received as a string or character n A string with only integers or decimal numbers is called a ________ string (e.g. “78.3”, “.0038”, “17”) n To convert a numeric string to an actual number in Java  Integer.parseInt(“17”) yields ____  Double.parseDouble(“78.3”) yields ____  Integer.parseInt(numInput) yields ____

13 13 Variables and Named Constants n How to store program data in main memory:  Write a statement to ___________ memory  Write a statement to put data in memory location n Data that may change during program execution are stored in a ___________.  int hoursWorked; // allocates memory  hoursWorked = 45; // puts data in  int overtimeHours = 5; // does both n Data that should not change during program execution are stored in a named ___________.  final double PAY_RATE = 7.50; Variables and constants are just __________ locations.

14 14 Assignment Statements n _______ variables in Java (allocate memory):  double cost;  String firstName, lastName;  int i, j, k; n ________ variables in Java (store in memory):  cost = 19.95;  firstName = “Richard”;  i = i + 1; // get i, add 1, store in i n The “=“ is an _________ operator, not “equals”. It literally means “is assigned the value”. n Assign a new value to an existing variable:  cost = materiaCost + laborCost;

15 15 Input: The Scanner Class n Java is a pure OO language and uses its own ___________ classes, objects, and methods. The Scanner input stream class (new in 5.0):  static Scanner console = new Scanner( System.in );  console.nextInt() // gets next item as an integer  console.nextDouble() // gets next item as a double  console.next() // gets next item as a String  console.nextLine() // gets everything to end of line n To read a single character:  char aCharacter;  aCharacter = console.next().charAt(0); n To convert string data to numeric data:  double price;  price = Double.parseDouble( console.nextDouble() );

16 16 Input: The JOptionPane Class n GUIs can be used for program input and output. n Using GUIs requires an _________ statement. The following statement (must be the first statement) imports the JOptionPane class:  import javax.swing.JOptionPane; This statement uses the showInputDialog method to get _________ input:  name = JOptionPane.showInputDialog ( “Enter your name.” ); This statement uses the showMessageDialog method to display output:  JOptionPane.showMessageDialog( null, outputMessage );

17 17 Increment and Decrement n The following type of statement is used a lot:  count = count + 1;  It means “get the value in count, add 1 to it, assign that to count”  A shortcut in Java: count++; or ++count; n Increment and decrement operators have prefix and _______ forms.  The prefix form is evaluated before the expression is evaluated.  The postfix form is evaluated ______ the expression is evaluated. n Example: int a = 0, b = 0, c = 0; c = 2 + (++a); // ___ will be stored in c c = a + (b++); // ___ will be stored in c c = a + (++b); // ___ will be stored in c

18 18 Using the String Class n A String object usually consists of one or more __________. String title = “War and Peace”; n The following shows an empty String and a null String:  String code = “”; // has memory address  String inputValue = null; // no address n _______ sequences use the \ to create new lines, tabs, or special characters. “\”Bud\”\tSmith \n Rick\tAnkiel”  “Bud”Smith RickAnkiel

19 19 String Class Methods/Joining n A method of a class is called using the object name, a ____, and the method name (with arguments). String choice=“X”; if (choice.equals(“x”)) // returns false n How to join (____________) String objects: String title = “War and Peace”; double price = 14.95; String message = “Title: ” + title + “\n” + “Price: ” + price; n Note the code is written to enhance readability.

20 20 Output In Java, the standard output object is __________ with methods print and _______. print leaves the cursor at the end of the current line while println moves it to the next line. n System.out.println(‘q’);// displays q n System.out.println(“Joe”);// displays Joe Escape sequence \n is for a new _____, \t is for a _____. n Example String name = “Joe”; System.out.println(“My name is \n” + name + “.”);

21 21 Packages and import n In Java, a package is a collection of related ________. n A class is a section of Java code in a file that contains methods and data definitions. n A method is a set of instructions to accomplish a specific _____. n The package _________ contains classes for program input and output. To make all classes in java.io available in your program you need the statement ________________ at the very beginning.

22 22 Java Application Programs n Your Java application program must contain at least one class and one of those classes must have a method called ______. The method main has two parts:  The heading: public static void main(String [] args) –public means main is accessible to other classes –static means main isn’t directly related to objects –void means main will not return data  The body Enclosed in { } ’s Contains declaration statements: int myAge, yourAge; Contains executable statements: myAge = 50;

23 23 Programming Style and Form n ________ rules must be followed. For example,  You must place a “ ; ” at the end of each program statement  { } ‘s must always occur in pairs n Form and style:  Write just one statement per line  Indent lines for readability (as shown in examples)  Add important _____________ using // and /*…*/ Always begin a program with comments for the program name, the programmer, the date, and the program purpose  Naming variables ( hourlyWage ), constants ( TAX_RATE )  Provide prompt text for input  Provide good explanation for input and output

24 A Java Application Program //A properly formatted Java program. import java.util.Scanner; public class IntegerNameHeight { static Scanner console = new Scanner( System.in ); public static void main( String[] args ) { // preparation int num; double height; String name; // input System.out.print("Enter an integer: "); System.out.flush(); num = console.nextInt(); System.out.println(); System.out.print( "Enter the first name: “ ); System.out.flush(); name = console.next(); System.out.println(); System.out.print( "Enter the height: “ ); System.out.flush(); height = console.nextDouble(); // output System.out.println(); System.out.println( "num: " + num ); System.out.println( "Name: " + name ); System.out.println( "Height: " + height ); }


Download ppt "CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing."

Similar presentations


Ads by Google