1 / 49

Chapter Topics

Chapter Topics. Chapter 2 discusses the following main topics: The Parts of a Java Program The print and println Methods, and the Java API Variables and Literals Primitive Data Types Arithmetic Operators Combined Assignment Operators. Chapter Topics (Continued).

casillas
Download Presentation

Chapter Topics

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. Chapter Topics Chapter 2 discusses the following main topics: The Parts of a Java Program The printand printlnMethods, and the Java API Variables and Literals Primitive Data Types Arithmetic Operators Combined Assignment Operators

  2. Chapter Topics (Continued) Creating named constants with final The String class Scope Comments Programming style Using the Scanner class for input Dialog boxes

  3. Parts of a Java Program Example program:

  4. Analyzing The Example This is a Java comment. It is ignored by the compiler. This is the class headerfor the class Simple This area is the body of the class Simple. All of the data and methods for this classwill be between these brackets.

  5. Analyzing The Example This is the method headerfor the main method. Themain method is where a Java application begins.

  6. Analyzing The Example This area is the body of the main method. All of the instructions to be completed during the main method will be between the brackets.

  7. Analyzing The Example This is the Java statement that is executed when the program runs.

  8. Programming LanguagesCommon Language Elements There are some concepts that are common to virtually all programming languages. Common concepts: Keywords Operators Punctuation Programmer-defined identifiers

  9. Programming LanguagesSample Program Keywords Punctuation Operators Programmer-Defined Names

  10. Language ElementsKeywords Keywordsare reserved and cannot be used for anything other than their designated purpose. For example, the int keyword signifies an integer variable is going to be declared. Key words are lowercase (Java is a case sensitive language).

  11. Language ElementsKeywords

  12. Language ElementsProgrammer-Defined Names • These words or names are defined by the programmer. • They are not part of the Java language. • Using sensible programmer-defined names increases the readability of programs.

  13. Language ElementsProgrammer-defined Names

  14. Language ElementsOperators • The operatorsperform various operations on data known as operands. • Examples: • + * / =

  15. Languages ElementsOperators

  16. Semicolons are used to end Java statements; however, not all lines of a Java program end a statement. Part of learning Java is learning where to properly use the punctuation. Language ElementsPunctuation

  17. Languages ElementsPunctuation

  18. Lines vs Statements There are differences between lines and statements when discussing source code. This is one Java statement written using two lines. Do you see the difference? A statement is a complete Java instruction that causes the computer to perform an action. All statements end with a semicolon → ;

  19. Where we don’t place a semicolon Comments are ignored by the Java compiler so they do not need a semicolon. Other Java code elements that do not need semicolons include: class headers Terminated by the code within its curly braces. method headers Terminated by the code within its curly braces. brackets Part of language framework (syntax) that does not need semicolon termination.

  20. Programming LanguagesVariables • What are the values of the variable a and b, after executing each statement. int a, b; a = 3; b = 5; a = a + b; b = b * a;

  21. a b Programming LanguagesVariables • What are the values of the variable a and b, after executing each statement. int a, b; a = 3; b = 5; a = a + b; b = b * a; 3 3 5 8 5 8 40

  22. Programming LanguagesVariables • Variables are the primary way a Java program stores an item of data. • Need a data type and a name. • Examples:

  23. Programming LanguagesVariables • The following is a program expressed as English statements. What would display on the screen? • The variable x starts with the value 0. • The variable y starts with the value 5. • Add 1 to x. • Add 1 to y. • Add x and y, and store the result in y. • Display the value in y on the screen.

  24. Programming LanguagesVariables – Answer! • The following is a program expressed as English statements. What would display on the screen? • The answer is 7!

  25. Live Demos • Instructions: The variable a starts with the value 10. The variable b starts with the value 2. The variable c starts with the value 4. Store the value of a times b in a. Store the value of b times c in c. Add a and c, and store the result in b. Display the value of b on the screen.

  26. Live Demos - Critical Thinking • You tell me:How would we swap the values in variables a, b, c such that:a = b, b = c, c = a?

  27. Live Demos - Critical Thinking • You tell me:How would we swap the values in variables a, b, c such that:a = b, b = c, c = a? Hint: placeholder

  28. Live Demos • Swapping variable values: • How would we write the code: • Given a = 5, b = 8, c = 9 • a = b, b = c, c = a • Print out the proof with System.out.println()

  29. Live Demos • Let’s consider a problem, rather than a list of steps:Fred likes to buy oranges and likes to use Java to write simple programs. Fred is not sure how he would write a program to multiply oranges by their price to plan his orange budget.

  30. Fred’s Oranges • Our problem is this: • Find a way to label the oranges • Find a way to label the price • Perform the multiplication • Inform Fred of his needed orange budget • What do we need from Fred? • The oranges he wants to buy • The price of them • Let’s try and write the output for a given orange count and price

  31. Fred’s Oranges • Break the story down into steps:declare int orangesdeclare double pricedeclare double budgetdisplay the budget to FredAm I missing anything?

  32. Fred’s Oranges – logic errors • Even if it seems obvious, it is always a good idea to state every major step! • Otherwise, we may forget and make an error • Declare and set int orangesDeclare and set double priceDeclare double budgetSet budget = oranges * priceDisplay the budget to Fred

  33. Now let’s write and run the code!

  34. What we just did – secretly the programming process! • Clearly define what the program is to do • Purpose: Calculate Fred’s orange budget • Input: Orange price and number of oranges • Process: Multiply price by the orange number • Output: Display a message with Fred’s budget • Visualize the program running • We wrote it on the board • Use design tools to create a model • Psuedocode in the slides

  35. What we just did – secretly the programming process! • Check the model for logical errors • We forgot to set our variable values • Write the code and compile it • Correct any errors found during compilation, repeat steps 5-6 as needed • Run the program with test data • Correct any runtime errors found, repeat 5-8 as needed • Validate the results

  36. Live Demo – Story • Another problem: Jim is not very smart, because he purchased an overpriced PS4 for $800 from a man of questionable morals. The tax rate for the purchase was 7%. Write pseudocode for a program that displays the total sales price and the sales tax.

  37. Another story, with partners • Jim is still not a very smart. After buying one piece of technology from a man with bad morals, he wants to buy more. Using the programming process discussed, let’s plan how Jim can write a program to figure out how much he needs to spend to buy: • Object a for $600, two of object b for $700, and object c for $50

  38. Programming styleProcedural Programming Older programming languages were procedural. A procedure is a set of programming language statements that, together, perform a specific task. Procedures typically operate on data items that are separate from the procedures. In a procedural program, the data items are commonly passed from one procedure to another.

  39. Procedural Programming Data Element Procedure B Procedure A

  40. Procedural Programming In procedural programming, procedures are developed to operate on the program’s data. Data in the program tends to be global to the entire program. Data formats might change and thus, the procedures that operate on that data must change.

  41. Object-Oriented Programming Object-oriented programming is centered on creating objects rather than procedures. Objectsare a melding of data and procedures that manipulate that data. Data in an object are known as attributes. Procedures in an object are known as methods.

  42. Object-Oriented Programming Object Attributes (data) Methods(behaviors / procedures)

  43. Object-Oriented Programming Object-oriented programming combines data and behavior via encapsulation. Data hidingis the ability of an object to hide data from other objects in the program. Only an object’s methods should be able to directly manipulate its attributes. Other objects are allowed manipulate an object’s attributes via the object’s methods. This indirect access is known as a programming interface.

  44. Object-Oriented Programming Object Attributes (data)typically private to this object Programming Interface Methods(behaviors / procedures) Other objects

  45. How this all happens – hardware and software • Hardwarerefers to the physical components that make up a system • CPU, Main Memory, Storage, Input & Output • Softwareis made up of the programs • The OS -> manage hardware and devices • Applications -> things that are directly useful to the user

  46. CPU output input memory ArithmeticLogicUnit Control Unit ALU CU Computer System Setup

  47. So how do variables work? 0x000 0x001 0x002 0x003 0x004 0x005 The variable hours is a symbolic name for the memory location 0x004. 0x006 0x007 • Variables are simply a name given to represent a place in memory. Assume that this variable declaration has been made. int hours; hours = 40; 40

  48. Main Memory Commonly known as random-access memory (RAM) RAM is divided into units called bytes. Each byte in memory is assigned a unique number known as an address. A byte consists of eight bits that may be either on or off(switch). A byte is made up of 8 bits. 0 1 0 0 1 1 0 1 1 1 1 1 0 0 0 0

More Related