





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!






Chapter 3: Objects as Models: Java is a purely object-oriented language, thus everything in Java is modeled via classes (Objects). An object has two basic properties:
These would indicate whether if the associated declarations are for use privately only (i.e. to be used within this class only – private) or could be accessed by other classes (i.e. available publicly). It is advisable to declare all instance variable to be private. Example private double balance; This is to facilitate information hiding. The out side cannot see what is stored in balance, as well as by not having access to it, it cannot be changed by the out side world. Declaring Instance methods: public void deposit (double amount) { ……… } Access modifier: The word public means that the method could be used (called) anywhere in the program, not only the class in which it is defined. Result type: indicate what type of answer the method will output (return) when the method is called. Method name: The name can be any legal identifier. Parameters: A method can have any number of parameters (form 0 to …) of any data type including user defined type. When a method is declared, these are called parameters. When the method is called, the values passed are regarded as arguments. Let us declare a class name Sword: public class Sword { private String description; //used to give the sword a descriptive name private int weight; //used to store the weight of the sword private int damage; //used to store the amount damage the sword can do private int magicDamage; //used to store the magical damage the sword can do // now put here the class methods } Method Overloading and Method Overriding:
Here, the printDescription method of the Sword class will override the printDescription of the Item class because the Sword class is a derived class of the base class Item. As such if a call is made for the printDescription base upon the Sword class, then the method defined in the Sword class is used. Inheritance: As indicated above, a base class offers its instance variables and methods to the derived class. The reason for a base class, and thus inheritance, is that very often a program may require many classes to be defined, but there may be some amount of relationship between these classes. The common relationships could be defined in the base class and then these be offered to all of the other classes that may need them. There are two important features about creating a base class and a derived class as the following example will show. public class Item { String description; //note the absence of the accessor word private int weight; //note the absence of the accessor word private public String getDescription() { return description; } public void setDescription(String newDescription) { description = newDescription; } Now for the declaration of the Sword class: public class Sword extends Item //note the use of extends Item { private int damage; private int magicDamage; public int getDamage() { return damage; } public void setDamage(int newDamage)
damage = newDamage; } Declaring constructors: Whenever a new object is created, the instance variables are initialized by a constructor.
Java String Class The string class package in java is probably one of the most important packages available. To use the methods found in this package, you will use the java.lang package. Note: this package is automatically imported into your program when you import the java.io.* package. So on creating string variables, we declare them similar to creating integer or double, example: String last_Name, first_Name, middle_Name; OR String last_Name = “ “, first_Name, middle_Name; In this last case, we have initialize last_Name to the blank or empty. We could assign a value to a string variable: first_Name = “John” Note the use of the quote around john. Also a string object could be created without the use of the word new. A string could be visualized as an array holding the individual characters example, first_Name could be visualized as the following array: J o h n 0 1 2 3 These are the index position starting from 0 We can now extract individual characters from the name JOHN example: char x; x = first_Name.charAt(2); now x will hold the value found at position 2 – “h”. We use the charAt(int) function to access the value at the position supplied (in this case 2). Other functions of immediate importance are listed on page 110. Note also when you call a function of the string class you must follow the following format:
Object_name.string_function(string parameters) If we look at the above example: The: object_name was first_Name followed by the dot String_function was charAt String parameter was integer (2) If a string function is used to search for a substring, the value returned would be an index (integer) indicating where that substring is located. If that substring was not found, the return value would be –1. The trim() method is very useful if you want to remove blank spaces from the beginning and/or end of a word. Example if last_Name = “ Helper “; (with 2 blank spaces at both before and after) Then last_Name = last_Name.trim(); Would now make last_Name = “Helper”: (with no leading or ending blanks). Using + to concatenate: This is one of the most versatile use for + in Java. It can be used to put two strings together making one string. Example: String f_L_Name; f_L_Name = first_Name + last_Name; //will produce John Helper. But we could also use it to concatenate a string with an integer example: System.out.println(“This is the value of: “ + integer value); The + operator will automatically convert the integer value to a string and then concatenate them to produce the result. The + operator automatically invoke the toString() method to convert an object to string. We could have done it explicitly by the following: If f was an object of type Fraction, then f.toString() would convert whatever value was returned by this call to a string. The use of the + operator must be cautious in that the following must be observed.