16
Lab 4 - Variables

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

Embed Size (px)

Citation preview

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

Lab 4 - Variables

Page 2: Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance 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

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

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 . . .

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

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

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

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

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

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.

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

All Primitive Types

We typically use int for integers and double for real values even if they holdmore information than we require

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

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.

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

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, 899 + 130 = -29?• This is called overflow and it happens when we

try to represent a number larger than our integer variable will hold

0899+0130 ------- 1029

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

Storing Real Values

• Real values are essentially stored in scientific notation

• 12.33 = 0.1233 * 102

• 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

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

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

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

ASCII (8 bit subset of unicode)

* Table from http://www.jimprice.com/ascii-0-127.gif

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

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

32

32--- 14

Output:

first = 32 second = 14

first = 32 second = 32

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

---- -6

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

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

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)

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

That’s All!