Week-1.1 - Tagged.pdf
Document Details
Uploaded by AbundantBaritoneSaxophone
King Faisal University
Tags
Full Transcript
Chapter 1 Introduction to Object Technology and Java ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Chapter 1 Objectives In this chapter, you will learn: The different types of p...
Chapter 1 Introduction to Object Technology and Java ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Chapter 1 Objectives In this chapter, you will learn: The different types of programming languages Basic object-technology concepts A typical Java program-development environment To write simple Java application To use different output statements ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Chapter 1 Content Instructor Information Course Information Course Objectives Introduction to Object Technology Programming Languages Java and a Typical Java Development Environment Your first program in Java: Printing a line of text Modifying your first Java program Displaying Text with printf. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Instructor Information Name: Hafiz Farooq Ahmad Dr Office Hours: Sun, Wed ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Self Introduction PhD – Tokyo Institute of Technology, Japan 2002 Comtec Japan – Specialist Engineer May 2002- May 2008 DTS Japan – Consultant Engineer- Jun 2010-April 2011 NUST SEECS Ass/Associate Prof- Oct 2002-May 2013 CCSIT KFU Associate Prof- June 2013- present HL7 Chair Pakistan (HL7 International) Software Engineering SAGE- a large project – international recognition- 2nd position at AAMAS Netherlands 2005 HL7 Middleware ICT RD Funds (2008-2011) SWAF- ICT R&D Funds (2010-2013) SEMR - ICT R&D Funds(2013-215) Won a number of research funding project from DSR KFU over the past 5 years Introduction Java Importance Java was Free (2019), Oracle license Java is Platform Independent Better Community Support Java has rich APIs and Frameworks Java is good choice for Mobile Apps Java jobs and Certifications Certified individuals usually earn more money than their non-certified peers. https://www.siliconrepublic.com/portfolio/2015/01/15/demand-for-it-staff-up-alm ost-a-third-as-irish-employers-look-beyond-eu-to-fill-roles https://gooroo.io/analytics/skill/Java/united-states#.VdiFEqjoSWE Some of the certifications of Java are; Certified Java Associate Certified Java Programmer Certified Java Developer http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=632 6 Introduction Some of the certifications of Java are; Certified Java Associate Certified Java Programmer Certified Java Developer http://education.oracle.com/pls/web_prod-plq-dad/ db_pages.getpage?page_id=632 7 Course Information Object Oriented Programming 2 Quizzes allocate 10% 2 Assignments allocate 10% Lab work allocates 20% Mid-term exam allocates 20% Final exam allocates 40% ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Course Objectives The purpose of this course is to provide students with concepts and practical introduction of object-oriented programming (OOP) paradigm. Briefly describe any plans for developing and improving the course that are being implemented. (e.g. increased use of IT or web based reference material, changes in content as a result of new research in the field) ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Introduction Java is the world’s most widely used computer programming language. You’ll learn to write instructions commanding computers to perform tasks. Software (i.e., the instructions you write) controls hardware (i.e., computers). You’ll learn object-oriented programming—today’s key programming methodology. You’ll create and work with many software objects in this text. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Introduction (Cont.) Java is the preferred language for meeting many organizations’ enterprise programming needs. Java has become the language of choice for implementing Internet- based applications and software for devices that communicate over a network. In use today are more than a billion general-purpose computers and billions more Java-enabled cell phones, smartphones and handheld devices (such as tablet computers). ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Introduction to Object Technology The Automobile as an Object Let’s begin with a simple analogy. Suppose you want to drive a car and make it go faster by pressing its accelerator pedal. Before you can drive a car, someone has to design it. A car typically begins as engineering drawings, similar to the blueprints that describe the design of a house. Drawings include the design for an accelerator pedal. Pedal hides from the driver the complex mechanisms that actually make the car go faster, just as the brake pedal hides the mechanisms that slow the car, and the steering wheel “hides” the mechanisms that turn the car. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Introduction to Object Technology (Cont.) Enables people with little or no knowledge of how engines, braking and steering mechanisms work to drive a car easily. Before you can drive a car, it must be built from the engineering drawings that describe it. A completed car has an actual accelerator pedal to make the car go faster, but even that’s not enough—the car won’t accelerate on its own (hopefully!), so the driver must press the pedal to accelerate the car. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Introduction to Object Technology (Cont.) Methods and Classes Performing a task in a program requires a method. The method houses the program statements that actually perform its tasks. Hides these statements from its user, just as the accelerator pedal of a car hides from the driver the mechanisms of making the car go faster. In Java, we create a program unit called a class to house the set of methods that perform the class’s tasks. A class is similar in concept to a car’s engineering drawings, which house the design of an accelerator pedal, steering wheel, and so on. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Introduction to Object Technology (Cont.) Reuse Just as a car’s engineering drawings can be reused many times to build many cars, you can reuse a class many times to build many objects. Reuse of existing classes when building new classes and programs saves time and effort. Reuse also helps you build more reliable and effective systems, because existing classes and components often have gone through extensive testing, debugging and performance tuning. Just as the notion of interchangeable parts was crucial to the Industrial Revolution, reusable classes are crucial to the software revolution that has been spurred by object technology. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Introduction to Object Technology (Cont.) Attributes and Instance Variables A car has attributes Color, its number of doors, the amount of gas in its tank, its current speed and its record of total miles driven (i.e., its odometer reading). The car’s attributes are represented as part of its design in its engineering diagrams. Every car maintains its own attributes. Each car knows how much gas is in its own gas tank, but not how much is in the tanks of other cars. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Introduction to Object Technology (Cont.) An object, has attributes that it carries along as it’s used in a program. Specified as part of the object’s class. A bank account object has a balance attribute that represents the amount of money in the account. Each bank account object knows the balance in the account it represents, but not the balances of the other accounts in the bank. Attributes are specified by the class’s instance variables. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Programming Languages ©1992-2012 by Pearson Education, Inc. All Rights Reserved. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Java and a Typical Java Development Environment Key goal of Java is to be able to write programs that will run on a great variety of computer systems and computer-control devices. This is sometimes called “write once, run anywhere.” Java garnered the attention of the business community because of the phenomenal interest in the web. Java is used to develop large-scale enterprise applications, to enhance the functionality of web servers, to provide applications for consumer devices and for many other purposes. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Java and a Typical Java Development Environment (Cont.) Java is the most widely used software development language in the world. Java Class Libraries Rich collections of existing classes and methods Also known as the Java APIs (Application Programming Interfaces). ©1992-2012 by Pearson Education, Inc. All Rights Reserved. ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Java and a Typical Java Development Environment (Cont.) Integrated development environments (IDEs) Provide tools that support the software development process, including editors for writing and editing programs and debuggers for locating logic errors—errors that cause programs to execute incorrectly. Popular IDEs Eclipse (www.eclipse.org) NetBeans (www.netbeans.org). jGRASP™ IDE (www.jgrasp.org) DrJava IDE (www.drjava.org/download.shtml) BlueJ IDE (www.bluej.org/) TextPad® Text Editor for Windows® (www.textpad.com/) ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Java and a Typical Java Development Environment (Cont.) CPU does not understand Java. Machinecode consists of 0s and 1s. Java is a high programming language (OOP): It consist of instructions in a ‘readable’ language. So Java-program must be converted to machinecode using a compiler. Some diffeculties face the exeuction of this conversion. 1) Each CPU uses its own machinecode. 2) Each PC has its own platform (CPU + Operating System). The mentioned problems become more complex due to: 1. Different versions of the same operating system (Window 98, 2000, VISTA, 7, etc..., Linux, Linux Red Hat,Unix,etc...). 2. Network(s). As each network may contain different types of computers (Sun, Apple and PC with Intel or AMD processor). Java and a Typical Java Development Environment (Cont.) 0 iconst 0 1 istore R1 To solve the mentioned problems: 2 aload R0 3 arraylength Java-program is translated to an in-between language 4 istore R2 called Java-bytecode. 5 goto 14 Programma in Java 8 iload R1 int sum(int array [] ) 9 aload R0 10 iload R2 {int som = 0; 11 iaload for (int i= array.length; i >=0; 12 iadd i--) 13 istore R1 som += array[i]; 14 iinc R2 - 1 return som; 17 iload R2 } 18 ifge 8 21 iload R1 ©1992-2012 by Pearson Education, Inc. All Rights 22 ireturn Reserved. Java and a Typical Java Development Environment (Cont.) Java Virtual Machine (JVM) is a program that can make bytecode understanable to any processor. JVM is developed by Sun. JVM is free to download from Internet. JVM is NOT platform indepentant Our First Program in Java: Printing a Line of Text Java application A computer program that executes when you use the java command to launch the Java Virtual Machine (JVM). Sample program in next slid displays a line of text. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Our First Program in Java: Printing a Line of Text (Cont.) Comments // Fig. 2.1: Welcome1.java // indicates that the line is a comment. Used to document programs and improve their readability. Compiler ignores comments. A comment that begins with // is an end-of-line comment—it terminates at the end of the line on which it appears. Traditional comment, can be spread over several lines as in This type of comment begins with. All text between the delimiters is ignored by the compiler. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Our First Program in Java: Printing a Line of Text (Cont.) Javadoc comments Delimited by. All text between the Javadoc comment delimiters is ignored by the compiler. Enable you to embed program documentation directly in your programs. The javadoc utility program (Appendix M) reads Javadoc comments and uses them to prepare your program’s documentation in HTML format. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Our First Program in Java: Printing a Line of Text (Cont.) Blank lines and space characters Make programs easier to read. Blank lines, spaces and tabs are known as white space (or whitespace). White space is ignored by the compiler. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Our First Program in Java: Printing a Line of Text (Cont.) Class declaration public class Welcome1 Every Java program consists of at least one class that you define. class keyword introduces a class declaration and is immediately followed by the class name. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Our First Program in Java: Printing a Line of Text (Cont.) Class names By convention, begin with a capital letter and capitalize the first letter of each word they include (e.g., SampleClassName). A class name is an identifier—a series of characters consisting of letters, digits, underscores (_) and dollar signs ($) that does not begin with a digit and does not contain spaces. Java is case sensitive—uppercase and lowercase letters are distinct—so a1 and A1 are different (but both valid) identifiers. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Our First Program in Java: Printing a Line of Text (Cont.) Braces A left brace, {, begins the body of every class declaration. A corresponding right brace, }, must end each class declaration. Code between braces should be indented. This indentation is one of the spacing conventions mentioned earlier. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Our First Program in Java: Printing a Line of Text (Cont.) Declaring the main Method public static void main( String[] args ) Starting point of every Java application. Parentheses after the identifier main indicate that it’s a program building block called a method. Java class declarations normally contain one or more methods. main must be defined as shown; otherwise, the JVM will not execute the application. Methods perform tasks and can return information when they complete their tasks. Keyword void indicates that this method will not return any information. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Our First Program in Java: Printing a Line of Text (Cont.) Body of the method declaration Enclosed in left and right braces. Statement System.out.println("Welcome to Java Programming!"); Instructs the computer to perform an action Print the string of characters contained between the double quotation marks. A string is sometimes called a character string or a string literal. White-space characters in strings are not ignored by the compiler. Strings cannot span multiple lines of code. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Our First Program in Java: Printing a Line of Text (Cont.) System.out object Standard output object. Allows Java applications to display strings in the command window from which the Java application executes. System.out.println method Displays (or prints) a line of text in the command window. The string in the parentheses the argument to the method. Positions the output cursor at the beginning of the next line in the command window. Most statements end with a semicolon. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Modifying Your First Java Program Class Welcome2, shown in Fig. 2.3, uses two statements to produce the same output as that shown in previews code. New and key features in each code listing are highlighted. System.out’s method print displays a string. Unlike println, print does not position the output cursor at the beginning of the next line in the command window. The next character the program displays will appear immediately after the last character that print displays. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Modifying Your First Java Program (Cont.) Newline characters indicate to System.out’s print and println methods when to position the output cursor at the beginning of the next line in the command window. Newline characters are white-space characters. The backslash (\) is called an escape character. Indicates a “special character” Backslash is combined with the next character to form an escape sequence. The escape sequence \n represents the newline character. Complete list of escape sequences java.sun.com/docs/books/jls/third_edition/html/ lexical.html#3.10.6. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Displaying Text with printf System.out.printf method f means “formatted” displays formatted data Multiple method arguments are placed in a comma-separated list. Java allows large statements to be split over many lines. Cannot split a statement in the middle of an identifier or string. Method printf’s first argument is a format string May consist of fixed text and format specifiers. Fixed text is output as it would be by print or println. Each format specifier is a placeholder for a value and specifies the type of data to output. Format specifiers begin with a percent sign (%) and are followed by a character that represents the data type. Format specifier %s is a placeholder for a string. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Thank You ! Reading Paul Deitel and Harvey Deitel, “Java How to Program Early Objects”, 11th Edition, 2018, Prentice Hall. ISBN-13: 978-0134743356. 1.5 1.8 1.9 © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.