Assignments CompSci 125 Lecture 02 Java and Java Programming with Eclipse! Homework:! http://coen.boisestate.edu/jconrad/compsci-125-homework! hw1 due Jan 28 (MW), 29 (TuTh)! Programming:! http://coen.boisestate.edu/jconrad/cs125-programming-assignments! p0 due Jan28 (MW), 29 (TuTh) Homework 1! hw1 posted on web site. Due Jan 28 (MW), 29 (TuTh)! Hand-in completed homework using onyx submit command:! CS125-003: submit dornelas cs125-3 hw1!! CS125-004: submit dornelas cs125-4 hw1!! dornelas refers to Diana Ornelas, our sections TA! Be nice to the TAs --- they grade your homework Program 0! Posted on web site. Due Jan 28 (MW), 29 (TuTh)! The instructions in the assignment to hand-in your completed program assume you placed the requested files (Hello.java, Quote.java and README) in a folder called p0! CS125-003: submit dornelas cs125-3 p0!! CS125-004: submit dornelas cs125-4 p0!! Be nice to the TAs --- they re going to save you
Course Web Site! http://coen.boisestate.edu/jconrad! Course syllabus! Link to the Facebook Group! Slides! Homework assignments! Programming assignments! Installing/Using programming tools! Onyx Computer Lab information! Files/documents for distribution Facebook Group! http://www.facebook.com/groups/157148657767138! Optional but well-received! Extends classroom discussion! Discuss questions about lecture, Java and Computing! Virtual helpdesk for assignments --- best source for help when you are off-campus Review:''Everything'is'in'RAM' Computer Organization Random Access Memory (RAM) SATA bus Central Processing Unit (CPU) Northbridge High-speed bus controller Southbridge Low-speed bus controllers Programs Numbers Documents Spreadsheets Photographs PowerPoint slides MP3s Most everything
Memory Organization! If the computer encodes everything in memory with 1s and 0s! How does Java know whether to interpret the 1s and 0s in memory as a number, or a text string, or a photograph, or music, or a video, or as a program? Memory Organized into Bytes! A byte is the smallest addressable location in RAM! Every byte has a unique numerical address assigned by hw! The CPU accesses a specific byte in memory by referencing its numerical address 01110000011010101000011111100010101110001110011111 01110000 11010111 10001010 01010111 00000000 11101000 00010111 11111111 00000001 00100000 01010101 10001000 10011001 11011011 10101010 10101000 Types determine how Java interprets the content of memory! If everything in memory is organized as a sequence of bytes, each containing 8 bits, each of these being a 1 or 0! How does a program know to interpret the content of memory as a number, text string, photograph, music, video?! A type determines how Java interprets the 0s and 1s in memory at a specified address Example Java Types! integer: A positive or negative whole number! double: Approximates a rational number! String: A sequence of characters (e.g. abc )! You will define your own type known as a class!! More!
Variables Store Data in RAM A Java variable stores data in memory Every Java variable has a numerical address, name and type Java programmers almost always refer to variables by name Java takes care of mapping the variable names to RAM locations Variable Name Numerical Address Value 1000 0 1001 1 1002 255 somecharacter 1003 A numberx 1005 21,983,114 Review: Java Data Types and Variables! Data Type! Determines how a value is encoded with 0s and 1s in memory! Does not occupy memory it s just a plan (like a blueprint)! Variable! Occupies one or more bytes of memory! Implements an instance of a data type! Has a unique name! Has a unique numerical (usually unknown) address in memory! Has a value Java Programs are Composed of es! Programs are organized into smaller units known as classes! Each class may define variables storing data (numbers, strings of characters, pictures, sound, or even other classes)! Each class may define methods containing a sequence of instructions for the computer! A class is one form of a Java type! A class does not occupy memory --- it s a plan (blueprint) Java source code! You compose and revise in a text editor! Understandable by programmers --- you can read it! The Java source code defines one or more classes! The CPU cannot directly execute Java source code!! The CPU executes a machine language! The Java compiler translates the source code into machine language for the CPU
The Java Vision Example: Java Source Code public class HelloWorld {!!public static void main(string[] args) {!!!System.out.println( Hello, world! );!!}! }!! Originally intended for mobile devices (e.g. cell phones)! Programmer would compose a program in Java source code! Then compile it into a bytecode version of the same program! Distribute machine-independent bytecode to end-users! End-users would install the bytecode on their devices! Vendors would include a Java Runtime Environment (JRE also known as the JVM) on every device! JRE executes the bytecode! Any program would run anywhere!! Didn t work out as planned, but Java became important anyway James Gosling Program Structure Java is! A high-level language! Machine independent! Object-oriented! An important programming language! Popular on servers Program Variables Methods Methods
An object is an instance of a class = Blueprint One blueprint to create several similar, but different, houses: An instance of a CheckingAccount class CheckingAccount An instance of a CheckingAccount A CheckingAccount class defines common behaviors (methods) for adding and withdrawing funds. The class also defines common attributes such as name, account number and balance But the values of these attributes are unique to each instance of a CheckingAccount object object Copyright 2012 Pearson Education, Inc. Object:( An'object'is'an'instance'of'a'class, ' An'object'may'implement'methods'and/or'variables'in' memory, ' A'reference,variable'may'contain'the'address'of'an'object'in' the'computer s'memory' ' You'compose'classes'in'your'java'source'code' ' Your'execuAng'program'builds'( new'up )'objects' ' Objects'may' model 'realgworld'things'(e.g.'a'student)' Packages help organize large programs Package Program Package Variables Methods Methods Variables Methods
Built-in Package java.lang Built-in Package java.util Package java.lang Package java.util System String Math Scanner Random Date Java Program Structure Example: public class HelloWorld {! public static void main(string[] args) {! System.out.println("Hello World!");! }! } //HelloWorld! // comments about the class public class MyProgram { Every Java program defines one or more classes. Hint: Java requires the class filename, e.g. HelloWorld.java, to match the class name with the.java extension. } Copyright 2012 Pearson Education, Inc.
Example: Method public class HelloWorld {! public static void main(string[] args) {! System.out.println("Hello World!");! }! } //HelloWorld! Most classes define one or more methods. This class defines a single method named by the identifier, main. This method contains a single statements explaining how the computer is to perform a specific task. The main Method! Special method known as main public void main(string[] args) { System.out.println( Hello world ); }! This method must be named main as in the example above! Your program defines exactly one main method! Java starts your program by invoking your main method Java Program Structure Example: Statement public class HelloWorld {! public static void main(string[] args) {! System.out.println("Hello World!");! }! } //HelloWorld! // comments about the class public class MyProgram { // comments about the method public static void main (String[] args) { } The statement System.out.println invokes the method println implemented by the class out built into the Java System package. } Confusing? It just prints a String to stdout. In this case, the String is Hello World!. Copyright 2012 Pearson Education, Inc.
Definitions: Code Block! Java source code placed inside of braces, a sub-unit of code { } System.out.println( Beginning of this code block ); System.out.println( Middle of this code block ); System.out.println( End of this code block ); Identifier Rules! You may use letters, digits, underscores or the dollar sign! Must start with a letter or a dollar sign! Must not be a Java keyword Valid Identifiers SoccerTeam soccer_team $cash4game numberofgoals Soccer Invalid Identifiers soccer team soccer-team 4game #goals class Java Reserved Words abstract assert boolean break byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while Review: Key Java Concepts! Programs are constructed with one or more classes! A class may define methods and/or variables! A method contains a sequence of statements! es are one form of a Java type! The class is just the plan, a blueprint! Your program may construct objects, instances of classes, in memory Copyright 2012 Pearson Education, Inc.
Language Levels There are four programming language levels: fourth-generation language (RPG-II, SPSS, SQL, R ) high-level language (Java, C#, C++) assembly language machine language Introduction to Computer Science I Java Program Development Each type of CPU has its own specific machine language The other levels were created to make it easier for a human being to read and write programs Copyright 2012 Pearson Education, Inc. How does a Java Program become Machine Language executed by the CPU? Text Editor Java Program Byte Code Java Compiler Java Runtime Environ Machine Language Low-Level vs. High-Level Languages! Instructions in a low-level language are specific to a particular CPU. We say they are machine-dependent. Examples: Machine language, assembly language.! Instructions in a high-level language are relatively independent of a particular CPU. We say these are machine-independent. Example: Java You write this CPU executes this
Syntax and Semantics The syntax rules of a language define how we can put together symbols, reserved words, and identifiers to make a valid program The semantics of a program statement define what that statement means (its purpose or role in a program) A program that is syntactically correct is not necessarily logically (semantically) correct A program will always do what we tell it to do, not what we meant to tell it to do Development Tools: What software do I install? Your Computer Your own LINUX Computer Your own OS X Computer ET213/214 LINUX Computers Java JDK Install fm ur distro Already installed (Lion asks on first use) Already installed eclipse Text Editor Install fm ur distro Install on hard drive if you choose eclipse Already installed Copyright 2012 Pearson Education, Inc. Development Tools: LINUX in ET213 and ET214! LINUX --- Important for CompSci majors! Software is already installed, but you have to learn how to use LINUX! Accounts are available --- see me if you haven t by now! Computer Labs Doc: http://cs.boisestate.edu/~jconrad A Couple Important Details! Java requires the name of the source code and byte code files to be the same except for the extensions! Eclipse will normally take care of this, but you can get in trouble outside of Eclipse! The source code file must have the.java extension! The bytecode file must have the.class extension
How to Install the Eclipse IDE How to Install Eclipse --- Part II! http://www.eclipse.org/downloads! You only need to do this on your own computers! Eclipse is already installed on the ET213/214 LINUX systems ( onyx )! Must install the Java Development Kit (JDK) prior to installing Eclipse! Installing the Eclipse IDE at http://cs.boisestate.edu/~jconrad! Problems? Post screenshots/questions on the FaceBook group! How to Install Eclipse --- Part VIII! Click the curvy arrow to get past the Eclipse welcome screen! Hint: Different versions of Eclipse place the arrow at different places How to Install Eclipse --- Part IX! This is what eclipse looks like after you get it running
Eclipse running on LINUX in ET213/214 How to run Eclipse on LINUX On the ET213/214 Systems (onyx)! Eclipse is already on installed on CompSci s LINUX systems! Login with your CompSci onyx account! Run from the Desktop menu! Or Get a terminal shell (e.g. bash) window and enter the command, /usr/local/eclipse/eclipse Writing your First Program: Using Eclipse! Eclipse organizes each homework assignment in a project! An Eclipse project is something like a folder! You begin by running Eclipse and creating a new project Create a new Java Project
Name your project (e.g. hw2 ) Result of creating my project hw2 Create a New Name your class HelloWorld
Writing your 1 st Program: Eclipse will compose part of your first program! You must write the remainder Click Green Arrow to Run Program Let Eclipse Save HelloWorld.java
Your Program s Output Appears Where did Eclipse hide HelloWorld.java? Where did Eclipse hide HelloWorld.class? Running your program in a command shell
Java Development Process Bytecode version What Eclipse did for You Text Editor HelloWorld.class Java Compiler Java Runtime Environ! Eclipse composed part of the HelloWorld class for you, but you ll need to know it on the exams! Eclipse invoked the Java Compiler automatically for you! Eclipse also invoked the Java Runtime Environment for you! The JRE invoked your program s main method to start it HelloWorld.java Machine Language Java source code CPU executes this