Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go



Similar documents
CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Moving from CS 61A Scheme to CS 61B Java

J a v a Quiz (Unit 3, Test 0 Practice)

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

1 Description of The Simpletron

CS 241 Data Organization Coding Standards

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Pseudo code Tutorial and Exercises Teacher s Version

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

VB.NET Programming Fundamentals

CS170 Lab 11 Abstract Data Types & Objects

Sources: On the Web: Slides will be available on:

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Module 10. Coding and Testing. Version 2 CSE IIT, Kharagpur

PL / SQL Basics. Chapter 3

The C Programming Language course syllabus associate level

COMP 110 Prasun Dewan 1

Chapter 2: Elements of Java

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Fundamentals of Programming and Software Development Lesson Objectives

Statements and Control Flow

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

C Compiler Targeting the Java Virtual Machine

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

13 Classes & Objects with Constructors/Destructors

Computer Programming I & II*

In this Chapter you ll learn:

C++FA 5.1 PRACTICE MID-TERM EXAM

Conditionals (with solutions)

Unit 6. Loop statements

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Chapter 2 Introduction to Java programming

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Software Engineering Techniques

Informatica e Sistemi in Tempo Reale

public static void main(string[] args) { System.out.println("hello, world"); } }

Programming Languages CIS 443

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Introduction to programming

Object Oriented Software Design

Chapter One Introduction to Programming

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

LAB4 Making Classes and Objects

CS 106 Introduction to Computer Science I

Java CPD (I) Frans Coenen Department of Computer Science

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

6. Control Structures

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Chapter 5 Functions. Introducing Functions

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

Chapter 3. Input and output. 3.1 The System class

Building Java Programs

Java Interview Questions and Answers

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Computational Mathematics with Python

Getting Started with the Internet Communications Engine

Semantic Analysis: Types and Type Checking

Chapter 5 Names, Bindings, Type Checking, and Scopes

Keil C51 Cross Compiler

Chapter 13: Program Development and Programming Languages

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

AP Computer Science Java Subset

Lecture 5: Java Fundamentals III

Java Basics: Data Types, Variables, and Loops

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Computer Programming I

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

Parsing Technology and its role in Legacy Modernization. A Metaware White Paper

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Java Program Coding Standards Programming for Information Technology

LOOPS CHAPTER CHAPTER GOALS

Basic Object-Oriented Programming in Java

EMC Publishing. Ontario Curriculum Computer and Information Science Grade 11

Crash Course in Java

#820 Computer Programming 1A

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Sample CSE8A midterm Multiple Choice (circle one)

C++ INTERVIEW QUESTIONS

Computational Mathematics with Python

Tutorial on C Language Programming

Test Driven Development

Transcription:

Debugging ESE112 Java Programming: API, Psuedo-Code, Scope It is highly unlikely that you will write code that will work on the first go Bugs or errors Syntax Fixable if you learn to read compiler error messages Lines number should take to erroneous section Semantic No easy fix Use print statements to our advantage ESE112 1 Common Semantic Errors No re-initialization condition that will eventually halt the loop For preferred over while due the nature of syntax Condition starting out to be false initially Wrong initialization value or end-test condition So be careful of < vs. <= Java Library Java provides a library or collection of useful programs A gold mine of well-tested code that can save you countless hours of development time This huge library information is provided in API - Application Programming Interface ESE112 2 ESE112 3 1

General Class Interface A class' public interface (notice the four headings): Field Summary Has information about public variables Constructor and Method summaries Describe the public constructors and methods We will cover constructors soon Method Detail Provides detail on method inputs (parameter(s))and output (return type) and some extra details The collection of all of a class' public fields, constructors, and methods constitute its "interface", that is, the public face that it shows the world. If method/variable is private, then it will be not a part of its interface ESE112 4 Example Math Class Notice the phrase java.lang at the top of the main panel above the word Math This means that the Math class is part of the core Java language and hence can be used directly Unlike the CPU class Math Class Interface Field Summary: none Constructor Summary: has no public constructor Methods Summary: the methods implemented (all which are static) Method Details: e.g. abs(int a) Find the absolute value of a value ESE112 5 Example CPU class If a class is not part of java language i.e. java.lang, you'll see package name What is a package?? Basically it's a directory that has a collection of related classes E.g. CPU class stamp.core.cpu Indicating that the CPU class program is stored in the folder stamp/core/ (within the Javelin folder) Since CPU is not part of Java Language we need to tell Java where to find it by saying import stamp.core.cpu; Another way is to use the asterisk "wildcard character. E.g. stamp.core.*; which allows any classes in core folder to be accessed Psuedo-Code Pseudo-code is a informal high-level description of a algorithm It uses the structural conventions of a programming language Intended for human reading vs. machine reading Pseudo-code typically omits details that are not essential for human understanding of the algorithm E.g. variable declarations,system-specific code and subroutines. 2

Psuedo-Code Rules Can use words such as while, if else-if E.g. for 1 to n Do not specify data declarations or types Use Words that specify an action such as set, reset, increment, compute, calculate, add, sum, multiply, print, getinput Use indentation for block of code i.e. { Example: Fibonacci sequences A Fibonacci sequence is an infinite list of integers The first two numbers are given Usually (but not necessarily) these are 1 and 1 Each subsequent number is the sum of the two preceding numbers: 1 1 2 3 5 8 13 21 34 55 89 144... Let s write a program to compute sequence when we get to a number that is 1000 or bigger ESE112 9 Starting the Fibonacci sequence We need to initialize two numbers in the sequence Set first to 1 Set second to 1 We need to print these out: Print first and second We need to compute and print the next number: Set next to sum of first & second; print next Now what? Taking the next step Need to add second and next set nextnext to sum of second & next What if the sequence is to long Do you want to make 100s of storage to hold each item The sequence so far is: first second next Do I see a pattern emerging? ESE112 10 ESE112 11 3

Preparing to make many steps Complete Psuedocode We need to make these moves: first second next first second next We can do it like this: Set first to second Set second to next first second next 1 1 2 1 2 3 2 3 5 3 5 8 Set first to 1 Set second to 1 Print first and second while next < 1000 Set next to sum of first & second; print next Set first to second Set second to next We can put these statements in a loop and do them as many times as we please ESE112 12 ESE112 13 Scope Scope means the area of code in which an entity is known (or alive) Mainly concerned with variables and methods Which h parts of the program can access them? Sometimes scope is explicitly designated with a keyword private: known only within the class public: known outside of (and within) the class Note that Methods have explicit scope Other times it is implicitly designated by location ESE112 14 Implicit Scope: Method Parameters A method parameter is an input variable Scope: the method in which it is defined No other method can access (read/write) it public int absolutevalue(int n) { if (n < 0) { return -n; else { return n; scope of n ESE112 15 4

Implicit Scope: Local Variables A local variable is defined within a method body { They are inherently private to the method in which they are defined We don t use public/private for local variables It may be defined in a block { within a method body Scope: point of declaration to end of closest enclosing block Local Variable Scope: Example 1 public static int islarger(int x, int y){ if (x > y) { int larger = x; else { int larger = y; return larger; Illegal: l not declared din current scope scope of larger scope of a different larger ESE112 16 ESE112 17 Local Variable Scope: Example 2 void fibonacci(int limit) { int first = 1; int second = 1; while (first < limit) i { System.out.print(first + " "); int next = first + second; first = second; second = next; System.out.println( ); next second first limit Global Variable Is variable declared outside of all methods within class Written at beginning of class Syntax public static data-type variablename E.g. public static int count; Scope Is accessible (read and modified) by all methods within the class Unless it is declared final Ideally used as constants i.e. public static final int MAX; To access global variable outside of the class it is declared in (as long as it is declared public) Classname.globalVariableName E.g. CPU.pin10 ESE112 18 5

Code Tracing A form of algorithm analysis in which you (the analyst) simulate computer operation i.e. you are evaluating the logic You step through each line of code, determining what the execution of that line causes to happen Scope & Code Tracing Example By default, copies of parameter are sent to a method public static void main(){ int x = 0; Output: System.out.println("In main: x = " + x); In main: x = 0 foo(x); In foo: x = 0 System.out.println("In main: x = " + x); In foo: x = 5 In main: x = 0 public static void foo(int x){ System.out.println("In foo: x = " + x); x = 5; System.out.println( In foo: x = "+ x); ESE112 21 Call by Value We call a method like this: result = add( 3, 5 ) ; Actual parameter values are copied into formally defined parameters Frame for method calls Whenever a method is called, some memory is set aside to contain information related to the call Example: parameter values, current statement being executed etc This is called the frame for the method call int add ( int number1, int number2 ) { int sum = number1 + number2 ; return sum ; The frame is discarded when the method returns This implies parameters and local variables (variables declared within method) are discarded Hence, this explains the reasoning for scope rules The frames are destroyed in the order in which they were created Follows first-in-last-out principle and the section of memory that allocates memory during method calls is known as stack ESE112 22 ESE112 23 6

Scope and for loops The for loop is a special case You can declare variables in the for statement The scope of those variables is the entire for loop This is true even if the loop is not a block i.e. { void multiplicationtable() { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++){ System.out.print(" " + i * j); System.out.println(" " ); ESE112 24 7