Lec09 - CS1101Y: Programming Methodology Software...

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

View Full Document Right Arrow Icon
Software Engineering. Classes with Class Members Lecture 9 — October 13, 2008 CS1101Y: Programming Methodology
Background image of page 1

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

View Full DocumentRight Arrow Icon
October 13, 2008 CS1101Y--group2, Lecture 9 2 • Coding-Style Conventions • Helper Methods • Encapsulation (with Instance Variables and Local Variables) • Algorithmic Design with OOP • Top-down Design • Stubs • Merging Driver Method into Driven Class • Accessing Instance Variables Without Using this Software Engineering
Background image of page 2
October 13, 2008 CS1101Y--group2, Lecture 9 3 /********************************************************* * StudentDriver.java * Dean & Dean * * This class acts as a driver for the Student class. *********************************************************/ public class StudentDriver { public static void main(String[] args) { Student s1; // first student Student s2; // second student s1 = new Student(); s1.setFirst("Adeeb"); s1.setLast("Jarrah"); s2 = new Student("Heejoo", "Chun"); s2.printFullName(); } // end main } // end class StudentDriver Coding-Style Conventions
Background image of page 3

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

View Full DocumentRight Arrow Icon
October 13, 2008 CS1101Y--group2, Lecture 9 4 /********************************************************* * Student.java * Dean & Dean * * This class handles processing of a student's name. *********************************************************/ import java.util.Scanner; public class Student { private String first = ""; // student's first name private String last = ""; // student's last name //******************************************************* public Student() { } // This constructor verifies that each passed-in name starts with // an uppercase letter and contains lowercase letters thereafter. public Student(String first, String last) { setFirst(first); setLast(last); } Coding-Style Conventions
Background image of page 4
October 13, 2008 CS1101Y--group2, Lecture 9 5 //******************************************************* // This method verifies that first starts with an uppercase // letter and contains lowercase letters thereafter. public void setFirst(String first) { // [A-Z][a-z]* is a regular expression. // See API Pattern class. if (first.matches("[A-Z][a-z]*")) { this.first = first; } else { System.out.println(first + " is an invalid name.\n" + "Names must start with an uppercase letter and have" + " lowercase letters thereafter."); } } // end setFirst Coding-Style Conventions
Background image of page 5

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

View Full DocumentRight Arrow Icon
October 13, 2008 CS1101Y--group2, Lecture 9 6 // This method verifies that last starts with an uppercase // letter and contains lowercase letters thereafter. public void setLast(String last) { // [A-Z][a-z]* is a regular expression. See API Pattern class. if (last.matches("[A-Z][a-z]*")) { this.last = last; } else { System.out.println(last + " is an invalid name.\n" + "Names must start with an uppercase letter and have" + " lowercase letters thereafter."); } } // end setLast //******************************************************* // Print the student's first and last names. public void printFullName() { System.out.println(this.first + " " + this.last); } // end printFullName } // end class Student Coding-Style Conventions
Background image of page 6
October 13, 2008 CS1101Y--group2, Lecture 9 7 • As with all examples discussed so far, methods normally use the public access modifier. • public methods are considered to be part of the class's interface . • Sometimes, you'll want to create a method that should not be part of the interface; instead it just supports the operation of other methods within its own class. • Those methods are called helper methods and they should use the private access modifier.
Background image of page 7

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

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

This note was uploaded on 03/19/2012 for the course SOC 1101 taught by Professor A.tan during the Fall '08 term at National University of Singapore.

Page1 / 37

Lec09 - CS1101Y: Programming Methodology Software...

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

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