





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)
CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)
1
Recall that methods / constructors can have parameters public Student (String newName, int IDDesired) { name = newName; id = IDDesired; tokenLevel = 3; } What is printed by the following? String newName = “Joe”; Student s = new Student(newName + “ Schmoe”, 123456789); System.out.println (s.name); System.out.println (newName); Joe Schmoe Joe
CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)
2
CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)
3
Library: implementation of useful routines that are shared by different programs Java mechanism for creating libraries: packages
Package: group of related clases Example: java.util (contains Scanner class)
To use a class from a package, you can use a fully qualified name (package name + class name): java.util.Scanner s = new java.util.Scanner(System.in);
You can also import the class in the beginning of the file
import java.util.Scanner;
To import class in a package: import java.util.*; (Imports Scanner as well as other classes in package)
CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)
6
String implements lots of string functions
Math implements lots of mathematical
functions
CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)
7
So far all classes / variables / methods have
been public
Sometimes access should be restricted!
CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)
8
Java also allows variables / methods to be
declared private:
private int tokenLevel = 3;
Private variables / members cannot be
accessed outside the class definition
Declaring instance variables private means
they can only be modified using public
methods
CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)
9
As long as interface is preserved, class can change without breaking other code The more limited the interface, the less there is to maintain
Make instance variables private Implement set / get methods
Make auxiliary methods private
CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)
12
Every variable (what is variable for?) Every method (what does method do?) Every class (what is class for? who wrote it?)
CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)
13
insert set #
CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)
14
prototype: public static void f(int x, float y) signature: f(int , float ) // names of variables and the return type are not part of it
CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)
15
On general assignments
On parameters
On return value