Download presentation
Presentation is loading. Please wait.
Published byLambert Oliver Stone Modified over 9 years ago
1
ITP © Ron Poet Lecture 3 1 Comments
2
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!
3
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.
4
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.
5
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.
6
ITP © Ron Poet Lecture 3 6 Scope of a Variable
7
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.
8
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);
9
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.
10
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.
11
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);
12
ITP © Ron Poet Lecture 3 12 Using Files
13
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
14
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
15
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
16
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.
17
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!
18
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.
19
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"
20
ITP © Ron Poet Lecture 3 20 Programming With Numbers
21
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.
22
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.
23
ITP © Ron Poet Lecture 3 23 The int Type a / b gives us the quotient. a % b gives us the remainder.
24
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.
25
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.
26
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. 3 + 4 * 5 + 6 is 29 (not 77). If we want to change the order we must use brackets. (3 + 4) * (5 + 6) is 77.
27
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?
28
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
29
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.
30
ITP © Ron Poet Lecture 3 30 Math Things
31
ITP © Ron Poet Lecture 3 31 Value of Pi We don’t need to type in 3.14159 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
32
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).
33
ITP © Ron Poet Lecture 3 33 Constants
34
ITP © Ron Poet Lecture 3 34 Naming Literals A literal is a hardcoded actual value in a program, such as 6.0221415e23. It is not very informative if we have to keep writing 6.0221415e23 everywhere in the program. It is more informative if we give it a name, such as AVAGADRO.
35
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 = 6.0221415e23;
36
ITP © Ron Poet Lecture 3 36 FormatIO and Numbers
37
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.
38
ITP © Ron Poet Lecture 3 38 Formatted Input Example con.print("Enter hours and minutes: "); int hours = con.readInt(); int mins = con.readInt();
39
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().
40
ITP © Ron Poet Lecture 3 40 String.format
41
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);
42
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.
43
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.
44
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 12345 with a fieldwidth of 4, then 5 character spaces would be used.
45
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 0705. We can specify the fill character to use.
46
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.
47
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)
48
ITP © Ron Poet Lecture 3 48 Fill Character 0 A 0 before the fieldwith means the fill character is 0. “%05d”, 42 produces 00042
49
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.
50
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.