Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Revamping Of our skills so far. Our Tools so Far Variable - a placeholder for a value Method - a set of code which accomplishes a task Class - a mold.

Similar presentations


Presentation on theme: "A Revamping Of our skills so far. Our Tools so Far Variable - a placeholder for a value Method - a set of code which accomplishes a task Class - a mold."— Presentation transcript:

1 A Revamping Of our skills so far

2 Our Tools so Far Variable - a placeholder for a value Method - a set of code which accomplishes a task Class - a mold for an object Object Instance - a variable which holds an object value If statement - alters flow of control based on boolean condition(s)

3 Variables -Have a type, name, and value int size = 5; Ball myBall = new Ball(50, Color.RED, 6); - Two kinds: Instance Variables and Local Variables typename value typename value

4 LOCAL VARIABLES -declared and defined w/in a method -can’t be accessed outside this method public class Tire { public Tire(){ int radius = 40; } public int getRad(){ return radius; } } X ‘radius’ is local to the constructor method and therefore can’t be accessed in this method INSTANCE VARIABLES -declared outside any methods (still w/in class) -can be accessed anywhere in the class -use the private keyword -should start with underscore public class Tire { private int _radius; public Tire(){ _radius = 40; } public int getRad(){ return _radius; } }

5 Methods -Block of code which can be called/executed public double average (double num1, double num2){ double sum = num1 + num2; double average = sum / 2.0; return average; } -always public -can have as many parameters as you want (including none) -use return type of void if method won’t return anything Modifier Return Type Method Name Parameters

6 void return type - Sometimes methods don’t need to return (output) anything - But every method needs to declare a return type public void greeting (boolean isMale){ if(isMale){ System.out.println(“Hello Sir”); } else{ System.out.println(“Hello Maam”); } } - note the lack of a return statement, void makes this fine!

7 Main Method This is where every java program starts One main method per program Looks like this: public static void main (String [] args){ //your java program starts here }

8 Classes The mold for our object instances Made up of two things: Instance Variables – model properties of the class Methods – model abilities of the class

9 Our Classic Ball Example public class Ball { //instance variables go here private int _size; private java.awt.Color _color; private int _bounciness; public Ball(int size, java.awt.Color color, int bounciness){ //this is the constructor method _size = size; _color = color; _bounciness = bounciness; } //here’s a method public void setSize(int newSize){ _size = newSize; } }

10 Object Instances Create one: Ball myBall = new Ball(60, Color.BLUE, 11); Call methods on it: myBall.setSize(30);

11 Now Something Random Many times you want to be able to create some randomness Java has a few solutions, here’s one: Random rand = new Random(); int randInt = rand.nextInt(5); A Java library object A method of the Random class randInt will be between 0 and 4

12 Call to Action Coding is inherently time-consuming You’ve been doing great work in lab But becoming a prolific coder will take more than 1.5hr/week You know enough to start embarking on your own projects outside of lab Dream big and start small Don’t be intimidated, you CAN do it! Stick with it!


Download ppt "A Revamping Of our skills so far. Our Tools so Far Variable - a placeholder for a value Method - a set of code which accomplishes a task Class - a mold."

Similar presentations


Ads by Google