Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction."— Presentation transcript:

1

2 College Board A.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.

3

4 Monster m = new Monster();

5 Monster m m is a reference variable that refers to a Monster object. 0x234

6 //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()); }

7

8 A method is a storage location for related program statements. When called, a method usually performs a specific task. System.out.println( )

9

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

11 All members with public access can be accessed or modified inside and outside of the class where they are defined.

12 public class Methods1 { public void method() { System.out.println("method called"); } public static void main(String args[]) { Methods1 test = new Methods1(); test.method(); } OUTPUT method called method called method called

13 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(); } OUTPUT method1 called method2 called method1 called method2 called Type this program into Dr. Java and run it. Make sure you understand the output it produces.

14 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(); } OUTPUT method1 called method2 called method1 called method2 called method1 called method2 called method1 called Type this program into Dr. Java and run it. Make sure you understand the output it produces.

15 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()

16

17 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)

18 Scanner keyboard = new Scanner(System.in); reference variable object instantiation / constructor call

19 Monster s = new Monster(); Monster s m is a reference variable that refers to a Monster object. 0x234

20 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 2 nd 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.

21 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?

22

23 A parameter/argument is a channel used to send information 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 and used in the method.

24 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.

25 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.

26

27 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 must have 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.


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

Similar presentations


Ads by Google