Department of Computer Engineering Using Objects 2140101 Computer
Department of Computer Engineering Using Objects 2140101 Computer Programming for International Engineers 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY
Department of Computer Engineering Objectives Students should: • Understand classes and objects. • Be able to use class methods and data from existing classes. • Be familiar with the String class and be able to use its methods. • Be able to use the Buffered. Reader class to get users’ input from keyboards. • Be able to process keyboard input as String and numeric values. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 2
Department of Computer Engineering Classes and Objects • Classes – Classes are non-primitive data types in Java – New classes can be made while there are no such things as new primitive data types. • Objects – object is an instance of a class. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 3
Department of Computer Engineering Classes and Objects (1) • String is a class in Java. a variable named s 1 is declared as a variable that is used for storing an object of the class String s 1; s 1 = “Chocolate Chip”; an object of class String is created with the content “Chocolate Chip” and assigned to the variable s 1. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 4
Department of Computer Engineering Classes and Objects (2) String s 1; s 1 is created. It does not contain anything yet. s 1 - s 1 “Chocolate Chip” 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY s 1 = “Chocolate Chip”; A String object containing “Chocolate Chip” is created, but unreferenced. s 1 = “Chocolate Chip”; s 1 is made to point to the String object “Chocolate Chip”. 5
Department of Computer Engineering Using Data and Methods provided in Classes • An object of a class contains data and methods. • For example – Class Rectangle – The data contained in each object • Such as height , width , x, and y • stores necessary attributes that define a rectangle. – several methods • related to using the rectangle • such as get. Height() , get. Width() , get. X() , get. Y(), and set. Location(). 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 6
Department of Computer Engineering Example Rectangle height width x y get. Height() get. Width() get. X() get. Y() set. Location() : Data Methods 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 7
Department of Computer Engineering Using Data and Methods provided in Classes (2) • The dot operator (. ) is used for accessing data or methods from a class or an object of a class. • Consider the two methods below. System. out. print(“Strawberry Sundae”); System. out. println(“Banana Split”); 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 8
Department of Computer Engineering Using Data and Methods provided in Classes (3) • System – System is a class in a standard Java package. – This class contains an object called out. • Out – An object whose class is a class called Print. Stream. – Using the dot operator, we refer to this out object in the class System by using System. out. • Print. Stream – The class contains print() and println() • we can access the two methods using System. out. print() and System. out. println(). 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 9
Department of Computer Engineering Class “System” System data in out Print. Stream data : : methods : methods print() println() System. out. println() 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 10
Department of Computer Engineering Using Data and Methods provided in Classes (4) • Some data and methods can be accessed by using the dot operator with the name of the class • Some can be accessed by using the name of the variable storing the object of that class. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 11
Department of Computer Engineering Using Data and Methods provided in Classes (5) • Class (or static) data and methods – Data and methods that are accessed via the class name • Instance (or non-static) data and methods – Data and methods that are accessed via the object name Now only realize the difference in accessing the two. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 12
Department of Computer Engineering Example public class Area. Of. Circle { public static void main(String[] args) { double area, r = 10; String s 1 = "The Area of a circle with "; String s 2 = " r = "; String s 3 = " is "; String s 4; area = Math. PI*Math. pow(r, 2); s 4 = s 1. concat(s 2); System. out. println(s 4+area); } } 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 13
Department of Computer Engineering Example • This Java program calculates the area of a circle with radius r, where r equals 10. double area, r = 10; • we calculate the area by multiplying Math. PI with Math. pow(r, 2). area = Math. PI*Math. pow(r, 2); 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 14
Department of Computer Engineering Example • Math. PI – refers to the value that is defined in a constant value names PI in the Math class. • Math. pow(r, 2) – the activation of a method called pow() that is also defined in the Math class. • Notice that we do not need to create an object of the Math class but we access the data and method from the name of the class directly. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 15
Department of Computer Engineering Example • A method called concat() is accessed from a variable that contains a String object. s 4 = s 1. concat(s 2); • s 1. concat(s 2) – returns a String object resulting from the concatenation of the String object in s 1 and the String object in s 2. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 16
Department of Computer Engineering Useful String methods • Some methods that we can use from a String object. • char. At() – Let s be a String object and i be an int. – s. char. At(i) returns the char value at the i th index. • length() – Let s be a String object. – s. length() returns the int value equals to the length of the String. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 17
Department of Computer Engineering Example public class Char. At. Demo { public static void main(String[] args) { String s = "ABCDn. EFGH"; char c; System. out. println("s = "); System. out. println(s); c = s. char. At(0); System. out. println("char. At(0)="+c); c = s. char. At(1); 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 18
Department of Computer Engineering Example System. out. println("char. At(1)="+c); c = s. char. At(5); System. out. println("char. At(5)="+c); System. out. print("The length of this string is ") System. out. println(s. length()+" characters"); c = s. char. At(s. length()-1); System. out. println("The last char ="+c); } } 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 19
Department of Computer Engineering Example 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 20
Department of Computer Engineering Example • the String s contains 9 characters – which are ‘A’, ‘B’, ‘C’, ‘D’, ‘n’, ‘E’, ‘F’, ‘G’, and ‘H’. – an escape sequence is considered a single character. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 21
Department of Computer Engineering Example • the characters at 0, 1, and 5 which are ‘A’, ‘B’ and ‘E’ are assigned to the char variable c. • Then, c is printed out to the screen after each assignment. c = s. char. At(0); System. out. println("char. At(0)="+c); c = s. char. At(1); System. out. println("char. At(1)="+c); c = s. char. At(5); System. out. println("char. At(5)="+c); 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 22
Department of Computer Engineering Example • the length of the String in s is extracted via the method length(). • Be aware that, the first index of a String is 0, so the location of the last character is s. length()-1. System. out. println(s. length()+" characters"); c = s. char. At(s. length()-1); 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 23
Department of Computer Engineering Useful String methods (2) • concat() – Let s be a String object and r be another String object. – s. concat(r) returns an new String object whose content is the concatenation of the String in s and r. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 24
Department of Computer Engineering Example public class Concat. Demo { public static void main(String[] args) { String s 1 = "First"; String s 2 = "Second"; String s 3, s 4; s 3 = s 1. concat(s 2); s 4 = s 2. concat(s 1); System. out. println("s 1 System. out. println("s 2 System. out. println("s 3 System. out. println("s 4 is is "+s 1); "+s 2); "+s 3); "+s 4); String s 5 = "AB". concat("CD"). concat("EF"); System. out. println("s 5 is "+s 5); } } 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 25
Department of Computer Engineering Example 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 26
Department of Computer Engineering Example • s 1. concat(s 1) ≠ s 1. concat(s 1). • the method concat() from a String s creates a new String object – based on s and the String input into the parentheses – it does not change the value of the original String object. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 27
Department of Computer Engineering Example • we can invoke String methods directly from a String object without having to be referred to by a variable – i. e. “AB”. concat(“CD”) • since “AB”. concat(“CD”) results in a new String object, we can call a String method from it directly – e. g. “AB”. concat(“CD”). concat(“EF”) String s 5 = "AB". concat("CD"). concat("EF"); 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 28
Department of Computer Engineering Useful String methods (3) • index. Of() – Let s be a String object and c be a char value. • s. index. Of(c) returns the index of the first c appearing in the String. • It returns -1 if there is no c in the String. – If i is an int value equals to the Unicode value of c • s. index. Of(i) returns the same result. – A String r can also be used in the place of c. • the method finds that String inside the String s • returns the index of the first character of r found in the String s. • it returns -1 if r is not found in s. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 29
Department of Computer Engineering Useful String methods (4) • last. Index. Of() – works similarly to index. Of() – but it returns the index of the last occurrence of the input character 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 30
Department of Computer Engineering Example public class Index. Of. Demo { public static void main(String[] args) { String s = "oxx-xo--xo. Xo"; System. out. println("The first 'x' is at "+s. index. Of('x')); System. out. println("The first 'o' is at "+s. index. Of('o')); System. out. println("The first '-' is at "+s. index. Of(45)); System. out. println("The first 'X' is at "+s. index. Of('X')); } } 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 31
Department of Computer Engineering Example 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 32
Department of Computer Engineering Example public class Index. Of. Demo 3 { public static void main(String[] args) { String s = "say ABC ABC"; System. out. println(s); System. out. println("last. Index. Of('B')="+s. last. Index. Of('B')); System. out. println("last. Index. Of("AB")="+s. last. Index. Of("AB")); } } 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 33
Department of Computer Engineering Useful String methods (5) • substring() – Let s be a String object. s. substing(a, b) – a and b are int values – returns a new String object whose content are the characters of the String s from the ath index to the (b-1)th index. – If b is omitted the substring runs from a to the end of s. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 34
Department of Computer Engineering Useful String methods (6) • to. Lower. Case() – Let s be a String object. – s. to. Lower. Case() returns a new String object which is a copy of s but with all uppercase characters converted to lowercase. • to. Upper. Case() – Let s be a String object. – s. to. Upper. Case()returns a new String object which is a copy of s but with all lowercase characters converted to uppercase. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 35
Department of Computer Engineering Example public class Substring. Demo { public static void main(String[] args) { String s = "Sir Isaac Newton"; System. out. println(s. substring(10)); int start. Idx = 4; int len = 5; System. out. println(s. to. Upper. Case(). substring(start. Idx, star t. Idx+len)); } } 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 36
Department of Computer Engineering More String Methods • There are many more methods provided by the String class. consult class notes and Java API doc. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 37
Department of Computer Engineering Reading Input String from Keyboards • It is usually a common requirement to obtain values from the user of the program via keyboards. • In Java, this capability is provided by some methods, already defined in classes. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 38
Department of Computer Engineering Reading Input String from Keyboards • A class called Buffered. Reader • Buffered. Reader provides a method called read. Line(). – read characters from keyboard input – until a newline character is found – store the characters into a String object. • Note that the newline character (n) signaling the end of the input is not included in the String. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 39
Department of Computer Engineering Using Buffered. Reader • First, since we are going to use the Buffered. Reader class, which is not in the standard Java packages – we need to let the compiler know where to look for the definition of this class – adding the following statement in to our source code on a line prior to the start of our program’s definition. import java. io. *; 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 40
Department of Computer Engineering create an object of class Buffered. Reader • we need to create an object of class Buffered. Reader by using the following statement. Buffered. Reader stdin = new Buffered. Reader(new Input. Stream. Reader(System. in)); • This statement creates a variable named stdin that refers to a Buffered. Reader object. • stdin is a Buffered. Reader object. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 41
Department of Computer Engineering Reading Keyboard Input • Once a Buffered. Reader object is created, we can access the read. Line() method from that object. • For example – we can use the following statement to read keyboard input to a String object called s. – stdin is the object we created in the previous statement. String s = stdin. read. Line(); 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 42
Department of Computer Engineering Reading Keyboard Input • Once the statement is executed, the program waits for the user to type in the input until a newline character is entered. • This input can be used later in the program from the String s. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 43
Department of Computer Engineering Reading Keyboard Input • The following program asks the user to input his/her first and last name. • it prints a message containing the names on to the screen. • Notice that another thing that is required to be added is throws IOException in the header of the main() method. – Explanation is omitted until you learn about exceptions in Java. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 44
Department of Computer Engineering Example import java. io. *; public class Greeting { public static void main(String[] args) throws IOException { String firstname, lastname; Buffered. Reader stdin = new Buffered. Reader(new Input. Stream. Reader(System. in)); System. out. print("Please enter your firstname: "); firstname = stdin. read. Line(); System. out. print("Please enter your lastname: "); lastname = stdin. read. Line(); System. out. println("---------------"); System. out. println("Hello "+firstname+" "+lastname); System. out. println("---------------"); } } 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 45
Department of Computer Engineering Example 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 46
Department of Computer Engineering Converting Strings to numbers • sometimes we expect the keyboard input to be numeric data so that we can process numerically • we need a way to convert a String object to an appropriate numeric value. • Java has provided methods responsible for doing so. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 47
Department of Computer Engineering Converting Strings to numbers (2) • parse. Int() – parse. Int() is a static method that takes in a String object – returns an int whose value associates with the content of that String. – parse. Int() is defined in a class called Integer. – calling a static method named parse. Int() from the Integer class Integer. parse. Int(s) – s is a String object whose content we wish to convert to int. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 48
Department of Computer Engineering Converting Strings to numbers (3) • parse. Double() – parse. Double() is a static method that takes in a String object – returns an double whose value associates with the content of that String. – parse. Double() is defined in a class called Double. – calling parse. Double() takes the form Double. parse. Double(s) – s is a String object whose content we wish to convert to double. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 49
Department of Computer Engineering Integer and Double • It is necessary to know that Integer is a class – not the primitive type int • Double is another class – not the primitive type double. 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 50
Department of Computer Engineering Example double theta, f, theta. Rad, fx, fy; Buffered. Reader stdin = new Buffered. Reader(new Input. Stream. Reader(System. in)); // prompt for f System. out. print("Enter F: "); f = Double. parse. Double(stdin. read. Line()); // prompt for theta System. out. print("Enter Theta: "); theta = Double. parse. Double(stdin. read. Line()); 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 51
Department of Computer Engineering In-class quiz No. 2 • Before Chapter 6 • Materials covered up to Chapter 5 2140101 Computer Programming for International Engineers INTERNATIONAL SCHOOL OF ENGINEERING CHULALONGKORN UNIVERSITY 52
- Slides: 52