0% found this document useful (0 votes)
2 views

Com Lecture 5 Debugging

The document discusses debugging, defining bugs as syntax and runtime errors, and emphasizes the importance of efficiency in programming. It provides examples of a factoring program, illustrating various errors such as syntax errors, runtime exceptions, and logical errors, along with their resolutions. The document concludes by highlighting the significance of testing programs with real input data to ensure they function as intended.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Com Lecture 5 Debugging

The document discusses debugging, defining bugs as syntax and runtime errors, and emphasizes the importance of efficiency in programming. It provides examples of a factoring program, illustrating various errors such as syntax errors, runtime exceptions, and logical errors, along with their resolutions. The document concludes by highlighting the significance of testing programs with real input data to ensure they function as intended.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Debugging

What is bug? Syntax error

•Some flaw with your code • Easiest to resolve (in theory)


• Caught by compiler
Runtime error • Program with syntax errors does
not compile
• Harder to find and resolve
• Compiler is not useful here
Efficiency
• Program compiles and runs but
may produce incorrect results • Is the program using computing
or crash resources efficiently?
• Resources = time + memory
• Problem typically only shows up
when the program runs on large
input
What is debugging?

Example: The factoring problem

Example: The factoring problem
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0])

for (i = 0; i < n; i++){


while (n % i == 0)
System.out.print(i + " ")
n = n / i
}
}
}
Example: The factoring Program
problem does not
public class Factors{ compile!
public static void main(String[] args){
long n = Long.parseLong(args[0])

for (i = 0; i < n; i++){


while (n % i == 0)
System.out.print(i + " ")
n = n / i Missing ;
}
}
}
Missing
type for i
Example: The factoring problem
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (int i = 0; i < n; i++){


while (n % i == 0)
System.out.print(i + " ");
n = n / i;
}
}
}
Example: The factoring problem
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (int i = 0; >java


i < n;Factors
i++){
Exception
while (n % i == 0) in thread "main"
java.lang.ArrayIndexOutOfBoundsExceptio
System.out.print(i + " ");
n = n / i; n: 0
} at Factors.main(Factors.java:3)
}
} >
Missing command-line
argument
Example: The factoring problem Runtime error!

public class Factors{


public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (int i = 0; >java


i < n;Factors
i++){ 6
Exception
while (n % i == 0) in thread "main"
java.lang.ArithmeticException:
System.out.print(i + " "); / by
n = n / i; zero
} at Factors.main(Factors.java:6)
}
} >
Example: The factoring problem
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (int i = 0; i < n; i++){


while (n % i == 0)
System.out.print(i + " ");
n = n / i;
}
} • 0 cause the “division by zero” error
} • Need to start at 2, since 0 and 1 are
not factors anyway
Example: The factoring problem
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (int i = 2; i < n; i++){


while (n % i == 0)
System.out.print(i + " ");
n = n / i;
}
}
}
Example: The factoring problemRuntime error – logical error
– non-terminating loop!
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (int i = 2; >java


i < n;Factors
i++){ 6
222222222222222222222222222222222222222
while (n % i == 0)
222222222222222222222222222222222222222
System.out.print(i + " ");
n = n / i; 222222222222222222222222222222222222222
} 222222222222222222222222222222222222222
} 222222222222222222222222222222222222222
} 222222222222222222222222222222222222222
222222222222222222222222222222222222222
222222222222222222222222222222222222222
222222222222222222222222222222222222222
Example: The factoring problem
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (int i = 2; i < n; i++){


while (n % i == 0)
System.out.print(i + " ");
n = n / i;
}
}
• These need to enclosed in a block,
}
otherwise n = n / i never gets executed,
and the value of never changes, hence
the while loops forever!
Example: The factoring problem
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (int i = 2; i < n; i++){


while (n % i == 0){
System.out.print(i + " ");
n = n / i;
}
}
}
}
Example: The factoring problem Runtime logical
error!
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (int i = 2; i>java


< n;Factors
i++){98
No output for 5
while (n % i ==2 70){
7
>java Factors
System.out.print(i + "5 ");
n = n / i;
} >java Factors 6
2 Missing the last
} factor (3) for 6
} >
}
Example: The factoring problem
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);
Runtime logical
for (int i = 2; i < n; i++){ error!
while (n % i == 0){
System.out.print(i + " ");
n = n / i;
} Because of <, i can
} never reach n, so n
} is never tried!
}
Example: The factoring problem
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (int i = 2; i <= n; i++){


while (n % i == 0){
System.out.print(i + " ");
n = n / i;
}
}
}
}
Example: The factoring problem
>javac Factors.java

public class Factors{ >java Factors 98


public static void main(String[]
2 7 7 args){
long n = Long.parseLong(args[0]);
>java Factors 5
5
for (int i = 2; i <= n; i++){
>java Factors 6
while (n % i == 20){
3
System.out.print(i + " "); 1111111111111111
>java Factors
n = n / i; 11 17 73 101 137 5882353
} >java Factors 11111111111111111
} 2071723 -1 -1 -1 -1 -1 -1 -1 -1 -1
} -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
} -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
Example: The factoring problem
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (int i = 2; i <= n; i++){


while (n % i == 0){
System.out.print(i + " ");
n = n / i;
}
}
}
}
Example: The factoring problem
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (long i = 2; i <= n; i++){


while (n % i == 0){
System.out.print(i + " ");
n = n / i;
}
}
}
}
Example: The factoring problem
public class Factors{ >java Factors 98
2 7 7
public static void main(String[] args){
>java Factors 5
long n = Long.parseLong(args[0]);
5
for (long i = 2; >java Factors
i <= n; i++){ 6
2 30){
while (n % i ==
>java Factors
System.out.print(i + " ");1111111111111111
n = n / i; 11 17 73 101 137 5882353
} >java Factors 11111111111111111
} 2071723 5363222357
} >
} Took too long to compute!
Example: The factoring problem
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (long i = 2; i <= n; i++){


while (n % i == 0){
System.out.print(i + " ");
n = n / i;
}
}
}
} Do we really need to test
all the way to n?
Example: The factoring problem

Example: The factoring problem
public class Factors{
public static void main(String[] args){
long n = Long.parseLong(args[0]);

for (long i = 2; i <= n / i; i++){


while (n % i == 0){
System.out.print(i + " ");
n = n / i;
}
}

if (n > 1)
System.out.println(n);
}
}
Example: The factoring problem
Digits in the largest i <= n i <= n / i
factor
3 Instant Instant
6 instant Instant
9 77 Instant
12 21 hours Instant
15 2.4 years 2.7 seconds
18 2,400 years 97 seconds
Example: The factoring problem

Summary
Edit program

Syntax error
Compile
e r r o r
t i m e
ru n
i c e r ror
Run – does it work as intended? e m a nt
S

c e e r ror
f o r m an
Test – with real input data per

You might also like