Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1: Introduction to Computers and Java

Similar presentations


Presentation on theme: "Chapter 1: Introduction to Computers and Java"— Presentation transcript:

1 Chapter 1: Introduction to Computers and Java

2 Agenda Introductions Course Administration PB sandwich demo
Computer Hardware Machine language and compiled languages The Java Virtual Machine Common features of programming languages Variables and Algorithms Java “Hello World” Demo Programming paradigms

3 Welcome to CSIS10A (5 mins)
Typical format for class meetings Lectures on Tuesday (monitors off, notebooks out) Do Pre-Lab before Thursday Thursday Lab, practice new material Syllabus Aim of this course (learning Java) What is a program? A set of step-by-step instructions that directs a computer to solve some problem. Pretty much anything you can do with a computer, you can do by programming in Java

4 Do introductions (15 mins)
class website: Passcode for website: Click at bottom on CSIS10A Guest Book Name Major and what year Why taking the course Something unique about yourself then we go around and tell each other

5 Do administrative stuff (10 mins)
Few if any handouts will be printed. They will be posted on the web instead. No late work (model solutions, quick turnaround) For conflicts with class meetings: let me know now! We will use the textbook Starting Out with Java by Gaddis (4th or 5th edition OK) A word about Academic Honesty How to get help Computer science tutor will be available – Steve Bruemmer our instructional tech, 9-5 most days

6 Warning – there is MATH in this class!!
If you didn’t like math (algebra), you probably will have a hard time in this class Solve for x: x + 3 = 13 What is 8 squared? Square root of 36 Area of a circle of radius 2: Average of 10, 14, 8, 4 Convert 3 kg into lbs Convert 43 ounces into pounds and ounces What is 15% of $30.00 ?

7 The Wise Approach If the class seems too hard for you
enroll in the advisory courses first like MATH263, ENGL111/112, CSIS1 then come back to CSIS10A A solid foundation in English and Math will prepare you for much of what you will discover at MPC and beyond! Java has a number of advanced concepts CSIS1 provides a gentler programming intro

8 Do PB & J demonstration (20 mins)
Get into groups of 3 and write up instructions (10 mins) Present the results and have me follow those directions (10 mins)

9 PBJ Debrief Talk about observations; similarities to programming
Explicitness Sequence, selection, repetition Dependencies - when does order matter? Closing open files and stuff What is a basic command? Unexpected uses of software Infinite loops

10 Course will be challenging, fun, painful at times - much like making a sandwich
But, you will make yourself more marketable or may get sold on computer science!

11 Basic computer anatomy

12 What the parts do CPU Storage Input devices (the ``I'' in ``I/O'')
``The brain''; performs relatively basic operations It only executes machine language The machine language varies from CPU to CPU Storage Primary storage/random-access memory/RAM/``memory'' Fast, but volatile and expensive Secondary storage/hard drive/hard disk Cheap and non-volatile, but slow Input devices (the ``I'' in ``I/O'') Mouse, keyboard Output devices (the ``O'' in ``I/O'') Monitor, speakers, printer

13 How hardware relates to programming
Your .java program files are files, therefore found on the hard drive (typically) Programs need to run fast, so the computer uses primary memory for programs that are currently running. There are ways to communicate with I/O devices in Java.

14 Communicating instructions to the computer
Problem: We want to give instructions to the CPU, but the CPU's language is hard for humans to read, write, and understand. How do we handle this? One (painful) solution: Just program in machine code Why is this so painful? Just note this C code: main() { printf("hello, world"); } does the same thing as the machine code on the next slide. Since machine code depends on the CPU, the program would only work on a limited number of machines anyway

15 "Hello World" program in machine code

16 One (bad) solution: Try to make a system where we can just type in instructions with natural language, and have the computer translate that into machine code. Extremely difficult Very ambiguous; for example, in "I called the guy with the cell phone'' - who is carrying the cell phone?

17 One (better) solution: Use an intermediate language that is somewhat easy for humans to use (i.e. Pascal, C++, etc.), and have the computer translate that into machine code. This translator is called a compiler. Compiled languages such as C work in this way.

18

19 Running programs the Java way
In Java, the computer simulates an imaginary CPU with very particular specifications. This imaginary CPU is called the Java virtual machine (or JVM). Java simulates the JVM much like an emulator simulates a Super-Nintendo Main advantage: The same code can be made to run on any machine without modification (see previous slide). Example: Good for Java applets on the web

20 Running programs the Java way
Another advantage: Much better security can be implemented when running code in a virtual machine. Example: again, crucial for Java applets on the web One disadvantage: Simulating the JVM makes the program run slower One caveat: There can actually be variations in your program from machine to machine, for a number of reasons Different I/O devices Different library implementations (or bugs) Bugs in a JVM implementation (yes, it happens)

21 Source file  compiler  bytecode
The JVM's language is bytecode, which is found inside .class files. The compiler converts java files into class files Class files are not human readable See the bytecode for the HelloWorld.java program

22 Java source file public class HelloWorld {     public static void main(String[] args) {         System.out.println("Hello, World!");     } }

23 Java Bytecode for previous slide

24 A source file contains source code and is really just a simple text file. It contains (among other things) instructions for the computer to execute. A ``.java'' ending is used to distinguish it as a Java source file Java files have special structure so that the computer can translate it into machine code. Use a text editor or an IDE (in this class we use BlueJ but you can also use Eclipse) to create source and make changes to it. We'll make a simple "Hello World" program now!

25 10 min Break then Demo Pair Programming (Show Video)
Pair up with the person sitting next to you One person types the other “steers” (checks for errors) Use BlueJ to write, compile, and run The “Hello World” program

26 Programming Languages
New High level programming languages are being created all the time, to fill different tasks Some common programming languages: Java C Visual Basic BASIC C++ Python COBOL C# Ruby Pascal PHP JavaScript

27 Programming Languages Common Language Elements
There are some concepts that are common to virtually all programming languages. Common concepts: Key words Operators Punctuation Programmer-defined identifiers Strict syntactic rules.

28 Programming Languages Sample Program
public class HelloWorld { public static void main(String[] args) System.out.println("Hello World"); }

29 Programming Languages Sample Program
Key words in the sample program are: Key words are lower case (Java is a case sensitive language). Key words cannot be used as a programmer-defined identifier. public class static void

30 Programming Languages
Semi-colons are used to end Java statements; however, not all lines of a Java program end a statement. Part of learning Java is to learn where to properly use the punctuation.

31 Programming Languages Lines vs Statements
There are differences between lines and statements when discussing source code. System.out.println( "Hello World"); 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.

32 Programming Languages Variables
Data in a Java program is stored in memory. Variable names represent a location in memory. Variables in Java are sometimes called fields. Variables are created by the programmer who assigns it a programmer-defined identifier. example: int hours = 40; In this example, the variable hours is created as an integer (more on this later) and assigned the value of 40.

33 Programming Languages Variables
Variables are simply a name given to represent a place in memory. 0x000 0x001 0x002 0x003 0x004 0x005 0x006 0x007

34 Programming Languages Variables
72 Assume that this variable declaration has been made. int length = 72; The variable length is a symbolic name for the memory location 0x003. 0x000 0x001 0x002 0x003 0x004 0x005 0x006 0x007 The Java Virtual Machine (JVM) actually decides where the value will be placed in memory.

35 Variables and println The System.out.println(); command can print out text in quotes, like "Hello World“ Or it can print the current value of a variable: System.out.println( length ); Would print on the screen Or it can print text and a value: System.out.println("Length = " length ); Would print Length = on the screen

36 The value in a variable can change!
If we add these statements after the previous length = 24; System.out.println(length); We will now see 24 on the screen The value stored in the length variable If we add the following, what will be displayed? length = length - 15;

37 Is your console output screen getting longer?
You can add an “escape character” to erase the results of previous program executions Do this at the top of your program System.out.println("\f"); Means “flush (or erase) the console screen” Put only once near the top, the first thing printed You can even put \f in front of the H in Hello: System.out.println("\fHello World");

38 Add to your “Hello World” demo
Create a variable to represent your age int age = 23; Write a statement to print “My age is ” and the value in age Write a statement to add the number 10 to your age variable Write a statement to print “In ten years I will be ” and the current value of age

39 The Programming Process
1. Clearly define what the program is to do. purpose, data in, processing, desired output 2. Visualize the program running on the computer. 3. Use design tools to create a model of the program. Pseudocode—cross between human/computer language 4. Check the model for logical errors.

40 The Programming Process
5. Enter the code and compile it. 6. Correct any errors found during compilation. Repeat Steps 5 and 6 as many times as necessary. 7. Run the program with test data for input. 8. Correct any runtime errors found while running the program. Repeat Steps 5 through 8 as many times as necessary. 9. Validate the results of the program.

41 Two Programming Paradigms
Historically, there have been two approaches to developing complex programming projects Procedural Programming Object Oriented Programming Both are still in use today, altho OOP is favored We will be exposed to both of these paradigms in this class

42 Procedural 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.

43 Procedural Programming
Data Element Procedure B Procedure A

44 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. Procedural programming can be harder to maintain than OOP.

45 Object-Oriented Programming
Object-oriented programming is centered on creating objects rather than procedures. Objects are 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.

46 Object-Oriented Programming
Attributes (data) Methods (behaviors / procedures) Physical Analogy: your iPod My iPod Attributes(data) playlist volume setting current track Methods play skip shuffle

47 Object-Oriented Programming
Object-oriented programming combines data and behavior via encapsulation. Data hiding is 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.

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

49 To prepare for Lab1, please begin prelab1
For full credit, due by Midnight Wednesday Start now! Visit the website and click on the link. You can solve some, go home, solve others. When done, click “done” then “submit” to turn in Each problem has hard and easy mode. Hard: worth 1.1 pts Easy: worth 0.7 pts Once you switch to easy you can’t go back to hard


Download ppt "Chapter 1: Introduction to Computers and Java"

Similar presentations


Ads by Google