Unit 5 - Writing Classes

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

The signature of a method is:

1. Its name 2. The number and types of its parameters, in order The signatures of the methods in a class must be unique.

What is not part of the signature of a method?

1. The names of the parameters 2. The return type 3. The visibility modifier

The Ten Commandments of Computer Ethics from CPSR

1. Thou shalt not use a computer to harm other people. 2. Thou shalt not interfere with other people's computer work. 3. Thou shalt not snoop around in other people's computer files. 4. Thou shalt not use a computer to steal. 5. Thou shalt not use a computer to bear false witness. 6. Thou shalt not copy or use proprietary software for which you have not paid. 7. Thou shalt not use other people's computer resources without authorization or proper compensation. 8. Thou shalt not appropriate other people's intellectual output. 9. Thou shalt think about the social consequences of the program you are writing or the system you are designing. 10. Thou shalt always use a computer in ways that ensure consideration and respect for your fellow humans.

Surface area of a cylinder (including the base)

A = π*r*( r + √(h2 + r2) )

Signature example

A class has the following two methods: float chargePenalty( int amount ) { ... } int chargePenalty( int penalty ) { ... } Do these methods have unique signatures? Answer: No. The names of the formal parameters are not part of the signature, nor is the return type. The signatures of the two methods are: chargePenalty( int ) chargePenalty( int ) Both methods have the same signature.

access modifier

A keyword in the declaration of a method that defines the circumstances under which a method or class can be accessed; the access level of a method. (ex. public, private)

mutator method (setter)

A method that changes the state of an object. Often the name of these methods starts with "set". Often this method checks the suggested change and does not make it if there is a problem. For example, Cones should not have negative values for height and radius.

access method (getter)

A method that returns a value from an instance variable. Their names often begins with "get".

identifiers

A name chosen by a programmer

public

Are constructors always public or private?

shadowing

Be careful about this! There is an instance variable and a local variable with the same name. But when called without using "this.", the local variable takes precedence (or shadows) the instance variable. Keep in mind that local variables must be declared.

NO, free error message for you. (ex. can't have the same name for a local variable and formal parameter)

Can you use the same name (identifier) in the same scope?

What conditions must be met for the program to be compiled and ran?

Cone.java contains the definition of the Cone class and TestCone.java contains the above program and both are in the same disk directory,

TRUE OR FALSE: A methods statement's can see the parameters and local variables of other methods.

FALSE

TRUE OR FALSE: It's often good to utilize shadowing, or using the same name for an instance variable and for a local variable.

FALSE

What are the differences between a field and a parameter?

Field syntax differs because they can be declared with the 'private' keyword. A field's scope is throughout the class, while a parameter's scope is limited to the method. A field is a variable that exists inside of an object, while a parameter is a variable inside a method whose value is passed in from outside.

Constructor Definition Syntax

For now, make constructors public.

a HelloObject created using default constructor

HelloObject anObject = new HelloObject();

Syntax Rule For Constructors

If you define one or more constructors for a class, then those are the only constructors that the class has. The default constructor is supplied automatically only if you define no constructors.

When the Java interpreter needs the definition HelloObject, where will it be found?

In the file of bytecodes, HelloObject.class.

Java Source File

Produces java class files using the Java compiler (files ending in .java). The filename must have the same name as the public class name in that file.

Privacy of Data

Protected through encryption, keeping data stored in a secret, encoded form.

TRUE OR FALSE: A method's statements can see the instance variables of their object.

TRUE

TRUE OR FALSE: Dividing the code into three sections, (instance variables, constructors, methods) is a matter of style. It is not a syntax rule of the language.

TRUE

TRUE OR FALSE: The data type of each parameter should match the data type of an instance variable.

TRUE

TRUE OR FALSE: The formal parameters of a method can be seen only by the statements of their own method.

TRUE

TRUE OR FALSE: The return statement can be omitted; the method will automatically return to the caller after it executes.

TRUE

one instance

The main() method is a static method, which means that there will be only ___ ________ of it and that it exists (as part of a class) before any objects have been created.

Method Visibility: One-way Glass Analogy

The method can see out of its box (for example each method can see the instance variable balance) but other methods can't see from the outside into the box of one-way glass. balance = instance variable amount = formal parameter charge = local variable A parameter is a local copy of whatever value the caller passed into the method. Any changes made to it affect only this local copy.

run time

The stage where the compiled program is running

Volume of a cylinder formula

V = π*r2*h/3

This method returns an int that is the difference from subtracting the two parameters.

What is this method doing?

Cone

a class with objects that represent a right circular cone. This is a class example (we're making it up) so it doesn't exist in the Java library.

running program

a collection of objects that are each doing their own task and communicating with other objects.

default constructor

a constructor with no parameters - same name as class - automatically supplied by the Java compiler if a class definition does not include a constructor

overridden method

a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. ex. toString()

constructor

a method that initializes a newly instantiated object

Data encapsulation

a technique in which the implementation details of a class are kept hidden from a user. When designing a class, programmers make decisions about what data to make accessible and modifiable from an external class. Data can be either accessible or modifiable, or it can be both or neither.

local variable

a variable that is declared inside of the body of a method

reference variable

a variable that points to an object of a given class, letting you access the value of an object. Does not store its own values.

The get methods may be used to _____. (More on this later.)

access the values

Data can be either accessible or modifiable, or it can be...

both or neither.

Each class defined in the source files compiles into a file of...

bytecode. Bytecodes are executed

private instance variables

can only be accessed by methods of the same class - CANNOT be referenced directly by name in a derived class.

The set methods may be used to _____ in selected instance variables and

change the values

Instance variables (like height) are usually made private so that only the __________ of the object may access them.

constructors and methods

class

description of a possible object.

object-oriented programming

designing a program by discovering objects, their properties, and their relationships when an object is an instance of a class. 3 Stages: Create, Compile, Run.

"Identity"

each object has its own variables.

If an exact match cannot be made, then the actual parameters are converted to types that match the ____ if this can be done without potential loss of information.

formal parameters

An object's methods use that object's instance variables. Remember that an object has...

identity, state, and behavior.

method call

invokes the method that matches it with both its name and its parameter list.

"Behavior" (of an object)

its methods, which use the object's own variables.

public (keyword)

keyword that allows access from classes outside the declaring class

private (keyword)

keyword that restricts access to the declaring class

The runner class will only contain the ____ and that will be where objects are created.

main method

Class header

marks the beginning of a class definition

Static methods

methods which are associated with the class, not objects of the class. They must have the keyword static in their method heading before the method name. - cannot access or change the values of instance variables. - When a static method is called it will use the class name, not the dot operator, since they are associated with a class, not objects of a class.

header of a method.

modifiers returnType methodName ( parameterList ) { statementList }

To access the instance variables and object data, methods need to be...

non-static

The instance variables are private which means they are...

only accessible in the class

Overloaded println()

println( ) println( boolean x ) println( char x ) println( double x ) println( int x ) println( String x ) ... others

Variables for the Cone class

private double height; private double radius;

Constructors for the Cone class

public Cone( double radius, double height )

Methods for the Cone class

public double area() - calculates and returns the area of the cone public double volume() - calculates and returns the volume of the cone public void setHeight() - changes the height of a cone public void setRadius() - changes the radius of a cone public double getHeight() - returns the height of a cone public double getRadius() - returns the radius of a cone

main() method

public static void main(String[] args) The method that is first called when a Java application executes. Usually this method constructs objects of various classes and calls their methods. These objects do the real work of the program.

A constructor returns a reference to the object it constructs. Don't put a ______ ____ in front of className and don't use a ______ _________in the body of the constructor.

return type, return statement

scope of a local variable

starts from where it is declared and ends at the end of the block that it is in. Recall that a block is a group of statements inside of braces, {}. Used to hold a temporary value while something is being calculated. They have a value only during the brief amount of time that a method is active.

The keywords public and private affect...

the access of classes, data, constructors, and methods.

actual parameter / argument

the actual value that is passed into the method by a caller ex. 200 used when method processDeposit is called

driver

the class that contains main(). It is convenient to have a separate class that serves no other purpose than to contain the main() method.

Instantiation

the creation of a new instance of a class. If no constructor is defined by the programmer, Java uses a default.

formal parameter

the identifier used in a method to stand for the value that is passed into the method by a caller ex. amount

signature of a method

the methodName and parameterList

scope of a formal parameter

the section of source code that can see the parameter. The scope of a formal parameter is the body of its method. For example, the scope of amount is the body of its method.

returnType

the type of value that the method hands back to the caller of the method. The return statement is used to hand back a value to the caller.

"State" (of an object )

the values held in its variables.

instance variables

the variables that each object (each instance of a class) has as part of itself. They are attributes of the object. Usually each is marked private, which means they are accessible only to the object itself.

Software Piracy

unauthorized copying or distribution of software for either personal use or use by others. It's not just for copyrighted works; it includes plagiarism of all or part of code that belongs to anyone else

object

unique instance of a class.

Accessor and mutator methods can be used to allow client code to...

use and modify data that is otherwise private.

this (keyword)

use the reserved word this to show when an identifier refers to an object's instance variable.

return statement

used to hand back a value to the caller.

If you want a method that does something, but does not return a value to the caller, use a return type of ____ and do not use a return value with the return statement.

void

passing a value (into the method)

when the caller uses a parameter (like amount) to send a value to the method.

Overloading

when two or more methods of a class have the same name but have different parameter lists. When a method is called, the correct method is picked by matching the actual parameters in the call to the formal parameter lists of the methods.

Syntax of a class definition

†For now, replace modifiers with public in the class that contains main and don't include it in other classes in the same file.


Kaugnay na mga set ng pag-aaral

AP Lang Vocab & Rhetorical Terms

View Set

Pharmacology Ch. 10: General Anesthetics

View Set

Chapter 4 Budgeting and Planning (Financial Literacy) Part 1

View Set

The Crucible Act 2 & Act 3 Study Guide

View Set

Openstax Anatomy & Physiology Chapter 6

View Set