classesObjects - Classes and Objects Motivation for Classes...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Classes and Objects Motivation for Classes We can write some fairly complicated programs in a single main method, and we can do just about anything using a bunch of static methods in a single file. However, there is a lot more to programming than just getting it to work we also want to be able to easily reuse pieces of our code, and for our programs to be easy for other people to read. This requires us to think about good design for our programs instead of just functionality. Program Design Computer programmers almost always work in teams, which means it is vital that all team members can use and understand code written by others in the group. If you think about how a big program (like a popular computer game) would look if it was written in a single would be a nightmare to read. Programs like that tend to have millions of lines of code it would be very difficult to ever find what you were after. It is much easier to read code that is divided into many files and many methods by functionality. That way, you could go directly to the section of code you were interested in without having to wade through everything else. When each method solves a small piece of the problem, and each file holds a group of methods that do related things, its very easy to figure out whats going on. New Data Types Creating programs with multiple classes also gives us the option to create new specialized data types. Later in this chapter, we will see how to classes into data types, complete with their own fields (pieces of relevant information) and methods to operate on those fields. For example, we will be able to create a Rectangle type that store the width and height for the rectangle, and that has area and perimeter methods that operate on the width and height. Class Syntax with Static Methods We have already seen how to write several methods within one class. Now, we will learn how to create several classes with different methods. This section will not discuss how to create objects (instances of classes) for now, were just dividing methods into different files as an organization trick. Example: Separate Class with Static Methods Suppose we want to create a lot of methods that perform mathematical operations (average, round, max, min, etc.). It would be nice to be able to reuse these methods in other projects, so we will want to divide them into a separate class. (Actually, there is a Math class in the Java libraries that contains these methods, but we will create our own.) Creating a separate class with static methods is exactly like the classes weve written in the past the only difference is that the separate class will not have a main method. Here is our MathOps class: //stored in the file public class MathOps { //assumes arr has at least one element public static int max(int arr) { int m = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > m) m = arr[i]; } return m; } //assumes arr has at least one element...
View Full Document

This note was uploaded on 03/02/2010 for the course CIS 200 taught by Professor Staff during the Spring '08 term at Kansas State University.

Page1 / 19

classesObjects - Classes and Objects Motivation for Classes...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online