Anda di halaman 1dari 60

CSC535

Grades

80% hw and reports

20% final exam

Problem Solving

humans are really good problem solvers

How do we solve problems?

memory

ask questions

repetition

Programming Language

mimic the way human beings solve problems with


the ultimate goal of telling a computer what to do.

humans speaks natural languages

computers cant understand natural languages

computers need unambiguous languages to


follow instructions without any room for
misinterpretation.

The mapping

memory = variables

asking questions = conditionals (if statements)

repetition = loops

CPU

central processing unit

perform the magic tricks that we tell them to


perform

if we tell them to perform the right magic tricks in


the right order some problem may be solved

There are 3 types of


programming languages

machine language - 0s and 1s

low level - is a human readable/writable


language that has a 1 to 1 relationship with the
magic tricks on the CPU (Assembly)

high level - is a human readable/writable


language that has a 1 to many relationship with
the magic trick on the CPU (JAVA, C++, C,
C#)

There are 2 types of


programming languages

interpreted - is typically a high level language


that uses a tool known as an interpreter which
translates a high level language into a low level
language in real time. (During RunTime) RELATIVELY SLOW

compiled - is typically a high level language that


uses a tool known as a compiler which translates
a high level language into a low level language.
(Before RunTime) - RELATIVELY FAST

There are 2 types of


programming languages

domain specific - are languages designed to


solve a very specific class of problem. For
example, HTML is a domain specific language.
LEGO mindstorm language

general purpose - are languages designed to


solve a large classification of problems. C++,
JAVA, C#.

There are 3 types of


programming languages

functional - are languages whose features are


encapsulated within specific functions that when used in a
certain order solve a problem. Many may be domain
specific or at least less than completely general purpose.
LISP, SCHEME, PROLOG

procedural - tend to be monolithic in nature and both


public and private details are exposed all the time.

object-oriented - allow humans to solve problems in a way


similar to how they solve them in real life. Objects are
represented as only the public interfaces that make them
usable.

There are 2 types of


programming languages

loosely typed - do not require us to specify what


kind of value a variable is to hold. (PHP, Java
Script) - Many loosely typed languages are also
Interpreted languages - SLOW

strongly typed - DO require us to specify what


kind of value a variable is to hold. (JAVA, C++,
C, C#) - Many strongly typed languages are also
compiled languages. FAST

History of Languages

Modern Programming Languages Big 3

C# (.NET languages - VC++, C#, VB,


ASP.NET) - Windows - Visual Studio - IDE

JAVA - Everything + Android - Eclipse

Objective-C - Mac & iOS - Xcode

hybrid: structs, function pointers


ObjC

C++
JAVA
C#

JAVA

Take everything that is good about C++, but:

platform independent - write once, run


anywhere

dont allow direct memory access

Java
Source
Code
Java
Source
Code

Java
Source
Code

Java
Byte
Code

MAC
JVM

compiler

Java
Byte
Code

PC
JVM

compiler

Java
Byte
Code

Android
JVM

compiler

JIT
interpreter

MAC
CPU

interpreter

PC
CPU

interpreter

ARM
CPU

VB
Source
Code
C#
Source
Code

compiler

MSIL

.NET
CA

JIT

CPU

VC++
Source
Code

C# is platform independent - write once, run anywhere..


as long as you are running the .NET component architecture
which Microsoft makes
only for WINDOWS

Why didnt JAVA die?

Google was a JAVA shop - Google hates


microsoft

Applet - a JAVA program that launches and runs


within a web browser

COM/Active-X

Variables

are named container for holding values which


mimic the way humans solve problems.

type var_name = value;

variable names should be expressive

your name might be stored in a variable called


myName rather than a variable call bob

type?

the kind of crap you will store in variables

types define the details of those values

Primitive Types

are basic types built into the language that are


ONLY capable of holding a value.

byte - signed whole integers - 8 bits -128 to +127

short -signed whole integers - 16 bits -32768 to +32767

int - pos/neg whole integers - 32 bits -2billion to +2billion

long - pos/neg whole integers - 64 bits -big num to + big num

char - pos whole integers - 16 bits 0 to 65535

float - pos/neg decimal point vals - 32 bits.16 bits

double - pos/neg decimal point vals - 64 bits. 16bits

boolean - pos whole integers 0 or 1 (true or false)

bits

0s and 1s

byte is 8 bits

128 64 32 16 8

100 10 1

(100 * 1) + (10 * 2) + (1 * 5) = 125

bits vs bytes

1 bit 0 or 1

1 byte = 8 bits

1 kb = 1024 bits

1 KB = 1024 bytes

1 mb = 1024 kb

1 MB = 1024 KB

1 gb = 1024 mb

1 GB = 1024 MB

1 tb = 1024 gb

1 TB = 1024 GB

100 Mbps

16 * 1024 * 1024 / 8 / 1024 / 1024

ASCII

american standard code for information


interchange

8 bit - 0 to 255

256 unique characters in the ASCII character set

UNICODE

16 bit character set

0 - 65535 or 65536 unique chars

boolean expression

is any expression that boils down to a single


value of true or false

primitive arithmetic boolean


operators

<

>

<=

>=

==

!=

primitive logical boolean


operators

&& - and - is at least one side of the expression si false,


the entire expression is false

|| - or - if at least one side of the expression is true, the


entire expression is true

! - not

^ - xor - like an or, but at most one side may be true

| - bitwise or

& - bitwise and

arithmetic operators

% - the result of a positive number n % a positive


number x will be in what range?

ask questions

conditional - if statements and switch statements

if(boolean-expr) { //zero or more statements }


else { //zero or more statements }

repetition

loops

for loops - pre-check loop which lends itself to


counting problems

while loops - pre-check loop which lends itself to


problems where we do not know how many times we
must repeat.

do while loops - post-check loops that lend


themselves to problems where the body must execute
at least one time.

for loop
for(var_defs ; boolean-expr ; var_changes)
{
//zero or more statements
}
for(int i = 0 ; i < 1000 ; i = i + 1)
{
System.out.println(i);
}
//here

while loop
while(boolean-expression)
{
//body
}

do while loop
do
{
//body
}
while(boolean-expression);

Variable Scope

where can you access a variable

variables always resolve to their MOST LOCAL


DEFINITION

public class Driver


{
public static void main(String[] args)
{
for(int i = 0; i < 10; i = i + 1)
{
System.out.println(i);
}
int i = 0;
while(i < 10)
{
System.out.println(i);
i = i + 1;
}
System.out.println(i);
}
}

++ ops

++ - increments an integer value

-- - decrements an integer value

int i = 0;

i++;

i = i + 1;

primitive vs object

primitives - built into the language and can


ONLY hold a value

objects - built into the language and can hold


zero or more values and potentially do MORE

methods

reusable chunk of code that takes zero or more


inputs (parameters) and potentially produces an
output.

its purpose is to allow us to write something


once and then use it over and over again

return_type method_name(inputs){ //do stuff }

String

Java is a String based language

collection of char

- empty string

String s = hello;
h

String Methods

int length() - returns the number of chars in the


calling String

char charAt(int index) - returns the char at the


specified index of the calling String

int indexOf(char c) - returns the int position in the


calling String where the char c first occurs or -1
if the char c is NOT FOUND

s = 1 2
100 10

3
1

10^2 10^110^0

3 -> 3

(3*1) + (2 * 10) + (1 * 100)


123

converts to int
true
stringToInt

isNumber
false

-5

throw exception

Character

static boolean isDigit(char c) - returns true if c is


a number and false otherwise

Class Methods

have the static keyword at the time the method is


being define

are called using the name of the class in which


the method was defined

Instance Methods

DO NOT have the static keyword at the time the


method is being defined

are called using an instance of the class in


which the method was defined

Context

the type of environment in which we are making


a variable or method call

static context - occur within a method defined as


a class method using the static keyword

non-static context

More String methods

String substring(int begin) - returns a string that


is the original string minus the character
between bucket 0 and begin (non-inclusive).

String substring(int begin, int end) - returns a


string that is all of the chars between begin and
end (non-inclusive)

Polymorphism

more than one method with the same name but


a different number and or type of parameter

String manipulation

123+5-7

StringTokenizer

0123456789ABCDEF
11 10 13
B A D
256 16 1
(256*11) + (16*10)+(1 * 13)

num = 110
num.charAt(2) -> 0
4
2 1
4 -> 4

2989

28/2
14/2
7/2
3/2
1/2
0

0
0
1
1
1
11100

2989/16
186/16
11/16
0

13 D
10 A
11 B
BAD

16

pointer

primitive type value that is capable of holding a


memory address

POINTERS DO NOT EXIST IN JAVA!!!!!!!!

JAVA does not have a specific syntax for


pointers because EVERYTHING is a pointer

Passing Variables

pass by value - sends a COPY of the value as a


parameter. (pass the actual house), (pass an int)

pass by address - sends a note with the address


of where the actual value can be found in
memory as a parameter. (pass the house by
giving me the address),

array

sometimes we need to store more than one value


inside of a single variable

a collection of like data stored contiguously in


memory.

syntax: type[] varName = new type[num vals];

are considered to be very FAST

are consider to be memory inefficient

new keyword

our real estate agent

it ALWAYS returns a memory address

int[] ar = new int[5];


ar.length
100 104 108 112 116

-10

100

ar
100

elephant
changeSomething(int[] ar)
1/2 + 1/4
4/8 + 2/8 = 6/8
3/4

Contact

michael.Litman@cuw.edu

262-353-6205

Anda mungkin juga menyukai