Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec 7 Using Static Class Methods. Today We work with static utility methods of a class We learn how JavaDocs can help us figure out how to use them.

Similar presentations


Presentation on theme: "Lec 7 Using Static Class Methods. Today We work with static utility methods of a class We learn how JavaDocs can help us figure out how to use them."— Presentation transcript:

1 Lec 7 Using Static Class Methods

2 Today We work with static utility methods of a class We learn how JavaDocs can help us figure out how to use them

3 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String [] args) 2.Instantiable Class – use to make objects from – String, Scanner 3.Classes with only static utility methods – like Math class

4 3 Different Kinds of Classes -- I Application Class – what we've been doing – has public static void main(String [] args) – where program begins execution – where we develop solutions to lab problems create variables, objects that help us read input, compute answers, display results

5 3 Different Kinds of Classes -- II Instantiable Class – used to make objects from – SO FAR ONLY: String, Scanner To use an Instantiable class we must – Declare a variable of that class type String message; Scanner myUserInput; – Instantiate a new object message = new String("hello"); myUserInput = new Scanner(System.in); – Use associated methods (name of object '.' method) message.trim(); myUserInput.nextInt();

6 3 Different Kinds of Classes -- III Classes with static utility methods – We never "instantiate" these classes – Math class (Math.random(), Math.round() ) – refer to the class name '.' method name We can get lots of help from these IF we learn how to read the JavaDocs manuals on them

7 Class NumericalStuff Demo "Helper" for working with numbers – finding prime numbers, dividing – Home made by Professor Kowalczk Javadocs for NumericalStuffNumericalStuff Lets study some of the method documentation:

8 NumericalStuff -- findPrime static: means it's a static utility method – will not be associated with an object – have to invoke by "Name of Class" NumericalStuff.findPrime

9 NumericalStuff -- findPrime int: means it's going to give you an integer back – you need a variable to receive it: int myPrime = NumericalStuff.findPrime... – OR you can print it out System.out.println("a prime is " + NumericalStuff.findPrime... )

10 NumericalStuff -- findPrime findPrime: the name of this particular method

11 NumericalStuff -- findPrime int n: means you must give it an integer in parens – int myPrime = NumericalStuff.findPrime (5); – System.out.println("a prime is " + NumericalStuff.findPrime(12));

12 Given this NumericalStuff method doc Which is/are using it correctly? boolean x = isPrime ( 23); int x = NumericalStuff.isPrime(23); if (NumericalStuff.isPrime(23)){.... boolean x = NumericalStuff.isPrime(3.14159);

13 Using Class OtherStuff... How would you show the current Time ? How would you save the 10th dictionary word?

14 Using Class StringUtil... How to determine what is the 5 th letter of "centaur" ?

15 Using Class Coin: One other return type -- void Means it doesn't return anything Use it on a line by itself Coin.dispenseDime(true); // prints a dime, beeps Coin.dispenseDime(false); // prints, doesn't beep

16 Lab 7 Making change with Coin Class Ask for amount of change requested Find how many coins that is (using Coin class) dispense Quarters until no more left – use a while loop with Coin.dispenseQuarters inside dispense Dimes until no more left – use a while loop with Coin.dispenseDimes inside and so on with nickels and pennies maybe disable "beep" while debugging!


Download ppt "Lec 7 Using Static Class Methods. Today We work with static utility methods of a class We learn how JavaDocs can help us figure out how to use them."

Similar presentations


Ads by Google