Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.

Similar presentations


Presentation on theme: "Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values."— Presentation transcript:

1 Classes/Objects

2 Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values –can be initialized to null Use new to instantiate new objects/initialize object variables –String name = new String(“Mickey”); –invokes Constructor of String class

3 Example int num = 5; String name = new String(“Mickey”); String name2 = “Minnie”; //String shortcut 5 num name Mickey name2 Minnie

4 Dot Operator Use dot operator to access methods of an object –name.length()

5 Aliases Two object variables can point to the same object With primitive types, a copy is made int a = 5; int b = 12; a = b; 12 a b

6 Aliases String name = “Mickey”; String name2 = “Mickey”; name Mickey name2 Mickey

7 Aliases name = name2; name name2 Mickey

8 Strings Strings are immutable String methods –char charAt(int index) –String replace(char oldChar, char newChar)

9 Strings String s = “Hello” String t = s.replace('l', 'p'); s Hello t Heppo

10 Packages Classes in the standard Java library are grouped into packages –java.util –java.math –java.text Using the API

11 import To use a class from the library you must either use the fully-qualified name or import the class java.util.Scanner s = new java.util.Scanner(System.in) import java.util.Scanner; import java.util.*; //import all classes in java.util.

12 Exercises Complete PP 3.1 –Write an application that prompts for and reads the user's first and last name (separately). Then, print a string composed of the first letter of the user's first name, followed by (no more than) the first five characters of the user's last name, followed by a random number in the range 10 to 99. Hint: You will need to use java.util.Random

13 Java Classes Contain data and methods Methods enable user to access and modify data

14 Encapsulation Data should be hidden from the user –Access to/modification of data is controlled by methods Modifiers –Enable programmer to specify which data and methods are private and which are public private are accessible within the class public are accessible from anywhere

15 Name.java public class Name { private String first; private String last; //Constructor public Name(String thefirst, String thelast) { first = thefirst; last = thelast; } public void printName() { System.out.println("First: " + first + " Last: " + last); }

16 Driver Classes The driver typically contains the main method –this is the starting point for the program The driver creates instances of other classes

17 NameTest.java public class NameTest { public static void main(String[] args) { Name n = new Name("Sami", "Rollins"); n.printName(); }

18 Methods public void printName() {... } modifier return_type name(type name,...) You must specify the return type and the type of each parameter

19 More Method Headers For each data member, consider whether you should create setter and getter methods public String getFirstName() public void setFirstName(String newname) public String getLastName() public void setLastName(String newname)

20 Constructors Called when a new object is created Like a method with no return type Should initialize all relevant data Should take as input initial values for any variables that do not have a logical default value A default constructor takes no parameters public Name(String thefirst, String thelast) { first = thefirst; last = thelast; }

21 Scope public Name(String first, String last) { this.first = first; this.last = last; } this provide access to class variable first/last are local to the constructor

22 Exercises Complete exercise PP4.4 –Design and implement a class called Book that contains instance data for the title, author, publisher, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Create a driver class called BookShelf, whose main method instantiates and updates several Book objects.


Download ppt "Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values."

Similar presentations


Ads by Google