Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1

2 College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction and encapsulation. Class Design - Design and implement a class; Design an interface. Programming Constructs - Class declarations; Parameter declarations. Program Analysis - Understand and modify existing code. Standard Data Structures - Simple data types (int, boolean, double) ; Classes.

3

4 String x = new String(“hello”); Monster m = new Monster();

5

6 { int fun = 99; } Any variable defined inside of braces, only exists within those braces. That variable has a scope limited to those braces.

7 When you need many methods to have access to the same variable, you make that variable an instance variable. The scope of an instance variable is the entire class where that variable is defined.

8 public class InstanceVars { private int one = 9, two = 7; public int add() { int sum = one + two; return sum; } public int subtract() { int difference = one – two; return difference; } public class InstanceVarsRunner { public static void main(String args[]) { InstanceVars test = new InstanceVars(); int total = test.add(); int diff = test.subtract(); System.out.println (total); System.out.println (diff); } OUTPUT 16 2 one and two are instance variables. We need to use them in both the add and the subtract methods. sum and difference are local variables. They only exist inside the methods that declare them. Once the method is complete these variables are de-allocated from memory.

9 When you need only one method to have access to a variable, you should make that variable a local variable. The scope of a local variable is limited to the method where it is defined.

10 Refer to the InstanceVars class on the previous slide. Because the scope of sum and difference is their methods, we could reuse these names in our main. In change 1, we try to print sum and difference. You will get an error, because these variables do not exist in the main. In change 2, notice that we have to declare them as int in again in the main. Try this change in Dr. Java. Change 1: public static void main(String args[]) { InstanceVars test = new InstanceVars(); int total = test.add(); int diff = test.subtract(); System.out.println (sum); System.out.println (difference); } Change 2: public static void main(String args[]) { InstanceVars test = new InstanceVars(); int sum = test.add(); int difference = test.subtract(); System.out.println (sum); System.out.println (difference); }

11

12 class Triangle { private int sidea, sideb, sidec; public Triangle() { sidea=0; sideb=0; sidec=0; } Triangle A = new Triangle(); A constructor that does not have parameters, is called a default constructor. Java provides a class with a default constructor automatically if one is not written by the programmer.

13 //constructor method example class Triangle { private int sidea, sideb, sidec; public Triangle() { sidea=0; sideb=0; sidec=0; } public String toString() { return (sidea + " " + sideb + " " + sidec); } public class ConstructorOne { public static void main ( String[] args ) { Triangle test = new Triangle(); System.out.println (test); } A default constructor takes no arguments (parameters)!

14 A parameter is a channel used to send information to a method. setColor is a method of the Graphics class. void setColor( Color theColor) window.setColor( Color.red );

15 class Triangle { private int sidea, sideb, sidec; public Triangle(int a, int b, int c) { sidea=a; sideb=b; sidec=c; } Triangle A = new Triangle(3,4,5);

16 have same name as class have no return type initialize instance variables

17 //constructor method example class Triangle { private int sidea, sideb, sidec; public Triangle(int a, int b, int c) { sidea=a; sideb=b; sidec=c; } public String toString() { return(sidea + " " + sideb + " " + sidec); } public class ConstructorTwo { public static void main ( String[] args ) { Triangle test = new Triangle(5,6,7); System.out.println (test); } This constructor takes in 3 parameters. The numbers represent the 3 side lengths of the triangle. Run it!

18

19 Scanner keyboard = new Scanner(System.in); System.out.print("Enter a integer :: "); int num = keyboard.nextInt(); return method

20 Return methods perform some action and return a result to the calling location. double root = Math.sqrt(144); root would be assigned the value 12.0. double roundup = Math.ceil(3.121); roundup would be assigned the value 4.0.

21 public class Return1 { public int twice( int x ) //this is a return method { return 2*x; } public class Return1Runner { public static void main (String [] args) { Return1 demo = new Return1(); System.out.println("First value returned :: " + demo.twice(25) ); }

22 //return method example import static java.lang.System.*; public class Return2 { public int willReturnHere( int x ) { return x*x*x; } public class Return2Runner { public static void main ( String[] args ) { Return2 demo = new Return2(); demo.willReturnHere(3); //this is how you use a return method correctly //int answer = demo.willReturnHere(3); //System.out.println(answer); }

23

24 public Triangle() { sidea=0; sideb=0; sidec=0; } Constructor methods are methods that set the properties of the an object to an initial state.

25 public void setNums( int n1, int n2 ) { one=n1; two=n2; } Modifier methods are methods that change the properties of the an object.

26 public void getNumOne() { return one; } Accessor methods are methods that retrieve the properties of the an object.

27 public class Triangle { private int sidea, sideb, sidec; public Triangle(int a, int b, int c) { sidea=a; sideb=b; sidec=c; } public void setSides(int a, int b, int c) { sidea=a; sideb=b; sidec=c; } public String toString() { return "sides " + sidea + " " + sideb + " " + sidec; } Create 2 classes, Triangle and OOP. Run this and make sure you understand it. public class OOP { public static void main ( String[] args ) { Triangle test = new Triangle(5,6,7); System.out.println(test); test.setSides(4,4,4); System.out.println(test); test.setSides(1,56,22); System.out.println(test); }


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

Similar presentations


Ads by Google