1 / 12

A Revamping

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

pier
Download Presentation

A Revamping

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 intsize = 5; Ball myBall = new Ball(50, Color.RED, 6); - Two kinds: Instance Variables and Local Variables value type name value type name

  4. 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

  5. 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

  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(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!

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

  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(); intrandInt = 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!

More Related