{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

chapter8 - 1 2 1 HelperMethods Encapsulation( Variables...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
1 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 2
Image of page 1

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

View Full Document Right Arrow Icon
2 /********************************************************* * 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 3 2 1 4 5
Image of page 2
3 /********************************************************* * 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 2 1 4
Image of page 3

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

View Full Document Right Arrow Icon
4 //******************************************************* // 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 3 2 1 4 5
Image of page 4
5 // 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 3 2 1
Image of page 5

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

View Full Document Right Arrow Icon
6 Read the notes. Coding-Style Conventions (hidden)
Image of page 6
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. Helper Methods 1
Image of page 7

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

View Full Document Right Arrow Icon
8 Suppose you're asked to write a program that handles order entries for  sports-uniform shirts. For each shirt order, the program should prompt  the user for a shirt's primary color and its trim color. For each color  selection, the program should perform the same input validation. It 
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern