1 / 53

Introduction to Computers and Java

Introduction to Computers and Java. Chapter 1. Introductions. Course Coordinator: Dr. Michael Huhns Instructor: Hongying Du Teaching Assistants Mr. Daniel Pade, Ms. Karina Liles (section 1) Mr. Jiting Xu , Ms. Aniqua Baset (section 2) Supplemental Instruction Mr. Daniel Conner

dixie
Download Presentation

Introduction to Computers and Java

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. Introduction to Computers and Java Chapter 1

  2. Introductions • Course Coordinator: Dr. Michael Huhns • Instructor: Hongying Du • Teaching Assistants • Mr. Daniel Pade, Ms. Karina Liles (section 1) • Mr. Jiting Xu, Ms. Aniqua Baset (section 2) • Supplemental Instruction • Mr. Daniel Conner • SI is available for this course to assist you in better understanding the course material. The SI program provides peer-facilitated study sessions led by qualified and trained undergraduate SI Leaders who attend classes with students and encourage students to practice and discuss course concepts in sessions. Sessions are open to all students who want to improve their understanding of the material, as well as their grades. SI sessions will focus on the most recent material covered in class. Each SILeader holds three sessions per week.

  3. Tools for the Course • Course information is on the web at www.cse.sc.edu/~huhns/csce145http://www.cse.sc.edu/~du5/csce145/index.html • Quiz: every Monday in class

  4. Course Information • Be certain that you read the information at the course website very carefully • Attendance regulations • Late program policy • How grades will be determined • Honor code • Other information available there • Course calendar • Instructor and TA contact information • How to get useful software • Access to the publisher’s website

  5. Grading Rubric—for Lab & Homework Assignments • Each main part will be weighted, for a total of 70%. The comments make up the remaining 30%. • Lab rubrics  • Homework : *2 • Late work is not accepted except under the direst of circumstances. • Illness is not an excuse, and will not normally be considered to allow for make-up work unless the illness keeps the student from coming to the lab on a given day.

  6. Why learning Java?

  7. Why learning Java? • Even non-science major need to do programming sometime • Learning the idea/ways of thinking behind java, could be used in any object-oriented programming

  8. Objectives • Understand program design and object-oriented programming • Learn the Java programming language • Learn Eclipse, a software development environment • Learn JavaScript (some)

  9. Outline • Computer Basics • Designing Programs • A Sip of Java

  10. Computer Basics: Outline • Programs • Programming Languages and Compilers • Java Byte-Code

  11. Running a Program • Figure 1.2

  12. Programming Languages • High-level languages are relatively easy to use • Java, C#, C++, Visual Basic, Python, Ruby. • Unfortunately, computer hardware does not understand high-level languages. • Therefore, a high-level language program must be translated into a low-level language.

  13. Compilers • A compiler translates a program from a high-level language to a low-level language the computer can run. • You compile a program by running the compiler on the high-level-language version of the program called the source program. • Compilers produce machine- or assembly-language programs called object programs.

  14. Compilers, JVM • Most high-level languages: • One compiler for each type of computer and for each operating system. • Java: • A compiler translates a Java program into byte-code, a machine language for a hypothetical computer ( a virtual machine). A byte-code program is easy to translate into machine language for any particular computer. • A program called an interpreter(Java Virtual Machine, JVM) translates each byte-code instruction, executing the resulting machine-language instructions on the particular computer before translating the next byte-code instruction.

  15. Compiling and Running a Program • Figure 1.3

  16. A Sip of Java: Outline • Applications and Applets • A First Java Application Program • Writing, Compiling, and Running a Java Program

  17. Applications and Applets • Two kinds of java programs: applications and applets • Applications • Regular programs • Meant to be run on your computer • Applets • Little applications • Meant to be sent to another location on the internet and run there

  18. A First Java Application • Java program is composed of classes • View Listing 1.1 • class FirstProgram Sample screen output

  19. Object-oriented programming (OOP) • Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). • Objects can perform actionswhich affect themselves and other objects in the world. • Object-oriented programming (OOP) treats a program as a collection of objects that interact by means of actions.

  20. Object-oriented programming (OOP) • Objects, appropriately, are called objects. • Actionsare called methods. • Objects of the same kind have the same type and belong to the same class. • Objects within a class have a common set of methods and the same kinds of data • but each object can have it’s own data values. • e.g. class: dog; objects: Tom’s dog, Kate’s dog etc. methods belong to the dog: run, drink (something the dog could do)

  21. Some Terminology • A packageis a library of classes that have been defined already. • import java.util.Scanner; • The item(s) inside parentheses are called argument(s)and provide the information needed by methods. • A variable is something that can store data. • An instruction to the computer is called a statement; it ends with a semicolon. • The grammar rules for a programming language are called the syntax of the language.

  22. Printing to the Screen • System.out is an object for sending output to the screen. • println is a method to print whatever is in parentheses to the screen. System.out.println (“Whatever you want to print”);

  23. Printing to the Screen • The object performs an action when you invoke or call one of its methods objectName.methodName(argumentsTheMethodNeeds);

  24. Compiling a Java Program or Class • A Java program consists of one or more classes, which must be compiled before running the program. • You need not compile classes that accompany Java (called java library e.g. Systemand Scanner). • Each class should be in a separate file. • The name of the file should be the same as the name of the class.

  25. Compiling and Running • Use an IDE (integrated development environment) which combines a text editor with commands for compiling and running Java programs. We use eclipse! • When a Java program is compiled, the byte-code version of the program has the same name, but the ending is changed from .java to .class.

  26. Compiling and Running • A Java program can involve any number of classes. • The class to run will contain the words public static void main(String[] args) somewhere in the file

  27. Errors • An error in a program is called a bug. • Eliminating errors is called debugging. • Three kinds or errors • Syntax errors • Runtime errors • Logic errors

  28. Syntax Errors • Grammatical mistakes in a program • The grammatical rules for writing a program are very strict • The compiler catches syntax errors and prints an error message. • Example: using a period where a program expects a comma; missing semicolon

  29. Runtime Errors • Errors that are detected when your program is running, but not during compilation • When the computer detects an error, it terminates the program and prints an error message. • Example: attempting to divide by 0

  30. Logic Errors • Errors that are not detected during compilation or while running, but which cause the program to produce incorrect results • Example: an attempt to calculate a Fahrenheit temperature from a Celsius temperature by multiplying by 9/5 and adding 23 instead of 32

  31. Description of class Scanner Package names Class names Software Reuse http://docs.oracle.com/javase/7/docs/

  32. Summary • You have been introduced to program design and object-oriented programming. • You have completed an overview of the Java programming language.

  33. Lab 2 • Program 1Write a program that asks a user to enter a favorite color, a favorite food, a favorite animal, and the first name of a friend or relative. The program should then print the following two lines, with the user's inputs replacing the items in italics: I had a dream that Name ate a Color Animal and said it tasted like Food! • For example, if the user entered blue for the color, hamburger for the food, dog for the animal, and Jake for the person's name, the output would be • I had a dream that Jake ate a blue dog and said it tasted like hamburger! • Don't forget to put the exclamation mark at the end.

  34. The Class String • We've used constants of type String already. "Enter a whole number from 1 to 99." • A value of type String is a • Sequence of characters • Treated as a single item.

  35. String Constants and Variables • Declaring String greeting; greeting = "Hello!"; or String greeting = "Hello!"; or String greeting = new String("Hello!"); • Printing System.out.println(greeting);

  36. Concatenation of Strings • Two strings are concatenated using the + operator. String greeting = "Hello"; String sentence; sentence = greeting + " officer"; System.out.println(sentence); • Any number of strings can be concatenated using the + operator.

  37. Hints • Step 1: print some instruction of the input • Step 2: read input from keyboard • Scanner keyboard = new Scanner(System.in); • String name = keyboard.next(); • //read next token from the input • Step 3: concatenate the strings and print it out

  38. Lab 2 • Program 2Write a program named Yoda that will read a line of text as input and then display the line with the first word moved to the end of the line. For example, a sample interaction with a user might be • Enter a line of text, with no punctuation. • Java is a good language • Yoda has rephrased that line to read: • Is a good language Java • Assume that there is no space before the first word and that the end of the first word is indicated by a blank, not a comma or other punctuation. Note that the new first word must begin with a capital letter. (Hint: for this, you will make use of the methods indexOf and substring.)

  39. Hints • Read the line from keyboard into a String • Find where the first space occurs • Store anything before the space as a string (e.g. s1) and after the space into a string (e.g. s2) • Capitalize the first letter of “after space” part (e.g. s3) • Concatenate s3 and s1 and print it out

  40. StringDemo.java from chapter 2 • public class StringDemo • { public static void main(String[] args) • { String sentence = "Text processing is hard!"; • int position = sentence.indexOf("hard"); • System.out.println(sentence); • System.out.println("012345678901234567890123"); • System.out.println("The word \"hard\" starts at index " • + position); • sentence = sentence.substring(0, position) + "easy!"; • sentence = sentence.toUpperCase(); • System.out.println("The changed string is:"); • System.out.println(sentence); • } • }

  41. The Method length() • The method length() returns an int. • You can use a call to methodlength()anywhere anintcan be used. int count = command.length(); System.out.println("Length is " + command.length()); count = command.length() + 3;

  42. String Indices • Figure 2.4 • Positions start with 0, not 1. • The 'J' in "Java is fun." is in position 0 • A position is referred to an an index. • The 'f' in "Java is fun." is at index 8.

  43. String Methods Figure 2.5b

  44. String Methods Figure 2.5c

  45. String Methods Figure 2.5d (substring: Polymorphism, an Object-oriented programming (OOP) feature)

  46. Object-Oriented Programming • Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). • Objects can perform actionswhich affect themselves and other objects in the world. • Object-oriented programming (OOP) treats a program as a collection of objects that interact by means of actions.

  47. OOP Terminology • Objects, appropriately, are called objects. • Actionsare called methods. • Objects of the same kind have the same type and belong to the same class. • Objects within a class have a common set of methods and the same kinds of data • but each object can have it’s own data values. • e.g.

  48. OOP Design Principles • OOP adheres to three primary design principles: • Encapsulation • Polymorphism • Inheritance

  49. Introduction to Encapsulation • The data and methods associated with any particular class are encapsulated (“put together in a capsule”), but only part of the contents is made accessible. • Encapsulation provides a means of using the class, but it omits the details of how the class works. • Encapsulation often is called information hiding. • e.g.

  50. Introduction to Polymorphism • From the Greek meaning “many forms” • The same program instruction adapts to mean different things in different contexts. • A method name, used as an instruction, produces results that depend on the class of the object that used the method. • e.g.

More Related