What does this program print?

Similar documents
11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

3. Java - Language Constructs I

QueueBlock, ReversalADT, LinkedList,CustomerAccount, not MaintainCustomerData

Lexical Considerations

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CSCI 2101 Java Style Guide

CS 251 Intermediate Programming Coding Standards

Index COPYRIGHTED MATERIAL

CSE 142/143 Unofficial Style Guide

UNIT- 3 Introduction to C++

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Java Notes. 10th ICSE. Saravanan Ganesh

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Full file at

CS 152 Computer Programming Fundamentals Coding Standards

Lexical Considerations

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

1 Lexical Considerations

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Basics of Java Programming

CS 351 Design of Large Programs Coding Standards

Operators. Java operators are classified into three categories:

Java Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M.

Coding Standards for Java

Introduction to C Programming

PYTHON- AN INNOVATION

Key Differences Between Python and Java

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Full file at C How to Program, 6/e Multiple Choice Test Bank

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Documentation Requirements Computer Science 2334 Spring 2016

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

APCS Semester #1 Final Exam Practice Problems

Java Bytecode (binary file)

4 Programming Fundamentals. Introduction to Programming 1 1

A variable is a name that represents a value. For

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Chapter 2 Author Notes

CSE 11 Style Guidelines

IT Web and Software Developer Software Development Standards

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Typescript on LLVM Language Reference Manual

The pixelman Language Reference Manual. Anthony Chan, Teresa Choe, Gabriel Kramer-Garcia, Brian Tsau

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

DEPARTMENT OF MATHS, MJ COLLEGE

Expanded Guidelines on Programming Style and Documentation

Fall 2017 CISC124 9/16/2017

ARG! Language Reference Manual

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Sprite an animation manipulation language Language Reference Manual

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Data Abstraction and Specification of ADTs

Course Coding Standards

Fundamental of Programming (C)

CHAPTER 2 Java Fundamentals

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

The Arithmetic Operators

Introduction to Java & Fundamental Data Types

Data Types, Literals, Operators

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

JME Language Reference Manual

Variables and Values

Chapter 2. Lexical Elements & Operators

Language Fundamentals Summary

Assignment 6. Methods Homework Spring 2017 P Question 1 all

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 17. Fundamental Concepts Expressed in JavaScript

Chapter 2: Basic Elements of C++

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

CSC 1214: Object-Oriented Programming

3. Java - Language Constructs I

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

QUIZ: What value is stored in a after this

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Java Programming Style Guide

Java Exceptions Java, winter semester

Pace University. Fundamental Concepts of CS121 1

BASIC ELEMENTS OF A COMPUTER PROGRAM

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

CS112 Lecture: Working with Numbers

DaMPL. Language Reference Manual. Henrique Grando

Program Fundamentals

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

IT 374 C# and Applications/ IT695 C# Data Structures

Supplement D: Expanded Guidelines on Programming Style and Documentation

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

( &% class MyClass { }

Language Reference Manual

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

JAVA Programming Fundamentals

CS 231 Data Structures and Algorithms, Fall 2016

Chapter 2: Introduction to C++

Transcription:

What does this program print? Attempt 1 public class Rec { private static int f(int x){ if(x<2){ return 1; }return f(x- 1)+f(x-2 );} public static void main( String[] args) { System.out.println(f(5));}} Initial Examples

What does this program print? Attempt 2: A hint... public class Rec { private static int f(int x) { if (x < 2) { return 1; } return f(x - 1) + f(x - 2); } } public static void main(string[] args) { System.out.println(f(5)); } Initial Examples

What does this class do? Attempt 1 public static final class Oc { private final Object[] e = new Object[1000000]; private int pe = -1; private int po = 0; public void a(object x) { e[po++] = x; } } public Object b() { return e[pe++]; } Initial Examples

What does this class do? Attempt 2: A hint... public static final class Queue { private final Object[] myvalues = new Object[BIG_VALUE]; private int myhead = -1; private int mytail = 0; public void enqueue(object x) { myvalues[mytail++] = x; } } public Object dequeue() { return myvalues[myhead++]; } Initial Examples

A bug fixed class Queue private int myhead = -1; private int mytail = 0; public void enqueue(object x) { myvalues[mytail] = x; mytail++; } public Object dequeue() { myhead++; return myvalues[myhead]; } Initial Examples

What does this function do? A humble two-line function boolean p(int x) { int y = x * (030 >> 4 << 030); return y == 0; } Initial Examples

What does this function do? A humble two-line function boolean p(int x) { int y = x * (030 >> 4 << 030); return y == 0; } Hints a << s = a * 2 s bitwise shift left a >> s = a / 2 s bitwise shift right 0x<DIGITS> hexadecimal number 0<DIGITS> octal number Initial Examples

What we have (hopefully) learned so far Initial Examples

What we have (hopefully) learned so far Example 1: Fibonacci numbers Format your programs properly! Initial Examples

What we have (hopefully) learned so far Example 1: Fibonacci numbers Format your programs properly! Example 2: Queue Give understandable names to program elements! Initial Examples

What we have (hopefully) learned so far Example 1: Fibonacci numbers Format your programs properly! Example 2: Queue Give understandable names to program elements! A good program does not demand comments other than JavaDoc for interfaces. Initial Examples

What we have (hopefully) learned so far Example 1: Fibonacci numbers Format your programs properly! Example 2: Queue Give understandable names to program elements! A good program does not demand comments other than JavaDoc for interfaces. Example 3: Divisibility by 256 Do not outsmart yourself! Initial Examples

What we have (hopefully) learned so far Example 1: Fibonacci numbers Format your programs properly! Example 2: Queue Give understandable names to program elements! A good program does not demand comments other than JavaDoc for interfaces. Example 3: Divisibility by 256 Do not outsmart yourself! Use understandable code constructs. Initial Examples

ITMO University, St. Petersburg / University of Tartu Oct. 30, 2009

Exercise 1 Groups: 4 people each

Exercise 1 Groups: 4 people each Time: 10 minutes

Exercise 1 Groups: 4 people each Time: 10 minutes Task: come up with 2 rules (conventions)

Exercise 1 Groups: 4 people each Time: 10 minutes Task: come up with 2 rules (conventions) Clear formulations

Exercise 1 Groups: 4 people each Time: 10 minutes Task: come up with 2 rules (conventions) Clear formulations Clear explanations

Sun s CCJ Code Conventions for Java TM Programming Language http://java.sun.com/docs/codeconv/ A standard provided by the Sun Microsystems Reflects the opinion of the creators of the language Adopted by many projects

Naming Conventions: Types and Methods Classes and Interfaces Class name is most likely a noun phrase Written in CamelCase MyFavouriteClass [Not in CCJ]: Interface names are prefixed with I : IModel Methods Method name is most likely a verb phrase Starts with a lower case letter, then CamelCase dothejob() Common prefixes get, is, set

Naming Conventions: Variables and Constants Variables: fields, local variables, parameters Starts with a lower case letter, then CamelCase counter, firstoccurrence Names should never start with $ or [Not in CCJ]: field names are prefixed with my Constants: static final, normally public Uppercase letters, words separated by underscores THE CONSTANT

Naming Conventions: Packages Packages Only lowercase ASCII letters Starts with a unique domain name (reversed) org.eclipse.emf.ecore

Declaration Order: Elements of a Class Only one top-level class should be declared in a file Order of elements in a class 1. Nested classes 2. Static fields 3. Static methods 4. Instance fields 5. Constructors 6. Instance methods

Declaration Order: Inside a Method Only one variable per line (applies also to fields) Bad int a = 0, b = 3; Good int a = 0; int b = 3; Variables are initialized upon declaration (if possible) Bad int a; Good int a = 0; [Not in CCJ]: Variables are defined as close to their usage as possible

Declaration Order: Inside a Method Only one variable per line (applies also to fields) Bad int a = 0, b = 3; Good int a = 0; int b = 3; Variables are initialized upon declaration (if possible) Bad int a; Good int a = 0; [Not in CCJ]: Variables are defined as close to their usage as possible Actually CCJ recommends the opposite

Declaration Order: Variable Declaration Example Bad int a = 0; int b = 5; while (b > 0) { a = b * b; if (a > 10) { c++; } } Good int b = 5; while (b > 0) { int a = b * b; if (a > 10) { c++; } }

Declaration Order: Warning May cause a problem int b = 5; while (b > 0) { A a = new A(); if (a.getv(b) > 10) { c++; } } Safe int b = 5; while (b > 0) { int a = b * b; if (a > 10) { c++; } }

Declaration Order: Declaring Arrays Brackets are put after the element type Bad int a[] = 1, 2, 3; Good int[] a = 1, 2, 3;

Whitespace: Where to Put Spaces Before an opening curly brace ( { ): public void method() {

Whitespace: Where to Put Spaces Before an opening curly brace ( { ): public void method() { After a comma (, ): public void method(int a, int b)

Whitespace: Where to Put Spaces Before an opening curly brace ( { ): public void method() { After a comma (, ): public void method(int a, int b) After a control operator keyword (e.g., if): if (cond) {

Whitespace: Where to Put Spaces Before an opening curly brace ( { ): public void method() { After a comma (, ): public void method(int a, int b) After a control operator keyword (e.g., if): if (cond) { After a semicolon ( ; ) inside for loop header: for (int i = 0; i < 10; i++) {

Whitespace: Where to Put Spaces Before an opening curly brace ( { ): public void method() { After a comma (, ): public void method(int a, int b) After a control operator keyword (e.g., if): if (cond) { After a semicolon ( ; ) inside for loop header: for (int i = 0; i < 10; i++) { Around binary operations (e.g., + and / ): int a = a + b * (c - 1 / 2.0);

Whitespace: Where to Put Spaces Before an opening curly brace ( { ): public void method() { After a comma (, ): public void method(int a, int b) After a control operator keyword (e.g., if): if (cond) { After a semicolon ( ; ) inside for loop header: for (int i = 0; i < 10; i++) { Around binary operations (e.g., + and / ): int a = a + b * (c - 1 / 2.0); In a ternary operator (...?... :...): (a > b)? a : b;

Whitespace: Where to Put Spaces Before an opening curly brace ( { ): public void method() { After a comma (, ): public void method(int a, int b) After a control operator keyword (e.g., if): if (cond) { After a semicolon ( ; ) inside for loop header: for (int i = 0; i < 10; i++) { Around binary operations (e.g., + and / ): int a = a + b * (c - 1 / 2.0); In a ternary operator (...?... :...): (a > b)? a : b; After a type cast: int a = (int) doublevalue;

Whitespace: Where NOT to Put Spaces Between a method name and an opening parenthesis ( ( ): doit(a)

Whitespace: Where NOT to Put Spaces Between a method name and an opening parenthesis ( ( ): doit(a) Between an unary operation (e.g., ++, --) and its argument: a++ --b

Whitespace: Where NOT to Put Spaces Between a method name and an opening parenthesis ( ( ): doit(a) Between an unary operation (e.g., ++, --) and its argument: a++ --b Between a parenthesis ( ( or ) ) and its contents: (a + b)

Whitespace: Where NOT to Put Spaces Between a method name and an opening parenthesis ( ( ): doit(a) Between an unary operation (e.g., ++, --) and its argument: a++ --b Between a parenthesis ( ( or ) ) and its contents: (a + b) Before brackets ( [ and ] ): int[] a int[][] bb a[1] bb[1][2]

Whitespace: Where to Put Newlines Before comments

Whitespace: Where to Put Newlines Before comments Between method declarations

Whitespace: Where to Put Newlines Before comments Between method declarations Between logical blocks inside a method

Whitespace: Where to Put Newlines Before comments Between method declarations Between logical blocks inside a method Between classes

Whitespace: Where to Put Newlines Before comments Between method declarations Between logical blocks inside a method Between classes Whenever the current line is too long

Whitespace: Where to Put Newlines Before comments Between method declarations Between logical blocks inside a method Between classes Whenever the current line is too long indent the remainder of a wrapped line

Whitespace: Where to Put Newlines Before comments Between method declarations Between logical blocks inside a method Between classes Whenever the current line is too long indent the remainder of a wrapped line break lines after a comma doit(a, b);

Whitespace: Where to Put Newlines Before comments Between method declarations Between logical blocks inside a method Between classes Whenever the current line is too long indent the remainder of a wrapped line break lines after a comma doit(a, b); break lines before a binary operation if ((a > b) && (b > c) && (c > d)) {

Blocks and Indentation Indentation width is 4 spaces.

Blocks and Indentation Indentation width is 4 spaces. Or 1 TAB

Blocks and Indentation Indentation width is 4 spaces. Or 1 TAB Never mix TABs and spaces

Blocks and Indentation Indentation width is 4 spaces. Or 1 TAB Never mix TABs and spaces Bodies of control structures are always enclosed into curly braces ( { and } )

Blocks and Indentation Indentation width is 4 spaces. Or 1 TAB Never mix TABs and spaces Bodies of control structures are always enclosed into curly braces ( { and } ) Even if there is only one line

Blocks and Indentation Indentation width is 4 spaces. Or 1 TAB Never mix TABs and spaces Bodies of control structures are always enclosed into curly braces ( { and } ) Even if there is only one line Block content must be indented: if (a > b) { a = 2 * a; b--; }

Blocks and Indentation Indentation width is 4 spaces. Or 1 TAB Never mix TABs and spaces Bodies of control structures are always enclosed into curly braces ( { and } ) Even if there is only one line Block content must be indented: if (a > b) { a = 2 * a; b--; } Class content must be indented class C { private final int myvalue = 0;... }

Conditional Operator Single if: if (condition) { } if (condition) { } else { Cascade if: if (condition1) { } else if (condition2) { } else { } }

When to Use the If Cascade It s needed to check several related coditions

When to Use the If Cascade It s needed to check several related coditions We cannot use switch: if (a > b) {... } else if (a < b) {... } else if (a == b) {... }

Switch switch (condition) { case ABC: statements; /* falls through */ case DEF: statements; break; case XYZ: statements; break; default: statements; break; }

Loops Precondition: while (condition) { //... } Postcondition: do { //... } while ( ); For: for (int i = 0; i < 10; i++) { //... }

Exception Handling try { } catch (ExceptionClass e) { } finally { }

Exception Handling try { } catch (ExceptionClass e) { } finally { } Never throw or catch

Exception Handling try { } catch (ExceptionClass e) { } finally { } Never throw or catch java.lang.nullpointerexception java.lang.classcastexception java.lang.runtimeexception java.lang.exception java.lang.throwable

Exception Handling try { } catch (ExceptionClass e) { } finally { } Never throw or catch java.lang.nullpointerexception java.lang.classcastexception java.lang.runtimeexception java.lang.exception java.lang.throwable Never catch java.lang.error

Idiots

Idioms

Immediately Returning Ifs Bad if (cond) { return true; } else { return false; }

Immediately Returning Ifs Bad if (cond) { return true; } else { return false; } Good return cond;

Redundant Else Not Recommended if (cond) {...... return; } else { doit(); }

Redundant Else Not Recommended if (cond) {...... return; } else { doit(); } Recommended if (cond) {...... return; } doit();

Do NOT Use Expressions with Side-Effects Bad int a = b = c = 0; Very Bad if (a = b) {... } Bad int a = b[c++];

Why We Need Conventions Code is complex

Why We Need Conventions Code is complex Code lives long

Why We Need Conventions Code is complex Code lives long Code is written and read by many people

Why We Need Conventions Code is complex Code lives long Code is written and read by many people Code is almost never properly documented

Why We Need Conventions Code is complex Code lives long Code is written and read by many people Code is almost never properly documented

Why We Need Conventions Code is complex Code lives long Code is written and read by many people Code is almost never properly documented NB: Some additional reasons are mentioned in CCJ

If You Remember Only One Thing...

If You Remember Only One Thing... Always look for explanations!

The STRONGLY Recommended Book

Homework 1 Point

Homework 1 Point Deadline: Nov. 13, Friday, 09:00

Homework 1 Point Deadline: Nov. 13, Friday, 09:00 Send the source code to abreslav@ut.ee

Homework 1 Point Deadline: Nov. 13, Friday, 09:00 Send the source code to abreslav@ut.ee Task:

Homework 1 Point Deadline: Nov. 13, Friday, 09:00 Send the source code to abreslav@ut.ee Task: Write a class MySet:

Homework 1 Point Deadline: Nov. 13, Friday, 09:00 Send the source code to abreslav@ut.ee Task: Write a class MySet: Implements a set of integers: each one occurs not more than once in the set

Homework 1 Point Deadline: Nov. 13, Friday, 09:00 Send the source code to abreslav@ut.ee Task: Write a class MySet: Implements a set of integers: each one occurs not more than once in the set void add(int x) adds a new element (if it is not in the set already)

Homework 1 Point Deadline: Nov. 13, Friday, 09:00 Send the source code to abreslav@ut.ee Task: Write a class MySet: Implements a set of integers: each one occurs not more than once in the set void add(int x) adds a new element (if it is not in the set already) boolean contains(int x) returns true iff x is present in the set

Homework 1 Point Deadline: Nov. 13, Friday, 09:00 Send the source code to abreslav@ut.ee Task: Write a class MySet: Implements a set of integers: each one occurs not more than once in the set void add(int x) adds a new element (if it is not in the set already) boolean contains(int x) returns true iff x is present in the set String tostring() returns a string with all the elements of the set: [1, 2, 3]

Homework 1 Point Deadline: Nov. 13, Friday, 09:00 Send the source code to abreslav@ut.ee Task: Write a class MySet: Implements a set of integers: each one occurs not more than once in the set void add(int x) adds a new element (if it is not in the set already) boolean contains(int x) returns true iff x is present in the set String tostring() returns a string with all the elements of the set: [1, 2, 3] Make the operations work as fast as possible.

Homework 1 Point Deadline: Nov. 13, Friday, 09:00 Send the source code to abreslav@ut.ee Task: Write a class MySet: Implements a set of integers: each one occurs not more than once in the set void add(int x) adds a new element (if it is not in the set already) boolean contains(int x) returns true iff x is present in the set String tostring() returns a string with all the elements of the set: [1, 2, 3] Make the operations work as fast as possible. Do not use any classes from the library having Set in their names.

Homework 1 Point Deadline: Nov. 13, Friday, 09:00 Send the source code to abreslav@ut.ee Task: Write a class MySet: Implements a set of integers: each one occurs not more than once in the set void add(int x) adds a new element (if it is not in the set already) boolean contains(int x) returns true iff x is present in the set String tostring() returns a string with all the elements of the set: [1, 2, 3] Make the operations work as fast as possible. Do not use any classes from the library having Set in their names. Write JUnit tests for your code.

Homework 1 Point Deadline: Nov. 13, Friday, 09:00 Send the source code to abreslav@ut.ee Task: Write a class MySet: Implements a set of integers: each one occurs not more than once in the set void add(int x) adds a new element (if it is not in the set already) boolean contains(int x) returns true iff x is present in the set String tostring() returns a string with all the elements of the set: [1, 2, 3] Make the operations work as fast as possible. Do not use any classes from the library having Set in their names. Write JUnit tests for your code. Follow the conventions!