Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Written by: Dr. JJ Shepherd
Constants and Data Types Constants Data Types Reading for this class: L&L,
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Road Map Introduction to object oriented programming. Classes
Introduction to Computers and Programming Lecture 7:
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Input & Output: Console
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
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,
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
Copyright © – Curt Hill Types What they do.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
1.2 Primitive Data Types and Variables
1 MATERI PENDUKUNG TIPE DATA Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Basic Java Syntax Comments Basic data types Operators and assignment.
Written by: Dr. JJ Shepherd
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Object Oriented Programming Lecture 2: BallWorld.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Week 2 - Wednesday CS 121.
Chapter 4 – Fundamental Data Types
Multiple variables can be created in one declaration
Selenium WebDriver Web Test Tool Training
IDENTIFIERS CSC 111.
Introduction to Java, and DrJava part 1
Coding Concepts (Data- Types)
Building Java Programs
Introduction to Java, and DrJava
Defining Classes and Methods
Introduction to Java, and DrJava part 1
Subtype Substitution Principle
Variables and Constants
Presentation transcript:

Lab 4 - Variables

Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables and methods with: – private – only that class can see – public – any class can see Standard: – all instance variable are private – methods others uses are public – methods used only by the current class are private

Getters and Setters Since we have hidden our instance variables we need some way to let others view or modify them Make public methods to provide that access: – getter: returns the value in the variable – setter: changes the value in the variable – naming conventions...

Example of Accessors public class SimpleClass { private int instanceVariable; public int getInstanceVariable() { return instanceVariable; } public void setInstanceVariable(int val) { instanceVariable = val; } Instance Variable Getter Setter SimpleClass x = new SimpleClass(); x.setInstanceVariable(32); System.out.println(x.getInstanceVariable()); Use

Why did we bother with that? There is one place where the value of the variable changes – since variable is private, no code in other classes can change it – only the setter changes it in our code A single breakpoint catches ANY changes to the value

Primitive Types for Integers Remember: primitive means that the compiler knows how much space to allocate This means that these types have fixed space and, therefore, have limitations on the values they can hold.

All Primitive Types We typically use int for integers and double for real values even if they hold more information than we require

Integers and Sign Bits Every integer has a boolean that represents whether the number is negative (true implies the number is negative) That boolean is stored as a 1 or 0 at the high end of the integer. Suppose we can hold three decimal digits in an integer, then -999 would be stored as 1999 and 999 would be stored as 0999.

What Happens When Things Get Too Large? Suppose we can only hold three decimal digits (so we can store numbers from -999 to 999) What happens if we add 130 to 899? So, = -29? This is called overflow and it happens when we try to represent a number larger than our integer variable will hold

Storing Real Values Real values are essentially stored in scientific notation = * 10 2 So the computer just has to store the two parts: 1233 and 2 Instead of having firm upper and lower bounds, real variable types vary in the precision they can represent

char Type and Unicode The computer stores EVERYTHING as a number When we store characters (in char variables or in Strings), we actually store integers Unicode defines the matching of these integers to characters

ASCII (8 bit subset of unicode) * Table from

Primitive vs. Reference in Assignment Statements int first; int second; first = 32; second = first; System.out.println("first = " + first + " second = " + second); second = 14; System.out.println("first = " + first + " second = " + second); first second Output: first = 32 second = 14 first = 32 second = 32

Now – With Reference Variables SimpleClass first; SimpleClass second; first = new SimpleClass(); first.setInstanceVariable(32); second = first; System.out.println(second.getInstanceVariable()); second.setInstanceVariable(-6); System.out.print(first.getInstanceVariable()); first second SimpleClass instanceVariable 32 Output: 32 -6

BigIntegers A class that comes with Java that can hold integers that are arbitrarily large – Think about the three ways you can interpret “class” in that statement – class implies reference (not primitive) As part of the lab, you have to go out and find the java API (Application Programming Interface)

That’s All!