ITP © Ron Poet Lecture 3 1 Comments. ITP © Ron Poet Lecture 3 2 Legibility  It is important that programs are easy to read.  It is easier to find bugs.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
ITP © Ron Poet Lecture 9 1 Exceptions. ITP © Ron Poet Lecture 9 2 The Standard Way for Object to Return Work  Object 1 gives object 2 some work.  It.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to C Programming
Chapter 2 Getting Started in C Programming
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMPSCI 101 Principles of Programming Lecture 28 – Docstrings & Doctests.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Input, Output, and Processing
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Variables.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
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 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Chapter 2 Introduction to C++ Programming
Chapter 3 Syntax, Errors, and Debugging
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Formatting Output.
Completing the Problem-Solving Process
Chapter 2 - Introduction to C Programming
Multiple variables can be created in one declaration
Chapter 2 - Introduction to C Programming
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 Variables.
Chapter 2 - Introduction to C Programming
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Unit 3: Variables in Java
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
Introduction to C Programming
Presentation transcript:

ITP © Ron Poet Lecture 3 1 Comments

ITP © Ron Poet Lecture 3 2 Legibility  It is important that programs are easy to read.  It is easier to find bugs.  All programs require maintenance.  You don’t want to return to a program after 6 months and not be able to understand it.  You don’t want to be a write only programmer!

ITP © Ron Poet Lecture 3 3 Layout  We can add white space (spaces, tabs and newlines) more or less anywhere.  You should break up logical parts of your program with blank lines.  You should keep up the indentation that the eclipse editor creates.  Select the whole file with CTL-A.  Indent the selected text with CTL-I.

ITP © Ron Poet Lecture 3 4 Comments  Comments are parts of the program that are ignored by the compiler.  They tell human readers what is going on.  A multi-line comments starts with /* and ends with */  A single line comment starts with // and ends at the end of the line.

ITP © Ron Poet Lecture 3 5 Commenting Style  Each logical section of your code should have a comment describing it.  Any difficult parts of the program should have a comment explaining how the difficulties were solved.  There should be a comment at the start of the program, explaining what it does.  See Lab 2 programs with added comments.

ITP © Ron Poet Lecture 3 6 Scope of a Variable

ITP © Ron Poet Lecture 3 7 Scope is Visibility  The scope of a variable is the part of the program where it can be used.  Its scope is the compound statement where it is defined.  A compound statement is also called a block.  Its scope includes all inner blocks.

ITP © Ron Poet Lecture 3 8 Scope Example String line = ""; FileIn fin = new FileIn("..\\radius.txt"); { // useless example block - legal line = fin.readLine(); } StringIn sin = new StringIn(line);

ITP © Ron Poet Lecture 3 9 Scope Example Explained  The variable line is declared in the main block.  It can be used throughout the code shown.  Including the inner block.  fin is also defined in the main block.  It can be used anywhere after it is defined.

ITP © Ron Poet Lecture 3 10 Example That Does Not Work  If we define the variable line inside the iiner block.  We cannot use it outside that block.  It no longer exists by the time we reach the sin definition.  The following code will generate a syntax error.

ITP © Ron Poet Lecture 3 11 Wrong Scope FileIn fin = new FileIn("..\\radius.txt"); { // useless example block - legal String line = fin.readLine(); } StringIn sin = new StringIn(line);

ITP © Ron Poet Lecture 3 12 Using Files

ITP © Ron Poet Lecture 3 13 Files And The Filesystem  Files are permanent pieces of data.  They last beyond the lifetime of a program.  They are managed by the operating system.  They will have a filename, which is a string.  This may be an absolute pathname, such as:  C:\My Stuff\eclipse\Lab2Ex1\radius.txt

ITP © Ron Poet Lecture 3 14 Relative Pathnames  When a program runs it has a working directory, for example  C:\My Stuff\eclipse\Lab2Ex1  If a filename does not contain a drive letter then it is a relative filename.  It is appended to the program's working directory.  A relative filename radius.txt is really  C:\My Stuff\eclipse\Lab2Ex1\radius.txt

ITP © Ron Poet Lecture 3 15 The Parent Directory  What if our working directory were  C:\My Stuff\eclipse\Lab2Ex2  and we wanted to get to the radius.txt file?  We need to go up to the parent eclipse directory and back down again.  Each file has only one parent, called..  So the desired relative pathname is ..\Lab2Ex1\radius.txt

ITP © Ron Poet Lecture 3 16 Files In A Program  Files are represented by a file object in programs.  There are many types, such as FileOut.  The file object remembers where we have got to when reading the file.  We must match the string name of the file in the operating system with the file object in our program.

ITP © Ron Poet Lecture 3 17 Example Using FileOut  Let us create a FileOut object that is connected to the relative file used earlier. Fileout fout = new FileOut("..\Lab2Ex1\radius.txt");  This will always fail to find the file!

ITP © Ron Poet Lecture 3 18 Problems with \ in Strings  The reason is that \ is a special character in strings.  It says that the next character has a special meaning.  For example, \n says that in this case n means newline, not the letter n.  \" means " is the " character, not the end of the string.

ITP © Ron Poet Lecture 3 19 Turning Off Special Meanings  To get a \ in a string we must write \\.  The first \ says that the second \ is not special, but a genuine \ character.  Thus \\ will be understood to mean \.  So our filename should be  "..\\Lab2Ex1\\radius.txt"

ITP © Ron Poet Lecture 3 20 Programming With Numbers

ITP © Ron Poet Lecture 3 21 Integers  Integers are whole numbers.  +, - and * work just as you might expect.  Division is different.  If we divide two integers we get 2 more integers, not just one.  The quotient and the remainder.

ITP © Ron Poet Lecture 3 22 Integer Division Example  23 divided by 5 is 4 remainder 3.  Quotient 4.  Remainder 3.  It is not 4.6 because that is not an integer.  This means we need two division operators.  One to give us the quotient.  The other to give us the remainder.

ITP © Ron Poet Lecture 3 23 The int Type  a / b gives us the quotient.  a % b gives us the remainder.

ITP © Ron Poet Lecture 3 24 Real Numbers  Real numbers have the four standard operations +, -, *, /.  We do not get remainders when using real numbers.  The real number type is double.

ITP © Ron Poet Lecture 3 25 Numbers Are Not Objects  The int and double types are not objects.  We do not need to use new to create a number. int perfect = 6;  will create an integer variable called perfect which has the value 6.

ITP © Ron Poet Lecture 3 26 Arithmetic: Precedence  If we have several different operators in a bit of arithmetic then:  * and / will be done first.  + and – will be done afterwards.  * is 29 (not 77).  If we want to change the order we must use brackets.  (3 + 4) * (5 + 6) is 77.

ITP © Ron Poet Lecture 3 27 Arithmetic: Associativity  If we have two / signs, or a * and / sign in some arithmetic then the left most one is done first.  20 / 5 / 2 is 2 and not 10.  20 / (5 / 2) is 10  Why is it never 8?

ITP © Ron Poet Lecture 3 28 Mixed int and double  If we use an arithmetic operation with one int and one double operand then  first the int is converted to a double  then the operation is done.  5 / 2.0 is 2.5

ITP © Ron Poet Lecture 3 29 Casting  We can force the conversion from one type to another using a cast operator.  The type we want to convert to inside (). int a = 5, b = 2; double c = a / (double)b;  c will be 2.5.  It will be 2 if we don't have a cast.  integer arithmetic will be used for a / b.  The int value will then be converted to a double.

ITP © Ron Poet Lecture 3 30 Math Things

ITP © Ron Poet Lecture 3 31 Value of Pi  We don’t need to type in for Pi.  Java has a Math object that knows about these things.  java.lang.Math  Pi is java.lang.Math.PI  E is java.lang.Math.E

ITP © Ron Poet Lecture 3 32 Math Functions  java.lang.Math knows about standard maths functions such as sin, cos, abs, max.  java.lang.Math.sin(x).

ITP © Ron Poet Lecture 3 33 Constants

ITP © Ron Poet Lecture 3 34 Naming Literals  A literal is a hardcoded actual value in a program, such as e23.  It is not very informative if we have to keep writing e23 everywhere in the program.  It is more informative if we give it a name, such as AVAGADRO.

ITP © Ron Poet Lecture 3 35 final  We make a variable a constant by putting the word final in front of a variable definition.  Naturally a final variable definition must include an initial value.  Because it can't be changed afterwards.  final double AVAGADRO = e23;

ITP © Ron Poet Lecture 3 36 FormatIO and Numbers

ITP © Ron Poet Lecture 3 37 Formatted Input  readInt() will read an int.  readDouble() will read a double.  They convert digit characters into numbers.  They skip white space to find the start of the number.  White space is space, tab and newline.  They stop at the first non-digit character.

ITP © Ron Poet Lecture 3 38 Formatted Input Example con.print("Enter hours and minutes: "); int hours = con.readInt(); int mins = con.readInt();

ITP © Ron Poet Lecture 3 39 Formatted Output  Formatted output is more complicated, we must consider:  fieldwith  precision  fill character  justification style.  It is easiest to use String.format().

ITP © Ron Poet Lecture 3 40 String.format

ITP © Ron Poet Lecture 3 41 Formatting using String.format  String.format returns a formatted string  Originates from C  Especially useful in formatting numbers / dates / times.  String s = String.format(“x = %d\n”, x);

ITP © Ron Poet Lecture 3 42 Format Specifiers  The first parameter to format is a format string.  It can contain format specifiers, which start with %.  It can also contain ordinary text.  The rest of the arguments are values to be formatted.  There is one argument per % specifier.

ITP © Ron Poet Lecture 3 43 Simple Formats  %d is an integer  %f is a floating point (real) number.  %s is a String.  %n is a newline.  String.format(“%s scored an average of %f from %d attempts%n”, “Ron”, ave, tries);  Argument types match specifiers.

ITP © Ron Poet Lecture 3 44 Fieldwidth  The integer 42 takes 2 digits to print.  The integer 123 takes 3 digits.  If we are producing a table we might want to say that all integers should take 4 characters.  The fieldwidth is 4.  If we tried to print with a fieldwidth of 4, then 5 character spaces would be used.

ITP © Ron Poet Lecture 3 45 Fill Character  If we print the number 42 with fieldwidth 4 we need to provide 2 extra characters.  In most cases these will be space characters.  But we might want to use '0' characters if we are dealing with time.  7 hours 5 minutes should appear as  We can specify the fill character to use.

ITP © Ron Poet Lecture 3 46 Justification Style  Where should the extra characters go?  To the left or the right of the number.  Or should the number be centred? This is the justification style.

ITP © Ron Poet Lecture 3 47 Fieldwidth in Format  A number after the % specifies the fieldwidth.  If it is positive then the item will be right justified.  If it is negative then it will be left justified.  “%5d”, 42 produces ~~~42 (~ denotes space)  “%-5d”, 42 produces 42~~~ (~ denotes space)

ITP © Ron Poet Lecture 3 48 Fill Character 0  A 0 before the fieldwith means the fill character is 0.  “%05d”, 42 produces 00042

ITP © Ron Poet Lecture 3 49 Precision and Double  Precision refers to the number of characters after the decimal point.  Useful for double values.  %7.2f uses a fieldwidth of 7, with two digits after the decimal point.  Precision can be useful with strings.  The string is truncated if it is too big.  %-10.10s means never use more then 10 places for the String argument.

ITP © Ron Poet Lecture 3 50 Date Formatting  %t introduces a large number of different time and date formats.  Calendar now = Calendar.getInstance()  Returns the current date and time.  There are separate formats for month (b), day in the month (a), year (y), day in the month (d), hours (H), minutes (M), seconds (S), and variants of these.  “Today is %ta”, cal