CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 20, 2004 Last update:

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Chapter 2 Review Questions
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.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Some basic I/O.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
CS150 Introduction to Computer Science 1
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.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
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.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 3: September 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.
Introduction to Computer Systems and the Java Programming Language.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Primitive Variables.
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.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette August 25, 2004 Last update:
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Chapter 2 Variables.
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.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette August 27, 2004 Last update:
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
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,
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Input, Output and Variables GCSE Computer Science – Python.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Topic 2 Elementary Programming
Chapter 2 Basic Computation
Chapter 4 – Fundamental Data Types
Introduction to Computer Science / Procedural – 67130
Documentation Need to have documentation in all programs
Type Conversion, Constants, and the String Object
Chapter 2.
IDENTIFIERS CSC 111.
Principles of Computer Programming (using Java) Chapter 2, Part 1
Unit-2 Objects and Classes
Computers & Programming Languages
An Introduction to Java – Part I, language basics
Introduction to Primitive Data types
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
Java Programming Review 1
CS2011 Introduction to Programming I Strings
Comparing Python and Java
Introduction to Primitive Data types
Section 6 Primitive Data Types
Presentation transcript:

CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 20, 2004 Last update: September 16, 2004 Importing and Exporting Data

Course Introduction  Aditya P. Mathur Learning Objectives What are primitive data types in Java? How to input data via an applet? How to output data via an applet? Reference: Chapter 6 of the textbook.

Course Introduction  Aditya P. Mathur Primitive data types: byte, short, int, long Java offers the several primitive data types for manipulating real-world data such as integers, floating point, or real, numbers, booleans, and characters. We begin with type int. int Example: int x, y; Variables x and y can assume integer values such as -20, 33, , 0, 45 There are four kinds of integers, int is one of them. The others are: byte, short, long. Refer to page 164 of the text for details.

Course Introduction  Aditya P. Mathur Primitive data types: double, float Example: double x,y; Variables x and y can assume floating point values such as 0.0, , , Example: float x,y; Variables x and y can assume floating point values such as 0.0, , , Variables of type double will be represented with higher accuracy than those of type float.

Course Introduction  Aditya P. Mathur Primitive data type: char Example: char x,y; Variables x and y may assume character values such as ‘p’, ‘*’, ‘+’, ‘/’. Notice that character constants are enclosed inside single quotes as in ‘/’ whereas strings are enclosed inside double quotes as in “hello!”. Also, variables of type String are DIFFERENT from variables of type char. See page 166 of your text for details.

Course Introduction  Aditya P. Mathur Primitive data type: boolean Example: boolean x,y; Variables x and y may assume boolean values true and false.

Course Introduction  Aditya P. Mathur Data input: Input an integer value While all kinds of data can be input via an applet, care needs to be taken to convert it from String to proper format. Let us understand why. Consider the following declarations: TextField ageField=new TextField(4); int age; ageField is an object of type TextField and can be displayed on the screen.

Course Introduction  Aditya P. Mathur Data input: String to int? No way! We want to type in an integer in the ageField area and assign this value to variable age. Can we do the following? age=ageField.getText(); The answer is NO because ageField.getText() is of type String whereas age is of type int. We cannot assign a value of type String to a variable of type int.

Course Introduction  Aditya P. Mathur Data input: Convert String to int To assign the number typed into the ageField to variable age, we need to first convert the string typed by the user to an integer. (Note that whatever is typed by the user is treated as a string by the JVM.) A String object can be converted to an int as follows: First obtain the string typed by the user: String ageTyped=ageField.getText(); Next convert the String object to an int value: age=Integer.parseInt(ageTyped);

Course Introduction  Aditya P. Mathur Data input: Convert String to double Similarly a String object can also be converted to a floating point number as follows. First obtain the string typed by the user: String weightTyped=weightField.getText(); Next convert the String object to an double value: weight=Double.parseDouble(weightTyped); Double weight; A String object can be converted to other values of primitive types. See Chapter 7.11 of your text.

Course Introduction  Aditya P. Mathur Data output: Convert int to String To output an integer value to a TextField or a TextArea object, one needs to first convert the value to a String. The string so obtained is then written into the object. int age; ageField.setText(Integer.toString(age)); The toString(int i) method takes an integer i as input and returns a string. For example, the following call: Integer.toString(-392) will return the string “-392”

Course Introduction  Aditya P. Mathur Data output: Convert double to String To output a double value to a TextField or a TextArea object, one needs to first convert the value to a String. The string so obtained is then written into the object. double weight; weightField.setText(Double.toString(weight));

Course Introduction  Aditya P. Mathur Data output: Convert boolean to String To output a boolean value to a TextField or a TextArea object, one needs to first convert the value to a String. The string so obtained is then written into the object. boolean fail=false; overWeightField.setText(.toString(fail));