Lecture 1: Prerequisites

Embed Size (px)


These notes are intended for use by students in CS0401 at the University of Pittsburgh and no one else These notes are provided free of charge and may not be sold in any shape or form Material from these notes is obtained from various sources, including, but not limited to, the following: - PowerPoint PPT Presentation

Text of Lecture 1: Prerequisites

PowerPoint Presentation

Course Notes forCS 0401Intermediate Programming (with Java)

ByJohn C. RamirezDepartment of Computer ScienceUniversity of Pittsburgh2These notes are intended for use by students in CS0401 at the University of Pittsburgh and no one elseThese notes are provided free of charge and may not be sold in any shape or formMaterial from these notes is obtained from various sources, including, but not limited to, the following:Starting Out with Java, From Control Structures through Objects, Third to Sixth Editions by GaddisJava Software Solutions, Fourth and Fifth Editions by Lewis and LoftusJava By Dissection by Pohl and McDowellThe Java Tutorial (click for link)The Java tech home page and its many sub-links:http://www.oracle.com/technetwork/java/index.html 3Lecture 1: PrerequisitesStudents taking CS401 should already have some programming background:Previous experience with Java (ex: CS 0007) is recommended, but Python, C, C++ and VB are also acceptableConcepts that you are expected to be familiar with and have used in programs include: Basic program structure and syntaxHow do we build programs and how do we get them to runPrimitive types and expressionsNumbers, characters, operators, precedence34Lecture 1: PrerequisitesControl Statements and DecisionsBoolean expressionsif and switch (or case) statementsLoops (for and while)Methods (or functions) and parametersCalling methods and flow of executionArguments and parametersArrays and their usesOne-dimensional onlyIf you do not have this background, you should consider taking CS 0007 before taking CS0401First Response QuestionSecond Response Question45Lecture 1: Goals of the CourseGoals for CS 0401 Course:To (quickly) cover the basics of the Java language (including items mentioned in the previous slide)These will be covered more from a Java implementa-tion point of view than from a conceptual point of viewYou should already be familiar with (most of) the concepts, so learning the Java implementations should be fairly straightforwardAlso will touch on the foundations of object-oriented programmingThis includes Chapters 1-5 of the Gaddis textThose who have had CS 0007 should consider this to be an extended review!6Lecture 1: Goals of CourseTo learn the principles of object-oriented programming and to see Java from an object-oriented point of viewObjects, methods and instance variablesReferences and their implicationsCreating new classesSyntax and logic requiredInheritance and compositionBuilding new classes from old classesPolymorphism and dynamic bindingAccessing different objects in a uniform wayChapters 6, 8-10 of GaddisWe will focus a lot of attention on these chapters7Lecture 1: Goals of CourseNote that we are covering OOP concepts using Java as our languageHowever, the general principles of object-oriented programming apply to any object-oriented languageEx: C++, Objective-C, C#, Smalltalk, etc.The more important goal here is to learn to program effectively in an object-oriented wayUnderstand why it is good and how to do it8Lecture 1: Goals of CourseTo cover additional useful programming techniques and features of Java in order to become proficient programmers (using the Java language)Array use and algorithms (sorting, searching) (Chapter 7)Reading and Writing Files (Chapters 4, 11 + Notes)Exception Handling (Chapter 11)Graphical User Interfaces and Applications (Chapters 12, 13, 14, 15)Introduction to recursion (Chapter 16)Third Response Question89Lecture 2: Why Java?JavaJava is an interpreted, platform-independent, object-oriented languageInterpreted, platform-independent: Source .java code is compiled into intermediate (byte) codeByte code is executed in software via another program called an interpreterBenefits: More safety features and run-time checks can be built into the language discussCode can be platform-independentAs long as the correct interpreter is installed, the same byte code can be executed on any platform910Lecture 2: Why Java?JavaSourceCode(.java)Java CompilerJavaByteCode(.class)JRE for WindowsJRE for SolarisJRE for MacJRE for LinuxProgram ExecutionThe same .class file can execute on any platform, as long as the JRE is installed there1011Lecture 2: Why Java?Drawback:Interpreted code executes more slowly than regular compiled codeSince program is run in software rather than hardware, it cannot match the execution times of code that is compiled for specific hardwareEx: C, C++ codeNo language is best for every applicationHowever, Java implementations can use JIT compilation of bytecode to execute fasterObject-orientedPrimary mode of execution is interaction of objects with each otherWe will discuss object-oriented programming in much more detail soon12Lecture 2: Getting Started with JavaHow do we execute Java programs?First we must compile our source (.java) code into the intermediate (.class) codeWe do this with the Java Compiler javac programNext we must interpret our .class code to see the resultWe do this with the Java Interpreter, or Java Run-time Environment (JRE)java program 12Demonstrate compiling and execution example, as well as platform independence13Lecture 2: Getting Started with JavaBoth programs come with the Java Development Kit (JDK)This is installed on all of the lab PCs other CS machinesThe most recent version (SE 8) can be easily downloaded and installed from the Oracle Web site:http://www.oracle.com/technetwork/java/index.html It is free!More on the basics of using the Java software development kit is shown in Lab 1Look for it online soon -- you will do it next weekBut lets look at an ex. and talk more about Java basicsSee ex1.java Carefully read the comments!14Lecture 2: Getting Started with JavaWhen you have a chance, try the following:Download ex1.java from the Web site onto a PC that has the JDK installed (yours or a lab PC)Open a terminal (command prompt) windowChange to the correct directoryCompile the program: javac ex1.javaExecute the program: java ex1Adding the .class extension is optional it is assumed even if you dont put it thereShow the directory to see that the .class file is now thereAlso try the same thing from one of the Lab workstations during your first lab session15Lecture 2: Getting Started with JavaNote: Most developers use an IDE (integrated development environment) for program devel.Here are two possibilities:http://www.netbeans.org/ http://www.eclipse.org/ Both are available freeThese allow you to edit, compile and debug Java programs in an easy, integrated wayHowever, you should realize that the final program does NOT depend on the IDE, and you should be able to compile and run Java programs without the IDEI will not be emphasizing these in lecture, but you are free to use one if you wishLecture 2: Java BasicsWhat fundamental entities / abilities do we need for any useful Java program?A way to get data into and out of our programI/OA way to create / name / variables and constants to store our dataIdentifiers and variablesA way to manipulate / operate on the dataStatements and ExpressionsA way to make decisions and control our flow of executionControl structures

16Lecture 2: Java Basics I/O(I)/O (we will defer input until after we discuss variables)Java has a predefined object called System.outThis object has the ability to output data to the standard output stream, which is usually the console (display)This ability is via methods (procedures)Ex: print, printlnWe pass information to the System.out object through methods and parameters, and the information is then shown on the displayFor example:System.out.println(Hello Java Students!);17Lecture 2: Java Basics I/O We can output strings, values of variables and expressions and other information using System.outWe will see more on this once we discuss variablesWe will understand how System.out works more precisely after we have discussed classes and objects later in the term1819Lecture 2: Java Basics Identifiers and VariablesLexical elements groups of characters used in program codeThese form all of the parts of the program codeEx: keywords, identifiers, literals, delimitersWe will discuss some of these in the Java languageKeywordsLexical elements that have a special, predefined meaning in the languageCannot be redefined or used in any other way in a programEx: program, if, class, throwsSee p. 10 in Gaddis for complete list

20Lecture 2: Java Basics Identifiers and VariablesPredefined IdentifiersIdentifiers that were written as part of some class / package that are already integrated into the languageEx: System, Applet, JFrame class namesEx: println, start, close method namesEx: E, PI constant names Programmers can use these within the context in which they are definedIn Java there are a LOT because Java has a large predefined class library

Lecture 2: Java Basics Identifiers and Variables Other IdentifiersDefined by programmerused to represent names of variables, methods, classes, etcCannot be keywordsWe could redefine predefined identifiers if we wanted to, but this is generally not a good ideaJava IDs must begin with a letter, followed by any number of letters, digits, _ (underscore) or $ charactersSimilar to identifier rules in most programming langs

2122Lecture 2: Java Basics Identifiers and VariablsImportant Note:Java identifiers are case-sensitive this means that upper and lower case letters are considered to be different be careful to be consistent!Ex: ThisVariable and thisvariable are NOT the sameNaming Convention:Many Java programmers use the following conventions:Classes: start with upper case, then start each word with an upper case letterEx: StringBuffer, BufferedInputStream, ArrayIndexOutOfBoundsExceptionMethods and variables: start with lower case, then start each word with an upper case letterEx: compareTo, lastIndexOf, mousePressedShow Question 12223Lecture 2: Java Basics Identifiers and VariablesVariablesMemory locations that are associated with identifi