Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
UCSD CSE 15L
 
Java Debugging and
the Scientific Method
Week 3
October 12, 2011
Quiz today
1.Keep your quiz sheet upside down.
2.At the announcement, turn it over and start.
3.Write on the front and, if necessary, on the back.
4.You have seven minutes.  
5.Turn the quiz over again, at the announcement.
Today’s quiz question
Your program terminates with the following uncaught exception:
Exception in thread "thread-0" 
java.lang.ArrayIndexOutOfBoundsException: 12
at Test$Runner.me(Test.java:20)
at Test$Runner.you(Test.java:16)
at Test$Runner.him(Test.java:12)
at Test$Runner.run(Test.java:24)
at java.lang.Thread.run(Unknown Source)
For the following three questions, write down the answer, if it can be determined 
from what is shown above.  If not, write ``cannot be determined.''
a. [2 points] Which array was attempted to be accessed?
b. [2 points] Which array index was attempted to be accessed?
c. [2 points] Inside which method was the exception triggered?
Feedback on last week’s quiz
... it is often a better idea to put the print command inside an if command like 
the following:
if (flag) 
   { System.out.println("the value of x is " + x); }
[6 points]  Explain why the latter is a better idea.  What should the type of 
flag be?  Should flag be a constant or a variable? 
• The text in red tells you that flag cannot be an expression, 
for example x > 0.
• See the handout from the first lab for what “debug mode” is.
Please check Gradesource
Everyone should have scores recorded for
• Notes 1 and Report 1
• Quiz 1
• Notes 2 (and most but not all Report 2)
For missing scores, contact Sanjukta at samitra@cs.ucsd.edu
We were a bit tougher in grading the second lab.
Bugs and ethics
From http://www.pionline.com/article/20110922/DAILYREG/110929952
Barr M. Rosenberg ... will pay $2.5 million in a settlement with 
the SEC over securities fraud charges for concealing a coding 
error. 
... follows a $25 million fine that was paid in February by AXA 
Rosenberg. The firm also returned $217 million to investors.
... the error was first noticed in June 2009 ... The employee 
notified Mr. Rosenberg.  “(Mr.) Rosenberg ... directed BRRC 
employees with knowledge of the error to keep quiet about the 
discovery of the error and to not inform others about it.”
Barr Rosenberg was a UC Berkeley professor.                           .
 Lab 3
In lecture now, I will talk about the scientific method.  
The scientific method should sound familiar and sensible.
It’s taught in second grade nowadays!
The scientific method (one version)
Six steps:
• Question     (a real-world goal)
• Hypothesis (a factual statement that is either true or false)
• Experiment  (a manipulation of the world)
• Prediction    (the expected outcome of the experiment)
• Observation (the actual outcome)
• Conclusion  (the hypothesis is falsified, or supported)
An example from 1783
of the scientific method.
Montgolfier brothers’ hot air balloon
Question:       Is it safe to fly in a hot air balloon?
Hypothesis:   An animal at high altitude cannot breathe.
Experiment:  Send an animal up in the basket of a balloon.
Prediction:    The animal will die.
Observation: The animal survived the experiment.
Conclusion:  The hypothesis is false.
From Wikipedia:
... The king proposed to launch two criminals
 
... On 19 September 1783 the Aerostat Réveillon was flown 
with ... a sheep called Montauciel, a duck and a rooster. 
• The sheep was believed to have a reasonable approximation 
of human physiology.
• The duck was expected to be unharmed by being lifted aloft.  
It was included as a control for effects created by the aircraft 
• The rooster was included as a further control as it was a bird 
that did not fly at high altitudes. 
Scientific method in debugging 
• Desired behavior
• Actual behavior (a symptom of a bug)
• Hypothesis   (a potential cause of the symptom)
• Experiment   (a manipulation of the world code)
• Prediction     (an expected outcome of the experiment)
• Observation  (the actual outcome)
• Conclusion   (the hypothesis is either falsified or supported)
The scientific method loop
The result of an experiment is that the hypothesis is 
disproved or supported.
Experiments can never prove a hypothesis.  They can only 
disprove it.
After enough supportive experiments, you move on.
Change one thing at a time
When doing an experiment, change the code in only one way.
Then, the experimental change is the cause of observed 
output differences.
If it isn’t broken, don’t fix it
Do not change code just based on looking at it.
• First, start with a symptom.
• Second, have a hypothesis for a defect that may be causing 
the symptom.
• Third, use experiment(s) to investigate the hypothesis.
• Last, only when the hypothesis is well-supported, change the 
code.
What is a good hypothesis?
A good hypothesis is a possible defect, e.g. “Variable x is not 
initialized at the start of the loop on line 99.”
• A hypothesis is true or false.
• If true, a hypothesis causes the observed symptom.
• A hypothesis should be clear and simple, but may need an 
associated explanation.
A hypothesis should not be about who wrote the program, why 
they wrote something, how to fix the program, etc.
About today’s lab
The program is named wc.java for “word count.”
It counts the number of words in a file, with various options.
In the notes for today’s lab
There are several bugs in today’s program.
Copy-and-paste step by step all your actions while finding and 
fixing the bugs.
Make clear that you are following the scientific method.
Write down your hypotheses.
In the report for today
Write about only one bug: the bug that is related to the -L 
option.
In the report, explain every step of the scientific method loop, 
for this one bug.
Grading guides
On the 15L web site, we have published the rubrics used to 
grade the notes and reports.
This week, the rubrics have changed because
                 The scientific method is required.
Changes are in red.
Let’s go!
The code for this week’s lab is on the class website at 
http://cseweb.ucsd.edu/~elkan/15L
Please move to the labs now: B230, 240, 250 and 260. 
Pick a partner and get started.