Presentation is loading. Please wait.

Presentation is loading. Please wait.

GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,

Similar presentations


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

1 GCOC – A.P. Computer Science A

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 GCOC – A.P. Computer Science A

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

5 GCOC – A.P. Computer Science A

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 GCOC – A.P. Computer Science A 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 GCOC – A.P. Computer Science A 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 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 GCOC – A.P. Computer Science A 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 GCOC – A.P. Computer Science A 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 GCOC – A.P. Computer Science A

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 GCOC – A.P. Computer Science A //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 GCOC – A.P. Computer Science A 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 GCOC – A.P. Computer Science A 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 GCOC – A.P. Computer Science A have same name as class have no return type initialize instance variables

17 GCOC – A.P. Computer Science A //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 GCOC – A.P. Computer Science A

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

20 GCOC – A.P. Computer Science A 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 GCOC – A.P. Computer Science A Math frequently used methods NameUse floor()rounds down ceil()rounds up pow(x,y)returns x to the power of y abs()returns the absolute value sqrt()returns the square root round()rounds the nearest whole number max(x,y)returns bigger of x and y min(x,y)returns smaller of x and y random()returns a double in the range [0, 1.0)

22 GCOC – A.P. Computer Science A All methods in the Math class are static. Static methods do not require the creation of an object to invoke them (use them). Static methods are invoked through the class name. When we have methods that will give the same result regardless of the method, we use static methods. We would want sqrt() method to compute the square root of the number the same every time, regardless of the individual object that may be created.

23 GCOC – A.P. Computer Science A Math.floor(3.254) = 3.0 Math.ceil(2.45)= 3.0 Math.pow(2,7) = 128.0 Math.abs(-9)= 9 Math.sqrt(256)= 16.0 Math.round(3.6)= 4 Math.max(5,7)= 7 Most Math methods return a decimal, but some do return integer values. We use the class name, Math, to call these methods, because they are static.

24 GCOC – A.P. Computer Science A //math return methods import static java.lang.Math.*; public class MathMethods { public static void main ( String[] args ) { System.out.println(Math.floor(3.254));//= 3.0 System.out.println(Math.ceil(2.45));//= 3.0 System.out.println(Math.pow(2,7)); //= 128.0 System.out.println(Math.abs(-9));//= 9 System.out.println(Math.sqrt(256));//= 16.0 System.out.println(Math.sqrt(144));//= 12.0 System.out.println(Math.round(3.6));//= 4 System.out.println(Math.max(5,7));//= 7 System.System.out.println(Math.max(5,-7));//= 5 System.out.println(Math.min(5,7));//= 5 System.out.println(Math.min(5,-7));//= -7 } Type this into Dr. Java and run it. Change the numbers and see how it changes your output.

25 GCOC – A.P. Computer Science A Some of the programming that is done in the real world is with games. Games must have the ability to vary or be random; thus, you must have the ability to generate random numbers. Math.random(); // returns a random number //between 0 up to, but not //including 1.

26 GCOC – A.P. Computer Science A public class RandomDemo { public static void main ( String[] args ) { double dblans; int intans; dblans = Math.random() * 10; intans = (int)Math.random() * 10; //this line needs help System.out.println("\nMath.random()"); System.out.println( dblans ); System.out.println( intans ); //why does it always output 0? } Math.random() * 10; returns a double between 0 and 9.999999 (int) Math.random() * 10; When we typecast a double as an int, we get a number between 0 and 9 inclusively

27 GCOC – A.P. Computer Science A public class RandomDemo { public static void main ( String[] args ) { double dblans; int intans; dblans = Math.random() * 10; intans = (int)(Math.random() * 10); System.out.println("\nMath.random()"); System.out.println( dblans ); System.out.println( intans ); } How does the addition of parenthesis change this program?

28 GCOC – A.P. Computer Science A Random Numbers How can you use Math.random() to simulate rolling a die? How can we get it to return the numbers 1 – 6? (int)(Math.random()*6) + 1;

29 GCOC – A.P. Computer Science A class Return1 { public int twice( int x ) //this is a return method { return 2*x; } //code in the main Return1 demo = new Return1(); System.out.println("First value returned :: " + demo.twice(25) ); }

30 GCOC – A.P. Computer Science A //return method example import static java.lang.System.*; public class Return2 { public int willReturnHere( int x ) { return x*x*x; } 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); }

31 GCOC – A.P. Computer Science A

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

33 GCOC – A.P. Computer Science A public void setNums( int n1, int n2 ) { one=n1; two=n2; } Modifier methods are methods that change the properties of the an object.

34 GCOC – A.P. Computer Science A public void getNumOne() { return one; } Accessor methods are methods that retrieve the properties of the an object.

35 GCOC – A.P. Computer Science A 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. 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 "GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,"

Similar presentations


Ads by Google