CS2 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

CS2

Description:

a main method. Here we make two. Greeter objects and use them. C ... Another class just holding a main. method. Here we make a Greeter. object and modify it. ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 16
Provided by: ccGa
Category:
Tags: amain | cs2

less

Transcript and Presenter's Notes

Title: CS2


1
CS2
  • Module 5
  • Category Elements of Java
  • Topic What's an object?
  • Objectives
  • Second look at elements of Java

2
CS 2
  • Introduction to
  • Object Oriented Programming
  • Module 5
  • Elements of Java
  • What's an Object?

3
Object Oriented Programming
  • OOP is all about programming using something
    called an object
  • We'll get much more deeply into this topic later
    (after some basic stuff)
  • But just in case you can't wait...

4
Previously...
  • public class HelloWorld
  • public void greet()
  • System.out.println("Hello World!")
  • public static void main(String argv)
  • HelloWorld hw new HelloWorld()
  • hw.greet()

5
A Class
A class will be used to define how objects made
from this class will operate
  • class Greeter
  • String greeting
  • public Greeter(String newGreeting)
  • greeting newGreeting
  • public void greet(String name)
  • System.out.println(greeting ", " name)

6
Field
A class typically will have one or more
fields which are the data items which the object
holds
  • class Greeter
  • String greeting
  • public Greeter(String newGreeting)
  • greeting newGreeting
  • public void greet(String name)
  • System.out.println(greeting ", " name)

7
A Constructor
Classes can have Constructors which contain code
only run when an object is created. Useful for
initialization
  • class Greeter
  • String greeting
  • public Greeter(String newGreeting)
  • greeting newGreeting
  • public void greet(String name)
  • System.out.println(greeting ", " name)

8
A Method
Once created an object can be asked to run a
"method" which will do something
normally involving data stored by the
object (plus sometimes data passed in.)
  • class Greeter
  • String greeting
  • public Greeter(String newGreeting)
  • greeting newGreeting
  • public void greet(String name)
  • System.out.println(greeting ", " name)

9
Another Class
This class is just being used to hold a main
method. Here we make two Greeter objects and use
them
  • class Driver
  • public static void main(String args)
  • Greeter g1 new Greeter("Hello")
  • Greeter g2
  • g2 new Greeter("Good morning")
  • g1.greet("Bob")
  • g2.greet("Mary")
  • g1.greet("Sam")
  • g2.greet("Joan")

Cgtjava Driver Hello, Bob Good morning,
Mary Hello, Sam Good morning, Joan Cgt
10
We can also...
  • class Greeter
  • String greeting
  • public Greeter(String newGreeting)
  • greeting newGreeting
  • public void greet(String name)
  • System.out.println(greeting ", " name)
  • public void setGreeting(String chgGreeting)
  • greeting chgGreeting
  • public String getGreeting()
  • return greeting

We can even write methods to change or retrieve
the value of the fields.
11
A Third Class
Another class just holding a main method. Here
we make a Greeter object and modify it.
  • class Driver2
  • public static void main(String args)
  • Greeter g2
  • g2 new Greeter("Good morning")
  • g2.greet("Bob")
  • System.out.println("Current "g2.getGreeting())
  • g2.setGreeting("Good evening")
  • g2.greet("Mary")

Cgtjava Driver2 Good morning, Bob Current Good
morning Good evening, Mary Cgtgt
12
OOP
  • OOP consists of designing a bunch of classes most
    of which will be used as templates to make
    objects
  • The objects will be used to hold data and will be
    initialized with constructors and perform useful
    functions by being asked to run their methods
  • As we will see they can even be used to make
    sophisticated graphical applications

13
Demo
14
Questions?
15
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com