Connecting with Computer Science, 2e Chapter 15 Programming IIConnecting with Computer Science, 2e 2 Objectives • In this chapter you will: – Gain an understanding of the basics of high-level programming languages, using Java and C++ as examples – Learn about variable types in Java and C++ and how they’re used – Explore the different control structures in Java and C++Connecting with Computer Science, 2e 3 Why You Need to Know About... Programming Languages • Time, money, and effort go into learning computer programming languages – The only real way to learn is practice, practice, and more practice • After reading this chapter, you must sit down at the computer and practice the concepts frequentlyConnecting with Computer Science, 2e 4 Java and C++ Programming Languages • Criteria for choosing a programming language: – Tasks to perform – Programmer’s skill level – Program’s lifetime – Software complexity being designed • C++ and Java characteristics – Support an object-oriented environment – Usable on different operating systems – Provide strong foundation for learning how to program – Provide a springboard to other languagesConnecting with Computer Science, 2e 5 Learning to Cook with Java and C++ • Four ingredients to write programs: – Variables – Operators – Control structures – Objects • Java and C++ high-level programming languages provide computer interaction – Without speaking in binary 1s and 0sConnecting with Computer Science, 2e 6 Learning to Cook with Java and C++ (cont’d.) • Java history: – Designed for Internet use – Introduced by Sun Microsystems in 1995 – Intended for small tasks or small applications (i.e., “applets”) • No need to write entire programs – Developed into full-blown programming language • Language of choice to develop communication devices and media applications (e.g., PDAs, cell phones, Internet, and networks)Learning to Cook with Java and C++ (cont’d.) • Java’s advantages: – Uses familiar syntax – Very portable – Powerful and popular • C++ history: – Created by Bjarne Stroustrup at Bell Labs in 1983 – Based on C with added features – Object-oriented programming language – Offers simplified memory management and access to low-level memory Connecting with Computer Science, 2e 7Connecting with Computer Science, 2e 8 Variables • Have specific effects on a program’s outcome • Must have an identifier or name prior to use • Declaration: statement associating an identifier with a variable, an action, or another programming element – When declared, you specify attributes: • Identifier (name) • Type (character, numeric, Boolean, and so forth) • Content – Example: int numTicketsBought;Connecting with Computer Science, 2e 9 Variable Naming Conventions • Rules for declaring a variable in Java or C++ – Use only letters, underscores, numbers – Begin name with a letter – Avoid Java and C++ reserved words • Reserved word – Keyword with a specific instructional meaning • Name cannot be used for a variable • Programming language already using it as an instructionVariable Types • Java and C++ are strongly typed – Must declare type of data a variable can hold • Major Java data types: – Six number-related data types – One character related – One for true and false (Boolean) • Major C++ data types – Adds a type for signed or unsigned numbers • Syntax for declaring a variable: type variableName; Connecting with Computer Science, 2e 10Connecting with Computer Science, 2e 11 Integer Data Types • Used for positive and negative whole numbers – Java example • int studentTickets; • short studentFees; • long studentTuition; • byte studentGrade; – C++ example • int studentTickets; • short int studentFees; • unsigned int totalPoints;Connecting with Computer Science, 2e 12 Table 15-2, C++ integer data types Table 15-1, Java integer data types Integer Data Types (cont’d.)Connecting with Computer Science, 2e 13 Floating-Point Data Types • Used for positive and negative numbers containing decimals – Examples of declaring variables in both languages: • float salary; • double billGatesSalary;Connecting with Computer Science, 2e 14 Table 15-4, C++ floating-point data types Table 15-3, Java floating-point data types Floating-Point Data Types (cont’d.)Connecting with Computer Science, 2e 15 Character Data Type • Used for variables holding only one character – Example: char studentMiddleInit; Table 15-5, Java character data type Table 15-6, C++ character data typesConnecting with Computer Science, 2e 16 Boolean Data Type • Used for only one of two values: true or false • Java and C++ – Cannot associate a number with a Boolean value – Rely on “true” or “false” • Java Boolean variable declaration: – boolean deserveRaise; • C++ Boolean variable declaration: – bool deserveRaise;Connecting with Computer Science, 2e 17 Table 15-7, Java Boolean data type Table 15-8, C++ Boolean data type Boolean Data Type (cont’d.)Connecting with Computer Science, 2e 18 String Data Type • Stores a piece of information – Not a number – Contains more than one character • Declared using double quotes • Uses the String or string keywords – Examples of an empty string: • String sName; //Java String • string sName; //C++ stringConnecting with Computer Science, 2e 19 String Data Type (cont’d.) • Examples of a string with contents assigned: – String sName = "Joe Blow"; //Java – string sName = "Joe Blow"; //C++ • Concatenation operator – The (+) operator – Process of combining or joining strings into one value • See example code on pages 517–518Connecting with Computer Science, 2e 20 Hungarian Notation • Variable-naming method – Adds a letter at the beginning of a variable name • Indicates data type Table 15-9, Hungarian notation examplesVariable Content • When variable is declared: – Use an equal sign (=) to assign a value immediately • Variable initialization: supplying value when variable is first declared • Do not always have
View Full Document