360 likes | 485 Views
Introduction to Java. Classes and Methods. What is a Class?. A way to define new types of objects. Can be considered as a blueprint, which is a model of the object that you are describing.
E N D
Introduction to Java Classes and Methods
What is a Class? • A way to define new types of objects. • Can 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.
Fields in a Class Definition • These are variables that store data items that typically differentiate one object of the class from another. • data members of a class.
Methods in a Class Definition • define the operations you can perform for the class • so they determine what you can do to, or with, objects of the class. • operate on the fields — the variables of the class.
Methods in a class definition • You can execute class methods even when no objects of a class exist, • whereas 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)
Class Definition • To 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. • You name a class using an identifier of the same sort you’ve been using for variables • By convention, class names in Java begin with a capital letter.
Class Definition class Sphere { static final double PI = 3.14; // Class variable that has a fixed value static int count = 0; // Class variable to count objects // Instance variables double radius; // Radius of a sphere double xCenter; // 3D coordinates double yCenter; // of the center double zCenter; // of a sphere // Plus the rest of the class definition... }
How objects behaves Remember: a class describes what an object knows and what an object does A class is the blueprint for an object
Every instance of a particular class has the same methods, but the methods can behave differently based on the value of the instance variables What’s Up 4 Non Blondes Anghuling el Bimbo Eraser heads Call me Maybe Jepsen
#16 What is the output of DogTestDrive after compiling and running?
Seatwork Create 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 seatwork class Student{ char name[10]; char course[10]; intyear_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(); } }
Getting things back from a method • To return a value from a method when its execution is complete you use a return statement.
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.
Seatwork • #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
Seatwork • #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 types • Getters and setters • Accessors and mutators • A Getter's sole purpose in life is to send back, as a return value • A setter lives and breathes for the chance to take an argument and use it to set the value of an instance variable.
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.
Seatwork: ½ lengthwise • Create a program that get and set your name and age and display the name and age according to the following criteria: • Past (Name and Age 10 years ago) • Present (Name and Present Age) • Future (Name and Age 10 years later)
class AgeName{ int age; String name; String x; public String getName(){ return name; } public intgetAge(){ 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)); } } 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(); } }