Building Java Programs

Slides:



Advertisements
Similar presentations
JAVA Revision Lecture Electronic Voting System Marina De Vos.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Introduction to Java Programming Language May 2015 Kyung Eun Park COSC Introduction to Computer Science II.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-x: Critters reading: HW9 Spec.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-4: Static Methods and Fields.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
CSC 142 Computer Science II Zhen Jiang West Chester University
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Building java programs, chapter 3 Parameters, Methods and Objects.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2010 by Pearson Education Homework 8: Critters reading: HW8 spec.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
Copyright 2009 by Pearson Education Building Java Programs Chapter 8: Classes Lecture 8-3: More Critters, static.
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
ENUMERATED TYPES Java Basics. Enumerated Type Topics Why use them? Defining an enumerated type Using an enumerated type Advanced features.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Building Java Programs
Building Java Programs
© 2016, Mike Murach & Associates, Inc.
Lecture 11: More on Classes
Building Java Programs
CSC240 Computer Science III
Object Based Programming
Lecture 8-3: Encapsulation, this
Building Java Programs
Building Java Programs
Homework 8: Critters (cont.)
Implementing Non-Static Features
Building Java Programs
Packages From Deitel & Deitel.
More on Classes and Objects
Building Java Programs
Building Java Programs
Lecture 12: Classes II Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs
Tonga Institute of Higher Education
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Homework 8: Critters (cont.)
Building Java Programs
Homework 9: Critters (cont.)
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
Chapter 9 9-3: Polymorphism reading: 9.3
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Object Oriented PHP.
Presentation transcript:

Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading: 9.3 - 9.4

Static fields static: Part of a class, rather than part of an object. Classes can have static fields. Static fields are not replicated into each object; a single field is shared by all objects of that class. private static type name; or, private static type name = value; Example: private static int count = 0;

Static field example public class Husky extends Critter { // count of Huskies created so far private static int objectCount = 0; private int number; // each Husky has a number public Husky() { objectCount++; number = objectCount; } ... public String toString() { return "I am Husky #" + number + "out of " + objectCount;

Static methods static method: Part of a class, not part of an object. shared by all objects of that class good for code related to a class but not to each object's state does not understand the implicit parameter, this; therefore, cannot access an object's fields directly if public, can be called from inside or outside the class Declaration syntax: (same as we have seen before) public static type name(parameters) { statements; }

Static method example 1 Java's built-in Math class has code that looks like this: public class Math { ... public static int abs(int a) { if (a >= 0) { return a; } else { return -a; } public static int max(int a, int b) { if (a >= b) { return b;

Static method example 2 public class Point { ... // Converts a String such as "(5, -2)" to a Point. // Pre: s must be in valid format. public static Point parse(String s) { s = s.substring(1, s.length() - 1); // "5, -2" s = s.replaceAll(",", ""); // "5 -2" // break apart the tokens, convert to ints Scanner scan = new Scanner(s); int x = scan.nextInt(); // 5 int y = scan.nextInt(); // 2 Point p = new Point(x, y); return p; }

Calling static methods class.method(parameters); This is the syntax client code uses to call a static method. Examples: int absVal = Math.max(5, 7); Point p3 = Point.parse("(-17, 52)"); From inside the same class, the class. is not required. method(parameters); This is the syntax you used to call methods in your programs.