Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2005.
Building Java Programs Primitive Data and Definite Loops.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.
Chapter 2 Variables.
Copyright © – Curt Hill Types What they do.
Introduction to Programming with Java AP Computer Science ASFA.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
© 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.
Introduction to Programming Part 2
Java Variables and Types
Introduction to Java part 2
Building Java Programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Manipulating Pictures, Arrays, and Loops part 3
Declaring Variables – Mod 4
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Type Conversion, Constants, and the String Object
Introduction to Java, and DrJava part 1
Declaring Variables – Mod 4
Building Java Programs
Chapter 2 Variables.
Building Java Programs Chapter 2
Introduction to Java, and DrJava
Building Java Programs
Building Java Programs
Introduction to Primitives
Building Java Programs
Primitive Types and Expressions
Building Java Programs Chapter 2
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Building Java Programs
Presentation transcript:

Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University

Georgia Institute of Technology Learning Goals Understand at a conceptual level –What can computers do? –What is DrJava? –How do you do math and relational operations in Java? –What is casting and why might you need it? –What are Java primitive types and how do they work? –What are the order of math operations in Java? How can you change them?

Georgia Institute of Technology What Can Computers Do? Basically they can –Add, subtract, multiply, divide –Compare values –Move data They are amazingly fast –Millions to billions of instructions per second They can store a huge amount of data –Entire movies, photo albums, dictionaries, encyclopedias, complex games They keep getting faster, cheaper, and smaller

Georgia Institute of Technology What is DrJava? DrJava is a free integrated development environment for doing Java programming –From Rice University –It is written in Java It has several window panes in it –For creating programs (definitions pane) –For trying out Java code (interactions pane) –Listing of open files (files pane)

Georgia Institute of Technology Math Operators in Java (+ * / - %) Addition Multiplication 3 * 4 Division 3 / 4 Subtraction 3 – 4 Negation -4 Modulo (Remainder) 10 % 2 and 11 % 2

Georgia Institute of Technology Math Operators Exercise Open DrJava and do the following in the interactions pane –Subtract 7 from 9 –Add 7 to 3 –Divide 3 by 2 –Divide 4.6 by 2 –Multiply 5 by 10 –Find the remainder when you divide 10 by 3

Georgia Institute of Technology Why is the result of 3 / 2 = 1? Java is a strongly typed language –Each value has a type associated with it –Tells the computer how to interpret the number It is an integer, floating point, letter, etc The compiler determines the type if it isn’t specified (literals) –3 is an integer –3.0 is a floating point number (has a fractional part) The result of an operation is in the same type as the operands –3 and 2 are integers so the answer is an integer 1

Georgia Institute of Technology Casting There are other ways to solve the problem of 3 / 2 has a result of 1 You can make one of the values floating point by adding.0 –3.0 / 2 –3 / 2.0 The result type will then be floating point Or you can cast one of the values to the primitive types: float or double –(double) 3 / 2 –3 / (float) 2

Georgia Institute of Technology Casting Exercise Use casting to get the values right for splitting up a bill for 3 people of 19 dollars. Try it first with a calculator Try it in DrJava without casting Try it in DrJava with casting

Georgia Institute of Technology Java Primitive Types –Integers (numbers without fractional parts) are represented by The types: int or short or long 235, -2, , etc –Floating point numbers (numbers with fractional parts) are represented by The types: double or float , etc –A single character is represented by The type: char ‘a’ ‘b’ ‘A’ etc –True and false values are represented by The type: boolean true or false

Georgia Institute of Technology Why so Many Different Types? They take up different amounts of space They have different precisions Usually use int, double, and boolean –byte uses 8 bits (1 byte) –short uses 16 bits (2 bytes) –int uses 32 bits (4 bytes) –long uses 64 bits (8 bytes) –float uses 32 bits (4 bytes) –double uses 64 bits (8 bytes) –char uses 16 bits (2 bytes)

Georgia Institute of Technology Sizes of Primitive Types byte 8 bits short int float long 8 bits double 8 bits char 8 bits

Georgia Institute of Technology Types Exercise Which type(s) take up the most space? Which type(s) take up the least space? What type would you use for –The number of people in your family –A grade –The price of an item –The answer to do you have insurance –The number of people in the class –The number of people in your school –The number of people in your state

Georgia Institute of Technology Floating Point Numbers Numbers with a fractional part – Stored as binary numbers in scientific notation is x 10 2 –The sign (1 bit) –The digits in the number (mantissa) –The exponent (8 bits) Two types –float – 6-7 significant digits accuracy –double – significant digits accuracy

Georgia Institute of Technology Comparison (Relational) Operators Greater than > –4 > 3 is true –3 > 3 is false –3 > 4 is false Less than < –2 < 3 is true –3 < 2 is false Equal == –3 == 3 is true –3 == 4 is false Not equal != –3 != 4 is true –3 != 3 is false Greater than or equal >= –3 >= 4 is true –3 >= 3 is true –2 >= 4 is false Less than or equal <= –2 <= 3 is true –2 <= 2 is true –4 <= 2 is false

Georgia Institute of Technology Comparison Operators Exercise In DrJava –Try out the comparison operators in the interactions pane with numbers 3 < 4 4 <= 4 5 < 4 6 == 6.0 with characters (single alphabet letter) Put single quote around a character ‘a’ < ‘b’ ‘b’ < ‘a’ ‘a’ == ‘a’

Georgia Institute of Technology Operator Order The default evaluation order is –Negation - –Multiplication * –Division / –Modulo (remainder) % –Addition + –Subtraction - The default order can be changed –By using parenthesis –(3 + 4) * 2 versus * 2

Georgia Institute of Technology Math Operator Order Exercise Try * Add parentheses to make it clear what is happening first How do you change it so that happens first? How do you change it so that it multiplies the result of and the result of 4 + 5?

Strings Strings inherit from object class Double quotes denote strings > System.out.println("Mark"); > System.out.println("13 + 5"); Georgia Institute of Technology

Concatenation of Strings > System.out.println("Barbara" + "Ericson"); BarbaraEricson > System.out.println("Barbara" + " " + "Ericson"); Barbara Ericson > System.out.println("Barbara " + "Ericson"); Barbara Ericson Georgia Institute of Technology

Strings and Operations > System.out.println("The total is " + (13 + 5)); The total is 18 > System.out.println("The total is " ); The total is 135 Georgia Institute of Technology

Escape character If you wish to use double qoutes in a string  System.out.println("Barb says, \"Hi\"."); Barb says, "Hi."  With new line, tab etc Georgia Institute of Technology

Variables > int numPeople = 2; > double bill = 32.45; > double tip = bill * 0.2; > System.out.println(tip); Georgia Institute of Technology

String Variables > String test; > System.out.println(test); null > test = "Hi"; > System.out.println(test); Hi Georgia Institute of Technology

Reusing Variables > String myName = "Mark"; > System.out.println(myName); Mark > myName = "Barb"; > System.out.println(myName); Barb Georgia Institute of Technology

Re declaration of Variables > String myName = "Mark"; > System.out.println(myName); Mark > String myName = "Sue"; Error: Redefinition of 'myName' Georgia Institute of Technology

Multiple References to same object > String name1 = "Suzanne Clark"; > System.out.println(name1); Suzanne Clark > String name2 = name1; > System.out.println(name2); Suzanne Clark Georgia Institute of Technology

Summary Computers –Can do math –Can execute billions of instructions per second –Keep getting faster, smaller, and cheaper Java has typical math and relational operators Java is a strongly typed language –This can lead to odd results integer division gives a integer result –You can use casting to solve this Java has primitive types to represent integer and floating point numbers Math operations have a default order –You can specify the order with parentheses