Download presentation
Presentation is loading. Please wait.
1
1 What makes a Good Class Original by Dr. Stumpf – modified by Dr V Copyright 1996-98 © Dale Carnegie & Associates, Inc.
2
2 What makes a Good Class By using a clean looking design a class can be understood quickly. There are several issues: Sequencing –Indentation and Formatting
3
3 What to Include in a Class Two constructors: –Default constructor public Employee(){}; –Populating Constructor public Employee(double rate, double hours) { this.rate = rate; this.hours = hours; }
4
4 What to Include in a Class toString part one: public String toString() { String classDescription = "Employee" + "["; classDescription += "name" + " = "; classDescription += name; classDescription += ", "; classDescription += "hours" + " = ";
5
5 What to Include in a Class toString part two: classDescription += hours; classDescription += ", "; classDescription += “rate" + " = "; classDescription += rate; classDescription += "]"; return classDescription; }
6
6 What to Include in a Class Mutators (modifiers): called setters public setHours(double hours) {this.hours = hours;} public setRate(double rate) {this.rate = rate;}
7
7 What to Include in a Class Accessors: ----- called getters public double getHours() {return hours;} public double getRate() {return rate;}
8
8 Sequencing Class Elements There is variety in styles here. A few authors place attributes last. Most, however, prefer to place attributes first. Then constructors. Lastly methods sequenced alphabetically.
9
9 Indentation and Formatting The big argument is the placement of braces James Gosling of Sun who is considered the primary author of Java uses the following style: public double computeWage(double hours){ if (hours < 40){ return hours * rate; } else{ return (hours - 40) * rate * 1.5 + 40.0 * rate; } }
10
10 Indentation and Formatting Others prefer to line up the braces as follows: public double computeWage(double hours) { if (hours < 40) { return hours * rate; } else { return (hours - 40) * rate * 1.5 + 40.0 * rate; } }
11
11 Indentation and Formatting The instructors in the CIS all prefer the second way. This goes against the majority of Computer Scientists. However, the second way is much easier to inspect for problems
12
12 Close By using clean a looking design a class can be understood quickly. There are several issues: –What to include –Sequencing –Indentation and Formatting
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.