Methods Flashcards

methods, char variables, and boolean

You may prefer our related Brainscape-certified flashcards:
1
Q

Write the General Structure of a Method

A

public static returntype methodname (zero or more input variables){
* // create local variables
* // do math ao logic
* // return result based on returntype (if applicable)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Function of “methodname” in General Structure

public static returntype methodname (zero or more input variables){
* // create local variables
* // do math ao logic
* // return result based on returntype (if applicable)
}

A

give a name to method
* use this name in main program to ‘call’ the method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Function of “zero or more input variables” in General Structure

public static returntype methodname (zero or more input variables){
* // create local variables
* // do math ao logic
* // return result based on returntype (if applicable)
}

A

take data in through input variables

  • Note: variable names in main program do not have to match names in method
  • Note: if there are no input values, leave ( ) blank when calling method.
  • Note: the input variables do not have to match the returntype/output
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Function of “returntype” in General Structure

public static returntype methodname (zero or more input variables){
* // create local variables
* // do math ao logic
* // return result based on returntype (if applicable)
}

A

specifies type of data being output by the method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Examples of ReturnTypes…

A

String
int
double
boolean
void (a.k.a. nothing)
long
char

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain Boolean

A

a variable type which holds the values: true or false
- has no quotations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What word is “char” short for?

A

character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Special features of Char variables

A

can use math comparison symbols on them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Function of:

strWord.charAt(integer#)

A

take the single character with the index/subscript of the number in () from a given string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write the line of code which takes a single letter in a string USING CHAR VARIABLES.

A

chrLetter = strWord.charAt(integer#);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Type of quotations used by char variables

A

single quotations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Type of quotations used by String variables

A

double quotations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Function of return keyword

A

returns a value back to main program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which return type does not need a return keyword?

A

return type: void

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Do local variables in methods exist in the main method?

A

No

17
Q

Write code to call a method in a different file (eg. allisontools file)

A

allisontools.methodname(input variables);

18
Q

Benefits of Methods

A
  • main programs are shorter
  • more efficient – you do not need to rewrite the same lines of code (math/logic) over and over again
  • reduces the number of errors – you can write parts of the program at a time and test as you go
  • easier collaboration/teamwork – different team members can work on different methods
  • money: tool files can be so useful that you can sell them for a price
19
Q

Function of Methods

A

pieces of code which programmers can access over and over again when its functionality is needed

20
Q

When do methods run?

A

when placed in main method & given proper input

21
Q

Alternate Names for Methods

A

Methods
Functions
Procedures

22
Q

Long Variable Type

A

integers that can hold numbers larger than 2 billion

23
Q

integer variable data that can hold numbers larger than 2 billion

A

Function of Long Variables

24
Q

Why use int when you have long?

A

to save RAM

25
Q

Where must tools files be located

A

in same folder as the programs which use it

26
Q

Implement a method double sphereVolume(double r) that calculates and returns the volume of a sphere given its radius r. Use the formula: V = (4/3) × π × r³. Test this method in a program.

A
27
Q

Write a method boolean isLeapYear(int year) that returns true if the given year is a leap year, otherwise false. Use the formula: A year is a leap year if it is divisible by 4 but not divisible by 100, unless it is also divisible by 400. Test this method in a program.

A
28
Q

Create a method int charFrequency(String text, char c) that counts and returns the number of times a character c appears in the given string text. Write a program to test this method.

A
29
Q

Define a method int countVowels(String word) that counts and returns the number of vowels (a, e, i, o, u) in the given string. Write a program to test this method.

USING CHAR

A