Sunteți pe pagina 1din 17

Muhammad Hasan, 2009-CE-253, Section “E”

Object Oriented Programming

Course Code: CE304

Submitted to:
Sir Farrukh Aziz

Submitted By:
Muhammad Hasan
2009-CE-253
Section “E”

Sir Syed University of Engineering &


Technology, Karachi.

1
Muhammad Hasan, 2009-CE-253, Section “E”

Object Oriented Programming


Assignment#1

Instructions To Be Followed Strictly:


 Case sensitivity errors in the programs will be considered.
 Copy question before each solution with the same naming convention mentioned
in the assignment.
 Use index page and put page numbers on each page.
 Use separate sheet for the title of Assignment and for the file.

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);
}
}

public class upper


{

public static void main(String args[])


{
String input = "hasan";
Box strbox = new Box();
char in[] = input.toCharArray();
//int a = input.length();

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 {

public static void main(String args[])


{

int arr1[] = {10, 12, 45, 65, 82, 78};


int arr2[] = new int[5];
int arr3[] = new int[5];

for(int i=0; i<6; i++)


{
int j = i%2;
int k = i%3;
if(j == 0)
{
arr2[i] = arr1[i];
System.out.println("Element of array 2: " + arr2[i]);

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 {

public static void main(String args[])


{
String s = "Sir Syed University of Engineering and
Technology (SSUET), Karachi, Pakistan";
String upper ="";
String lower = "";
String other = "";

for (int i = 0; i < s.length(); i++) {


char thisChar = s.charAt(i);
if (thisChar >= 65 && thisChar <= 90) {
upper += thisChar;
} else if (thisChar >= 97 && thisChar <= 122) {
lower += thisChar;
} else {
other += thisChar;
}
}
System.out.println("Upper case letters: " + upper.length() +
" - " + upper);
System.out.println("Lower case letters: " + lower.length() +
" - " + lower);
System.out.println("Other characters: " + other.length() + "
- " + other);
}

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 {

public static void main(String args[])


{
String s = "Sir Syed University of Engineering and
Technology (SSUET), Karachi, Pakistan";
String upper ="";
String lower = "";

for (int i = 0; i < s.length(); i++) {


char thisChar = s.charAt(i);
if (thisChar >= 65 && thisChar <= 90) {
upper += thisChar;
} else if (thisChar >= 97 && thisChar <= 122) {
lower += thisChar;
}
}
int a = upper.length();
int b = lower.length();
int c = a+b;

System.out.println("Total Number of Character counts are:" +


c);

OUTPUT:

Total Number of Character counts are:63

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 {

public static void main(String args[])


{
int x,y;

for(x=0; x<6; x++)


{
for(y=x; y<6; y++)
{
System.out.print("*");

}
System.out.println();

}
}

OUPUT:
******
*****
****
***
**
*

9
Muhammad Hasan, 2009-CE-253, Section “E”

ANS 1(j2):

public class ass{

public static void main(String[] args)


{
int x,b=0,c=0,y;
for(x=1;x<=6;x++)
{
for(y=6;y>=x;y--)
{ System.out.print("*");

} 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.

Keyword Range Memory Size


Byte -128 to +127 8 Bits
Short -32,768 to + 32,767 16 Bits
Int - 2,147,483,648 to + 2,147,483,647 32 Bits
-9,223,372,036,854,775,808 to
Long 64 Bits
9,223,372,036,854,775,807
1.40129846432481707e-45 to
Float 4 Bytes
3.40282346638528860e+38
Double 4.94065645841246544e-324d to 8 Bytes

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”

Exception in thread "main" java.lang.Error: Unresolved compilation


problem:
Cannot switch on a value of type boolean. Only convertible int
values or enum constants are permitted

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 {

public static void main(String args[])


{
int a = 45;
int b = 40;

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

S-ar putea să vă placă și