CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 13 / 2006 Instructor: Michael Eckmann.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 14 / 2007 Instructor: Michael Eckmann.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
CMT Programming Software Applications
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CS 106 Introduction to Computer Science I 09 / 11 / 2006 Instructor: Michael Eckmann.
Hello, world! Dissect HelloWorld.java Compile it Run it.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
String Escape Sequences
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CS 102 Computers In Context (Multimedia)‏ 01 / 23 / 2009 Instructor: Michael Eckmann.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2: Java Fundamentals
Week 1 Algorithmization and Programming Languages.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Primitive Data Types. Identifiers What word does it sound like?
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
ECE 122 Feb. 1, Introduction to Eclipse Java Statements Declaration Assignment Method calls.
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
CS 106 Introduction to Computer Science I 01 / 26 / 2007 Instructor: Michael Eckmann.
Chapter 2 Variables.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CS 106 Introduction to Computer Science I 01 / 22 / 2007 Instructor: Michael Eckmann.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 22 / 2008 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
Console Output, Variables, Literals, and Introduction to Type
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Variables and Primative Types
Introduction to C++ Programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Unit 3: Variables in Java
Chapter 2 Variables.
Instructor: Alexander Stoytchev
Presentation transcript:

CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2007 Today’s Topics Introductory comments about Java programs An example of a simple Java program

Phases of Java Programs create / compile the program –Create / Edit the source code of the program –Compile the source code into bytecodes –After the.java file is compiled we get a.class file E.g. if we compile a program called HelloWorld.java the result is HelloWorld.class When we execute the program (via the java interpreter), the interpreter does the following: –Loads the Classes –Verifies the Bytecodes –Interprets the Bytecodes All of these “execution” steps are done on the.class file(s) Michael Eckmann - Skidmore College - CS Spring 2007

Phases of Java Programs terminology you need to understand –source code –compile, build –bytecode –execute, run Michael Eckmann - Skidmore College - CS Spring 2007

Phases of Java Programs terminology you need to understand –source code (the.java files) –compile, build (the source code into bytecode) –bytecode (the.class files) –execute, run (the.class file(s) using the Java bytecode interpreter) Michael Eckmann - Skidmore College - CS Spring 2007

Syntax vs. semantics Time flies like an arrow –3 meanings (semantics) for 1 syntax –That's why English is not a programming language! Errors –Run time (crashes) –Compile time (syntax errors) –Logic (semantic errors) Has anyone encountered any of these with software that you use? Which kinds of errors? Examples? Michael Eckmann - Skidmore College - CS Spring 2007

Hello, World! program /* CS106 first program written by Michael Eckmann purpose: print the text “Hello, World!” to the screen */ public class HelloWorld { public static void main(String args[]) { System.out.println("Hello, World!"); } // end of main method } // end of HelloWorld class Michael Eckmann - Skidmore College - CS Spring 2007

Discussion of “Hello, World!” comments in source code basic structure of a Java program every Java program must have at least one class every Java program must have exactly one main method System.out.println the use of braces -- begin and end classes and method bodies. Braces are { and } – I call them curly braces. reserved words public, static, void, class, etc... see text book Michael Eckmann - Skidmore College - CS Spring 2007

Some common programming mistakes (bugs) Case matters --- upper case letters in class names etc. are different than lower case letters --- e.g. String and string are understood differently by the compiler. Additionally the capitalized String is necessary in the main method argument list declaration. System must be capitalized when referring to something like: System.out because System is the name of the class, system with a lower case s is not. Spaces are not allowed in a class name or any identifier / variable name e.g. HelloWorld is legal, Hello World is not. Michael Eckmann - Skidmore College - CS Spring 2007

Some common programming mistakes (bugs) (continued) Double quotes must be paired up when giving the value of a String --- can’t forget the second double quote. Curly braces must be paired correctly. An extra one dangling somewhere in the code will cause errors, same if one is forgotten. Class name must be the same as the main part of the file name. e.g. a public class named HelloWorld must reside in a file named HelloWorld.java --- not in something like HW.java or any other file. Java source code can only be in plain text files. If a Java source code file is stored as a Word document or any other file type besides plain text, the code will not be able to be compiled. Michael Eckmann - Skidmore College - CS Spring 2007

Some common programming mistakes (bugs) (continued) Multi-line comments starting with /* must end with */ and cannot contain a */ in between. The reason is, once the compiler sees /*, it considers everything a comment until the first */ that it sees, then starts interpreting things as code once again. Note: Encountering bugs is a good thing because it helps the learning process --- e.g. when you encounter similar bugs in the future, you’re likely to find the solution more quickly. Experience. Michael Eckmann - Skidmore College - CS Spring 2007

Comments Comments are ignored by the compiler Single line comments start with // and the comment continues until a newline Another type of multi-line comment starts with /** and ends with */ What’s the purpose of comments if the compiler ignores them? Michael Eckmann - Skidmore College - CS Spring 2007

println vs. print println adds a newline character (carriage return) after the String is printed. print does not. Michael Eckmann - Skidmore College - CS Spring 2007

println vs. print System.out.println(“I love java.”); System.out.println(“It tastes good.”); Will print: I love java. It tastes good. Michael Eckmann - Skidmore College - CS Spring 2007

println vs. print System.out.print(“I love java.”); System.out.println(“It tastes good.”); Will print: I love java.It tastes good. Michael Eckmann - Skidmore College - CS Spring 2007

println vs. print System.out.print(“I love java.”); System.out.print(“It tastes good.”); How will this print? Michael Eckmann - Skidmore College - CS Spring 2007

Eclipse Eclipse is the integrated development environment that we will use for this course. You are free to use others but I will give instructions for Eclipse and show examples during labs, etc. in Eclipse Let's try it. Michael Eckmann - Skidmore College - CS Spring 2007

Algorithms An algorithm is a well defined sequence of steps that transform some input into some output. Let's see how the book defines it. Many books liken it to a recipe to make a cake. Let's try a little exercise to create an algorithm and discuss it in terms of the constructs that are available in Java to implement the algorithm. Michael Eckmann - Skidmore College - CS Spring 2007

Escape sequences A String's value can be specified between double quotes. Within a string the backslash character \ is special. The backslash character followed by another character is called an escape sequence. Escape sequences are handled differently than regular characters. \n is a common escape sequence to force a newline \t is a way to add a tab to a string E.g. System.out.println(“Hello\nWorld”); prints: Hello World Michael Eckmann - Skidmore College - CS Spring 2007

Escape sequences \b is the backspace character There are many others, some of which are listed in the book on page 43 Let’s say you wanted to print a sarcastic message using double quotes. If we do System.out.println(“Hamilton has a “great” lacrosse team”); there will be an error at compile time. Michael Eckmann - Skidmore College - CS Spring 2007

Escape sequences System.out.println(“Hamilton has a “great” lacrosse team”); The string will be –Hamilton has a and then the compiler will see a g and will generate an error because the second double quote (the one before great) causes the string to end. What do we do? Michael Eckmann - Skidmore College - CS Spring 2007

Escape sequences An escape sequence is provided to print a “. It is \” System.out.println(“ Hamilton has a \“great\” lacrosse team ”); The above line of code will do what we want now. Michael Eckmann - Skidmore College - CS Spring 2007

Escape sequences How might we add a backslash character to a String? Let’s say we want a String like: –C:\My Documents Will this work? –System.out.println(“C:\My Documents”); Michael Eckmann - Skidmore College - CS Spring 2007

Let’s try this problem Write an application that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. Write the program using the following methods: –A) Using one System.out statement –B) Using four System.out statements Now let's do the same thing but on 4 different lines. Michael Eckmann - Skidmore College - CS Spring 2007

String concatenation To concatenate two Strings together, Java provides the operator + e.g. System.out.println(“Hey “ + “now.”); prints Hey now. Michael Eckmann - Skidmore College - CS Spring 2007

String concatenation Strings can only be concatenated to other Strings, but numbers are converted to Strings by Java automatically if they are being concatentated to another String. e.g. System.out.println(“I am “ “ years old.”) Will work the same as: System.out.println(“I am “ + “21” + “ years old.”) Will work the same as: System.out.println(“I am 21 years old.”) Michael Eckmann - Skidmore College - CS Spring 2007

Variables and Type A variable is a location in memory (RAM) with the following properties. name --- given by the programmer --- try to be descriptive type --- decided by the programmer from all available types, depending on what kind of values the programmer wants to use size --- amount of memory used, this is determined by the type value --- self-explanatory –value can be stored / changed / read by a program. Michael Eckmann - Skidmore College - CS Spring 2007

Primitive Types for Variables byte, short, int, long --- these are integer types -- - they hold whole numbers like –31, 4256, 56, 2632, 755, -901 char --- this is a type that holds one character like a letter, digit, *, &, or an escape sequence … boolean --- holds the values of true and false float, double --- numeric types that hold numbers with decimal values like -5.5, 98.6, etc. see page 53 for the sizes / ranges of values Michael Eckmann - Skidmore College - CS Spring 2007

Variables and Type int area = 50; // This line of code not only // declares a variable but also initializes its value. name of variable is area type of variable is int size of variable is 32 bits (because an int has a size of 32 bits.) value of variable is currently 50 note the same thing can be done with 2 lines of code: int area; area = 50; Michael Eckmann - Skidmore College - CS Spring 2007

public class VariableExample { public static void main(String args[]) { int result; // declares variable with name result and type int // allocates enough memory for an int to be stored int some_number; // declares variable with name some_number and type // int and allocates enough memory for an int to be stored result = 35; // an assignment statement that stores the value 35 in the // variable result. some_number = 24; // an assignment statement that stores the value 24 // in the variable some_number. result = some_number + 5; // uses the + operator to add the value in some_number to 5 // and stores (assigns) the sum to the variable result, thereby // changing result’s value. } // end main method body } // end class VariableExample body Michael Eckmann - Skidmore College - CS Spring 2007

Types for Variables Variables can be of the primitive types as well as predefined classes provided by Java or any class that a programmer creates. For example a variable can be of type String which is a class provided by Java. We’ll see more of variables and types shortly. Michael Eckmann - Skidmore College - CS Spring 2007

Operators + when used with numeric types (e.g. int, float etc.) acts as an add, however, + when used with Strings is a concatenation operator. –example: text_string = “Hello ” + first_name; = is the assignment operator. * is multiplication, / is division, % is modulus and - is subtraction. ( ) left and right parentheses are considered operators as well. Michael Eckmann - Skidmore College - CS Spring 2007