120 likes | 352 Views
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 for an object Object Instance - a variable which holds an object value
E N D
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 for an object • Object Instance - a variable which holds an object value • If statement - alters flow of control based on boolean condition(s)
Variables • Have a type, name, and value intsize = 5; Ball myBall = new Ball(50, Color.RED, 6); - Two kinds: Instance Variables and Local Variables value type name value type name
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 publicclass Tire { private int_radius; public Tire(){ _radius = 40; } public intgetRad(){ return _radius; } } LOCAL VARIABLES • declared and defined w/in a method • can’t be accessed outside this method publicclass Tire { public Tire(){ intradius = 40; } public intgetRad(){ return radius; } } X ‘radius’ is local to the constructor method and therefore can’t be accessed in this method
Methods Parameters -Block of code which can be called/executed public double average(double num1, double num2){ double sum = num1 + num2; double average = sum / 2.0; returnaverage; } -always public -can have as many parameters as you want (including none) -use return type of void if method won’t return anything Return Type Method Name Modifier
void return type - Sometimes methods don’t need to return (output) anything - But every method needs to declare a return type public void greeting(booleanisMale){ if(isMale){ System.out.println(“Hello Sir”); } else{ System.out.println(“Hello Maam”); } } - note the lack of a return statement, void makes this fine!
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 }
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
Our Classic Ball Example publicclass 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 voidsetSize(intnewSize){ _size = newSize; } }
Object Instances • Create one: Ball myBall = new Ball(60, Color.BLUE, 11); • Call methods on it: myBall.setSize(30);
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(); intrandInt = rand.nextInt(5); A Java library object A method of the Random class randInt will be between 0 and 4
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!