Computer Science : Exercises 07.06-12

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the difference between an actual parameter and a formal parameter?

*formal parameters* are in the method heading *actual parameters* are in the method call.

What is the difference between class and local variables?

- *Local variables* are defined inside a particular method. Only that method will have access to these variables. They simply do not exist outside of the method. - Variables that are declared inside a method or block are called local variables. Local variables are only accessible inside the method or block that they are defined in. - If a variable is only used by one method, it should be declared inside that method as a local variable. ----------------------------------------- - *Class variables* are not defined in any method, but they are defined within the Class and can be accessed by ALL methods of that class. - Variables that are declared inside a class, but outside any method, are class variables. Class variables are accessible by any method of the class. - Class variables are also called attributes. - If a variable is used by 2 or more methods of a class, it should be declared as a class variable.

What does *PIN* stand for? What does *ATM* stand for?

- *P*ersonal *I*dentification *N*umber - *A*utomatic *T*eller *M*achine

What are the differences between between a void and return method?

- Void methods do not return a value and use the void keyword in the method heading. - Return methods do return a value. The data type of the returned value is declared in the method heading. Additionally, the last statement in a return method must be a return statement. - It is very important to understand that return methods are called in a program statement that uses the return value. Void method calls stand by themselves.

Parameters in the method call are called ____________________________________. An example of this parameter is "displayParameter(*100*);" Parameters in the method heading are called ______________________________. An example of this parameter is "public static void displayParameter (int *number*)"

- actual parameters - formal parameters

Void method calls are like a __________________________________. They can exist on their own. Return methods are like a ____________________________. They are incomplete and need to be part of something else. This is because a return method returns a value and you need to do something with it. You can ______________, or __________________ or ___________________ to another value.

- complete sentence - subordinate clause - print it, store it, compare it

An actual parameter can be several different things. List 5 examples.

- constants (13) or (Math.PI) - variables (x) - expressions with constants only (20 + 30) - expressions with variables & constants (x + 5) - return method calls (Math.sqrt(225)) Exs: displayParameter(13); or displayParameter(Math.PI); displayParameter(x); displayParameter(20 + 30); displayParameter(x + 5); displayParameter(Math.sqrt(225));

When creating a return method, *void* in the method heading should be replaced by a _____________. However, in a return method, the last statement must always be a _________________________.

- data type - return statement Ex: public static *int* getNextNumber(int n) { n++; *return n*; }

The _____________________ of the ____________________ in the ____________________ must match the sequence of the formal parameters in the heading.

- sequence - parameters - method call

When stating a *formal* parameter, make sure to include ___________________________________________; however, actual parameters do not _______________________________.

- the data type of each parameter - specify data type

Look at program Parameter11.java. List 3 ways a return method can be called.

1) *In a println statement*: System.out.println("Sum: " + sum(200,300)); 2) *In an assignment statement*: int sum = sum(200,300); 3) *In a conditional statement*: int checking = 200; int savings = 300; if (sum(checking,savings) <=0) System.out.println("You are broke!");

List 2 methods that do NOT require any parameters?

1) println(); 2) nextInt();

Which methods have access to a class variable?

Class variables are accessible by any method in the class.

Look at program Design05.java. What kind of programming does it use?

Modular programming

If a method has several parameters, do they all need to be of the same type?

No

What should the main method be used for?

Program sequence, NOT large numbers of program statements.

What is the essence of program design?

The creation of practical classes and the proper interaction of these classes.

What is the driving class?

The driving class of a program is the class that contains the main method or the paint method.

Describe the pattern of parameter sequence.

The first actual parameter passes information to the first formal parameter. The second actual parameter passes information to the second formal parameter. Parameters placed out of sequence may result in compile errors or logic errors.

What are the simple rules about using methods with parameters?

The number of actual parameters must match the number of formal parameters. The corresponding actual parameters must be the same type as the formal parameters. The sequence of the actual parameters must match the sequence of the formal parameters. The identifiers of the actual parameters may be the same as or different from the identifiers of the formal parameters.

The number of parameters in the method call must match what?

The number of parameters in the method heading.

Look at program Design01.java. This program does compile and does execute. What is wrong with this program?

The program design is horrible

What is the driving class responsible for?

The program execution sequence.

Look at program Design02.java. How is this an improvement from Design01.java?

The program places each statement on one line and uses indentation.

Look at program Design03.java. How is this an improvement from Design02.java?

The program uses self-commenting identifiers

What is the purpose of a return method?

The purpose of a return method is to return a value to the calling method statement. This value has a data type. The data type of the returning value is declared in the method heading. If the method does not return a value, the return data type is not applicable and becomes void.

What is still wrong with program Design02.java?

The variable names make no sense (not self-commenting) and there are no comments.

What are local variables?

Variables that are declared inside a method or block.

Can the actual parameter identifiers be the different from as the formal parameter identifiers?

Yes

Can the actual parameter identifiers be the same as the formal parameter identifiers?

Yes

Does the order of the parameters of a method matter?

Yes Ex: { int num1 = 100; int num2 = 50; showDifference(*num1,num2*); showDifference(*num2,num1*); } public static void showDifference(*int a, int b*) { int difference = *a - b*; } Output: 50 & *-50*

Can a method have multiple parameters?

Yes Ex: g.drawLine(*x1,y1,x2,y2*); { four parameters }

Class variables are also called _______.

attributes or static variables

What is another name for an actual parameter? What is another name for a formal parameter?

calling parameter ----------------------------------- receiving parameter

Similar methods accessing the same data should be placed in a __________________.

class

Control structures and block structure need to use a __________________ indentation style.

consistent

What method is contained in the driving class?

main method

Specific tasks should be placed in modules called _______________.

methods

Do the first 4 Payroll Case Study programs use Object Oriented Programming?

no

Java's graphic's class contains ________________________ methods.

object

When a method is used in different situations with different types of parameters or a different number of parameters, then the method is . . .

overloaded.

The key difference between creating no-parameter methods and parameter methods is the ___________________________.

parameter declaration

All method declarations have an identifier followed by _____________________.

parentheses

Utility classes contain methods that . . .

perform common and repetitive tasks, to avoid writing the methods again and again in new programs.

Look at program Java0730.java.Write the code for a 5th method called rem which will compute and display the integer remainder of nbr1 divided by nbr2.

public static void rem(int n1, int n2) { int result = n1%n2; System.out.println(n1+"%"+n2+"="+result); }

Actual parameters and formal parameters need to match in what three ways?

quantity, type and sequence.

Java's Math class contains ________________ methods.

return class

Programs should use __________________ identifiers.

self-commenting

The most common parameter mistakes are . . .

specifying data types in an actual parameter *Wrong*: qwerty(int num1, int num2); *Correct* : int num1 = 100; int num2 = 200; qwerty(num1,num2); ----------------------------------------------- not giving a data type to each parameter in a formal parameter *Wrong* : public static void qwerty(int a, b); *Correct* : public static void qwerty(int a, int b)

The batons are not passed based on the names of the runners. They are passed based on _____________________________.

the lanes they run in

Wha type of method is the *main* method?

void method

What are the main program design rules?

• Programs should use self-commenting identifiers. • Control structures and block structures need to use a consistent indentation style. • Specific tasks should be placed in modules called methods. • Similar methods accessing the same data should be placed in a class. • The main method should be used for program sequence, not large numbers of program statements.

Look at program Parameter05.java. What is wrong with Line 1? Code: public class Parameter05 { public static void main(String[ ] args) { showDifference(int num1, int num2); } public static void showDifference(int a, b) // Line 2 { System.out.println(); int difference = a - b;System.out.println("The difference is " + difference); System.out.println(); } }

In line 1 two *actual* parameters, num1 and num2, have data types inside the parentheses of the method call. This will not work. Code: public class Parameter05 { public static void main(String[ ] args) { *showDifference(int num1, int num2);* } public static void showDifference(int a, b) // Line 2 { System.out.println(); int difference = a - b; System.out.println("The difference is " + difference); System.out.println(); } }

Look at program Parameter05.java. What is wrong with Line 2? Code: public class Parameter05 { public static void main(String[ ] args) { showDifference(int num1, int num2); } public static void showDifference(int a, b) { System.out.println();int difference = a - b; System.out.println("The difference is " + difference); System.out.println(); } }

In line 2 there are two *formal* parameters, which appear to be declared as int. This also does not compile. Each formal parameter requires its own data type. Code: public class Parameter05 { public static void main(String[ ] args) { showDifference(int num1, int num2); } *public static void showDifference(int a, b)* { System.out.println(); int difference = a - b; System.out.println("The difference is " + difference); System.out.println(); } }

Where are class variables declared?

Inside a class, but outside of any method

Where can local variables be accessed?

Inside the method or block that they are defined in

If a variable is used by two or more methods of a class, how should it be declared?

It should be declared as a class variable.

If a variable is only used by one method, how and where should it be declared?

It should be declared inside that method as a local variable.

Look at program Design07.java. How is this an improvement from Design06.java?

It uses better design. All payroll methods and data are now in separate payroll classes.

Look at program Design04.java. How is this an improvement from Design03.java?

It uses extensive comments and organizes the program in segments.

The corresponding actual parameters in the method call must be the same ________ as the formal parameters in the heading.

data type


Conjuntos de estudio relacionados

Chapter 16, Chapter 16, Marketing Chapter 17, HRIM 442 Ch 17 Exam 3, Marketing Ch 17, Marketing Ch 17-19, Marketing Chapter 17 & 18, Marketing Chapter 17, mkt ch 16, Marketing 4, MKT 301 - Ch. 16, Marketing Chapter 17, Marketing Chapters 16-18, mktg...

View Set

Chapter 9 The Pediatric Examination

View Set

Chapter 2.2 - Agency Relationship

View Set

Biology Regents Review 10th grade

View Set