Introduction to Computer Science and Object-Oriented Programming

Slides:



Advertisements
Similar presentations
Classes and Objects CMSC 202. Version 9/122 Programming & Abstraction All programming languages provide some form of abstraction. – Also called information.
Advertisements

Designing Classes Chapter 8. Classes Collection of objects Objects are not actions Class names – Nouns Method names – Verbs What Makes a Good Class Represent.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
Chapter 9 Designing Classes. Chapter Goals To learn how to choose appropriate classes to implement To understand the concepts of cohesion and coupling.
Chapter 7 Designing Classes Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling  To.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
Class Design CSC 171 FALL 2004 LECTURE 11. READING Read Chapter 7 It’s abstract But it should help with project #1.
Java Library Java provides a huge library or collection of useful programs A gold mine of well-tested code that can save you countless hours of development.
The different types of variables in a Java program.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Object Oriented Programming in Java Habib Rostami Lecture 5.
Introduction to Object-Oriented Programming
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 12 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
ICOM 4015: Advanced Programming Lecture 8 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Eight:
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain Name.
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Static. Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A static method does not operate on an object double dY.
Packages. Package 1.Avoid naming conflicts. Put classes into packages so that they can be referenced through package names. 2.Distribute software conveniently.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Eight: Designing Classes.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming1 Programming.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain, or a.
CSS446 Spring 2014 Nan Wang.  A Java program consists of a collection of classes.  Package is a set of related classes. 2.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
Chapter 8: Designing Classes Accessors, Mutators, and Immutable Classes(8.3) Side Effects(8.4) Parameter Passing (Advanced Topic 8.1) Preconditions and.
Chapter 8 – Designing Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Chapter 9 – Designing Classes Goals Learning to design classes Learning to design classes Preconditions and postconditions for methods Preconditions.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7 Designing Classes. Chapter Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling.
Fall 2006Adapded from Java Concepts Slides1 Designing Classes Advanced Programming ICOM 4015 Lecture 8 Reading: Java Concepts Chapter 8.
Chapter 9 – Designing Classes. Chapter Goals Discovering and learning to Design classes Discovering and learning to Design classes Preconditions and postconditions.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 8 – Designing Classes
A final variable is a constant
Lecture 3 John Woodward.
Chapter 8 Designing Classes
Section 11.1 Class Variables and Methods
Switch, Rounding Errors, Libraries
Classes (review): A Java program consists of classes.
Chapter Three - Implementing Classes
Chapter 8 – Designing Classes
Chapter 8 – Designing Classes
JAVA CLASSES.
Introduction to Object-Oriented Programming
Using java libraries CGS3416 spring 2019.
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Introduction to Computer Science and Object-Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Introduction to Computer Science and Object-Oriented Programming Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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

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

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; }

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

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); }

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);

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.

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;

13.1.3 Static Fields Cont. public class BankAccount () { private static int lastAcctNbr = 1000; public BankAccount() lastAcctNbr++; accountNumber = lastAcctNbr; } . . .

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

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.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

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

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

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

Reference: Big Java 3rd Edition by Cay Horstmann 13.1.1 Preconditions and Postconditions (section 8.5 in Big Java) 13.1.2 Static Methods (section 8.6 in Big Java) 13.1.3 Static Fields (section 8.7 in Big Java) 13.1.4 Scope (section 8.8 in Big Java) 13.1.5 Packages (section 8.9 in Big Java)