Lecture 1: Comments, Variables, Assignment. Definitions The formal (human-readable) instructions that we give to the computer is called source code The.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
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.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS 106 Introduction to Computer Science I 09 / 11 / 2006 Instructor: Michael Eckmann.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Lecture 2: Classes and Objects, using Scanner and String.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Chapter 2: Java Fundamentals
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Primitive Data Types. Identifiers What word does it sound like?
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
CS 177 Recitation Week 1 – Intro to Java. Questions?
1 2. Program Construction in Java. 01 Java basics.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
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.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
User Interaction and Variables
Chapter 2 Basic Computation
Building Java Programs
Lecture 2: Operations and Data Types
Introduction to Computer Science / Procedural – 67130
Multiple variables can be created in one declaration
Chapter 2.
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
COM-152: Computer Programming Types, Variables, Operators Part 1 of 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Building Java Programs
In this class, we will cover:
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Chapter 2: Java Fundamentals cont’d
Building Java Programs
Building Java Programs
Presentation transcript:

Lecture 1: Comments, Variables, Assignment

Definitions The formal (human-readable) instructions that we give to the computer is called source code The compiler translates our source code into a format that the computer can understand

Comments Comments are places in your source code where you write stuff intended only for humans to read The compiler always totally ignores comments, as if they weren't even there There are two types of comments in Java Single-line comments – Everything including the ``//'' and after on that line is ignored. – System.out.println("Hello World!"); //Greet the user – //This is a comment, too. – ``Commenting-out'' code is a quick way to make a line of code do nothing temporarily. // System.out.println("Hello World!");

Multi-line comments – Start with ``/*'' and end with ``*/'' – /* They can be just one line, too */ Here is a Java program that illustrates different ways to use comments:

/////////////////////////////////////////////////////////////////////////////// // ALL STUDENTS COMPLETE THESE SECTIONS // Title: HelloWorld2 // // Author: Michael Kowalczyk // CS Login: mkowalcz // Lecturer's Name: // Lab Section: // public class HelloWorld2 { public static void main(String[] args) { //This is a single line comment /* This is a multi-line comment */ //Note that the following System.out.println statement is //commented out, so it won't do anything. //System.out.println("Hello, World!"); System.out.println("Hi!"); //Prints a greeting to the user. } }

Identifiers Identifiers are the names that we give to different things (variables, methods, classes) in Java. – Rules for making identifiers: Identifiers can be made up of a-z, A-Z, 0-9, as well as _ and $ Identifiers cannot start with a number Can't be a ``reserved word'' (public, void, class, int, etc.) Don't try to memorize all the reserved words; just remember that they are words with special meaning in Javaall the reserved words Case sensitive, so myIdentifier is different than MyIdentifier or MYIDENTIFIER. Identifiers in this code fragment have been underlined.this code fragment Some examples of good/bad identifiers?

Data Types There are precisely two categories of data types in Java: primitives and classes. There are only 8 primitive types and they are all listed herehere – The most important ones in this course are int, double, boolean, and char A ton of classes are already given in the Java libraries, and we can make new ones, too.

Variables Every variable has a name, type, and contents – The name must be an identifier – The type is fixed, and is either one of the 8 primitives or some class – The contents must match the type, and can be changed over time (unlike typical variables in math) In general, you can declare (that is, define) a variable by using a statement of one of the following 2 forms: – type identifier; – type identifier = value; You have to declare a variable before using it and you can only declare it once. If you want to change the contents of a variable once declared, you just use an assignment statement, of this form: – identifier = value; Let's write up an example to make this all clear

/* * This application class demonstrates how to declare, assign, and use * variables with primitive types. */ public class Primitives { public static void main(String[] args) { //Declare a variable called letter that can hold a char char letter; //Declare a variable called myInteger that can hold an int, and make it 3 int myInteger = 3; //Store the character "a" in the variable called letter letter = 'a'; System.out.println(letter); //prints out the letter a //Declare j (which can hold an int) and make it equal 5 int j = 5; j = j - myInteger; //evaluates 5-3 and stores the result in j System.out.println(j); //prints 2 double d = 3.7; //myInteger = d; //causes compile-time error (can't convert) d = myInteger; //But this conversion is ok. System.out.println(myInteger); //prints 3 System.out.println(d); //prints 3.0 } }

We'll now start our first lab Mind-Reading Number Trick 1) Think of a number, any positive integer (but keep it small so you can do computations in your head). 2) Square it. 3) Add the result to your original number. 4) Divide by your original number. 5) Add, oh I don't know, say 17. 6) Subtract your original number. 7) Divide by 6. 8) The number you are thinking of now is 3!