Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Java Byte Code l The Java compiler generates Java Byte Code. (Most compilers generate machine language code.) l The Java source code (the code the programmer writes) is stored in a.java file. The byte code is stored in a.class file. l Java byte code can be run on any machine with a Java interpreter – therefore Java is more portable than other language.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 2 Object-Oriented Programming: OOP l A design and programming technique l Some terminology: »object - usually a person, place or thing (a noun) »method - an action performed by an object (a verb) »type or class - a category of similar objects (such as automobiles) l Objects have both data and methods l Objects of the same class have the same data elements and methods l Objects send and receive messages to invoke actions
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 3 Design Principles of OOP Three main design principles of Object- Oriented Programming(OOP): l Encapsulation l Polymorphism l Inheritance
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 4 Encapsulation l Encapsulation means to design, produce, and describe software so that it can be easily used without knowing the details of how it works. l Also known as information hiding l Class example: Driving a car l Any other examples?
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 5 Encapsulation l Additional examples: »Elevator: When you push a button, you go to the right floor – you don’t need to know how the pulleys, lights, etc. work. »A software example: sorting a list of numbers –Another programmer would give your program a list of numbers and your program would return the list sorted. They do not need to know how your program sorted the numbers.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 6 Polymorphism l Polymorphism—the same word or phrase can mean different things in different contexts l Analogy: in English, bank can mean side of a river or a place to put money Class example: an output method in different classes. l Any other examples?
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 7 Polymorphism l Additional examples »If someone said “Go play your favorite sport”, some would play basketball, some football... »Two classes, Cat and Dog, may both have methods called makeNoise, but they would output different things.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 8 Inheritance l Inheritance—a way of organizing classes l Term comes from inheritance of traits like eye color, hair color, and so on. l Classes with properties in common can be grouped so that their common properties are only defined once. l Class example: vehicle hierarchy l Any other examples?
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 9 Inheritance l Additional examples »For a university program: Person FacultyStaffStudent UndergraduateGraduateCleaning StaffDept. ChairAssistant Prof.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 10 Types of Errors l Syntax, Run-Time, and Logic l Syntax Errors: »Occurs if your program is not a valid Java program. »E.g. Typing “rturn” instead of “return” »Caught by the compiler – a compile-time error
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 11 Run-Time Errors l An execution error (during run-time) l Not always so easy to fix l Error message may or may not be helpful l Not detected by the compiler but is detected by the run-time system. Example: Division by zero - if your program attempts to divide by zero it automatically terminates and prints an error message. Note: May not happen every time you run the program!
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 12 Logic Errors Just because it compiles and runs without getting an error message does not mean the code is correct! l An error in the design (the algorithm) or its implementation »code compiles without errors »no run-time error messages »but incorrect action or data occurs during execution l Generally the most difficult to find and fix l Need to be alert and test thoroughly »think about test cases and predict results before executing the code
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 13 Logic Error Examples l Algorithm Error: »averageOfFiveScores = SumOfScores/2; (should divide by 5) l Implementation Error: »typed in wrong symbol in source code - sum = a - b; (should be sum = a + b; )
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 14 Finally! Now, a taste of Java! History l James Gosling, Sun Microsystems, Inc. l originally a language for programming home appliances l later (1994) used for World Wide Web applications (since byte code can be downloaded and run without compiling it) l eventually used as a general-purpose programming language (for the same reason as above plus it is object- oriented) l Why the name “Java”? Not sure - it may just be a name that came during a coffee break and it had not been copyrighted, yet.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 15 Applets vs. Java Applications l Applets »Java programs intended to be downloaded via the Web and run immediately »“little applications” »requires a web browser l Applications »Java programs intended to be installed then run »often larger applications l Slightly different programming for each, but both are easy to do
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 16 public class FirstProgram { public static void main(String[] args) { System.out.println("Hello out there."); System.out.println("Want to talk some more?"); System.out.println("Answer y for yes or n for no."); char answerLetter; answerLetter = SavitchIn.readLineNonwhiteChar(); if (answerLetter == 'y') System.out.println("Nice weather we are having."); System.out.println("Good-bye."); System.out.println("Press enter key to end..."); String junk; junk = SavitchIn.readLine(); } A Sample Java Program
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 17 Explanation of Code... l Code to begin the program (to be explained later): public class FirstProgram { public static void main(String[ ] args) { l Java applications all have similar code at the beginning »The name of the class differs from one program to another. »Other information about the class might also be included on the first line.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 18 Explanation of Code... l Code to display a text string: System.out.println("Hello out there."); System.out.println("Want to talk some more?"); System.out.println("Answer y for yes or n for no."); »Note the “dot” operator »System.out is an object »println is a method that it invokes »double-quoted text inside the parentheses is an argument to the method »general syntax: Object_Name.Method_Name(Arguments)
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 19 … Explanation of Code... Code to create a variable named answerLetter to contain a single character of data: char answerLetter; l This variable is used to store the user’s response.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 20 … Explanation of Code... Read a character typed in from the keyboard and store it in the variable answerLetter : answerLetter = SavitchIn.readLineNonwhiteChar(); »SavitchIn is a class used for obtaining input from the keyboard »readLineNonwhiteChar() is a method that reads a single, non-blank character from the keyboard and discards any remaining characters on the line. »the equal sign is not the same as in math; it means “assign the value on the right to the variable on the left;” in this case, store the value read from the keyboard into the variable answerLetter
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 21 … Explanation of Code... Question: If “=“ means “assign the value of the expression on the right to the variable on the left,” how do we indicate “equals”? Answer: use a double equals (“==“) Example: check to see if the character entered is ‘y’: if (answerLetter == 'y') »the value inside the parentheses will be True if the letter ‘y’ was typed in, otherwise it will be False (if any other letter was typed in)
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 22 … Explanation of Code... l Code to display the line “Nice weather we are having.” if the user entered the character ‘y’: if (answerLetter == 'y') System.out.println("Nice weather we are having."); »Note that the line will not be printed if any letter other than ‘y’ is entered. l Unconditionally display the line “Good-bye.”: System.out.println("Good-bye."); »only the previous System.out.println is conditionally printed, depending on the value entered; the next instruction is executed regardless of the value entered.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 23 … Explanation of Code l Code to prevent the display from scrolling off the screen before you can read it: System.out.println("Press enter key to end program."); String junk; junk = SavitchIn.readLine(); »junk is a variable that can contain a string of characters. »readLine() is a method to read in an entire line of text. »The program halts until a character is entered. »Any character entered will make the program continue. »The character entered is assigned to the variable junk, but is ignored (it is not used). »There are no more lines of code, so the program terminates.