A Revamping
description
Transcript of A Revamping
A RevampingOf 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
int size = 5;
Ball myBall = new Ball(50, Color.RED, 6);
- Two kinds: Instance Variables and Local Variables
type name value
type namevalue
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;}
}
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
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!
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 Examplepublic class Ball {
//instance variables go hereprivate 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 methodpublic void setSize(int newSize){_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();int randInt = rand.nextInt(5);
A Java library object
A method of the Random classrandInt 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!