Download presentation
Presentation is loading. Please wait.
Published byJeffery Henry Modified over 8 years ago
1
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham
2
Week 13 Topics 13.1.1 Preconditions and Postconditions 13.1.2 Static Methods 13.1.3 Static Fields 13.1.4 Scope 13.1.5 Packages
3
13.1.1 Preconditions and Postconditions A precondition is a requirement that the caller of a method must meet for the method to execute properly If a method is called in violation of a precondition, the method is not responsible for computing the correct result
4
13.1.1 Preconditions and Postconditions Cont. public void deposit (double amount) { if (amount < 0) throw new IllegalArguementException (“Amount is less than zero”); balance = balance + amount; }
5
13.1.1 Preconditions and Postconditions Cont. A postcondition is a guarantee made by the method to its caller about a return value or the final state of an object after the execution of the method Postconditions are enforced by good testing practices
6
13.1.1 Preconditions and Postconditions Cont. public void testDeposit () { BankAccount b = new BankAccount(100.00); b.deposit(50.00); assertEquals(150.00, b.getDeposit(),.0001); }
7
13.1.2 Static Methods A method that is called on a class rather than an object is called a static method or a class method A classic example is the Math class, which is a utility class that has only static methods and static instance fields double d = Math.sqrt(33.33); double d = Financial.percentOf(5.5, 7.00);
8
13.1.2 Static Methods Cont. public class Financial () { public static double percentOf (double p, double a) { return (p / 100) * a; } // More financial methods can be added here. }
9
13.1.3 Static Fields A static field belongs to the class, not to any object of the class All instances of an object “share” the static variable – it belongs to the class, not to each instance of the class public static final double PI = 3.14159265358979323846;
10
13.1.3 Static Fields Cont. public class BankAccount () { private static int lastAcctNbr = 1000; public BankAccount() { lastAcctNbr++; accountNumber = lastAcctNbr; }... }
11
13.1.4 Scope The scope of a variable is the region of a program in which the variable can be accessed The scope of a local variable cannot contain the definition of another variable with the same name
12
13.1.4 Scope Cont. A qualified name is prefixed by it class name or by an object reference, such as Math.sqrt or harrysChecking.getBalance() An unqualified instance field or method refers to the this parameter A local variable can shadow an instance field with the same name (the local variable “wins out”). Use this to access the shadowed instance field.
13
13.1.5 Packages A package is a set of related classes The Java library consists of dozens of packages To put classes in a package, you must put package packageName; as the first instruction in the source file containing the classes
14
13.1.5 Packages Cont. The import directive lets you refer to a class of a package by its class name, without the package prefix java.util.Scanner in = new java.util.Scanner(System.in); import java.util.Scanner; Now you don’t have to fully qualify by package
15
13.1.5 Packages Cont. You can import all class of a package with an import statement that ends in.* import java.util.*; Another reason for packages is to avoid name clashes java.util.Timer versus javax.swing.Timer
16
13.1.5 Packages Cont. If you write your own packages, you need to study concepts of how package names and the underlying directory structure that the packages are stored in work You will also need to understand how packages work with your operating system CLASSPATH
17
Reference: Big Java 2 nd Edition by Cay Horstmann 13.1.1 Preconditions and Postconditions (section 9.5 in Big Java) 13.1.2 Static Methods (section 9.6 in Big Java) 13.1.3 Static Fields (section 9.7 in Big Java) 13.1.4 Scope (section 9.8 in Big Java) 13.1.5 Packages (section 9.9 in Big Java)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.