35
Introduction to Java Classes and Methods

Introduction to Java

  • Upload
    charo

  • View
    39

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to  Java

Introduction to Java

Classes and Methods

Page 2: Introduction to  Java

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.

Page 3: Introduction to  Java

Class example

Page 4: Introduction to  Java

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.

Page 5: Introduction to  Java

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.

Page 6: Introduction to  Java

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)

Page 7: Introduction to  Java

Static vs. non-static

Page 8: Introduction to  Java
Page 9: Introduction to  Java

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.

Page 10: Introduction to  Java

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

Page 11: Introduction to  Java

How objects behavesRemember: a class describes what an object knows and what an object does

A class is the blueprint for an object

Page 12: Introduction to  Java

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

Page 13: Introduction to  Java

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

Page 14: Introduction to  Java

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.

Page 15: Introduction to  Java

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

Page 16: Introduction to  Java

Method Definition

Page 17: Introduction to  Java

The Parameter List

Page 18: Introduction to  Java

Sending things to a method

Page 19: Introduction to  Java

Getting things back from a method

Page 20: Introduction to  Java

Getting things back from a method

0To return a value from a method when its execution is complete you use a return statement.

Page 21: Introduction to  Java

You can send more than 1 thing to a method

Page 22: Introduction to  Java
Page 23: Introduction to  Java

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

Page 24: Introduction to  Java

Seatwork

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

Page 25: Introduction to  Java

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

Page 26: Introduction to  Java

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?

Page 27: Introduction to  Java

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.

Page 28: Introduction to  Java

Getters and Setters

Page 29: Introduction to  Java

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.

Page 30: Introduction to  Java

The compiler be with

you

Page 31: Introduction to  Java
Page 32: Introduction to  Java

The compiler be with

you

Page 33: Introduction to  Java
Page 34: Introduction to  Java

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)

Page 35: Introduction to  Java

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));

} }