Download presentation
Presentation is loading. Please wait.
Published byNoel Scott Cooper Modified over 9 years ago
1
Problem of the Day Why are manhole covers round?
2
Problem of the Day Why are manhole covers round? No matter how circle rotated, radius is constant so… …only shape that could not fall into the hole
4
Announcements If you need more review of Java… I have lots of good resources – talk to me Use “Additional Help” link on webpage Weekly assignments problems due before class Remove rust from summer and get back into coding During lectures will also add some new ideas
5
Style Guide Good style helps when writing code Program logic becomes easier to follow Easier to read and fix errors Starting & stopping work on project is far easier Prevents Prevents many common errors Style includes two parts: Formatting Documentation
6
Indentation Traditionally indent code within set of braces ({}) Be consistent with indentation size (I use 2 spaces)
7
Indentation Traditionally indent code within set of braces ({}) Be consistent with indentation size (I use 2 spaces) Nothing is
8
Indentation Traditionally indent code within set of braces ({}) Be consistent with indentation size (I use 2 spaces) Nothing is more
9
Indentation Traditionally indent code within set of braces ({}) Be consistent with indentation size (I use 2 spaces) Nothing is more annoying
10
Indentation Traditionally indent code within set of braces ({}) Be consistent with indentation size (I use 2 spaces) Nothing is more annoying than
11
Indentation Traditionally indent code within set of braces ({}) Be consistent with indentation size (I use 2 spaces) Nothing is more annoying than looking for the next line.
12
Indentation char _3141592654[3141 ],__3141[3141];_314159[31415],_3141[31415];main(){register char* _3_141,*_3_1415, *_3__1415; register int _314,_31415,__31415,*_31, _3_14159,__3_1415;*_3141592654=__31415=2,_3141592654[0][_3141592654 -1]=1[__3141]=5;__3_1415=1;do{_3_14159=_314=0,__31415++;for( _31415 =0;_31415<(3,14-4)*__31415;_31415++)_31415[_3141]=_314159[_31415]= - 1;_3141[*_314159=_3_14159]=_314;_3_141=_3141592654+__3_1415;_3_1415= __3_1415 +__3141;for(_31415 = 3141- __3_1415 ;_31415;_31415--,_3_141 ++,_3_1415++){_314 +=_314<<2 ;_314<<=1;_314+= *_3_1415;_31 =_314159+_314; if(!(*_31+1) )* _31 =_314 / __31415,_314 [_3141]=_314 % __31415 ;* ( _3__1415=_3_141 )+= *_3_1415 = *_31;while(* _3__1415 >= 31415/3141 ) * _3__1415+= - 10,(*--_3__1415 )++;_314=_314 [_3141]; if ( ! _3_14159 && * _3_1415)_3_14159 =1,__3_1415 = 3141-_31415;}if( _314+(__31415 >>1)>=__31415 ) while ( ++ * _3_141==3141/314 )*_3_141--=0 ;}while(_3_14159 ) ; { char * __3_14= "3.1415"; write((3,1) (--*__3_14,__3_14 ),(_3_14159 ++,++_3_14159))+ 3.1415926; } for ( _31415 = 1; _31415<3141- 1;_31415++)write( 31415% 314-( 3,14),_3141592654[ _31415 ] + "0123456789","314" [ 3]+1)-_314; puts((*_3141592654=0,_3141592654)) ;_314= *"3.141592";}
13
Braces Always use braces: Always use braces: for (int i = 0; i < n; i++); sum = sum + i; sumSquare = sumSquare + (i*i);
14
Braces Always use braces: Always use braces: for (int i = 0; i < n; i++); sum = sum + i; sumSquare = sumSquare + (i*i); Using braces vs. fixing a bug 10 seconds 30 minutes
15
Increment/Decrement Operators Only use ++ & -- on own line (or for loops) // What is result of each line? while (++k < n) { a[i++] = (2 * i) + 6; b[j] = (j++ * j) - 1; c[j] = a[j] +++ b[j]; }
16
Statements Use variables to break up complex ideas // What is this computing? return ( (year % 4 == 0) && (year % 100 != 0)) || ( (year % 100 == 0) && (year % 400 == 0));
17
Statements Use variables to break up complex ideas divisibleBy4 = ((year % 4) == 0); divisible100 = ((year % 100) == 0); divisible400 = ((year % 400) == 0); return (divisible4 && !divisible100) || (divisible100 && divisible400);
18
Comments We program using “code” Not easy to read even simple projects Review your CSC 111 projects (even better, review a friend’s CSC 111 project) Comments break up this code Provide simple English descriptions State assumptions and preconditions Describe the outcome of a section
19
Comments private static void compileResults(String server, String handle) throws IOException { for (Experiment e : Experiment.values()) { Benchmark[] benches = e.getResults(); File newFile = getOutFile(server, e.name(), handle); newFile.createNewFile(); PrintStream out = new PrintStream(newFile); Double[] baselines = computeBaselineMeans(server, e.getResults(), handle, BASELINE, benches); for (Build b : Build.values()) { File[] files = new File[benches.length]; Scanner[] scanners = new Scanner[benches.length]; for (int i = 0; i < files.length; i++) { files[i] = getInFile(server, benches[i], b, handle); scanners[i] = new Scanner(files[i]); } String[] lines = new String[scanners.length]; int heapSize = 0; while (scanners[0].hasNextLine()) { out.print(b.name() + "\t"); for (int i = 0; i < scanners.length; i++) { lines[i] = scanners[i].nextLine(); out.print(lines[i] + "\t"); }
20
Ever Have A Bug? At some point, everyone must fix bug in code What should you do first after discovering the bug?
21
Ever Have A Bug? At some point, everyone must fix bug in code What should you do first after discovering the bug?
22
Ever Have A Bug? At some point, everyone must fix bug in code What should you do first after discovering the bug?
23
Ever Have A Bug? At some point, everyone must fix bug in code What should you do first after discovering the bug?
24
Ever Have A Bug? At some point, everyone must fix bug in code What should you do first second after discovering it?
25
Fixing Bugs Three common ways students fix bugs: Guess where error is and make random change Use Eclipse debugger to run program & find source Get out your pencil & paper and draw a memory trace
26
Normal Method of Debugging Also called "debugging-via-drunken-walk" SMALL Can work for SMALL programs with limited choices Guessing for millions of lines of code harder Not at all effective at actually fixing errors Entire point is to avoid understanding bug's cause "Fix" may cause other errors later on in program Cannot prevent future errors using this approach
27
Better Method of Debugging Could use Eclipse debugger to find and fix bug Set breakpoints to stop program where bug likely Use step filters to walk through the program bit by bit Spend a few hours learning how to make it work Can get to root of problem once it is executed But requires executing entire program to trigger bug Cannot work backward to try and understand why
28
Best Method of Debugging Generating memory trace has many benefits Could check algorithm – don’t need any code to try Can make up situations and just trace a method Look back through trace to see root cause of bug Trace code by running it like you were computer DON’T SKIP STEPS DON’T SKIP STEPS – easy to (wrongly) assume things As you go along update variables’ values GIGO effects limit tracing’s effectiveness
29
Best Method of Debugging Generating memory trace has many benefits Could check algorithm – don’t need any code to try Can make up situations and just trace a method Look back through trace to see root cause of bug Trace code by running it like you were computer DON’T SKIP STEPS DON’T SKIP STEPS – easy to (wrongly) assume things As you go along update variables’ values GIGO effects limit tracing’s effectiveness
30
Benefits of Memory Traces Draw what actually occurs in memory Pictures used to debug rather than bare text Much easier to understand what a method does Excellent way to learn new programming topics Past studies found improves student grades Debugging time decreased greatly at same time Greater support for these traces in Eclipse Drawing of static trace integrated with debugger Over this term should see tools improve
31
Starting a Memory Trace Paper holds 3 areas separated by vertical lines Program stack drawn on left part of page Right side of page holds the program heap Program output shown outside the stack & heap Objects allocated during run drawn in the heap Will discuss how this is done next week Draw frame on top of stack each method call Fancy name for box labeled with method name Each parameter & local variable needs space in box
32
Data Types 8+1 primitive data types Examples: boolean, byte, char, int, double, String * Only types that work with Java operators Operators include: +, -, %, &&, ||, >=, <, ! Primitives used natively by computers Means using them is very quick Implies greater support within the language
33
Primitive Types Primitive variables are simple to use Each variable is “xerox” holding a value Assignment copies value Update assigned variable only; changes not reflected
34
Starting a Memory Trace public static void main(String[] args) { String s = “”; for (int i = 0; i < 5; i+=2) { s = s + i; System.out.println(s); } }
35
Writing a Memory Trace public static int fibber(int i, int j) { return i + j; } public static void main(String[] args) { int f = 0; int s = 1; for (int i = 0; i < 5; i+=4) { int oldF = f; f = s; s = fibber(oldF, f); } }
36
Your Turn Get into groups and complete activity
37
For Next Lecture Reading AF Chapter 3 & 7.13 for Friday Continues Java review & looks at references Introduces enum s – first really new concept of term Use language template to take notes on enum s! There is weekly assignment problem on Angel Due before Friday’s lecture (via e-mail) Get back into the swing of writing Java code
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.