Lab Tutorial 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

COMP 295 JAVA Programming

Fall 2016

FORMAN CHRISTIAN COLLEGE (A Chartered University)


COMP 295 Java Programming
Fall 2016
Section: A
Instructor: Tahir Javaid

Lab 5
1) Characters in JAVA
Character is a single digit, letter or symbol. Java provides char as primitive data type also a
meta-class, “Character” that contain some useful methods to be used.
Note: We cannot add, subtract or multiply characters, but we can compare them using ==, !=, <,
… etc. operators.
Syntax to make char array:
chararrayName [] = { value1, value2,….};

Syntax to use Character class methods:


Character.methodName(char parameter);

2) Character Class Methods


MethodName Uses
compareTo(char1,char2) Compares two chars. Returns 0 if both are equal, -ve integer if string
isWhitespace(char) Returns true if character is a blank space. ‘ ’.
isDigit(char) Returns true if that character is a digit. i.e.: 1, -3, 65,.. etc.
isLetter(char) Returns true if that character is a letter. i.e.: A, B, c.. etc.
isLetterOrDigit(char) Returns true if that character is a letter or digit. i.e.: A, 45, c.. etc.
isLowerCase(char) Returns true if letter is in lowercase.
isUpperCase(char) Returns true if letter is in uppercase.
toLowerCase(char) Returns character in lowercase.
toUpperCase(char) Returns character in uppercase.

Activity 1:Declare a lowercase char and display it in uppercase.

Activity 2:Declare a char type array and store characters of your name in it,
with 1st letter in uppercase and others in lower.

Activity 3:Declare a function that takes char as parameter and returns true if
it‟s a symbol (i.e.: $,%,+…), otherwise false.

1
COMP 295 JAVA Programming
Fall 2016

3) Strings in JAVA
Stringsare a sequence of characters. Strings are objects (i.e.: Build-in class) and not a primitive
data type.A String is an array of characters.

4) Some String‟s Methods


Methods Uses
length() Returns the length of string. (no. of characters).
trim() Remove extra spaces before and after letters.
toUpperCase() Converts all characters to uppercase.
toLowerCase() Converts all characters to lowercase.
equals(string) Compares two strings. Returns true if both have same characters in same state.
compareTo(st) Compares two strings. Returns 0 if both are equal, -ve integer if string
toCharArray() Converts String to char array.
charAt(index) Returns character at index position. (index = 0-string.length()-1)
split(string) Breaks String into String[] at argument ‘string’.

5) Sample String program:


publicclass TestString
{
publicstaticvoid main(String[] args)
{
// To Declare a String.
String s;
// To Declare multiple Strings.
String s1,s2,s3,s4;

// Initialize String by Constructor Call.


s1 = new String("Hello World!");

// To initialize a String.
s2 = "Hello World!";

// Adding Strings.
s3 = "Hello";
s4 = " World!";
s = s3 + s4;

// To convert character array to string


char array [] = {'a','b'};
String sp = new String(array);
}
}

6) Concatenating Strings:
Concatenation means combining two or more Strings or variables as String.
The String class includes a method for concatenating two strings:

2
COMP 295 JAVA Programming
Fall 2016

string1.concat(string2);

This returns a new string that is string1 with string2 added to it at the end.
Strings are more commonly concatenated with the + operator, as in

"Hello," + " world" + "!"

which results in:


"Hello, world!"

The + operator is widely used in print statements. For example:

String string1 = " is ";


System.out.println("JAVA " + string1 + "Fun!");

which prints:
JAVA is Fun!

Note:Such a concatenation can be a mixture of any objects. For each object that is not a String,
its toString() method is called to convert it to a String.

Activity 4:Declare 2 Strings „a‟ & „b‟, initialize one with constructor calland
other without constructor call. Print their contents. Now make a new String
made up of first char of „a‟ and last char of „b‟.

Activity 5:Declare a String containing list of 5 fruits separated by comma ( , )


and print the list such that 1 fruit‟s name appear in single line with
numbering.(HINT: Split the string at comma.)

Activity 6:Make a function that takes char array as parameter, pass and
convert char array created in Activity2 into String with all uppercase letters.
Return greeting of the form “HELLO BOB”.

Activity 7:Given a string, return a new string made of 3 copies of the first 2
chars of the original string. The string may be any length. If there are fewer
than 2 chars, use whatever is there.
Eg.: Input= Hello Output= HeHeHe. Input= h Output= hhh

All The Best

You might also like