Introduction to Java Classes and Methods
Feb 23, 2016
Introduction to Java
Classes and Methods
What is a Class?0A way to define new types of objects.0Can be considered as a blueprint, which is a model of the
object that you are describing.
An object of a class is also referred to as an instance of that class. When you create an object, the object will contain all the fields that were included in the class definition.
Class example
Fields in a Class Definition0 These are variables that store data items that typically
differentiate one object of the class from another. 0 data members of a class.
Methods in a Class Definition0 define the operations you can perform for the class 0 so they determine what you can do to, or with, objects of
the class.0 operate on the fields — the variables of the class.
Methods in a class definition
0You can execute class methods even when no objects of a class exist,
0whereas instance methods can be executed only in relation to a particular object,
static methods can be executed when there are no objects in existence,
Ex. Public static void main(String[] args)
Static vs. non-static
Class Definition
0To define a class you use the keyword class followed by the name of the class, followed by a pair of braces enclosing the details of the definition.
0You name a class using an identifier of the same sort you’ve been using for variables
0By convention, class names in Java begin with a capital letter.
Class Definitionclass Sphere {static final double PI = 3.14; // Class variable that has a fixed valuestatic int count = 0; // Class variable to count objects
// Instance variablesdouble radius; // Radius of a spheredouble xCenter; // 3D coordinatesdouble yCenter; // of the centerdouble zCenter; // of a sphere// Plus the rest of the class definition...}
How objects behavesRemember: a class describes what an object knows and what an object does
A class is the blueprint for an object
0Every instance of a particular class has the same methods, but the methods can behave differently based on the value of the instance variables
Call me Maybe
Jepsen
What’s Up
4 Non Blondes
Ang huling el Bimbo
Eraser heads
#16 What is the output of DogTestDrive after compiling and running?
SeatworkCreate a program that will display the student’s name
and year level.
Sample output: name = “Jose” course = “Engineering”
year_level = 1
Hello Jose an Engineering 1st year freshman student.
Answer to seatworkclass Student{ char name[10]; char course[10]; int year_level;
void Display(){ if (year_level==1){ System.out.println("Hello" + name + " an" + course + " freshman student"); } if (year_level==2){ System.out.println("Hello" + name + " an" + course + " sophomore student"); } if (year_level==3){ System.out.println("Hello" + name + " an" + course + " junior student"); } if (year_level==4){ System.out.println("Hello" + name + " an" + course + " senior student"); } if (year_level==5){ System.out.println("Hello" + name + " an" + course + " 5th year student"); } } }
class StudentTest{ public static void main(String[] args){
Student s = new Student();
s.name= " Jose ";
s.course= " Engineering ";
s.year_level= 2;
s.Display(); } }
Method Definition
The Parameter List
Sending things to a method
Getting things back from a method
Getting things back from a method
0To return a value from a method when its execution is complete you use a return statement.
You can send more than 1 thing to a method
How arguments are passed to a method
In Java, all argument values are transferred to a method using the so called pass-by-value mechanism
Seatwork
1-3) Fill in the correct values of the 3 objects that is currently playing.
Seatwork0 #4 The code beside this Represent a complete source code. Your Job is to play the compiler and determine whether each of the classes will compile. If they don’t compile, how would you fix them. If they do compile, what is the output?
The compiler be with
you
Seatwork0 #5 The code beside this Represent a complete source code.Your Job is to play the compiler and determine whether each of the classes will compile. If they don’t compile, how would you fix them. If they do compile, what is the output?
Good things we do with parameters and return types0Getters and setters0Accessors and mutators
0A Getter's sole purpose in life is to send back, as a return value
0A setter lives and breathes for the chance to take an argument and use it to set the value of an instance variable.
Getters and Setters
Encapsulation – leaving our data out there for anyone to see and even touch
Private keyword declares that the attribute is accessible only to the methods within this class.
The compiler be with
you
The compiler be with
you
Seatwork: ½ lengthwise
0Create a program that get and set your name and age and display the name and age according to the following criteria:
0Past (Name and Age 10 years ago)0Present (Name and Present Age)0Future (Name and Age 10 years later)
Answer to Assignment
class TestAgeName{ public static void main(String[] args){ AgeName test = new AgeName(); test.age = 20; test.name = "Juan"; test.x = "Future"; test.Display(); } }
class AgeName{ int age; String name; String x; public String getName(){ return name; } public int getAge(){ return age; } public void setName(int n){ name = ; } public void setAge(int a){ age = a; } void Display(){ if(x == "Past"){ System.out.println(name + " " + (age-10));} else if(x == "Present"){ System.out.println(name + " " + age);} else System.out.println(name + " " + (age+10));
} }