Class and Method Definitions UML Class Diagram Automobile

Class and Method Definitions

UML Class Diagram Automobile - fuel: double - speed: double - license: String + increase. Speed(double how. Hard. Press): void + stop(double how. Hard. Press): void

Class Files and Separate Compilation o Each Java class definition should be in a file by itself. o The name of the file should be the same as the name of the class, and the file name should end in. java. o You can compile a Java class before you have a program in which to use it. o The compiled byte-code for the class will be stored in a file of the same name but ending in. class.

Class Files and Separate Compilation (cont’d) o Later you can compile a program file with a main part that uses classes that you have already compiled. o Every program with a main part has a class name at the start of the file; this is the name you need to use for the file that holds the program. o As long as all the classes you use in a program are in the same directory as the program file, you don’t need to worry about directories.

Example of a Class Definition import java. util. *; Public class Species { public String name; public int population; public double growth. Rate; public void read. Input() { Scanner keyboard = new Scanner(System. in); System. out. println(“What is the species’ name? ”; name = keyboard. next. Line(); System. out. println(“What is the population of the species? ”); population = keyboard. next. Int(); while (population < 0) { System. out. println(“Population cannot be negative. ”); System. out. println(“Reenter population: ”); population = keyboard. next. Int(); } System. out. println(“Enter growth rate (percent increase per year: ”); growth. Rate = keyboard. next. Double(); }

Example of a Class Definition (cont’d) public void write. Output() { System. out. println(“Name = “ + name); System. out. println(“Population = “ + population); System. out. println(“Growth rate = + growth. Rate + “%”); } public int population. In 10() { double population. Amount = population; int count = 10; while ((count > 0) && (population. Amount > 0)) { populationamount = (population. Amount + (growth. Rate/100)*population. Amount); count--; } if (population. Amount > 0) return (int)population. Amount; else return 0; } }

Instance Variables o The class name in the previous example is Species. o The class is designed to hold records of endangered species. o Each object in the class has three pieces of data: a name, a population size, and a growth rate. o Objects of this class have three methods: read. Input, write. Output, and population. In 10.

Instance Variables (cont’d) o The data items and methods are called o o members of the object. The data items are sometimes called fields, but we will refer to them as instance variables. In the previous example the instance variables were name, population, and growth. Rate. The word public simply means that there are no restrictions on how these variables are used. An object is a complex item with instance variables inside it.

Example of a Program that Uses the Class Species public class Species. Demo. Program { public static void main(String[] args) { Species species. Of. The. Month = new Species(); int future. Population; System. out. println(“Enter data on the Species of the Month: ”); species. Of. The. Month. read. Input (); species. Of. The. Month. write. Output (); future. Population = species. Of. The. Month. population. In 10(); System. out. println(“In ten years the population will be “ + future. Population); species. Of. The. Month. name = “Klingon ox”; species. Of. The. Month. population = 10; species. Of. The. Month. growth. Rate = 15; System. out. println(“The new Species of the Month: ”); species. Of. The. Month. write. Output (); System. out. println(“In ten years the population will be “ + species. Of. The. Month. population. In 10()); } }

Example of a Program that Uses the Class Species (cont’d) o You can refer to one of the instance variables of species. Of. Themonth by writing the object name followed by a dot and then followed by the instance variable’s name, e. g. , species. Of. The. Month. name o Each instance variable has a type, and the type of the previous example is String.

Example of a Program that Uses the Class Species (cont’d) o The new is used to create a new object of the class Species. o You can think of it as creating the instance variables of the object.

Using Methods o All methods perform some action or actions. o Some return a value, e. g. , next. Int. o Some do not, e. g. , println. o A method defined in a class is usually invoked using an object of that class. This object is known as the calling object, e. g. , keyboard. next. Int();

Using Methods (cont’d) o You invoke a method by writing the calling object followed by a dot, then the name of the method, and finally a set of parentheses that may or may not have information to pass to the method. o If the method invocation returns a value, then you can use the method invocation anyplace that you are allowed to write a value of the type returned by the method, e. g. , future. Population = species. Of. The. Month. population. In 10();

Using Methods (cont’d) o If the method invocation does not return a value, then you place a semicolon after the method invocation and that produces a Java statement, e. g. , species. Of. The. Month. read. Input();

Void Method Definitions o Each method definition is given inside the definition of the class to which it belongs. o The definition of a method that does not return a value starts with the keywords public void. o The word void means that nothing is returned. o The keyword public may be replaced with other words that restrict the use of the method.

Void Method Definitions (cont’d) o The first part of the method definition is called the heading. o The statements that follow, enclosed in braces { } are referred to as the body. o When a void method is invoked it is as if the method invocation were replaced with the body of the method where the instance variable names are replaced with those specific to the calling object, e. g. , name is replaced by species. Of. The. Month. name

Void Method Definitions (cont’d) o A program definition is just a class definition that has a method named main. o When you run a program you are simply invoking the void method that is name main. o The extra words static and String [] args will be explained later.

Methods that Return a Value o Methods that return a value are defined the same way as those that don’t with the following exceptions: n Instead of the word void the type of the value returned by the method is given, e. g. , public int population. In 10() n The body of the method must contain one or more return statements to return a value, e. g. , return (int)population. Amount;

Naming Methods o Choose clear meaningful names. o It’s usually a good idea to use verbs to name methods that perform some action but do not return values, e. g. println. o Nouns are usually the best choice for methods that return a value, e. g. , population. In 10. o By convention, methods begin with lowercase letters, and class names begin with uppercase

Use of return in void Methods o A return statement can also be used in methods that do not return values just to cause the method to terminate and return control to the method it was invoked from. o In this case you simply say return; with nothing following the return.

The this Parameter o When giving a method definition, you can use the keyword this as a name for the calling object. o For example, in the definition the write. Output() method of the Species class we wrote, System. out. println(“name = “ + name); We could have written, System. out. println(“name = “ + this. name);

The this Parameter (cont’d) o You can think of this as just a place holder for the calling object to be given when the method is invoked. o this is usually omitted although there are some situations where it is needed.

Local Variables o Methods usually declarations for variables used within them beyond those declared in the class. o These are local to the method in which they are declared and cannot be used outside the method. o Two methods may have variables of the same name but they are treated as separate variables.

Blocks o A block is any set of Java statements enclosed within braces ({}). o Sometimes blocks are referred to as compound statements. o If you declare a variable within a block, it is not valid outside the block. o In Java you cannot use the same name for different variables in different blocks even though it is valid only in the block where it is declared.

Parameters of a Primitive Type o The method population. In 10 for the class Species returns the projected population of a species 10 years in the future. o If we wanted to generalize this method to calculate the projected population for any number of years in the future we could pass number of years as a parameter to the method.

Revised Method for Projecting Population public int projected. Population(int years) { double population. Amount = population; int count = years: while ((count > 0) && (population. Amount > 0)) { populationamount = (population. Amount + (growth. Rate/100)*population. Amount); count--; } if (population. Amount > 0) return (int)population. Amount; else return 0; }

Example of Using a Method with a Parameter public class Species. Demo. Program 2 { public static void main(String[] args) { Species species. Of. The. Month = new Species(); int future. Population; System. out. println(“Enter data on the Species of the Month: ”); species. Of. The. Month. read. Input (); species. Of. The. Month. write. Output (); future. Population = species. Of. The. Month. projected. Population(10); System. out. println(“In ten years the population will be “ + future. Population); species. Of. The. Month. name = “Klingon ox”; species. Of. The. Month. population = 10; species. Of. The. Month. growth. Rate = 15; System. out. println(“The new Species of the Month: ”); species. Of. The. Month. write. Output (); System. out. println(“In ten years the population will be “ + species. Of. The. Month. projected. Population(10)); } }

Formal Parameters and Arguments o In the projected. Population(int years) method definition the word years is called a formal parameter or simply a parameter. o It is a stand-in for a value that will be plugged in when the method is invoked. o The item that is plugged in is called an argument ( or sometimes an actual parameter).

Formal Parameters and Arguments (cont’d) o In the invocation: future. Population = species. Of. The. Month. projected. Population(10); the value 10 is an argument. o It is important to note that only the value of the argument is used in this substitution process. If the argument is a variable only the value of the variable is plugged in, not the name of the variable. This is called call-by-value.

Correspondence Between Formal Parameters and Arguments o Formal parameters are given in parentheses after the method name at the beginning of a method definition. o In a method invocation, arguments are given in parentheses after the method name. o There must be exactly the same number of arguments as formal parameters, and they must be given in the same order.
- Slides: 30