1 / 35

Introduction to Java

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.

charo
Download Presentation

Introduction to Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Java Classes and Methods

  2. 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.

  3. Class example

  4. 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.

  5. 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.

  6. 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)

  7. Static vs. non-static

  8. 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.

  9. 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... }

  10. How objects behaves Remember: a class describes what an object knows and what an object does A class is the blueprint for an object

  11. 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

  12. #16 What is the output of DogTestDrive after compiling and running?

  13. 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.

  14. 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(); } }

  15. Method Definition

  16. The Parameter List

  17. Sending things to a method

  18. Getting things back from a method

  19. Getting things back from a method • To return a value from a method when its execution is complete you use a return statement.

  20. You can send more than 1 thing to a method

  21. 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

  22. Seatwork 1-3) Fill in the correct values of the 3 objects that is currently playing.

  23. 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

  24. 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?

  25. 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.

  26. Getters and Setters

  27. 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.

  28. The compiler be with you

  29. The compiler be with you

  30. 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)

  31. 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(); } }

More Related