Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
CMT Programming Software Applications
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Data Input and Output Using the Library Software CSC 1401: Introduction to Programming with Java Lecture 4 – Part 1 Wanda M. Kunkle.
Chapter 2: Introduction to C++.
The Java String Type CSC 1401: Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Week 1 Algorithmization and Programming Languages.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 2 Variables.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Topics for today: 1.Comments 2.Data types 3.Variable declaration.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Yanal Alahmad Java Workshop Yanal Alahmad
Revision Lecture
Data types and variables
Java Programming: From Problem Analysis to Program Design, 4e
IDENTIFIERS CSC 111.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Chapter 2 - Introduction to C Programming
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Chapter 2 - Introduction to C Programming
Expressions and Assignment
elementary programming
Chapter 2: Introduction to C++.
Chapter 2 - Introduction to C Programming
Primitive Types and Expressions
Chapter 2 Variables.
Introduction to C Programming
Getting Started With Coding
Presentation transcript:

Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle

2 Sample Java Program: Time.java Let’s begin by examining the components of the second program from Lab 1 … Let’s begin by examining the components of the second program from Lab 1 …

3 Sample Java Program: Time.java 1 class Time { 2 public static void main ( String[] args ) { 3 float distance, speed, time; 4 output out = new output(); 5 input in = new input(); 6 out.writeln( "Please enter distance (in miles)" ); 7 distance = in.readfloat(); 8 out.writeln( "Please enter speed (in MPH)" ); 9 speed = in.readfloat(); 10 time = distance / speed; 11 out.writeln( "The time taken to travel " + distance + " miles going at " + speed + " MPH is " + time + “ hours " ); 12 } //end main 13 } //end Time class 1 class Time { 2 public static void main ( String[] args ) { 3 float distance, speed, time; 4 output out = new output(); 5 input in = new input(); 6 out.writeln( "Please enter distance (in miles)" ); 7 distance = in.readfloat(); 8 out.writeln( "Please enter speed (in MPH)" ); 9 speed = in.readfloat(); 10 time = distance / speed; 11 out.writeln( "The time taken to travel " + distance + " miles going at " + speed + " MPH is " + time + “ hours " ); 12 } //end main 13 } //end Time class

4 Sample Java Program: Time.java Overall form of a Java program: Overall form of a Java program: class Time { public static void main ( String[] args ) { class Time { public static void main ( String[] args ) { // Code to do the work of the program // Code to do the work of the program // (i.e., instructions to the computer) } //end main } //end Time class // (i.e., instructions to the computer) } //end main } //end Time class Inner block Outer block Name of program Note: A "block" is a list of statements in curly braces. Indicates a comment, text used to annotate a program that the compiler will ignore

5 Sample Java Program: Time.java Line 3: float distance, speed, time; Line 3: float distance, speed, time; Line 3 says that distance, speed, time are variables that can store floating point numbers (decimal numbers). Line 3 says that distance, speed, time are variables that can store floating point numbers (decimal numbers). Line 3 is called a statement. All statements in Java must end with a semicolon. Line 3 is called a statement. All statements in Java must end with a semicolon.

6 Variables A variable is a location in the computer’s memory where a piece of data can be stored for use in a program. A variable is a location in the computer’s memory where a piece of data can be stored for use in a program. All variables in Java must be given a name and a type before they can be used. This is called “declaring” a variable. All variables in Java must be given a name and a type before they can be used. This is called “declaring” a variable. Line 3: float distance, speed, time; Line 3: float distance, speed, time; type names

7 Rules for Naming Java Variables Variable names can consist of: Variable names can consist of: Letters Letters Digits Digits Underscores ( _ ) Underscores ( _ ) Dollar signs ( $ ) Dollar signs ( $ ) Variable names cannot start with a digit or contain spaces. Variable names cannot start with a digit or contain spaces. Valid variable names: Valid variable names: num1, firstName$, exam_one num1, firstName$, exam_one Invalid variable names: Invalid variable names: 3integers, course average 3integers, course average

8 Rules for Naming Java Variables Variable names can consist of any number of valid characters (i.e., there is no official limit to the length of a variable name). Variable names can consist of any number of valid characters (i.e., there is no official limit to the length of a variable name). Variable names should be descriptive of their use. For example: Variable names should be descriptive of their use. For example: grade would be a good name to use for a variable that stores a student's test grade. grade would be a good name to use for a variable that stores a student's test grade. Variable names are case sensitive. For example: Variable names are case sensitive. For example: x1 is not the same as X1 x1 is not the same as X1 Keywords such as int and float cannot be used as variable names. Keywords such as int and float cannot be used as variable names. Keywords are words reserved for use by the Java language. Keywords are words reserved for use by the Java language. Additional keywords that appear in Time.java include: class, public, static, void, new Additional keywords that appear in Time.java include: class, public, static, void, new

9 More on Declaring Variables float distance, speed, time; could also have been declared separately as: float distance; float speed; float time; since the three variables are all the same type. float distance, speed, time; could also have been declared separately as: float distance; float speed; float time; since the three variables are all the same type.

10 Data Types There are two main kinds of types in Java: There are two main kinds of types in Java: Primitive type Primitive type Stores a single piece of data of a specific type Stores a single piece of data of a specific type Some primitive types and the kinds of data they store are: Some primitive types and the kinds of data they store are: int – a 32-bit integer (A bit is a 0 or 1.) int – a 32-bit integer (A bit is a 0 or 1.) float – a 32-bit floating point (decimal) number float – a 32-bit floating point (decimal) number double – a 64-bit floating point (decimal) number double – a 64-bit floating point (decimal) number char – a single character (e.g., ‘Y’) char – a single character (e.g., ‘Y’)

11 Data Types Class type Class type Stores multiple pieces of data and provides methods for manipulating that data Stores multiple pieces of data and provides methods for manipulating that data Some class types are: Some class types are: String – stores and manipulates a string of characters contained between double quotation marks, e.g., “Susan” String – stores and manipulates a string of characters contained between double quotation marks, e.g., “Susan” input – stores and manipulates text entered at the keyboard input – stores and manipulates text entered at the keyboard Variables that are class types are called objects. Variables that are class types are called objects.

12 Declaring Variables/Objects of Different Data Types Examples: Examples: int firstNum, secondNum; // integer variables int firstNum, secondNum; // integer variables double root1, root2; // floating point (decimal) // variables double root1, root2; // floating point (decimal) // variables char response; // character variable char response; // character variable String first_name, last_name; // string objects String first_name, last_name; // string objects output out; // output object output out; // output object

13 Assignment in Java To give a variable/object a specific value, we assign that value to the variable/object using the assignment operator, ‘=’. To give a variable/object a specific value, we assign that value to the variable/object using the assignment operator, ‘=’. Assignment requires that the types on both sides of the assignment operator be compatible. Assignment requires that the types on both sides of the assignment operator be compatible.

14 Assignment in Java Examples using primitive types: Examples using primitive types: float distance; distance = 100.5;// compatible distance = 80;// compatible distance = “25 miles”;// NOT compatible float distance; distance = 100.5;// compatible distance = 80;// compatible distance = “25 miles”;// NOT compatible int num1; num1 = 10;// compatible num1 = 16.4;// NOT compatible int num1; num1 = 10;// compatible num1 = 16.4;// NOT compatible char response; response = ‘Y’;// compatible response = “Y”;// NOT compatible char response; response = ‘Y’;// compatible response = “Y”;// NOT compatible

15 Assignment in Java Examples using class types: Examples using class types: String fullName; fullName = “John Smith”; // compatible fullName = John Smith; // NOT compatible String fullName; fullName = “John Smith”; // compatible fullName = John Smith; // NOT compatible output out; out = new output();// compatible input in; in = new input();// compatible out = in;// NOT compatible output out; out = new output();// compatible input in; in = new input();// compatible out = in;// NOT compatible

16 Assignment in Java Examples mixing primitive and class types: Examples mixing primitive and class types: input in; in = new input(); float distance; distance = in.readfloat(); // compatible int newDistance; newDistance = in.readint();// compatible newDistance = distance;// NOT compatible (Why not?) input in; in = new input(); float distance; distance = in.readfloat(); // compatible int newDistance; newDistance = in.readint();// compatible newDistance = distance;// NOT compatible (Why not?) Notes: readfloat is a method that retrieves a decimal number. readint is a method that retrieves an integer. Notes: readfloat is a method that retrieves a decimal number. readint is a method that retrieves an integer.

17 Friday’s Topic Arithmetic expressions Arithmetic expressions