Top Banner
27

College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

Dec 28, 2015

Download

Documents

Todd Stanley
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Page 2: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

College BoardA.P. Computer Science A Topics Program Design - Read and understand a

problem's description, purpose, and goals; Apply data abstraction and encapsulation; Identify reusable components from existing code using classes and class libraries.

Class Design - Design and implement a class.

Program Implementation - Method declarations; Parameter declarations; Object-oriented development.

Page 3: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Page 4: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

Monster m = new Monster();

Page 5: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

Monster m = new Monster();

Monster

m

m is a reference variable that refersto a Monster object.

0x234

0x234

Page 6: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

//basic class example

public class Monster{

public Monster(){//this is a default constructor. Constructors are used to set values to //instance variables.Monster has no instant variables.}public String toString( ){

return "I am a Monster!\n";}

}

public class Class1{

public static void main( String[ ] args ){

Monster m = new Monster();System.out.println (m.toString());

}}

Page 7: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Page 8: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

A method is a storage locationfor related program statements.When called, a method usually performs a specific task.

System.out.println( )

Page 9: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Page 10: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

public String toString( ){ return “I am a Monster!";}

Page 11: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

All members with publicaccess can be accessed ormodified inside and outside of the class where they aredefined.

Page 12: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

public class Methods1 { public void method() { System.out.println("method called"); }

public static void main(String args[]) { Methods1 test = new Methods1(); test.method(); test.method(); test.method(); }}

OUTPUTmethod calledmethod calledmethod called

Page 13: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

public class Methods2{ public void method1() { System.out.println("method1 called"); }

public void method2() { System.out.println("method2 called"); }

public static void main(String args[]) { Methods2 test = new Methods2(); test.method1(); test.method2(); test.method1(); test.method2(); test.method2(); }}

OUTPUTmethod1 calledmethod2 calledmethod1 calledmethod2 calledmethod2 called

Type this program into Dr. Javaand run it. Make sure you understandthe output it produces.

Page 14: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

public class Methods3{ public void method1() { System.out.println("method1 called"); }

public void method2() { System.out.println("method2 called"); this.method1(); }

public static void main(String args[]) { Methods3 test = new Methods3(); test.method1(); test.method2(); test.method1(); test.method2(); test.method2(); }}

OUTPUTmethod1 calledmethod2 calledmethod1 calledmethod1 calledmethod2 calledmethod1 calledmethod2 calledmethod1 called

Type this program into Dr. Javaand run it. Make sure you understandthe output it produces.

Page 15: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

Keyword this

This method appears in class Method3 in previous slide:

public void method2()

{

System.out.println("method2 called");

this.method1();

}

this is a keyword, like int, void, public, return, double, …

We use the keyword this to refer to the object that called the function. Anytime a method is called within the class we would not want to create a new object, instead we say this.

It keeps us consistent:

object.method()

this.method1()

Page 16: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Page 17: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

Constructor methods always have the same name as the class.

GraphOne test = new GraphOne();

Monster rob = new Monster();

UrRobot carol = new UrRobot(2, 3, East, 7)

Page 18: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

Scanner keyboard = new

Scanner(System.in);

reference variable

object instantiation / constructor call

Page 19: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

Monster s = new Monster();

Monster

s

m is a reference variable that refersto a Monster object.

0x234

0x234

Page 20: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

public class Circle

{

private double radius;

public Circle()

{

radius = 1;

}

public Circle (double r)

{

radius = r;

}

public double computeArea ()

{

double area;

area = Math.PI *Math.sqrt(radius, 2);

return area;

}

}

This class has 2 constructors.

Default Constructor: The first constructor has no parameters (values in parenthesis), and is called the default constructor.

The 2nd constructor takes in a double from the client program (program using this class).

Why have constructors? We use constructors to give values to the instance variables. In this case, the private variable radius, is an instance variable. Notice that both constructors assign radius a value.

Page 21: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

public class Circle

{

private double radius;

public Circle()

{

radius = 1;

}

public Circle (double r)

{

radius = r;

}

public double computeArea ()

{

double area;

area = Math.PI *Math.sqrt(radius, 2);

return area;

}

}

What is the difference between the constructor methods and the computeArea method?

Constructors:

•names are always the same as the class

•you can have more than 1 constructor as long as their signatures… parameter lists are different.

•There is no type listed between public and the class name… Constructors have NO return type.

•Note computeArea is proceed by the word double… this is its return type… it has to supply the client program (calling program) with a double when it is finished. What double is it returning?

Page 22: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Page 23: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

A parameter/argument is a channel used to sendinformation back and forth. setRadius(double r) is a

method of the Circle class.

public void setRadius (double r)

myCircle.setRadius( 5);

The number 5 is assigned to the variable parameter r andused in the method.

Page 24: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

2 types of Parameters

Formal Parameters: When you look at the class, the methods provide formal parameters, that act as identifiers for the information that will be passed.

public Circle (double r)

r is a Formal Parameter. It will store the value that is passed.

Page 25: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

Types of Parameters Cont

Actual Parameters: These are the values that are sent from the client programs to the method. Their value is stored in the formal parameters.

Circle myCircle = new Circle (5);

5 is the actual parameter. It will be assigned to r.

Page 26: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Page 27: College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.

A return is a value that is sent back to the calling method. computeArea() is a method of the Circle

class. public double computeArea () double myarea =myCircle.computeArea();

double tell what the type that is being returned. You musthave a variable in the client program ready to store this value. Or you could also output this value. In the computeArea method you will see return area; This is what is being sent back to the calling program.