College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
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.
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.
CMT Programming Software Applications
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
String Escape Sequences
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Input & Output: Console
C Tokens Identifiers Keywords Constants Operators Special symbols.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Primitive Variables.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
© A+ Computer Science - Variables Data Types © A+ Computer Science - Lab 0B.
Primitive Variables.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done.
Copyright Curt Hill Variables What are they? Why do we need them?
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 2 Variables.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Fundamentals 2.
Chapter # 2 Part 2 Programs And data
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Java Variables and Types
Lecture 2 Data Types Richard Gesick.
Documentation Need to have documentation in all programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Variables and Primative Types
Data Types The type states how much and what kind of data the variable can store. integers- whole numbers no fractional parts int, short, long floating.
Escape Sequences What if we wanted to print the quote character?
IDENTIFIERS CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Variables ICS2O.
© A+ Computer Science - VARIABLES © A+ Computer Science -
Chapter 2 Variables.
Fundamentals 2.
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Variables Data Types Lab 0B
Chapter # 2 Part 2 Programs And data
Chapter 2: Java Fundamentals
Primitive Types and Expressions
© A+ Computer Science - Variables & Data Types © A+ Computer Science - ©A+ Computer Science
Names of variables, functions, classes
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Variables.
Variables and Constants
Presentation transcript:

College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs - Constant declarations ; Variable declarations Standard Data Stuctures - Simple data types (int, boolean, double)

Vocabulary & Terms Reference Variable Keyword Identifier Data type int char double boolean Object Initializing a variable Assignment operator

A reference variable refers to a location in memory that stores an object. Monster fred = new Monster(); Monster sally = new Monster(); In the above example Monster is a class. fred and sally are references to Monster objects. new is a keyword which is used to create an object. It is followed by the constructor method, which is the class name, and parenthesis. The first part: Monster fred – declares a reference variable to hold a Monster object. The second part: = new Monster(); - creates the Monster object, and assigns fred to the location where this object is in memory.

fred Monster Monster fred = new Monster(); 0xF5 Monster fred creates a reference. new Monster(); creates the Monster at location 0xF5. The assignment operator (=) gives fred the address 0xF5 to store

What is a variable?

A variable is a storage location for some type of value. Type the statements below into the interactions pane. Type the name of the variable to see what is stored in the variable. What is stored in yesOrNo? days 102 yesOrNo int days = 102; double taxRate = 7.75; boolean yesOrNo; taxRate 7.75

int days = 102; days 102 int days – declares days as a variable that will store integer values. = 102 assigns days to store the integer value 102.

How do you name variables when defining them?

When naming a variable follow these rules and conventions: Make the variable name meaningful. That means that L is not a meaningful variable name but length is meaningful. When reading your program, I shouldn’t be guessing what the variable is meant to hold. Start all variable names with a lower-case letter. Variable names may also contain letters, numbers and underscores. If a variable name is more than one word long, capitalize each of the other words without adding any spaces. Below are some examples of valid variable names: number sum32 testAverage area_Of_Trapezoid and below are some examples of invalid variable names: 2ndValue test Average question#2

Which of these would be legal variable identifiers? int 1stYear; double jump Up; double feet2Inches; IDENTIFIER is a fancy word for variable.

Java is case sensitive – it interprets upper and lower case letters as different letters. Brandon does not equal brandon. Brandon != brandon

Keywords are reserved words that the language uses for a specific purpose. You cannot use keywords as identifier names. Some that you’ve seen are: int double return voidmain public static long break continue class You can find the complete list of keywords here:

What is a data type?

byte short int long float double char boolean int num = 9; double total = 3.4; We will use int, double and boolean in this course.

TYPESIZERANGE byte8 bits-128 to 127 short16 bits to int32 bits-2 billion to 2 billion long64 bits-big to +big

Think about a light bulb. It has 2 states ON or OFF. The same principals can be applied to a computer. The electronic circuits are either On or Off. Different combinations of On and Off mean different things for the computer. For the computer On is 1, and Off is 0. A number system made up of 1’s and 0’s is known as the Binary Number System. We use the Decimal Number System: digits 0 – 9.

What is a BIT? Each 1 or 0 is called a BIT. It is short hand… take the b at the start of binary and the it at the end of digit… BIT = Binary digIT. Memory consists of bits and bytes. 1 byte = 8 bits Every character on the keyboard has a binary digit associated with it in accordance with the UNICODES. Look at the following site: What is the Binary Number for the letter A? What about a? 7? The more bits you have the more you can store.

int one = 120; int two = ; System.out.println(one); System.out.println(two); (Type the code above into the interactions pane in Dr. Java and check your output.) OUTPUT

Type the following code into Dr. Java exactly as it is written. Compile the code and run the program. Are the results what you expected? //integer example public class Integers { public static void main(String args[]) { int one = 120; //legal assignment int two = ; int three = ; System.out.println(one); System.out.println(two); three = three * 3; //creates an overflow error at runtime System.out.println(three); }

TYPESIZERANGE float32 bits-big to +big double64 bits-big to +big Float has 7 digits of precision and double has 15 digits of precision. We are talking about decimal numbers now! int can NOT store decimal numbers.

double one = 99.57; double two = 3217; double three = 23.32; System.out.println(one); System.out.println(two); System.out.println(three); (type the code above into the interactions pane in Dr. Java and check your output.) OUTPUT

Type the following program into Dr. Java exactly as it is written. Compile the code and then run the program. Are the results what you would have expected? //real number example public class Reals { public static void main(String args[]) { double one = 99.57; double two = 3217; System.out.println(one); System.out.println(two); }

Characters store letters. char let = ‘A’; char is a 16-bit unsigned integer data type. We use single quotes for character literals. We use double quotes for String literals… more than 1 character.

char is a 16-bit unsigned int data type. For example, here is a 16 bit pattern: char let = 65; ASCII VALUES YOU SHOULD KNOW!!! ‘A’ – 65 ‘a’ – 97 ‘0’ - 48

char alpha = 'A'; char ascii = 65; char sum = 'B' + 1; System.out.println(alpha); System.out.println(ascii); System.out.println('B'+1); //’B’ gets converted to its ASCII value //66 and then 1 is added to it. System.out.println(sum); //since sum holds 67 and it is a char, we //get C. OUTPUT A 67 C

Type the following program in Dr. Java exactly as it is written. Compile the code and run the program. Are the results what you expected? public class Char { public static void main(String args[]) { char alpha = 'A'; char ascii = 65; char sum = 'B' + 1; System.out.println(alpha); System.out.println(ascii); System.out.println('B'+1); //char is an integer type System.out.println(sum); }

The boolean data type can store true or false only. boolean heads = true; boolean tails= false;

Type the following program into Dr. Java exactly as it is written. Compile the code and run the program. Are the results what you expected? public class BooleanTest { public static void main(String args[]) { boolean stop = true; boolean go = false; System.out.println( stop ); stop = go; System.out.println( go ); System.out.println( stop ); }

In JAVA, you have 8 primitive data types that take up very little memory. Everything else in Java in an Object. Strings are objects. String temp = "abc";

receiver = 57; receiver is assigned( = ) the value 57. In an assignment statement, the receiver is always on the left of the assignment symbol ( = ). Assignment statements examples : total = 10; amount = ;

You can initialize more than one variable in one statement if they have the same type. Just separate them using a comma. int number = 75, it=99; float taxrate = 7.75; char letter = ‘A’, newlet = ‘a’; boolean isprime = false; String sone = "abc";

Java is a strong typed language. As a result, you must be very careful to look at data types when assigning values. int one=90; char letter= ‘A’; char let= 65; one=letter; letter=let; one=let;

int one = 90; double dec = 234.5; char letter = 'A'; System.out.println( one ); one = letter; //char to int System.out.println( one ); one = 'A'; //char to int System.out.println( one ); System.out.println( dec ); dec = one; //int to double System.out.println( dec ); System.out.println (one); one = dec; //double to int… Illegal System.out.println (one); OUTPUT Error

Type the following program into Dr. Java. Compile the code and run the program. Are the results what you expected? //strong typed language example public class MixingData { public static void main(String args[]) { int one = 95; double dec = 12.4; char letter = ‘B'; System.out.println( one ); one = letter; //char to int System.out.println( one ); one = 'A'; //char to int System.out.println( one ); System.out.println( dec ); dec = one; //int to double System.out.println( dec ); System.out.println( letter ); //letter = dec; //double to int - not legal //System.out.println( letter ); }