Recap Week 2 and 3.

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

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
BBIT 212/ CISY 111 Object Oriented Programming (OOP) Objects and classes, object interactions in Java
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
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
Java Syntax Primitive data types Operators Control statements.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
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;
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Variables.
Chapter 1.2 Introduction to C++ Programming
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 2 Basic Computation
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Java Programming by Ramiro Rocha.
Yanal Alahmad Java Workshop Yanal Alahmad
Lecture 2: Data Types, Variables, Operators, and Expressions
2.5 Another Java Application: Adding Integers
Multiple variables can be created in one declaration
Computer Science 3 Hobart College
Java Programming: From Problem Analysis to Program Design, 4e
Starting JavaProgramming
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Unit 6 - Variables - Fundamental Data Types
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Building Java Programs Chapter 2
elementary programming
Chapter 2 Programming Basics.
Building Java Programs
In this class, we will cover:
Unit 3: Variables in Java
Building Java Programs Chapter 2
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Variables.
Just Enough Java 17-May-19.
Building Java Programs
Problem 1 Given n, calculate 2n
Presentation transcript:

Recap Week 2 and 3

Variable types int boolean char double float long byte short String Object

Variable values int i = 0; boolean b = true; char c = ‘x’; double d = 4.2; String e = “text 5 6.1;”;

Variable declaration and assignment Declaration (only once) – type then name int i; Assignment (any number of times) – name = value i = 7; Can be performed together: int i = 7;

Variable names Cannot be reserved word: for, if, int Cannot contain certain characters: variable_name is okay variable-name is not

Scope Variables exist only in their scope, defined by {} public class Example { private int field; public void method(int argument) { int local = 0; … if (…) { int local2 = 1; }

Fields Declared outside of methods Fields have visibility modifier Public variables can be accessed anywhere, private variables can only be accessed by the owner Declaration of field – visibility then type then name public int i; private int j;

Default value Fields have default values before any assignment is made int : 0 boolean : false Any object : null Local variables do not have default, they always require an assignment before use

Methods Method signature – visibility then return type then name then (arguments) public int method1(int x, int y) private void method2()

Return type void means no return If a method is non-void, it must return public boolean isPositive(int x) { if (x > 0) { return true; } else { return false;

Constructors A constructor is run when an object is first made Constructor signature – visibility then class name then (arguments) public Example(int arg1, String arg2)

Multiple constructors Can have multiple constructors if they have different arguments Only one will be run when the object is made public class Example { public Example() {…} public Example(int arg) {…} }

New Object Make new objects with new Example ex = new Example(); The arguments given determines which constructor is used Example ex2 = new Example(1);

Conditionals if (x > 0) { … }

Else Must begin with if Can have any number of else if if (x > 0) { … } else if (x < -7) { else {

Printing System.out.println(text) will print text to the terminal

String concatenation Strings can be combined with + to make new Strings “abc” + “def” becomes “abcdef” “abc” + 5 + 7 becomes “abc57” 5 + 7 + “abc” becomes “12abc”

Comments //This is a single-line comment int i = 17; //this bit gets ignored /* this is a multiline comment*/ int i = /* we can put multiline comments anywhere */ 18;

Syntax Every statement must end with a semicolon Conditionals do not end with a semicolon – this is wrong: if (x == 19); {…}

Method call Internal method calls – methodName(arguments) sum = add(1,2); incrementTotal(); External method calls – variableName.methodName(arguments) Square s = new Square(); s.moveLeft();

Primitives hold values int a = 0; int b = 1; a = b; b = 2; After these calls, a is 1 and b is 2 Primitives don’t have methods

Objects hold references Square s1 = new Square(); Square s2 = s1; s1.moveLeft(); Both s1 and s2 are pointing to the same Square which has just been moved left

Integer division and modulus 5 / 2 = 2 5 % 2 = 1

Null null is a special object value Any object variable can be assigned null Square s = null; Null objects don’t hold any information – this will cause an error: s.moveLeft();

Chain method calls If a method returns an object, its methods can be immediately called This: String name = getName(); char c = name.charAt(0); Is equivalent to: char c = getName().charAt(0);