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 ID: 390039
Download Presentation The PPT/PDF document "A Revamping" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
A Revamping
Of our skills so farSlide2
Our Tools so Far
Variable - a placeholder for a valueMethod - a set of code which accomplishes a task
Class - a mold for an object
Object Instance - a variable which holds an object value
I
f statement - alters flow of control based on
boolean
condition(s)Slide3
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
v
alue
t
ype
name
v
alueSlide4
LOCAL VARIABLES
declared and defined w/in a methodcan’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
u
se the
private
keyword
should start with underscore
public
class Tire { private int _radius; public Tire(){ _radius = 40; } public int getRad(){ return _radius; }}Slide5
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
ParametersSlide6
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!Slide7
Main Method
This is where every java program startsOne main method per program
Looks like this
:
public static void
main (String []
args){ //your java program starts here }Slide8
Classes
The mold for our object instancesMade up of two things:
Instance Variables
– model properties of the class
Methods
– model abilities of the classSlide9
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
;
}}Slide10
Object Instances
Create one:
Ball
myBall
=
new
Ball(60, Color.BLUE, 11);Call methods on it:myBall.setSize(30);Slide11
Now Something Random
Many times you want to be able to create some randomnessJava 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 4Slide12
Call to Action
Coding is inherently time-consumingYou’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 labDream big and start smallDon’t be intimidated, you CAN do it! Stick with it!