Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 9

Department of Computer and Information Science,

School of Science, IUPUI

Introduction to Java

- Access Specifiers

Dale Roberts, Lecturer


Computer Science, IUPUI
E-mail: droberts@cs.iupui.edu

Dale Roberts
Access Modifiers public and private

private keyword
Used for most instance variables
private variables and methods are accessible only to
methods of the class in which they are declared
Declaring instance variables private is known as data
hiding
public keyword
Exposes variables or methods outside the Class.
Declaring public methods is know as defining the class’
public interface.
Return type
Indicates item returned by method
Declared in method header

2 Dale Roberts
1 // Fig. 3.7: GradeBook.java
2
3
// GradeBook class that contains a courseName instance variable
// and methods to set and get its value.
Outline 3

4
5 public class GradeBook Instance variable
6 { courseName
7
8
private String courseName; // course name for this GradeBook
GradeBo
// method to set the course name
9
10 public void setCourseName( String name ) ok.java
11 {
set method for courseName
12 courseName = name; // store the course name
13 } // end method setCourseName
14
15 // method to retrieve the course name
16 public String getCourseName()
17 {
get method for courseName
18 return courseName;
19 } // end method getCourseName
20
21 // display a welcome message to the GradeBook user
22 public void displayMessage()
23 {
24 // this statement calls getCourseName to get the
25 // name of the course this GradeBook represents
26 System.out.printf( "Welcome to the grade book for\n%s!\n",
27 getCourseName() );
28 } // end method displayMessage Call get method
29
30 } // end class GradeBook

Dale Roberts
1 // Fig. 3.8: GradeBookTest.java
2
3
// Create and manipulate a GradeBook object.
import java.util.Scanner; // program uses Scanner
Outline 4

4
5 public class GradeBookTest
6 {
7
8
// main method begins program execution
public static void main( String args[] )
GradeBo
9
10
{
// create Scanner to obtain input from command window
okTest.j
11
12
Scanner input = new Scanner( System.in ); ava
13 // create a GradeBook object and assign it to myGradeBook (1 of 2)
14 GradeBook myGradeBook = new GradeBook();
15
16 // display initial value of courseName
17 System.out.printf( "Initial course name is: %s\n\n",
18 myGradeBook.getCourseName() );
19
Call get method for
courseName

Dale Roberts
1 // Fig. 3.10: GradeBook.java
2 // GradeBook class with a constructor to initialize the course name. Outline 5
3
4 public class GradeBook
5 {
6 private String courseName; // course name for this GradeBook
7 GradeBo
8 // constructor initializes courseName with String supplied as argument
9 public GradeBook( String name ) ok.java
10 { Constructor to initialize (1 of 2)
11 courseName = name; // initializes courseName courseName variable
12 } // end constructor
13
14 // method to set the course name
15 public void setCourseName( String name )
16 {
17 courseName = name; // store the course name
18 } // end method setCourseName
19
20 // method to retrieve the course name
21 public String getCourseName()
22 {
23 return courseName;
24 } // end method getCourseName

Dale Roberts
25
26 // display a welcome message to the GradeBook user Outline 6
27 public void displayMessage()
28 {
29 // this statement calls getCourseName to get the
30 // name of the course this GradeBook represents
GradeBook.j
31 System.out.printf( "Welcome to the grade book for\n%s!\n", ava
32 getCourseName() ); (2 of 2)
33 } // end method displayMessage
34
35 } // end class GradeBook

Dale Roberts
1 // Fig. 3.11: GradeBookTest.java
2 // GradeBook constructor used to specify the course name at the Outline 7
3 // time each GradeBook object is created.
4
5 public class GradeBookTest
GradeBookT
6 {
7 // main method begins program execution est.java
8 public static void main( String args[] ) Call constructor to create first
9 { grade book object
10 // create GradeBook object
11 GradeBook gradeBook1 = new GradeBook(
12 "CS101 Introduction to Java Programming" );
13 GradeBook gradeBook2 = new GradeBook(
14 "CS102 Data Structures in Java" );
15 Create second grade book
16 // display initial value of courseName for each GradeBook object
17 System.out.printf( "gradeBook1 course name is: %s\n",
18 gradeBook1.getCourseName() );
19 System.out.printf( "gradeBook2 course name is: %s\n",
20 gradeBook2.getCourseName() );
21 } // end main
22
23 } // end class GradeBookTest

gradeBook1 course name is: CS101 Introduction to Java Programming


gradeBook2 course name is: CS102 Data Structures in Java

Dale Roberts
1 // Fig. 3.11: GradeBookTest.java
2 // GradeBook constructor used to specify the course name at the Outline 8
3 // time each GradeBook object is created.
4
5 public class GradeBookTest
6 {
7 // main method begins program execution GradeBo
8 public static void main( String args[] ) Call constructor to create first
9 { grade book object okTest.j
10
11
// create GradeBook object
GradeBook gradeBook1 = new GradeBook(
ava
12 "CS101 Introduction to Java Programming" );
13 GradeBook gradeBook2 = new GradeBook(
14 "CS102 Data Structures in Java" );
15 Create second grade book
16 // display initial value of courseName for each GradeBook object
17 System.out.printf( "gradeBook1 course name is: %s\n",
18 gradeBook1.getCourseName() );
19 System.out.printf( "gradeBook2 course name is: %s\n",
20 gradeBook2.getCourseName() );
21 } // end main
22
23 } // end class GradeBookTest

gradeBook1 course name is: CS101 Introduction to Java Programming


gradeBook2 course name is: CS102 Data Structures in Java

Dale Roberts
Acknowledgements
http://java.sun.com/docs/books/tutorial/getStarted/TOC.html
Pearson Education, Lewis and Loftus.
Deitel, Java How to Program
http://www.cs.wustl.edu/~plezbert/contcom/thesis/node6.html
http://www.cs.usfca.edu/~parrt/course/652/lectures-Spring-
2004/language.impl.overview.pdf
http://ei.cs.vt.edu/~history/Youmans.Java.html

Dale Roberts

You might also like