0% found this document useful (0 votes)
129 views

Java Classes

Classes allow programmers to create templates called classes that define properties and behaviors of objects. A class acts like a blueprint that describes the attributes and methods of a type of object. The class ApplePie is used as an example, with properties like diameter and ingredients, and methods like cooking and eating the pie. The key aspects of a class include constructors, which initialize new objects; get and set methods to access or modify property values; and other methods that define actions objects can take. A driver class with a main method is used to test the ApplePie class by creating pie objects and calling their methods.

Uploaded by

Jonathan Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

Java Classes

Classes allow programmers to create templates called classes that define properties and behaviors of objects. A class acts like a blueprint that describes the attributes and methods of a type of object. The class ApplePie is used as an example, with properties like diameter and ingredients, and methods like cooking and eating the pie. The key aspects of a class include constructors, which initialize new objects; get and set methods to access or modify property values; and other methods that define actions objects can take. A driver class with a main method is used to test the ApplePie class by creating pie objects and calling their methods.

Uploaded by

Jonathan Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Jonathan Nguyen

C LA SS ES

Classes:W TF IS IT?!
Okay, so youre probably referring to

this cause you have no clue what a


class is
Lets start with an example
You want apple pie
How do you make it?
Recipe
What do you need?
Ingredients (i.e. apples, sugar, flour, etc.)

O kay,so w hat does that


anything to do w ith classes?
Classes = recipe
Objects = product (in this case,

apple pie)
You need the recipe in order to make
the apple pie, but the recipe doesnt
make it for you (you need to make it
yourself)
Classes makes objects
Apple pie recipe makes apple pie

H ow To KillA M ockingbird
M ake Apple Pie
So, what is needed in a class?
Classes consist of two major parts:
Properties / attributes
Methods

H ow To KillA M ockingbird
M ake Apple Pie
Lets use the apple pie again =D (cause I

really want some right now T^T)


Describe it to me
Filled with apples
Is made in a 12 pie pan (diameter = 12)
Is tan colored

What can you do with the apple pie?


Shove it in someones face
Eat it (like what I would do right now if I had
some)
Throw it away
Cut it up (or just leave it whole and give it all to
me! )

Apple Pie Recipes -> Classes


Those main components also make

up a class
The properties are the arguments that

you enter into the Objects arg field in


order to state what you want to use to
make that apple pie oh so yummy

Apple Pie Recipes -> Classes


Four major components of a class
Constructor (as the name implies, it

constructs or makes something. In this


case, our apple pie )
Get statements
Set statements
Methods (what youre going to do with
the apple pie)

Constructor = Bob the


Builder?!
In order to make our apple pie, well

need apples right?


What if I wanted to make it bigger so

more people can eat it?


What if I want to use pears instead of
apples?
What if I want to make a pie that is
exactly 14.3 inches in diameter and is
made of unicorns!?
This is where the constructor comes in

Constructing a Constructor
Defined by public className (args)
Can have no args (known as the default)
Can have as many arguments as you

want (but do you really wanna argue


that much?)
Helps to make that apple pie

If no arguments is stated, creates an

apple pie using the predefined


amounts listed in the recipe

Exam ple ofN o-Args Constructor


(Yay! N o Fights!)
public ApplePie ()
{ //assume all variables have types
(i.e. int)
diameter = 1;
sugar = 2;
isHot = true;
}
In this case, we want a simple pie, so
we dont get fancy and instead just
let the recipe stay

Exam ple ofArgs Construtors


(arghhhhhh m atey!)
public ApplePie(int x, int y, Boolean z)
{ //assume all variables are declared
diameter = x;
sugar = y;
isHot = z;
}
In this case, we had three args, but we can
have as little as 0 or 1
Remember, you can have as many of these
constructors (but dont overmake them!)

H O LD U P! W H AT TH E H ECK IS
TH IS(.)?!
Okay, before we continue, I feel this is the

perfect spot to explain this.whatever


Lets say your class looks like this
int diameter;
public ApplePie (int diameter)
{
//code
}
How can you tell which is the variable and
which is the argument that youre reading
in?

So TH ATS w hat TH IS(.) is


(youllsee in a second)
this.variable = ApplePie.variable
Its a way to distinguish the variable that

youre storing from the argument you want


the user to give
Its a tidy-upper and helps you recognize
which is a variable
I didnt have it in the first example because
I used x instead of diameter as an arg name
If youre anal about variable names but you want

to call them the same name, this is for you

AN YW AYS
Now that we have a constructor set

(and assuming you defined your


variable above), we need something
called gets/sets
Get = get the information that you
need
How big IS this pie?!
How many cups of sugar did you put in

this darn pie?!


IS THIS PIE HOT?!
You get the point

Continuing O n
Sets = setting a variable to equal

something
Sir, my apple pie has two cups of sugar,

but now I want to add another cup, so


make that 3 (though I dont know why
you want it that sweet unless you want
diabetes)
Sir, the fan you made is on, but can you
turn it off now because my cat doesnt
appreciate it?

Basically, its methods that allow you

Basic Rules for G etting


(som e :P)
Should have NO ARGS
Youre not trying to set anything
You just want to GET the data thats

already there

Return type should match variable

type
public int getDiameter()
Is int because I want the diameter, which

is an int

Just return the variable

Exam ples ofgets


public int getDiameter ()
{
return this.diameter;
}
public Boolean getHotness()
{
return this.isHot;
}

Ready,set, and thats it!


Should have an argument (this is what you

want to set the variable equal to)


public void setDiameter (int diameter)
Arg will be used to figure out what you want to

be so picky about

Return type is VOID (always always

ALWAYS)
This is because you only want to set the

variable equal to something, you dont want it


to output anything or give you back anything

Then , just set the variable equal to the arg

Exam ples ofsets


public void setDiameter(int diameter)
{
this.diameter = diameter;\
}
public void setHotness(Boolean x)
{
isHot = x;
//notice I didnt use this.var because
I //already knew which was which
}

Easy right?
BUT WAIT!
Now that we know exactly how to

make our apple pie, made a little


man to take care of making it, and
we know how to change stuff around
and figure out whats going on with
the pie, we need one more thing
That thing is WHAT TO DO WITH IT!
(aka methods)

M ethods ofEating (just bite!)


You have an apple pie, but youre not

going to just STARE at it, are you?


(OR ARE YOU?!)
Well, were going to need to do some
stuff that we can only do with apple
pies (like eat it!)
For this, we have methods

Pi/8 = 22.5 degrees


So, what do you want to do with the pie?
Well, we can figure out how much pie to

give everyone
public int howMuchPie(int pieces)
{
totalArea = //some equation to figure out
//area of pie
return totalArea / pieces;
}

M ore Exam ples! (But seriously,


just let m e eat it)
int pieces;
Boolean isEaten;
//constructors, gets, and sets
public Boolean isPieFinished ()
{
//check how many pieces have been used
//if 0 , return true for isEaten
//else, return false
}

Soooo,say that again?


Constructor : makes our apple pie.

We tell him what we want and he


makes it for us =] (such a nice guy)
Gets : tells us what kind of mystery
meat or junk is in our trunk stuff
Sets : tells us what we want to
change on our apple pie (though you
couldve just told him before he
made the darn pie!)
Methods : what you want to do with

But w ait! W TH IS A D RIVER


TH EN !?
Hold on, Im getting to that
Okay, so one important thing about

classes: THERE ARE NO MAIN


The purpose of this is so that once
you make an object, ANYONE can
chuck it into their program without
rewriting a whole bunch of stuff
Like they say, Why reinvent the
wheel?

But how do w Basically, if you want to use a class,

you import it, right?


Well, in our case, we dont need to
This is because our driver is usually in

the same folder as the class


REMEMBER: YOU DONT NEED TO
IMPORT IF YOU ALREADY HAVE THE
CLASS IN THE SAME FOLDER (which is
why you have to import stuff like
PrintWriter and Scanner)

But,how do Iuse m y class?


Well, in our case, our class makes an

object
Take Scanner for example
How do you use scanner?
Scanner input = new Scanner

(System.in);

In our case, we do the same, but

with our class


ApplePie pie1 = new ApplePie ();
And..? That means WHAT?!

W hat allthat crap m eant:


ApplePie pie1 = new ApplePie ();
The FIRST ApplePie states that youre

making a variable with type ApplePie


(because its not a boolean, its not
an int, its a freaking apple pie.)
pie1 = variable name (so you can
use it later)
new = declares that youre making a
new object

W hat allthat crap m eant:


Second ApplePie = States that youre

using the constructor from the


ApplePie class
() = args that you want to define (but
really, do you really need to be THAT
picky? I mean, just take the pie as
is!)

Then
We just write stuff to test and make sure

our class is working (yeah, so thats the


whole point of the driver)
Notice how we dont give the user a
chance to input data
This is for the programmers benefit only
Thats why predefined values for variables are

acceptable
This is just to say Hey! This class works
correctly

Can be as small as 1 line and as big as a

regular program

Sam ple driver # 1


public class APDriver
{
public static void main (String[] args)
{
ApplePie applePie = new ApplePie();
System.out.println(The pie is hot = + applePie.getHotness() + .);
System.out.println(The diameter of this apple pie is + applePie.getDiameter() + .);
applePie.setHotness(false);
System.out.println(The pie is hot = + applePie.getHotness() + .);
}
}
/* Output :
* The pie is hot = true.
* The diameter of this apple pie is 1..
* The pie is hot = false.
*/

Few things
Normally, you SHOULD check for

everything to make sure it works, but that


might take forever, so in class they usually
have predefined output they expect
With this, remember that you can
manipulate what you have and can make
this more fancy (e.g. using an if then
statement to make the string equal to hot
or cold depending on the boolean value,
and then inserting that into the output)
As with normal programs, you can still use
methods (so use them!) Be concise as
possible

ANY QUESTIONS?

TH E EN D

You might also like