chapter 5- even more about objects and methods. overview n designing methods n methods, methods,...

55
Chapter 5- Even more about objects and methods

Upload: augustine-reynolds

Post on 25-Dec-2015

230 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Chapter 5- Even more about objects and methods

Page 2: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Overview

Designing methods Methods, methods, methods Overloading methods Constructor methods Static methods and variables Math class and static methods Information Hiding again Packages

Page 3: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Method Design

Page 4: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Designing methods

Now that we are starting to get into some larger programs, things can get a lot more difficult very quickly. Design becomes more and more important from here on out.

Every class can have as many methods as we want, but how many do we really want? And what do they need to do?

Page 5: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Designing steps

Determine what you need the class to do. (Like the class exercise).

Figure out what types of data you need. Write down the method declarations

(the names and the types taken/returned) that you need.

Design the methods next.

Page 6: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Designing Methods

Write in English what the method should do.

Write pseudocode for what the major steps should be in the method.

Fill in the pseudocode with actual Java code to finish out the method.

Page 7: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Pseudocode

A mixture of plain English and whatever programming language you are using.

for(I = 1 to 20){

add I to the sum.Print out the sum. System.out.println(sum);

}

Page 8: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Filling in the pseudocode Fill in any English with Java code.

for(I = 1 to 20){

add I to the sum.Print out the sum. System.out.println(sum);

}

for(I = 1; I <=20; I++){

sum+=I;System.out.println(sum);

}

Page 9: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Keep refining step by step.

The previously described method is sometimes called the top-down method or stepwise refinement.

Just slowly chisel away at large problems, refining the design one step at a time until you get to code.

Page 10: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

More details about methods

Page 11: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Methods are everywhere…

You can have as many methods as you want in a class.

Not all of them have to be public, any ones that you don’t want accessible outside of the class can be made private.

You can call methods from other methods (in fact this is the only way we do call methods).

Can also have a main method in every class.

Page 12: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Example of methods within methods(suppose instanceInt is a private variable for the class)…

public void enterInt(){

System.out.println(“Enter an integer:”);instanceInt = SavitchIn.readLineInt();displayInfo();

}

public void displayInfo(){

System.out.println(“The integer stored” +“ is “ + instanceInt);

}

Could also usethis.displayInfo();

Page 13: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Having a main method in every class. Having a main method in every class can be a good

tool in debugging problems. When making a class, make a main method in it to

test the functionality. Be careful that in your main method you are not

taking liberties that others won’t have when trying to use your class (you will have access to private information/methods). DON’T make calls to private methods or access private data from your main method!

The only main method that is called when your program starts is the one that has the same name as in the java command (or whatever file is visible in TextPad when you use the run command).

Page 14: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

From now on…

Homework assignments can all be done in one file if you wish (sometimes it is nicer to have more than one file though). You can write the class and have the testing program located inside that same class (inside the main method). Using private methods or accessing private data is not acceptable, though, so be careful.

Page 15: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Things not to do in your main methodpublic class Triangle{ private double base, height;… public static void main(String [] args) { Triangle a = new Triangle();

System.out.println(“The base is: “ +a.base);System.out.println(“The height is:”+a.height);

}}

Should use a.getBase() and a.getHeight()

Page 16: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Overloading methods

Page 17: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Overloading methods Method overloading is the act of having

multiple methods in a class that have the same name.

Each overloaded method has to have different parameters than any of the other methods.

When that method name is invoked, the number and type of arguments in the invocation determine which method is called.

Page 18: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Method overloading examples.public int add(int num1, int num2){ return (num1+num2);}public char add(char let1, char let2){ let2 =(char)((int)let2 – (int)’a’);

return(char)((int)let1 + (int)let2);}public int add(int num1, int num2, int num3){ return num1+num2+num3;}public String add(String phrase1, int num1){ return phrase1 + num1;}

add(1,2) = 3 add(‘b’,’b’) = ‘c’add(1,2,3) = 6 add(“Hello”,3) = “Hello3”

Page 19: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Things to be careful about

The parameters have to be different, but the return type is ignored. The following would be invalid:

public int add(int num1, int num2){…}

public double add(int num1, int num2){…} Automatic conversions on the data types

being passed in can have unexpected results in just which method is called. add(‘1’,’2’) = ?.

Page 20: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Are the following sets of method headers valid for overloading?public void method1(int num1, int num2);public void method1();public void method1(int num1, double num2);public void method1(char num1, char num2);

public void method2(int num1, int num2);public double method2(int num1, int num2);public char method2(int num1, int num3);

public void method3(int num1);public double method2(int num1, int num2);public double method3(int num1, int num2, int num3);

Yes

No, thetypes arethe same.

Yes

Page 21: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Constructors

Page 22: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Constructors

Constructors are very special methods of a class. They have the exact same name as the class and are only called when you create an object of the class.

If you do not specify any constructors, Java will provide one for you (though it may not act as you want).

Constructors are never static (later) and never return any information. They are mainly just for initializing your objects.

Page 23: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

More constructors

Constructors are often overloaded. Often have a constructor for every possible

combination of data initializations. This way users can create a new item and

initialize it to some value in one step. A constructor with no parameters is called a

default constructor.

Page 24: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Constructor examplespublic class Muglet{ private int number; public Muglet() {

number = 0; }

public Muglet(int numIn) {

number = numIn; } }

Called a default constructor(no args)

Page 25: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Using the Constructor example

public class MugletTest{ public static void main(String [] args) {

Muglet muglet1 = new Muglet();Muglet muglet2 = new Muglet(45);

}}

Page 26: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Constructors with more data typespublic class Muglet{ private int number; private int otherNumber; private String name; public Muglet(){…} public Muglet(int aNumber) {…} public Muglet(String aName) {…} public Muglet(int aNumber, int anotherNumber){…} public Muglet(int aNumber, int anotherNumber,

String aName){…} }Note there is no Muglet(int anotherNumber) constructor, as it would look the same as Muglet(int aNumber). This would break the overloading.

Page 27: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Which are not valid groups of constructors for the class Muglet.

public Muglet(){…}public Muglet(int a, int b){…}public Muglet(int b, int c){…}public Muglet(double g, int c){…}

public Muglet(){…}public Muglet(int a){…}public Muglet(double a){…}public Muglet(String a){…}public Muglet(String a, int a, double a){…}

Page 28: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

What constructors might we want?

If the class Muglet contained just one private integer instance variable?

If the class Blar contained one private String and one private double instance variables?

Page 29: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Static variables and methods

Page 30: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Static methods

Static methods are methods that can be called without a calling object (it is called with the class instead).

Math and SavitchIn are examples of classes that have static methods.

What is a static method that we ourselves have already been programming?

Page 31: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Example of using static methods

int readInt = SavitchIn.readLineInt();

double exponent = Math.pow(4.0,5.0);

int absolute = Math.abs(readInt);

Classes, not objects.

Static methods.

Page 32: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Making static methods.

Can’t use any instance variables. Can only access static variables (we’ll get to that in minute).

Can’t use any instance methods(nothing that would use “this”).

Just put the word static in between the public/private modifier and the return type of the method:public static void main(String [] args)

public static double area(double radius)

Page 33: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Static variables

Not associated with any one object, it’s associated with all objects of a class.

“Class-wide” variables. Every object of the class can see the same variable, and it is the same value for all of the objects.

Good way of keeping counts of the number of objects created, or for keeping constants that all objects need access to.

Page 34: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Example of creating static methods

public class Muglet{ public static int number; public String name;

public Muglet(String someString) { this.name = someString; number++; }

public static void mugletReport() { System.out.println("You have made " + number + " Muglets"); }}

Page 35: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Using the previous examplepublic class MugletTest{ public static void main(String []args) {

Muglet.mugletReport();Muglet muglet1 = new Muglet(“1”);Muglet muglet2 = new Muglet(“15”);Muglet muglet3 = new Muglet(“Arr”);Muglet.mugletreport();

}}

03

Page 36: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Math class and the random method

Page 37: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Math methods

The Math class has a TON of useful static methods that you can use.

We have already seen a few in the last section such as abs and pow.

We will concentrate on the random method in this section.

Page 38: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

random method.

When we call Math.random(), it will give us a double value greater than or equal to 0.0 and less than 1.0.

We can use the multiply and addition operators as well as integer casts to make this any sort of random integer range.

Page 39: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

random examplesx = Math.random(); //0.0<= x < 1.0

x = Math.random()*2; //0.0 <= x < 2.0

x = Math.random()*2 + 1; //1.0<=x < 3.0

x = (int)(Math.random()*2 +1); //1<=x<=2

x = (int)(Math.random()*6 + 1); //1<=x<=6

x = (int)(Math.random()*6 +1)*3;//x is one of 3,6,9,12,15,18.

Page 40: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

What to use the random function for

Basically, games. Whenever you need a random event to

occur, create a random number and choose the event based on the number.

Now you can start making very basic games of chance (once we do arrays, you’ll be able to do about everything for games except graphics).

Page 41: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Tricks in Information Hiding…

Page 42: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Keeping private data private

If you have a private instance Class variable(you have an object as one of your private instance variables), it is a good idea not to return the object from your methods.

The object you return, though private, is an actual memory address, so people can take that memory address and corrupt the data that it points to.

Page 43: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Keeping private data private

You don’t have to worry about what you return if you are only returning primitives or a String.

If you want to be safe with objects, you’ll want to return a clone of the object (use the clone() method, assuming it is provided in the object’s class).

Page 44: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Packages

Page 45: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Packages

There are hundreds of libraries out there that have pre-written code from other people and companies that we can use in our program.

To use these libraries, called packages, we need to just use and import statement.

import java.text.*; //import all classes //from java.text package

Page 46: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Our own packages

We can even make our own libraries of classes for other people to use.

To make a collection of classes into a package, you only need to put a package statement at the top of each class you want to include.package org.eggnogg.roborally;package com.ibm.widgets;

Page 47: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Package details

To compile code correctly from packages, the package must be in specific directory that is related to the package name.

For details on this, see java.sun.com or read more in Chapter 5 about packages.

Page 48: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Review

Page 49: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Chapter Review- Designing

What is the topdown design method, in your own words?

What languages are used in pseudocode?

Page 50: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Chapter Review- Method Details

Is there a limit to the number of methods you can have in a class? How many?

Can you call another method from inside of a method?

Can you have a main method in every class? Which classes can’t you have them in?

Page 51: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Chapter Review- Overloading

What is overloading? What has to be different about two methods if

we wish to have one of them overload the other one?

What role does the return type play in overloading?

Is the following valid?public int method1(int number1){…}public int method1(double number1){…}

Page 52: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Chapter Review-Constructors

What are constructors? What are they usually used for?

How do you specify that a method is a constructor?

What type does a constructor method return?

Can you overload constructors?

Page 53: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Chapter Review-Static stuff

What is the difference between a static method and an instance method?

What is the difference between a static variable and an instance variable?

What types of variables can’t you use in static methods?

What types of variables can’t you use in instance methods?

Page 54: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Chapter Review-Privacy

What types of instance variables do you not need to worry about when returning?

Why is returning a private instance object sometimes a bad thing to do in a method?

Page 55: Chapter 5- Even more about objects and methods. Overview n Designing methods n Methods, methods, methods n Overloading methods n Constructor methods n

Chapter review-Packages

What are packages? What statement would we use to have

access to the package edu.cs103.stuff? What statement would we use in our

class files to make it part of package edu.cs103.stuff?