COMP 110 Primitive Types, Strings, and Console I/O Tabitha Peck M.S. January 23, 2008 MWF 3-3:50 pm Philips 367 1.

Slides:



Advertisements
Similar presentations
Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
COMP 110 Errors, Strings, and Review Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
COMP 110 Introduction to Programming Mr. Joshua Stough August 29, 2007 Monday/Wednesday/Friday 3:00-4:15 Gardner Hall 307.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 2 Elementary Programming
COMP Mid-Term Review Yi Hong May 27, 2015.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
COMP String and Console I/O Yi Hong May 18, 2015.
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Computer programming Lecture#2 أ. إلهام باسندوه 1.
Primitive Types, Strings, and Console I/O Chapter 2.
COMP 110: Spring Announcements Lab 0 was due today (noon) Lab 1 is on Friday Bring laptops New students see me after class.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Primitive Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Catie Welsh January 31,  Project 1 Due Wednesday  Lab 1 Grades are posted 2.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Catie Welsh March 4,  Midterm on Monday, March 14th ◦ Closed books, no notes, no computer  No office hours during Spring Break ◦ However, I will.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
A Sample Program #include using namespace std; int main(void) { cout
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.
COMP Review of Chapter 1 & 2
Topic 2 Elementary Programming
Crash course in the Java Programming Language
Java Variables and Types
Elementary Programming
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Chapter 2: Java Fundamentals
elementary programming
Primitive Types and Expressions
Unit 3: Variables in Java
Presentation transcript:

COMP 110 Primitive Types, Strings, and Console I/O Tabitha Peck M.S. January 23, 2008 MWF 3-3:50 pm Philips 367 1

Announcements Lab 1 Due Friday Before Class Follow submission instructions.jar tutorial 2

Questions? 3

Today in COMP 110 Primitive Types and Expressions Strings Console I/O 4

Variables Used to store data in program The data currently in a variable is its value Name of variable is an identifier Can change value throughout program Choose variable names that are helpful!!! 5

Variable Declarations Syntax: Type Variable_1, Variable_2, …; Examples: int count, score, myInt; char letter; double totalCost, ratio; 6

How to name an identifier Letters, digits(0-9), underscore (_) First character cannot be a digit Java is case sensitive Legal names pinkFloyd, the_coup, b3atles Illegal names michael.bolton, kenny-G, 1CP 7

Keywords Reserved words with predefined meanings You cannot name your variables keywords Appendix 1 if, else, return, new 8

Type What kind of value the variable can hold Class type - objects with both data and methods Name begins with uppercase letter Scanner, String Primitive type - indecomposable values Names begin with lowercase letters int, double, char, boolean See display 2.2, p 53 for full list 9

Variables Change int changingVar = 0; changingVar = 5; changingVar = changingVar + 4; 10 identifier type 5 9

Primitive Types Integer (byte, short, int, long) 0, -3, 5, 43 Floating-point number (double, float) 0.5, , -4.3 Characters (char) A, r, %, T Boolean (boolean) true, false 11

Assignment Statements something = otherSomethings; answerToLife = 42; theUniverse = (answerToLife*4) + everything; 12 Variable Expression Assignment operator

Specialized Assignment Operators mario += 5; // is the same as mario = mario + 5; luigi++; // is the same as luigi = luigi + 1; 13

Assignment Compatibilities You can only put small things into bigger things   14

Assignment Compatibilities byte->short->int->long->float->double myShort  myInt; myByte  myLong; myFloat  mybyte; myLong  myInt; 15

Type Casting byte->short->int->long->float->double myFloat = (float)myDouble; myByte = (byte)myInt; myShort = (byte)myFloat; 16

Arithmetic Operators Unary operators (more info later) +, -, ++, --, ! Binary arithmetic operators *, /, %, +, - rate*rate + delta 1/(time + 3*mass) (a - 7)/(t + 9*v) 17

Modular Arithmetic - % “clock arithmetic” Minutes on a clock are mod 60 Remainder 7 % 3 = 1 (7 / 3 = 2, remainder 1) 8 % 3 = 2 (8 / 3 = 2, remainder 2) 9 % 3 = 0 (9 / 3 = 3, remainder 0) 18

String String animal = “Mad Max”; System.out.println(animal); Mad Max 19

String Concatenation String animal = “Mad Max”; String sentence; Sentence = “My dog’s name is ” + animal; My dog’s name is Mad Max 20

String (Class Type) Class types have methods String myString = “COMP110”; int len = myString.length(); Object Method 7 21

Strings Methods (pp ) myString.length(); myString.equals(“a string”); myString.toLowerCase(); myString.trim(); You will see these in Lab on Friday 22

String Indices UNCisGreat String output = myString.substring(1, 7); 23

Escape Characters System.out.println(“How do I put \“quotes\” in my string?”); \”Double quote \’Single quote \\Backslash \nNew line \rCarriage return \tTab 24

I/O (Input/Output) System.out.print(“this is a string”); System.out.println(“this is a string”); What is the difference? 25

Keyboard Input Scanner Scanner_object_name = new Scanner(System.in); Scanner_object_name.nextLine(); Scanner_object_name.nextInt(); Scanner_object_name.nextDouble(); See p. 93 Make sure to read Gotcha on p

Documentation and Style Meaningful names Indenting Documentation Defined Constants 27

Named constants public static final Type Variable = Constant; Named in ALL_CAPS public class NamedConstant { public static final double PI = ; public static void main(String[] args) { 28

Friday Recitation (bring charged laptop) Bring your book!!!!! Lab 2 Programming help for Program 1 Monday - In class exercise Bring your book 29