COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.

Slides:



Advertisements
Similar presentations
This is Java Jeopardy.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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.
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.
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.
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.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
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.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
Methods: a first introduction Two ways to make tea: 1: boil water; put teabag into cup;... etc : tell your younger brother: makeTea(1 milk, 0 sugar);
String Escape Sequences
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;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Lecture 2: Classes and Objects, using Scanner and String.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
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?
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
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.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS 106 Introduction to Computer Science I 09 / 10 / 2007 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.
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,
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.
Java Fundamentals MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Building Java Programs
Introduction to Computer Science / Procedural – 67130
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Computer Programming Methodology Introduction to Java
Examples of Primitive Values
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Primitives
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to Primitives
Building Java Programs
In this class, we will cover:
Primitive Types and Expressions
Unit 3: Variables in Java
Building Java Programs Chapter 2
Just Enough Java 17-May-19.
Building Java Programs
Building Java Programs
Presentation transcript:

COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis

Announcements Could you get Visual J++? Book? Is everyone signed up for an ISIS account? Office Hours? Lab Assistants’ hours are up Too fast?

Review What are the rules for a Java identifier? What symbol indicates the end of a statement in Java? What symbols start and end a section in a Java program? What are the two ways of indicating comments?

Outline for today Data in Java Data Types Variables Methods Strings Class libraries: Using Objects

Need 2 things for a program Just like baking a cake. You need: –ingredients –a recipe in programming, we need: –data –algorithm Today we’ll talk about our “ingredients”

How does Java handle data? Primitive data –data fundamental to a computer –numbers, characters (because people use them) Objects –represent more complex concepts by combining primitive data –e.g., a car or a hotel

Data Types Both primitive data and objects can be described by a data type Data types have two parts: –a set of possible values –a set of operations

Data Type example Integers Set of values: –{…, -2, -1, 0, 1, 2, …} Set of operations: –addition, subtraction, multiplication, division

Another example Let’s take the car object example Values: –more complex: we’ll limit it to speed, amount of gas in tank, and what gear it’s in Operations: –speed up, slow down, change gear, etc.

Objects and Data Types You tell the computer the data type for an object by writing a class A class contains: –variables to keep track of the value –methods to perform the operations

Variables A location in memory associated with an identifier and a specific data type Declaration: –tell the compiler to make space for a specific kind of data and assign it a name –you can also specify a value for the variable int sum; int sum = 0;

Primitive Data Types We’ll only use int, double, char, boolean: –int bits -- integer from -2 billion to 2 billion (approx.) –double bits -- floating point value from -1.7x to 1.7x (approx) –char bits -- one character such as ‘a’, ‘G’, ‘!’, ‘4’, etc. (also things like space, enter) –boolean -- holds true or false

Assignment Changes the value held by a variable Example: Destroys the old value in the variable sum = 10; int numBooks = 5; numBooks = 25;

Constants What do we do if the variable’s value never changes? We can tell the compiler this: final means “I won’t ever change the value of this variable” (thus, it’s value is constant) use all capital letters for identifier final int MAX_BRAINS = 1;

Expressions You can combine operators and operands to form expressions (usually as part of an assignment statement) for int some operations are: +, -, *, /, % answer = 2 + 2; answer = 17 / 3 + 6; answer = answer * 2;

Remember this? You tell the computer the data type for an object by writing a class A class contains: –variables to keep track of the value –methods to perform the operations

Methods Methods are collections of programming statements that are given a name Usually they perform one of the operations of the data type Called procedures and functions when not in an object-oriented language

We’ve already seen methods public class Simple { public static void main(String[] args) { System.out.println(“Hello!”); } Here we are using a method (making a “call” to that method)

Let’s break it apart System.out.println (“Hello!”); object method Information provided to the method (parameters)

System.out.println(“Hello!”); println is the name of a method that belongs to the System.out object It tells the System.out object that we’d like to print Hello! on the screen System.out.print is the same, but it doesn’t go to the beginning of the next line

So what is “Hello!”? We’re sending this data to the println method, but what is its data type? It’s a special case in Java called a string literal It’s data type is actually the String class

Objects work like primitive data You declare an object variable just like primitive data, but with a class data type: So we could do our simple program in a slightly different way if we wanted... String greeting; String greeting = “Hello!”;

Simple Program modified public class Simple { public static void main(String[] args) { String greeting = “Hello!”; System.out.println(greeting); }

Things work a little differently... When you declare a variable with a class data type, it really just creates a reference You have to create an object of that data type (class) -- this is called instantiation String greeting; greeting = new String(“Hello!”); = new ( );

You saw a shortcut for strings... Before, I did it this way though: That’s because strings are so common, that Java lets you use string literals as a shortcut: String greeting = “Hello!”; greeting = “Hello!”; is the same as gretting = new String(“Hello!”);

Concatenating Strings What if we want to print out something long that doesn’t fit on one line? Java doesn’t like this: string literals must be on one line System.out.println(“Hello, how are you this fine day?”);

The solution The + operator can also be used to glue two strings together This works with primitive data too! System.out.println(“Hello, how” + “ are you this fine day?”); int result = 5; System.out.println(“Result: “ + result);

Other string “problems” What if you want a in the string? You can’t do this: You could do: System.out.println(“Hello. Nice to meet you.”); System.out.println(“Hello.”); System.out.println(“Nice to meet you.”);

Escape Sequences There’s another way! If you put ‘\n’ in the string literal, it becomes a in the output System.out.println(“Hello.\nNice to meet you.”); works the same as System.out.println(“Hello.”); System.out.println(“Nice to meet you.”);

Escape Sequences Called this because the ‘\’ is an escape character, which means we want to do something special Two others you should know: –\” -- puts a “ in the string (why do we need it?) –\\ -- puts a \ in the string (why do we need it?)

Class Libraries Java comes with a bunch of classes that you can use in your programs (e.g., String ) a class library is just a name for a set of such classes Java puts a bunch of classes together in a package

Packages When you want to use a class in a package, you have to let the compiler know Even though you don’t have to type it, Java behaves like all programs have this import: import import java.lang.*;

Examples

Homework Reading: P1 (Program1) goes out today and is due Friday. There is no “programming” involved in this assignment. The purpose is to get you used to Visual J++. Additionally, you see how you will hand in your programs.