lab session 3 and 4
DESCRIPTION
Lab session 3 and 4. Topics to be covered Escape sequences Variables /identifiers Constants assignment statement String concatenation String methods. ESCAPE SEQUENCES. Java defines several Escape sequences to represent Special characters. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/1.jpg)
Lab sessionLab session3 and 43 and 4
Topics to be coveredTopics to be covered
• Escape sequences Escape sequences • Variables /identifiers Variables /identifiers • ConstantsConstants• assignment statementassignment statement• String concatenationString concatenation• String methodsString methods
![Page 2: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/2.jpg)
ESCAPE SEQUENCESESCAPE SEQUENCES
• Java defines several Escape sequences to represent Special characters.
• An escape sequence begins with a backslash character (\) and indicates a character or characters that follow should be interpreted in some special way.
![Page 3: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/3.jpg)
What does \t doWhat does \t do• \t - tab• When u use this in a println
statement • System.out.println(“Roses are red,\t Violets are blue,\t,Sugar is sweet”)
• OUTPUT
Roses are red, Violets are blue , Sugar is sweet
![Page 4: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/4.jpg)
Escape Seq Meaning \b backspace \t tab \n newline \r carriage return \” Double quote \’ Single quote \\ backslash
![Page 5: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/5.jpg)
What does \n doWhat does \n do
• \n - newline• When u use this in a println statement
• System.out.println(“Roses are red,\n Violets are blue,\n Sugar is sweet”);
OUTPUT
Roses are red, Violets are blue , Sugar is sweet
![Page 6: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/6.jpg)
What does \” doWhat does \” do
• \n” - Double quote• When u use this in a println statement
• System.out.println(“\ Roses are red,\”Violets are blue”,\n Sugar is sweet”);
OUTPUT
Roses are red, “Violets are blue” , Sugar is sweet
![Page 7: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/7.jpg)
What does \’ doWhat does \’ do
• \’ - Single quote• When u use this in a println statement
• System.out.println(“Roses are red, \‘Violets are blue\’Sugar is sweet.”);
OUTPUT
Roses are red, ‘Violets are blue’, Sugar is sweet.
![Page 8: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/8.jpg)
What does \\ doWhat does \\ do
• \\ - backslash
• When u use this in a println statement
• System.out.println(“Roses are red\\Violets are blue\\Sugar is sweet.”);
OUTPUT
Roses are red\Violets are blue\Sugar is sweet.
![Page 9: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/9.jpg)
What does \b doWhat does \b do
• \’ - backspace
• When u use this in a println statement
• System.out.println(“Roses are red, \bViolets are blue,\bSugar is sweet.”);
OUTPUT
Roses are red, Violets are blue,Sugar is sweet.
![Page 10: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/10.jpg)
String ConcatenationString Concatenation• Strings that are printed using a print statement
can be concatenated using +
• Eg• System.out.println(“Roses are red”+“ ” +“Violets are blue” );
OUTPUT
Roses are red Violets are blue
![Page 11: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/11.jpg)
VARIABLES /IDENTIFIERSVARIABLES /IDENTIFIERS• A Variable is a name for a location in
the memory used to hold a data value
num=25
25
num
memory
![Page 12: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/12.jpg)
• Eg public class keys { Public static void main(String args[]) { int key ; //declaration statement key = 88; //initialization statement System.out.println(“A piano has” + key + “keys.”); System.out.println( key );} OUTPUT:
A piano has 88 keys 88
![Page 13: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/13.jpg)
Primitive Data typePrimitive Data typeThere are 8 data types in java• Integer - has 4 subsets (byte, short, int,long)• Floating point numbers - 2 subsets ( float, double)• Boolean• Character
![Page 14: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/14.jpg)
• Examples: • int num; // num can store whole numbers
• float average; // average can store decimal numbers
• char ch; // stores alphabets
• Boolean value; // stores either True/False
![Page 15: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/15.jpg)
INTIALISATION STATEMENTINTIALISATION STATEMENT
• num=23;• average=23.00;• ch= ‘s’;• value=true;
![Page 16: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/16.jpg)
Assignment statementAssignment statement• Assignment statement –assigns a value to a variable.Public class assign{Public static void main (String args[]){ int no; no=89; int num=56; //assignment st System.out.println(num); System.out.println (“The no is :“+no); }
![Page 17: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/17.jpg)
Arithmetic expressionArithmetic expression• An expression is a combination of one or more
operators and operands• the basic arithmetic operations defined for integer type
and floating point type are• addition(+),subtraction(-,multiplication(*),division(/).• Java has one more arithmetic operation remainder(%)• 17%4 will return 1• a= b + c; a,b,c – operands + -- operator
![Page 18: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/18.jpg)
Operator precedenceOperator precedence• Result=14+8/2; ans=18 ans=11
correct ans is 18
operator precedence hierarchy division/multiplication/% add/sub /concatenation
assignment
![Page 19: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/19.jpg)
• Result=(14+8)/2; ans=11;
• result=((18-4)*(2+2)); =(14*4) = 56
expression should be syntactically correct no of left parenthesis should be =no of right parenthesis Result=((19+6)%3)*3); //invalid Result=2+8-6; usually u start from left when same level of precedence
![Page 20: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/20.jpg)
objectsobjects• Class is used to define an object• Class name is nothing but type of object;• String name; //declaration 1st step• The above declaration creates a reference to
string object• No object actually exist• To create an obj use NEW operator:• name=new String (“Adam”); //obj created 2nd
step
![Page 21: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/21.jpg)
• 1st and 2nd can be combined• String name=new String (“Adam”);• The String class has a no of methodsWhich its objects can useThey are
charat,replace,substring,length, toLowercase,toUppercase, concat,equals
![Page 22: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/22.jpg)
String methodsString methods• String(String str)
• Constructor: creates a new string object with the same characters As str
• Eg String name=new String(“kenny”);Is eqvivalent to String name=“kenny”;
![Page 23: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/23.jpg)
Length methodLength method• Int length ()
• Returns the no of characters in the string
• Eg String greeting= “Hello!”; greeting. length(); returns 7
![Page 24: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/24.jpg)
LowercaseLowercase• String toLowerCase()
• Returns a new string identical to this string except all uppercase letters converted to lowercase
• Eg String greeting= “Hello!”; greeting. toLowerCase(); returns “hello!”.
![Page 25: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/25.jpg)
UPPER CASEUPPER CASE• String toUpperCase()
• Returns a new string identical to this string except all lowercase letters converted to uppercase Eg
String greeting= “Hello!”; greeting. toLowerCase(); returns “HELLO!”.
![Page 26: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/26.jpg)
TrimTrim• String trim()
• Returns a new string identical to this string but with leading and trailing white space removed
String pause= “ hhh “; pause. trim() returns “hhh”.
![Page 27: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/27.jpg)
concatenationconcatenation• String concat(String str)
• Returns a new string consisting of this string concatenated with str
• Eg String name=new String (“kenny”); name.concat(“ is great “) returns “kenny is great”;
![Page 28: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/28.jpg)
Char atChar at• Char charat(int index)
• Returns a character at specified index
• eg String name=new String (“kenny”); name.charat(0) returns ‘k’; name.charat(4) returns ‘y’;
![Page 29: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/29.jpg)
SubstringSubstring• String substring(int start)
• Returns a substring of a string starting from index till end of this string.
• Eg String name=new String (“kenny”); name.substring(2) returns “nny”;
![Page 30: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/30.jpg)
• String substring(int start,int end)
• Returns a substring of a string starting from index start through but not including index endof this string.
• Eg String name=new String (“kenny”); name.substring(2,4) returns “nn”;
![Page 31: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/31.jpg)
ReplaceReplace• String replace(char old,char new)
Returns a new string identical to this string except that every occurrence of old is replaced by new.
• Eg String name=new String (“kenny”); name. replace (‘n’ ,’l’) returns “kelly”;
![Page 32: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/32.jpg)
equalsequals
• Boolean equals(String str);
• Returns true if the string contains the same characters as str and false otherwise
(including case)
String greeting= “Hello!”; greeting. equals(“Hello”) returns true. but greeting. equals(“HELL0”) returns false
![Page 33: Lab session 3 and 4](https://reader035.vdocument.in/reader035/viewer/2022062309/56813b6a550346895da46e4c/html5/thumbnails/33.jpg)
equalsIgnorecaseequalsIgnorecase• Boolean equalsIgnoreCase(String str);
• Returns true if the string contains the same characters as str and false otherwise
(without regard to case);
String greeting= “Hello!”;
greeting. equals(“HELL0”) returns true