Full Transcript

Classes and Objects Introduction Computer programming involves writing instructions with the goal of solving a specific problem. Traditional programming languages involve the programmer defining and solving the problem in terms defined by the computer. Object oriented programming languages allow the...

Classes and Objects Introduction Computer programming involves writing instructions with the goal of solving a specific problem. Traditional programming languages involve the programmer defining and solving the problem in terms defined by the computer. Object oriented programming languages allow the programmer to define problems in terms of the problem The real power of a computer language comes from its ability to define complex, multipart, structured data types that model the complex properties of real world objects Object types Every value used in a Java program has a type. So far we have used primitive data types in our programs. Object oriented programming languages allow the use of both primitive and Object types. A Java programmer can create their own Object types, or can create Objects from types defined by others. State and Behaviour An Object is like a super-type. It can store data, and can also perform operations on itself. An Object is said to have State and Behaviour. State is the things an Object knows. Behaviour is the things an Object does. This is built around the concept of objects in the real world. For example, a car has state (colour, size) and behaviour (drive). Instance Variables State is represented in the form of Instance Variables. This is the information that an Object knows. For example, a BankAccount Object might have instance variables accountNumber and balance. A Student Object might have instance variables studentID and studentName Methods Behaviour is represented in the form of Methods. A method is an operation which the object can perform, usually by accessing or manipulating its instance variables. For example, a BankAccount Object might have getBalance( ) and withdrawMoney ( ) methods. These methods perform operations on the instance variables of that Object. Objects and Classes An Object is an entity that you can manipulate in your program. An Object is created from a template known as a Class. Each Object is an instance of a Class. For example, keyboardIn is an instance of the Scanner Class. Once a Class is established, you can create as many Objects of that Class as you like. Objects and Classes Class Objects (instances) of the Student class Bill Mary Student Seamus Jim Hannah Declaring an Object Scanner keyboardIn = new Scanner(System.in); This line of code declares an object called keyboardIn, which is an instance of the Scanner class The object keyboardIn has all the variables and methods defined in the Scanner class. The Scanner class Scanner …. variables nextInt( ) nextDouble() nextLine( ) ….. methods A Scanner object Scanner keyboardIn = new Scanner(System.in); int age; age = keyboardIn.nextInt(); keyboardIn variables methods …. nextInt( ) nextDouble() nextLine( ) next( ) ….. The String class String myName = new String(); myName = "John"; A String is a sequence of characters which is enclosed in quotation marks. Here, myName is an object which is an instance of the String class. myName has all the variables and methods defined in the String class. The String Class String …. length( ) charAt( ) toUpperCase( ) toLowerCase( ) … methods A String object int numLetters = 0; String myString = new String(); myString = "Hello World"; numLetters = myString.length(); System.out.print(numLetters); variables methods myString …. length( ) charAt( ) toUpperCase( ) toLowerCase( ) ….. Calling Methods The programmer calls the methods of a particular object to perform operations on its instance variables. For example, a programmer might call the nextInt( ) method of a keyboardIn object. Scanner keyboardIn = new Scanner(System.in); int studentAge; studentAge = keyboardIn.nextInt(); It is important to understand how to provide inputs into a method, and how to obtain the output from a method. Method parameters Some methods can perform their operations without any inputs. For example, the length( ) method of the String class does not need any input. It can simply be called as is. The same is true for the nextInt( ) method of the Scanner class. Other methods require information from the programmer in order to work. For example, in order to use the charAt( ) method of the String class, you need to provide the position from which you require the character. Method parameters An input for a method is known as a method parameter. Methods are written to expect parameters of a certain type. For example, the charAt( ) method of the String class expects an int value as a parameter. The programmer must use the right type when passing parameters to a method. Failure to do this will result in either a syntax error, or unexpected results. public class StringTester { public static void main(String [] args) { String myName = new String(); Method parameter myName = "Clare Doherty"; char firstLetter; firstLetter = myName.charAt(0); System.out.print("The name starts with " + } } firstLetter); public class StringTester { public static void main(String [] args) { String myName = new String(); Invalid Method parameter myName = "Clare Doherty"; This will cause a Syntax error char firstLetter; firstLetter = myName.charAt(“Clare"); System.out.print("The name starts with " + } } firstLetter); Method return values A return value is a value which is returned as the result of a call to a method. Some methods perform operations and do not return any information.For example, the println( ) method of the System.out object simply performs an operation. It does not return any information. Other methods return information when invoked. For example, the length( ) method of the String class returns a value representing the length of that String. Method return values Methods will return values of a certain type. For example, the charAt( ) method of the String class returns a char value. The programmer must write code to deal with the values which a method returns. String myName = new String(); myName = "Clare Doherty"; int numLetters; numLetters = myName.length(); System.out.print(numLetters + " characters"); Some String methods Method Input Return type length() None int charAt() int char concat() String String equals() Object boolean equalsIgnoreCase() String boolean toUpperCase() None String toLowerCase() None String compareTo() String int startsWith() String boolean endsWith() String boolean Method definitions When a method is defined in a class, its input parameter types and return types are also defined. This takes the following syntax: returnType methodName (input parameters) For Example: int length( ) char charAt(int position) The Java API Programmers can look in the.java file of a particular class to find out what input parameter types and return types a method has. This information is more readily available in the Java API (Application Program Interface), which is easily found on the web. The Java API documents the classes available in the Java library. This includes the provision of information on the methods and instance variables of each class. The Java API http://java.sun.com/j2se/1.5.0/docs/api/ Using the Java API Problem: How do I find the first character in a particular String? String myName = new String(); myName = "Clare Doherty"; Step 1: Find the String class in the Java API Using the Java API Step 2: Read through the Method Summary section to find the method which will retrieve a particular character. Using the Java API Step 3: Figure out how to use the method by identifying its input parameter type and return type Using the Java API Step 4: Based on these, write code to call the method, and deal with any returned values char firstLetter; firstLetter = myName.charAt(0); System.out.println(firstLetter); import java.util.Scanner; public class StringTester{ public static void main(String [] args) { // declare variables Scanner keyboardIn = new Scanner(System.in); String userName = new String(); int numberLetters = 0; char firstLetter; // get user name from the user System.out.print("Please enter your user name: "); userName = keyboardIn.nextLine(); // use an appropriate method to find the number of letters numberLetters = userName.length(); // use an appropriate method to find the first letter firstLetter = userName.charAt(0); System.out.println(“That has " + numberLetters + " characters"); System.out.println(“It starts with the letter " + firstLetter); } }

Use Quizgecko on...
Browser
Browser