Constants A constant is a variable whose value is expected to remain the same throughout a program It is considered “good programming style” to use constants.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Java
Chapter 2: Java Fundamentals cont’d
Strings Testing for equality with strings.
Java Programming Strings Chapter 7.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Variables – the Key to Programming. ICS-3M1 - Mr. Martens - Variables Part 1 What is a Variable? A storage location that is given a “name” You can store.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Fundamental Programming Structures in Java: Strings.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
CS0007: Introduction to Computer Programming Scope, Comments, Code Style, Keyboard Input.
CPS120: Introduction to Computer Science Lecture 8.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
CSC Programming I Lecture 9 September 11, 2002.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
The Math Class Methods Utilizing the Important Math Operations of Java!
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
CPS120: Introduction to Computer Science Variables and Constants.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Declaring variables The type could be: int double char String name is anything you want like lowerCaseWord.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Java-02 Basic Concepts Review concepts and examine how java handles them.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Arithmetic You can perform arithmetic with numbers and/or variables. Java follows mathematical order of operations (PEMDAS). Example: / 2  this.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Java for Beginners University Greenwich Computing At School DASCO
A variable is a name for a value stored in memory.
Agenda Warmup Lesson 1.4 (double precision, String methods, etc)
Chapter 2 Basic Computation
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Type Conversion, Constants, and the String Object
Chapter 7: Strings and Characters
Chapter 2 Edited by JJ Shepherd
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Chapter 3: Selection Structures: Making Decisions
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
Chapter 3: Selection Structures: Making Decisions
Agenda Warmup Lesson 1.4 (double precision, String methods, etc)
Other types of variables
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2: Java Fundamentals cont’d
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
AP Java Unit 3 Strings & Arrays.
Presentation transcript:

Constants A constant is a variable whose value is expected to remain the same throughout a program It is considered “good programming style” to use constants for certain variables – for instance, interest rate on a loan, pi, etc Constants are always written in uppercase To declare a constant, add the word final: final double PI= ; final int HOURS_IN_A_DAY= 24; PI = 3.7; // this would cause an error

Class Libraries and Packages A class library is a set of packages, each of which contains classes (files) of code that can be used by programmers You can use this code simply by importing a package into your program, with a single line of code Top line of code: import library.package.class This is why Object-Oriented languages like Java are so popular – you can use code someone else has written, over and over again

Double precision problem In Java, doubles are slightly imprecise. For example, sometimes if you multiply 6.0 by 3.0, the answer is , or So, if your program requires precision, either do not use doubles, or, use them but round them off to a certain amount of decimal places. Demo: DecimalRound

Working with Strings Characters in a String can be identified by their index #. The first character in a String has index # 0. You need to know the following String methods: Length( ) returns the number of characters in the String charAt(index) returns the char at the given index indexOf(char) reverse of charAt. returns the first index # of the given char substring(Start, End) returns the chars from index Start to index ( End – 1) substring(Start) when only the starting index is given, returns the chars from start to the end of the String toUpperCase, toLowerCase converts the entire String to upper or lowercase

Examples… String coin = “Nickel”; Demo… coderesult coin.length( )6 (even though the last letter is at index #5) coin.toUpperCase( )NICKEL coin.charAt(1)i coin.indexOf(“k”)3 coin.indexOf(“g”)–1 (since g was not found) coin.substring(1,3)“ic” (notice that letters 1 and 2 are the result, but not 3) coin.substring(2)“ckel” (starts at 2 and goes to the end)

Assignments 1.(Rush)  Prompt the user to enter a word.  If the word is “Neil,” then ask the user to enter a letter. If the letter is a or b, display “Peart.” If it’s another letter, display “Xanadu.”  If the word is “Geddy,” then ask the user to enter a number. If the number is odd and divisible by 9, display “Lee.” If the number is negative or equals 2112, display “YYZ.”  If the word is something else, display the 3 rd letter of the word.  Display how many letters are in the word.  Do you like the band Rush? You should. (see next slide)

2. (Middle)  Prompt the user to enter their middle name.  If the 1 st letter is a vowel, display “Vowel”, otherwise, “consonant”  If there are more than 8 letters in the name, display “long”, if between 5 and 8 (inclusive), display “medium”, otherwise, display “short.”  Display whether the # of letters in the name is even or odd. Also, if it’s even, display whether or not it is divisible by 4. If it’s odd, display whether or not it is divisible by 5.  If the letter ‘a’ is in the name, display the first index # it was found at (example: “The letter a was first found at index #3”) If it is not found, then say “The letter a was not found.”  If the first and last letters in the name are the same, display “same,” otherwise “different”  If the 2 nd letter and the 2 nd -to-last letter are both k, display “YES K”  Display the 1 st through the 3 rd letters.  Display all letters from the 2 nd to the end of the name.