Lab4 Presentation
Lab4 Presentation
Lab 4:
Postoffice
Objectives
1
2/12/2020
User-Defined Classes
● Used to create an Abstract Data Type
○ Meaning a data type built off other data types to represent a real world
thing, like Ice Cream or a Shopper
● Then, you can use that data type in a client program (the one with the main
method)
● Common Parts to a User-Defined Class
○ Attributes
○ Constructor
○ Setters
○ Getters
○ Extra Methods
○ Helper Methods
User-Defined Classes
2
2/12/2020
Attributes
● Remember:
○ An object is a bunch of data hidden behind the scenes and methods to operate on that data
○ A class is a blueprint for making an object
● That “bunch of data” is called the group of attributes for that class
○ A.k.a. Instance variables
● Attributes are variables that describe the characteristics of whatever the
object represents
● When defining attributes for a class
○ For your purposes they have private access
○ They are only declared - do not assign them until the constructor!
● I will be using a Chair class for the rest of my examples
Attributes
3
2/12/2020
Constructors
● You’ve used them for other objects a lot by now… time to make your own!
● Remember how you didn’t assign value to the attributes (instance variables)?
○ It’s the constructor’s job!
● The constructor will assign values to the instance variables and do any other
actions required for the beginning of an object
○ E.g. the Scanner object’s constructor probably does some additional stuff behind the scenes to
connect to the keyboard to read user input
● You can declare parameters to take additional information, or you can not
○ Gives information that affects how the constructor does its job
● You can have multiple constructors
○ If they take different parameters, Java sees them as different entities and allows both to exist
Constructors
● Constructors are always public
(unlike attributes)
● Always match the constructor
name with the name of the class
(see examples on last slide, too)
● See how the attributes can be
used because they are defined
above
● See how the parameters can be
used as if already initialized
4
2/12/2020
Methods
Setters
● Setters allow the program using the objects to set the instance variable
values, changing the state of the object
● Works like the constructor in terms of setting the data, but happens AFTER
the constructor has been called and the object exists
● Needs a public access modifier
● After public, write void
○ This just means the call to the setter method won’t return a value (void as in nothing)
● Give the method an identifier (usually setSomething(type parameter, …))
● Setters need parameters so they can receive information to change the
attributes
10
5
2/12/2020
Setters
● For our chair example, here are the setters
● Notice the indentation as file grows
○ Underrated
○ Makes code easy to read
○ Is considered good practice to indent properly
● Notice the words before the function is defined
● Notice the name of each function
○ Starts lower case
○ Camel case (capitalize each word’s first letter)
○ A format of setAttribute()
● Notice how attributes and parameters are used
11
Getters
● Getters are class methods that you can
invoke on an object of that class to get
information about the attributes
● Shouldn’t need parameters
● Uses the keyword return to give
information back to where it was called
● Some of the simplest methods to write
● Getters need an access modifier of public
to be accessed other places and the return
type so Java knows how to compile where
this method is used
12
6
2/12/2020
Additional Methods
13
7
2/12/2020
15
Client Class
16
8
2/12/2020
Submission
● One zipped folder per group
○ lastNameLastNameLab4.zip
○ Put only source code (.java files) in the folder, like in past labs
○ You should have IceCream.java, Shopper.java, and Lab4Client.java (or a similarly named
client class)
● Use a comment to put your names in the source files
● Upload the zipped folder with only the source files to Blackboard
● If you need help with how to do submission, ask us
● If you need help at all, ask us
● The TAs want to help you
○ Come to office hours if you need help with the class or want to know anything at all really
about CS 115 topics
17