Download presentation
Presentation is loading. Please wait.
Published byWhitney Flynn Modified over 9 years ago
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 plan
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"); } syntax
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, … identifiers keyword
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, ___________ false
7
7 Arithmetic Operators n Possible arithmetic operators for integral and floating- point data types: +, -, *, /, % 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 _____ 3 1.1999999999999993
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 ____ 5 2
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 ) 2.5 -2.0
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 ______ casting 6 15 ‘A’ 5.0
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, … quotes null characters
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 ____ numeric 17 78.3 ???
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. allocate variable constant memory
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; Declare Assign assignment
15
15 Input (Read) Statement in Java n Java is a pure OO language and uses its own special classes, objects, and methods. The two Java statements below use the BufferedReader and ___________________ classes and the ___ object of the System class to create an object called keyboard, which uses a predefined method called readLine() to store characters in a variable called name : BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); name = keyboard.readLine(); n To convert string data to numeric data: price = Double.parseDouble(keyboard.readLine()); InputStreamReader in
16
16 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 add, assign it 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; c = 2 + (++a); // ___ will be stored in c c = a + (b++); // ___ will be stored in c c = a + (++b); // ___ will be stored in c postfix after 3 1 3
17
17 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 = “”; String inputValue = null; n _______ sequences use the \ to create new lines, tabs, or special characters. “\”Bud\”\tSmith \n Rick\tAnkiel” “Bud”Smith RickAnkiel characters Escape
18
18 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. dot concatenate
19
19 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 + “.”); System.outprintln line tab
20
20 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. classes task java.io import java.io
21
21 Java Application Programs n Your Java application program must contain at least one class and one of those classes must have a method called ______. n 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; main
22
22 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 lines for input Provide good explanation for output Syntax documentation
23
A Java Application Program /* * Conversion.java * Created by Malik & Nair * 9/3/04 * Convert inches to centimeters */ import java.io.*; public class Conversion { static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); static final double CONVERSION = 2.54; static final int INCHES_PER_FOOT = 12; public static void main (String[] args) throws IOException { //declare variables int feet, inches, totalInches; double centimeter; // get input from user System.out.println("Enter feet: "); feet = Integer.parseInt(keyboard.readLine()); System.out.println(); System.out.println("Enter inches: "); inches = Integer.parseInt(keyboard.readLine()); // process data totalInches = INCHES_PER_FOOT * feet + inches; centimeter = totalInches * CONVERSION; // display output to user System.out.println("\nThe numbers you entered are " + feet + " for feet " + "and " + inches + " for inches. "); System.out.println("\nThe total number of inches = " + totalInches); System.out.println("The number of centimeters = " + centimeter); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.