Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.

Slides:



Advertisements
Similar presentations
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Some basic I/O.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Chapter 5 Loops.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4: Control Structures II
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Iteration Hussein Suleman UCT Dept of Computer Science CS115 ~ 2004.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Application development with Java Lecture 6 Rina Zviel-Girshin.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
COMP Loop Statements Yi Hong May 21, 2015.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Debugging and Testing Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
1 Sections 7.2 – 7.7 Nested Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
UCT Department of Computer Science Computer Science 1015F Iteration
Testing and Debugging UCT Department of Computer Science Computer Science 1015F Hussein Suleman March 2009.
Chapter 4 Repetition Statements (loops)
Control Structures.
Chapter 6 More Conditionals and Loops
Maha AlSaif Maryam AlQattan
Repetition-Sentinel,Flag Loop/Do_While
Expressions and Control Flow in JavaScript
Outline Altering flow of control Boolean expressions
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
Outline Software Development Activities
CSS161: Fundamentals of Computing
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Control Statements:.
Presentation transcript:

Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F

Java: the story so far …  A variable is a memory location where data can be stored. It usually has a name/identifier and data can be stored in it or retrieved from it. e.g., int i = 12;  Data types (int, char, etc.) specify what kinds of data can be manipulated.  Arithmetic expressions are evaluated to numbers. e.g., j = i + 12  Boolean expressions are evaluated to true or false.  Strings are sequences of characters.  Input is done using the Scanner  Output is done using System.out  Instruction execution order can be controlled using selection (if, if-else, switch) and iteration (for, while, do-while)

Software Testing  Test Cases  Tracing  Assertions  Debugging

Test Cases  Based on Input: Choose one value in each equivalence classes – sets of input values that behave similarly. Choose values on either side of and at boundary values – values between equivalence classes.  Based on Code: Path coverage: choose values that test every possible path through the statements at least once. Statement coverage: Choose values that test every possible statement at least once.

Test Cases Example // Software Testing sample // hussein suleman // CSC1015F // 19 March 2007 import java.util.Scanner; class SoftwareTesting { public static void main ( String [] args ) { // get a number Scanner scan = new Scanner (System.in); int input = scan.nextInt(); input += 10; // check if it is small or large if (input < 20) System.out.print ("small number"); else System.out.print ("large number"); // check if it is divisible by 5 if (input % 5 == 0) System.out.println (" divisible by 5"); } } equivalence classes: small multiple of 5: 5 small non-multiples of 5: 3 large multiple of 5: 25 large non-multiples of 5: 23 boundary values: 4, 5, 6, 9, 10, 11, 14, 15, 16 statement coverage: 5, 14 path coverage: 5, 7, 13, 20

Assertions  In Java a programmer can specify conditions that must always be satisfied at particular points (invariants) or the program produces an error. This is an assertion.  Example: assert (input > 0);

Tracing  Insert temporary statements into code to output values during calculation.  Very useful when there is no debugger!  Example: int x = y*y*2; int z = x+5; System.out.println (z); if (z == 13) { … } trace instruction

Debugging  Debugging is the process of finding errors or bugs in the code.  A debugger is a tool for executing an application where the programmer can carefully control execution and inspect data.  Features include: step through code one instruction at a time viewing variables insert and remove breakpoints to pause execution

Problem  Write a program to calculate the value of Sin(x) for any real value of x. Use the infinite Taylor series approximation:  Extend your program to draw a Sin(x) graph using ASCII art.

Solution  First calculate Sin(x). Use variables for numerator, denominator, factorial, etc. and loop over terms until the term is very small.  Add a loop over all x values to calculate a list of Sin(x) values.  Add a loop over all y-axis values to visit each (x,y) pair and check if the graph intersects, i.e., if y = Sin(x) approximately  Add axes if desired.