Lecture 5

Lecture 5 - Lecture 5 Midterm covers chapters 1 2 3 and 4 and parts of 5 in the text Review all self-test questions in chapters 1 2 3 and 4 Review

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 5 Midterm covers chapters 1, 2, 3 and 4 and parts of 5 in the text. Review all self-test questions in chapters 1, 2, 3 and 4 Review all of the lecture notes 1 though 5. Important Classes: Point, Complex, Math, String, Random, Scanner Method Invocation and Static Methods and Fields We have usually invoked a method from an object in the class that defines the method. object.method(); In contrast, static methods of a class can be called without creating an object of that class. We have actually seen several examples of the invocation of static methods. There are two ways to call a static method. 1. A static method can be called just by the method’s name from within the class that defines it. method(); 2. From outside the defining class, use the invocation: Class.method(); An example of the latter approach was already seen when we used methods from the Math class. Math.sqrt(); Static fields are common to the class and are shared by all objects of that class. An example is the field for π in the Math class. Math.PI For more on static methods from the Math class see Chapter 5 page 335.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
An example of the first invocation of a static method is seen in this example from last week. Complete Code: Question3.java from Lecture 4 //Question3 import java.util.*; public class Question3 { public static void main (String[] args) {Scanner keyboard = new Scanner(System.in); System.out.println("Please enter a whole number n."); int n = keyboard.nextInt( ); System.out.println("Enter a digit d from 0 to 9 which you would like to count."); int d = keyboard.nextInt( ); int count = countDigits(n,d); System.out.printf("The digit %d appears %d time(s) in the number %d. \n", d, count, n); } // end of method main public static int countDigits ( int n, int d ) { if (n== 0) return 0; else return ( ( n % 10 == d ) ? countDigits( n/10,d ) + 1 : countDigits( n/10 ,d) ); } // end of countDigits method } // end of class Overloaded Methods and Method Signatures A method’s signature is a combination of the method name and the number, types and order of its parameters. It is this unique signature that allows the definition of overloaded methods as seen in the next example. The method CountDigits does not require an object or class to invoke it since it is static. The main method is static. Here we declare that the method countDigits is static.
Background image of page 2
Below are two methods for constructing complex numbers. Both constructor methods have the same overloaded name Complex , but have different signatures. Below we only show part of the class Complex . public class Complex { private double u; private double v; // Constructor to create a new complex number x+iy by specifying both components Complex (double x, double y) {u=x; v=y; } // Constructor to create a new complex number x+iy assuming the imaginary component is zero. // Only the real component is used as an input argument.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/17/2012 for the course ECE 203 taught by Professor Robincarr during the Fall '07 term at Drexel.

Page1 / 15

Lecture 5 - Lecture 5 Midterm covers chapters 1 2 3 and 4 and parts of 5 in the text Review all self-test questions in chapters 1 2 3 and 4 Review

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online