Title: Chapter 5- Even more about objects and methods
1Chapter 5- Even more about objects and methods
2Overview
- Designing methods
- Methods, methods, methods
- Overloading methods
- Constructor methods
- Static methods and variables
- Math class and static methods
- Information Hiding again
- Packages
3Method Design
4Designing 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?
5Designing 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.
6Designing 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.
7Pseudocode
- 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)
8Filling 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 lt20 I) sumI System.out.pri
ntln(sum)
9Keep 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.
10More details about methods
11Methods 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 dont 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.
12Example of methods within methods(suppose
instanceInt is a private variable for the class)
public void enterInt() System.out.println(Ente
r an integer) instanceInt
SavitchIn.readLineInt() displayInfo()
public void displayInfo() System.out.println(
The integer stored is instanceInt)
Could also use this.displayInfo()
13Having 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 wont have when
trying to use your class (you will have access to
private information/methods). DONT 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).
14From 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.
15Things not to do in your main method
public class Triangle private double base,
height public static void main(String
args) Triangle a new Triangle() System.ou
t.println(The base is a.base) System.out.pr
intln(The height isa.height)
Should use a.getBase() and a.getHeight()
16Overloading methods
17Overloading 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.
18Method overloading examples.
public int add(int num1, int num2) return
(num1num2) 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 num1num2num3 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
19Things 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) ?.
20Are the following sets of method headers valid
for overloading?
Yes
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)
No, the types are the same.
Yes
21Constructors
22Constructors
- 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.
23More 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.
24Constructor examples
public class Muglet private int number
public Muglet() number 0
public Muglet(int numIn) number numIn
Called a default constructor(no args)
25Using the Constructor example
public class MugletTest public static void
main(String args) Muglet muglet1 new
Muglet() Muglet muglet2 new Muglet(45)
26Constructors with more data types
public 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.
27Which 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)
28What 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?
29Static variables and methods
30Static 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?
31Example 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.
32Making static methods.
- Cant use any instance variables. Can only access
static variables (well get to that in minute). - Cant 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)
33Static variables
- Not associated with any one object, its
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.
34Example 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")
35Using the previous example
public class MugletTest public static void
main(String args) Muglet.mugletReport() M
uglet muglet1 new Muglet(1) Muglet muglet2
new Muglet(15) Muglet muglet3 new
Muglet(Arr) Muglet.mugletreport() 0 3
36Math class and the random method
37Math 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.
38random 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.
39random examples
x Math.random() //0.0lt x lt 1.0 x
Math.random()2 //0.0 lt x lt 2.0 x
Math.random()2 1 //1.0ltx lt 3.0 x
(int)(Math.random()2 1) //1ltxlt2 x
(int)(Math.random()6 1) //1ltxlt6 x
(int)(Math.random()6 1)3 //x is one of
3,6,9,12,15,18.
40What 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, youll be able to do
about everything for games except graphics).
41Tricks in Information Hiding
42Keeping 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.
43Keeping private data private
- You dont have to worry about what you return if
you are only returning primitives or a String. - If you want to be safe with objects, youll want
to return a clone of the object (use the clone()
method, assuming it is provided in the objects
class).
44Packages
45Packages
- 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
46Our 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
47Package 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.
48Review
49Chapter Review- Designing
- What is the topdown design method, in your own
words? - What languages are used in pseudocode?
50Chapter 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 cant you have them in?
51Chapter 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)
52Chapter 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?
53Chapter 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 cant you use in static
methods? - What types of variables cant you use in instance
methods?
54Chapter 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?
55Chapter 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?