Classes Java is an Object-Orented Programming language, and one of the main advantages of OOP is...
-
Upload
arnold-barrett -
Category
Documents
-
view
217 -
download
3
Transcript of Classes Java is an Object-Orented Programming language, and one of the main advantages of OOP is...
Classes• Java is an Object-Orented Programming language,
and one of the main advantages of OOP is code reuse.
• Code reuse: write code only once, put it in a CLASS, then reuse it anytime you need it, without having to type it again, by creating an OBJECT that is an instance of that class.
• A class performs specific tasks. These tasks are called methods.
• Classes don’t have a main– they don’t actually do anything if you execute them on their own.
• To use methods from a class, you “connect” to the class by creating an object in your main program.
• The object is just a variable, whose type is the name of the class.
• Syntax for declaring an object:className objectName = new
className( );• Syntax for “invoking” or “calling” a method:
objectName.methodName()
Objects• We’ve already used classes and objects:
– String is a class.
String name = “Peppers”– name is an object – an “instance” of the String class. Its value
is “Peppers”.
• Every class has methods. We’ve already used methods:
name.length( )
(this is called “invoking” or “calling” a method.)
**String and SavitchIn are two classes that we’ve used already, but they are special “static” classes that we connect to slightly differently than others. We will learn why later.
• In order to use a class, you need a regular program that will call its methods. This program is called a client.
• Most classes have variables, called instance variables.
• You should declare them before any of the methods.
• In a class, all code (other than declaring variables) must be inside a method. Code cannot “float” between methods.
• Demos: CLASSdemo & CLIENTdemo
Types of Methods• Methods starting with “public void” perform a task,
but they do not send anything to the client• If you want a method to send (“return”) something
to the client, do not use “void”• Instead, use whatever it is that you are returning to
the client – for example, if returning a double, start the method with “public double”
• A method that is returning something must have a return statement as the last line of code in the method
• Demos: CLASSdemo & CLIENTdemo
Assignment• Create a CLASS called FamilyClass.• It will require about 10 instance variables…see below to figure
out which ones.• Its first method, called setData( ), does not return anything. But
it does do the following:– Asks how many people are in your family– Asks for the age of each person– Uses if statements to determine certain information (see below…)
• Next, create the following methods. Each should return one value: – getAvg - getNumTeens– getOldest - getNumAdults (adults: age 20+)– getYoungest - getNumEvens– getNumKids (age 0-12) - getNumOdds
• Next, create a CLIENT called FamilyClient that calls the methods in FamilyClass, then displays the information.