CHAPTER 6 MOST COMMONLY USED LIBRARIES

Similar documents
"Hello" " This " + "is String " + "concatenation"

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

Lab 14 & 15: String Handling

String. Other languages that implement strings as character arrays

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

appreciate the difference between a char and a string understand and use the String class methods

String related classes

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

Object-Oriented Programming

Creating Strings. String Length

CS112 Lecture: Characters and Strings

Lecture Notes K.Yellaswamy Assistant Professor K L University

Building Strings and Exploring String Class:

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

STUDENT LESSON A10 The String Class

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

OOP-Lecture Java Loop Controls: 1 Lecturer: Hawraa Sh. You can use one of the following three loops: while Loop do...while Loop for Loop

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

We now start exploring some key elements of the Java programming language and ways of performing I/O

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

More non-primitive types Lesson 06

USING LIBRARY CLASSES

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

Class Library java.lang Package. Bok, Jong Soon

Programming with Java

Class. Chapter 6: Data Abstraction. Example. Class

TEXT-BASED APPLICATIONS

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS 1301 Ch 8, Part A

A variable is a name for a location in memory A variable must be declared

CLASS X. THEORY 100 Marks

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics


Lesson:9 Working with Array and String

Mathematics for Computer Graphics - Lecture 12

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) - All Sections Final Examination

Using Java Classes Fall 2018 Margaret Reid-Miller

Chapter 4 Mathematical Functions, Characters, and Strings

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

STRINGS AND STRINGBUILDERS. Spring 2019

Ans: Store s as an expandable array of chars. (Double its size whenever we run out of space.) Cast the final array to a String.

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

More on variables and methods

TechSparxJavaTuitionsTechSparxJava

More on Strings. Lecture 10 CGS 3416 Fall October 13, 2015

Eng. Mohammed Abdualal

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 13 th February

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Intro to Computer Science II

ADVANCED PROGRAMMING CONCEPTS

CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

Lecture 8: The String Class and Boolean Zen

Visit for more.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

AP Computer Science. Strings. Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

CSE1710. Big Picture. Today is last day covering Ch 6. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

Java: Learning to Program with Robots

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault

Strings! Today! CSE String Methods!

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

Interaction with Android

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Strings

Comments in a Java Program. Java Overview. Identifiers. Identifier Conventions. Primitive Data Types and Declaring Variables

Chapter 12 Strings and Characters. Dr. Hikmat Jaber

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Final Examination

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Java String Java String provides a lot of concepts that can be performed on a string such as compare, concat, equals, split, length, replace,

Computer Science 145 Midterm 1 Fall 2016

- Thus there is a String class (a large class)

TEST (MODULE:- 1 and 2)

Chapter 3: Using Classes and Objects

Bellwork Strings 11/25/13

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 13 th February

Java Programming. String Processing. 1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

COMPUTER APPLICATIONS (86)

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

CISC 370: Introduction to Java

C08c: A Few Classes in the Java Library (II)

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Topic 13 procedural design and Strings

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Java Foundations: Unit 3. Parts of a Java Program

Advanced Object Concepts

Transcription:

LIBRARY CHAPTER 6 - A set of ready-made software routines (class definitions) that can be reused in new programs, is called a Library. - Some commonly used java libraries are : Math Library String Library IO(Input/output) Library WORKING WITH STRINGS Java offers two classes to work with string data: 1. String Class Whose instances or object can hold unchanging string (immutable string) i.e. once initialized its contents cannot be modified. 2. StringBuffer Class Whose instance or objects can hold (mutable) strings that can be changed or modified. CREATING STRINGS String <object/variable> = <value of object/variable> ; e.g. String name = I am a Student ; CREATING STRINGBUFFERS Java provide three ways to create Stringbuffer object: 1. StringBuffer s = new StringBuffer( ); This method creates an empty StringBuffer object namely s. 2. StringBuffer str = new StringBuffer( First ); This method creates a StringBuffer object namely str and initializes it with string value First. 3. int n =15; StringBuffer str2 = new StringBuffer(n); This method creates the StringBuffer with an initial capacity equal to n (=15 here) number of characters. METHODS OF STRING CLASS 1. char charat( int index) - Returns the character at the specified index. e.g. String a = INFORMATICS PRACTICE ; char ch=a.charat(0); System.out.println( +ch ); The value in ch will be I as the index of this character is 0. 2. int capacity( ) - Returns maximum no. of characters that can be entered in the string object. Page 1 of 8

e.g. CHAPTER 6 String a = INFORMATICS PRACTICES ; int cap =a.capacity( ); System.out.println( +cap ); The output is 21. 3. int compareto( String1 ) - Compares two strings lexicographically. 0 (Zero) is returned if the strings are identical. e.g.1 String a = ABC ; String b = ABC ; int c = a.compareto(b); System.out.println( +c ); The output is 0. e.g.2 String a = ABC ; String b = DBC ; int c = a.compareto(b); System.out.println( +c ); The output is -3. 4. String concat(string str) - Concatenates the specified string to the end of this string (current string object). e.g. String a = INFORMATICS ; String b = PRACTICES ; String c = a.concat (b); OR String c = a + b ; System.out.println(c) ; 6. boolean equals(string) The output is INFORMATICSPRACTICES - compares this string (current string object) to the specified string. e.g. e.g. 2. String a = IBCM ; String b = IBCM ; boolean c = a. equals(b); The output is true String a = IBCM ; String b = ibcm ; boolean c = a. equals(b); The output is false Page 2 of 8

7. boolean equalsignorecase(string) - compares this string (current string object) to the specified string, ignoring case consideration. e.g. String p= INFORM ; String q= inform ; boolean r = p.equalsignorecase(q); System.out.println( +r); The output is true 8. int indexof(char) - Returns the index within the this string (current string object) of the first occurrence of the specified character. 9. int lastindexof(char) String b = INTIMATION ; int z = b.indexof( I ); System.out.println( +z); The output is 0 - Returns the character within this string of the last occurrence of the specified character. 10. int length( ) String b = INTIMATION ; int z = b.lastindexof( I ); System.out.println( +z); The output is 7 - Returns the length of this string. String a= INFORMATICS ; int b = a.length( ); System.out.println( +b); The output is 11 11. String replace(char oldchar, char newchar) - Returns a new string resulting from replacing all occurrence of old char in this string with new char. String a = INFORMATION ; String c = a.replace( I, P ); System.out.println( +b); 12. boolean endswith(string) The output is PNFORMATPON - Tests if this string (current String object) ends with the specified suffix. e.g. String a = INFORMATICS ; boolean s = a.endswith( MATICS ); Page 3 of 8

13. boolean startswith(string) CHAPTER 6 System.out.println( s); The output is true for above code. - Tests if this string (current String object) starts with specified suffix. e.g. String a = JAVAPROGRAM ; boolean s = a.startswith( JAVA ); System.out.println( s); The output is true for above code. 14. String substring(int, int ) - Returns a new string that is a substring of the this string. e.g.1. String a = INFORMATICS PRACTICES ; String c = a.substring(0,5); System.out.println( +c); The output is INFOR e.g.2. String a = INFORMATICS PRACTICES ; String c = a.substring(8,13); System.out.println( +c); The output is ICS P 15. String tolowercase( ) - Converts all the characters in this String to lower case. e.g. String z = PRACTICES ; String n = z.tolowercase( ); System.out.println( +n); The output is practices 16. String touppercase( ) - Converts all the characters in this String to upper case. e.g. String z = Informatics ; String n = z.touppercase( ); System.out.println( +n); The output is INFORMATICS 17. String trim( ) - Removes white spaces from both sides of this String. e.g. String a = INFORMATION ; String c = a. trim ( ) ; The output is INFORMATION Page 4 of 8

18. String valueof(all types) - Returns the String representation of the passed argument. e.g. int a = 123 ; valueof(a) ; The value retuned by above statement is 123 STRING BUFFER METHODS 1. String append (String) - adds the specified string in parameter at the end of current String object. e.g. StringBuffer s = new StringBuffer( INFORMATICS ); String c = s.append( PRACTICES ); The Output is INFORMATICSPRACTICES 2. String insert(int, String) - adds the string specified in parameter, at the index specified in first parameter. e.g. StringBuffer s = new StringBuffer( IFORM ); String c = s.insert(1, N ); The output is INFORM 3. String setcharat (int, char ) - Alter just one character. It replaces character specified in second parameter at the index specified in first parameter. e.g. StringBuffer b = new StringBuffer( POGRAM ); String c = b.setcharat(1, R ); The output is PROGRAM 4. String delete (int beginindex, int endindex) -delete the characters from begin to end index. e.g. StringBuffer s = new StringBuffer( INFORMATICS ); String c = s.delete(6,10); The output is INFORM 5. String reverse( ) - Reverse the content of String Buffer. e.g. StringBuffer str = new StringBuffer( POWDER ); String a = str.reverse( ); System.out.println( +a); The output is REDWOP Page 5 of 8

MATH CLASS METHODS 1. pow ( ) - This method returns the first argument raised to the power of the second argument. Syntax is : double pow(double, double) OR int pow(int, int) e.g. Math.pow(2, 3) = 2 3 = 8. Math.pow(2.0, 2.0 ) = 2.0 2.0 = 4.0 2. round( ) - Round off the number to the nearest integer. Syntax is : double round( double) OR float round(float) e.g. Math.round(1.5) = 2 Math.round(1.568, 2) = 1.57 Math.round(2.68, 0) = 3 PROBLEMS Q1. Write the output that will be displayed in the textfields by the following Java code : String x ; String stream = Commerce ; jtextfield1.settext((stream.length( ) + 9) + ); x = stream.concat( Humainities ); jtextfield2.settext(x); jtextfield3.settext( +x. substring(2,5)); jtextfield4.settext(stream.tolowercase( )); Q2. Predict the output of the following code : String s = COMPUTER ; int L = s.length(); for(int i =L ; i >=0 ; i--) System.out.println(s.substring(0, i)); Q3. What will be the content of jtextarea1 after the execution of the following statement? String message = All the Best ; jtextfield1.settext((message.length() - 6 )+ ); Q4. What will be the value of X1 after the execution of the following code? String X1= Graduate, X2= Post ; X1=X2.concat(X1); Page 6 of 8

Q5. Give the output of the following java code: String name = Sid Nagar ; int T = name.length( ), N ; N = 150 T ; jtextfield2.settext(integer.tostring(t)); jtextfield3.settext(integer.tostring(n)); Q6. What will be the contents of jtextfield1 and jtextfield2 after executing the following code : String s = Best ; jtextfield1.settext(s.length( ) + ); jtextfield2.settext(s.touppercase( )); Q7. What will be the contents of str1 and str2 after the following code is executed? String Str2, Str1 ; Str1 = Dear Friend ; Str2 = Hello ; Str1 = Str2.concat(Str1); Q8. What will be the contents of jtextfield1 and jtextfield2 after executing the following : String name1 = Hello World ; Name1 = name.tolowercase( ) ; String name2 = name1.replace(,! ); jtextfield1.settext(name1); jtextfield2.settext(name2); Q9. Given an int variable K with value 253. It is converted into equivalent string i.e. 253. What are two ways of doing this? Q10. Write the output : System.out.println(Math.pow(4.0,2.0)); System.out.println(Math.pow(6.459)); Q11.What will be the contents of jtextfield1 and jtextfield2 after executing the following code: String s = ABC MicroSystems ; jtextfield1.settext(s.length( ) + ); jtextfield2.settext(s.tolowercase( )); Q12. Given a String object named code having value as 908 stored in it. What will be result of the following? JOptionPane.showMessageDialog(null, +(code.length( ) +Integer.parseInt(code)); Q13. Assuming String age = 17 ; Write the Java expression to store value of age in a variable of double type(say double d). Q14. What will be the value of X1 after execution of the following code : String X1= Spread, X2= PEACE ; X1 = X2.concat(X1); Q15. What will be the content of jtextfield1 and jtextfield2 after executing the following code: String st = New to Information Technology ; jtextfield1.settext(st.replace( Technology, Practices )); jtextfield2.settext(st.substring(7)); Page 7 of 8

Page 8 of 8