0% found this document useful (0 votes)
17 views

Lab4 Presentation

This document provides guidance on creating user-defined classes in Java. It explains that classes are blueprints for objects that contain attributes (instance variables) and methods. The document outlines the key parts of a class like attributes, constructors, setters, getters and additional methods. It also distinguishes between a class implementation, which defines the class, and a client class that uses the class by creating objects and calling methods. The document uses a Chair class as a running example and provides code snippets for defining attributes, constructors, setters and getters for such a class. It recommends submitting the source code files for the user-defined class and client class in a zip folder.

Uploaded by

Ghassan Rbeiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab4 Presentation

This document provides guidance on creating user-defined classes in Java. It explains that classes are blueprints for objects that contain attributes (instance variables) and methods. The document outlines the key parts of a class like attributes, constructors, setters, getters and additional methods. It also distinguishes between a class implementation, which defines the class, and a client class that uses the class by creating objects and calling methods. The document uses a Chair class as a running example and provides code snippets for defining attributes, constructors, setters and getters for such a class. It recommends submitting the source code files for the user-defined class and client class in a zip folder.

Uploaded by

Ghassan Rbeiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

2/12/2020

Lab 4:
Postoffice

Objectives

● Make your own user-defined classes


● Write a client program (with main method) that uses your class
● Understand the difference between client class and implementation class

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

● With these parts in mind, I


recommend starting to write your
class like you can see to the right
● This will remind you the steps you
need to take to write your class
● It is also an effective way to apply
pseudocode to your class!

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

● Each attribute (instance variable) is private - cannot be accessed outside of


this file
● Each attribute has a type (as all variables need in Java)
● Each attribute has an identifier (as all variables need in pretty much any
language
● NONE of these are assigned values yet, until...

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

● Pieces of code tied together by a method identifier


● When that method identifier is invoked, the code will execute
● Requires:
○ An access modifier (usually public or private) to say where the method can be used
○ A return type (int if an integer variable is returned, could be char, String, and is void if nothing
is returned
○ An identifier like a variable name to call when you want to run the method later
○ A set of parameters if outside information needs to be passed along to the method so it can
perform its job
○ Finally, the code that will be executed when that method is called.

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

● Using rules of how to write


methods, you can write a method to
do any abstract action to the object
being represented
● For the chair example, something
abstract that can happen to the
chair is that coffee is spilled
○ We can represent the effect by modifying
the attributes of the object!

13

Ta-Da! The Chair class is complete!

Notice there is NO main method here


14

7
2/12/2020

Implementation vs. Interface


● Implementation is the way a class is made, usually behind the scenes,
usually you have no knowledge of how (like Scanner or Random)
○ When you write a user-defined class, that is the class’s implementation
● Interface is the use of the class’s public methods in another class
● A Client class is used to test and use the other classes you implement
● When you make a class that will make an object, there should NOT be a main
method
● You should write a client class that has the main method
○ Only classes with a main method can be run
○ Your code must be in a main method for you to run it
● Once in your client, use your now user-defined class to make objects just like
you did in past labs with Scanner, Point, and Random

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

You might also like