Names for Java classes follow the same rules as for variable names. It Is convention that each word In the class name begins with an upper case letter. V. The body of the class is contained within a set of braces, and typically consists of type declarations... Which are often referred to as the data members of the class, method definitions, and sometimes even other class definitions. Vi. And, the body of the class defines a scope. Ill. The variables, methods, and classes that are part of the class definition are commonly called class members. Declaring Class Member Variables l.

This is the general syntax for declaring the variables associated with a class. I. The first element is the access mode. It can be public, private, protected, or not appear at all. II. The optional keyword static Is used to declare a special kind of variable called a class variable. Class variables will be discussed In another lecture. The type name is the name of a primitive or non-preemptive type. Lb. And the variable name is the name of the variable. Defining Class Member Methods l. This is the general syntax for declaring the methods associated with a class. Appear at all. II.

We will write a custom essay sample on

AP Computer Science- Classes specifically for you

for only $13.90/page

Order Now

The optional keyboard static Is used to declare a special kind of method called a class method. (Actually, all the methods I've asked you to write so far in this course are class methods. ) ill. The return type is the name of a primitive or non-primitive type returned by the method, or void If the method doesn't return anything. L. When you design a class there are three major decisions you must make: I. What does the class represent? A. Single, well defined thing b. Easy to understand it. What data type is needed to describe the class? A. Primitive types b. Class types ii. What must the class do? . What do we need to get from it/do to it II. Example: Let's see how we could come up with a design for a cube class. I. First, since what we are modeling is a cube... Cube would be a good name for the class... And would clearly indicate what is being represented. It. Next, we need to determine what data is needed to model a cube. A. A cube is a geometric solid, and can be described by a width measure, a depth measure, and a height measure. B. In fact, for a cube each of these dimensions are equal, so it would suffice to describe a cube y specifying the length of one side.

And... Let's use a double precision type for the length of a side. Iii. One thing we may want to do with a cube is to calculate its volume. 'v. Another thing we'd need to be able to do is to set the length off side Ill. Let's see how we would write a class definition for cubes. Defining a Cube Class l. Here's a class definition for our Cube class: public class Cube { double side; public void seaside( double s ) { side = s; public double calculatedly() { return side * side * side; II. Notice that the class member methods can reference the class data member side.