Object Oriented Programming Course Code: CE304: Muhammad Hasan, 2009-CE-253, Section "E"
Object Oriented Programming Course Code: CE304: Muhammad Hasan, 2009-CE-253, Section "E"
Submitted to:
Sir Farrukh Aziz
Submitted By:
Muhammad Hasan
2009-CE-253
Section “E”
1
Muhammad Hasan, 2009-CE-253, Section “E”
Question 1(a): Check using JAVA program weather $ sign is allowed in between other
characters in the naming convention for Identifiers? For example: check if a$1 is valid?
Question 1(b): Check the validity of the following Identifiers. What rules you want to
convey regarding naming conventions for Identifiers:
Example: (1) 1bed (2) -1 (3) b_1_c_
2
Muhammad Hasan, 2009-CE-253, Section “E”
Question 1(c): Write a JAVA program that checks the value of a character, if it is in
lower case, it changes it to upper case and vice versa. Do not use built-in methods. Use
appropriate print messages.
ANS 1©:
class Box
{
String n;
void str()
{
System.out.println(n);
}
}
if(Character.isUpperCase(in[0]))
{
System.out.println("You're String is in Upper Case");
String up = input.toLowerCase();
strbox.n = up;
strbox.str();
}
else
if(Character.isLowerCase(in[0]))
{
System.out.println("You're String is in
Lower Case");
String up = input.toUpperCase();
strbox.n = up;
strbox.str();
}
}
}
OUPUT:
You're String is in Lower Case
HASAN
3
Muhammad Hasan, 2009-CE-253, Section “E”
Question 1(d): Write a JAVA program that defines 3 (1-Dimensional) arrays of same
size and move elements of the first array into Even or Odd array, depending on the
nature of the element. Print these arrays.
ANS 1(d):
public class a {
else
if(k == 0)
{
arr3[i] = arr1[i];
System.out.println("Element of array3: " +
arr2[i]);
}
}
}
OUTPUT:
Element of array 2: 10
Element of array 2: 45
Element of array 2: 82
4
Muhammad Hasan, 2009-CE-253, Section “E”
Question 1(e): Write a JAVA program that initializes a string with “Sir Syed University
of Engineering and Technology (SSUET), Karachi, Pakistan”. Print the string after
toggling the case, with the appropriate display message. Do not use built-in methods. Use
only one for loop.
5
Muhammad Hasan, 2009-CE-253, Section “E”
Question 1(f): Initialize a string with “Sir Syed University of Engineering and
Technology (SSUET), Karachi, Pakistan”. Count the number of upper case and lower
case letters and vowels. Use only one for loop. You can use built-in methods. Print the
output with appropriate display messages.
ANS 1(f):
public class cntup {
OUPUT:
Upper case letters: 12 - SSUETSSUETKP
Lower case letters: 51 -
iryedniversityofngineeringandechnologyarachiakistan
Other characters: 13 - (), ,
6
Muhammad Hasan, 2009-CE-253, Section “E”
Question 1(g): Write a JAVA program that initializes a string and display the count of
characters other than space, tab, letter and digit in that string. Print the output with
appropriate display message.
ANS 1(g):
public class cntchar {
OUTPUT:
7
Muhammad Hasan, 2009-CE-253, Section “E”
Question 1(h): Write two (1(h1), 1(h2)) JAVA programs for Question 1(c) and
Question 1(e) with the help of built-in methods. Print the output with appropriate display
message.
8
Muhammad Hasan, 2009-CE-253, Section “E”
Question 1(j): Write three (1(j1), 1(j2), 1(j3)) JAVA programs to print the following
patterns with the help of nested loops and conditional statements.
***** ****** *****
**** ?????? $$$$
*** **** ***
** ???? $$
* ** *
??
ANS 1(j1):
public class desgin_1J1 {
}
System.out.println();
}
}
OUPUT:
******
*****
****
***
**
*
9
Muhammad Hasan, 2009-CE-253, Section “E”
ANS 1(j2):
} System.out.println("");
for(y=6;y>=x;y--)
{
System.out.print("?");
}
x++;
y--;
System.out.println(""); }
}
}
OUTPUT
******
??????
****
????
**
??
10
Muhammad Hasan, 2009-CE-253, Section “E”
ANS 1(j3):
Question 1(k): Is there any problem in accessing a string character by character using
index, without using any JAVA built-in method? Justify your answer.
11
Muhammad Hasan, 2009-CE-253, Section “E”
Question 1(m): In JAVA, arrays are technically objects. Justify this argument.
ANS 1(m):
As in java we have “String” type. This is not a simple type or a simple char array. Rather
it defines an object. You can declare arrays of string. A quoted string constant can be
assigned to a String variable. Your can use an object of type String as an argument to
println() function. For example, consider the following fragment
Eg.
1. String mystring = “My name is Muhammad Hasan”;
2. System.out.println(mystring);
12
Muhammad Hasan, 2009-CE-253, Section “E”
In above example, we have mystring which is an object of type String. It is assigned the
string “My name is Muhammad Hasan”. This string is print by println().
Question 1(n): Draw a table of all primitive data types of JAVA containing the data
type keyword, range and size in the memory.
13
Muhammad Hasan, 2009-CE-253, Section “E”
1.79769313486231570e+308d
Char 0 to 65,535 2 Bytes
Question 1(p): Write JAVA programs for Question 1(c) with the help of switch
statement. Do not use built-in methods. Print the output with appropriate display
message.
14
Muhammad Hasan, 2009-CE-253, Section “E”
Question 1(q): What type of errors is found in the following program? What lesson have
you learnt from that error?
public class b {
public static void main(String a[]) {
char c='a'; boolean b=Character.isLetter(c);
switch(b) {
case true: System.out.println("English alphabet"); break;
default : System.out.println("NOT an English alphabet");
}}}
ANS 1(q):
The errors found in programs are
15
Muhammad Hasan, 2009-CE-253, Section “E”
at test.main(test.java:4)
As switch statement only allows the integer type values and enum values. As Boolean
is’nt that type of value. It returns only TRUE or FALSE.
Question 1(r): Write a JAVA program that uses ternary conditional operator to
determine the greater number out of two integers. Use appropriate display message.
ANS 1®:
public class ternary_condition {
int c = 20;
int d = 15;
int min,max;
16
Muhammad Hasan, 2009-CE-253, Section “E”
min = (a < b) ? a : b;
System.out.println(min);
max = (c > d) ? c : d;
System.out.println(max);
}
OUPUT:
40
20
------------------------------------------------------XXX-------------------------------------------------------------------
17