chapter8_nn - Coding-Style Conventions Helper Methods...

Info iconThis preview shows pages 1–7. 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

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

View Full DocumentRight 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: 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 Chapter 8 Software Engineering 1 /********************************************************* * 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 2 /********************************************************* * 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 3 //******************************************************* // 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 4 // 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 5 As with all examples discussed so far, methods normally use the public access modifier....
View Full Document

Page1 / 22

chapter8_nn - Coding-Style Conventions Helper Methods...

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

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