+ All Categories
Home > Documents > GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design -...

GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design -...

Date post: 02-Jan-2016
Category:
Upload: scarlett-sharp
View: 222 times
Download: 0 times
Share this document with a friend
Popular Tags:
35
GCOC – A.P. Computer Science A
Transcript
Page 1: GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,

GCOC – A.P. Computer

Science A

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

GCOC – A.P. Computer

Science A

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.

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

GCOC – A.P. Computer

Science A

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

GCOC – A.P. Computer

Science A

String x = new String(“hello”);

Monster m = new Monster();

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

GCOC – A.P. Computer

Science A

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

GCOC – A.P. Computer

Science A

{ int fun = 99;}

Any variable defined inside of braces,only exists within those braces.

That variable has a scope limited to those braces.

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

GCOC – A.P. Computer

Science A

When you need many methodsto have access to the samevariable, you make that variablean instance variable.

The scope of an instance variableis the entire class where that variable is defined.

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

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 completethese variables are de-allocated from memory.

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

GCOC – A.P. Computer

Science A

When you need only one methodto have access to a variable, you should make that variablea local variable.

The scope of a local variable islimited to the method where itis defined.

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

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); }

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

GCOC – A.P. Computer

Science A

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

GCOC – A.P. Computer

Science A

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 calleda default constructor.

Java provides a class with adefault constructor automatically if one is not written by the programmer.

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

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 constructortakes no arguments(parameters)!

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

GCOC – A.P. Computer

Science A

A parameter is a channel used to send informationto a method.

setColor is a method of the Graphics class.

void setColor( color theColor)

window.setColor( Color.red );

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

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

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

GCOC – A.P. Computer

Science A

have same name as class

have no return type

initialize instance variables

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

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 the3 side lengths of the triangle. Run it!

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

GCOC – A.P. Computer

Science A

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

GCOC – A.P. Computer

Science A

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a integer :: ");int num = keyboard.nextInt();

return method

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

GCOC – A.P. Computer

Science A

Return methods perform some actionand 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.

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

GCOC – A.P. Computer

Science A

Mathfrequently used methods

Name Usefloor() 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)

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

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.

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

GCOC – A.P. Computer

Science A

Math.floor(3.254) = 3.0Math.ceil(2.45) = 3.0Math.pow(2,7) = 128.0Math.abs(-9) = 9Math.sqrt(256) = 16.0Math.round(3.6) = 4Math.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, becausethey are static.

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

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 intoDr. Java and runit. Change the numbers and seehow it changesyour output.

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

GCOC – A.P. Computer

Science A

Some of the programming that is done in the real world is with games. Games must havethe ability to vary or be random; thus,you must have the ability to generaterandom numbers.

Math.random(); // returns a random number//between 0 up to, but not//including 1.

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

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

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

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 changethis program?

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

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;

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

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) ); }}

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

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); }}

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

GCOC – A.P. Computer

Science A

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

GCOC – A.P. Computer

Science A

public Triangle(){ sidea=0; sideb=0; sidec=0;}

Constructor methods are methods that set the properties of the an object to aninitial state.

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

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.

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

GCOC – A.P. Computer

Science A

public void getNumOne(){

return one;}

Accessor methods are methods that retrieve the properties of the an object.

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

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 youunderstand 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); }}


Recommended