You are on page 1of 18

SUKKUR IBA UNIVERSITY

MERIT – QUALITY – EXCELLENCE

Assignment 02
Object Oriented Programming (CSE-211), Spring 2022
Name: Uzam Haider CMS ID#: 033-20-0037 Instructor: Dr. Abdul Aziz
Section: B Lab group: NA Department: Electrical Engineering Marks obtained 100% out of 100%
NOTE: Submit before next lab Submitted on 25-03-2022

Assignment Rubric

Progressing to meet criteria (40~59%)


Almost meets criteria (60~79%)
Fully meets criteria (80~100 %)

Below Expectations (0~39%)


Criteria

Total out of 100%


Excellent

Average
Weight

Good

Poor
Assignment understanding, layout,
0.25 100 25
methodology, creativity and results
No guidance required and helping
0.25 100 25
group/class mates/teacher
Mentioned program number, title and
prints student name as first line with each 0.25 100 25
program
Submitted assignment on time 0.25 100 25

Obtained score 100


Submission guidelines

NOTE: All submitted program codes and screenshots of results must have first line to print
your name using “println command”, otherwise it won’t be considered.
For example:

Don’t take screenshot of code, submit it as text to be copied for verification.


Assignment 02
Part-I
1. Why does Java strictly specify the range and behavior of its primitive types?
2. What is Java’s character type, and how does it differ from the character type used by some other
programming languages?
3. A boolean value can have any value you like because any non-zero value is true. True or False?

4. Given this output,


One Two Three
using a single string, show the println( ) statement that produced it.
5. What is wrong with this fragment?
for(i = 0; i < 10; i++) { int sum; sum = sum + i; } System.out.println("Sum is: " + sum);
6. Explain the difference between the prefix and postfix forms of the increment operator.
7. Show how a short-circuit AND can be used to prevent a divide-by-zero error.
8. In an expression, what type are byte and short promoted to?
9. In general, when is a cast needed?
10. Write a program that finds all of the prime numbers between 2 and 100.
11. Does the use of redundant parentheses affect program performance?
12. Does a block define a scope?

Part-II
1. Show two ways to declare a one-dimensional array of 12 doubles.
2. Show how to initialize a one-dimensional array of integers to the values 1 through 5.
3. Write a program that uses an array to find the average of 10 double values. Use any 10 values you like.
4. Write a program to sort an array of strings. Demonstrate that it works.
5. What is the difference between the String methods indexOf( ) and lastIndexOf( )?
6. Since all strings are objects of type String, show how you can call the length( ) and charAt( ) methods on
this string literal: "I like Java".
7. Write a program to multiply two matrices.
8. Can the bitwise operators be applied to the double type?
9. Show how this sequence can be rewritten using the ? operator.
if(x < 0) y = 10; else y = 20;
10. In the following fragment, is the & a bitwise or logical operator? Why?
boolean a, b; // ... if(a & b) ...
11. Is it an error to overrun the end of an array? Is it an error to index an array with a negative value?
12. What is the unsigned right-shift operator?
13. Write a program to calculate mean, median, mode of an integer array.
14. Write a program to add or subtract two matrics.
15. Can a String control a switch statement?
16. What keyword is reserved for use with local variable type inference?
17. Show how to use local variable type inference to declare a boolean variable called done that has an initial
value of false.
18. Can var be the name of a variable? Can var be the name of a class?
19. Is the following declaration valid? If not, why not.
var[] avgTemps = new double[7];
20. Is the following declaration valid? If not, why not?
var alpha = 10, beta = 20;
Submission of Assignment 02
Part-I
Answer to Question 01
We know that Java provides us 7 primitive data types. Byte, char, short, int, long, float,
double and Boolean (True or False). So, java cares about user to manage its memory
according to its work. Like if you want to store a single digit using integer then you are
wasting your memory you can also do it with the help of byte or short.

Answer to Question 02
Java’s character type is char. It is different from other ones as it uses the format of Unicode
rather than ASCII.

Answer to Question 03
False, because either Boolean must be True or False.

Answer to Question 04

//Code
//Output Using a single string
public class MyString {
public static void main(String args[]) {
System.out.println("Engr.Uzam Haider");

String s1 = "One Two Three"; //Declaring and initializing a string

System.out.println(s1); //Printing the string


}
}
//Thanks

//Output
Answer to Question 05
There are two problems. First you had not declared the “i” within the for loop statement.
Second You declared the sum variable within the for block and expecting it to be print out
of the block how can this be possible you have to declare it with in the main program
outside of the loop and on the upper side of the for loop. Then you can get your desired
output.

Answer to Question 06
Pre means first ++i, it means first it increase the value of the variable then returns the value
and Post means after i++, first it returns the value of the variable then increment it after.
That’s what called Prefix and Postfix.

Answer to Question 07
//Code
import java.util.Scanner;

public class AND{


public static void main(String[] args){
System.out.println("Engr.Uzam Haider");

//Creating an object of Scanner


Scanner input = new Scanner(System.in);

int userinput = input.nextInt();


int val=5;

if((userinput!=0) && (val/userinput!=0)){


System.out.println("Challe ga!");
}
else{
System.out.println("Undefined");
}
}
}
Result:

Answer to Question 08
All byte and short value are promoted to int datatype if there is long then all will be in
long.

Answer to Question 09
Casting is needed whenever you want o convert one primitive data type to another.

Answer to Question 10
//Code
//To find all the prime numbers between 2 and 100
public class Primenumbers{
public static void main(String[] args){
System.out.println("Engr.Uzam Haider");
int count; //Declaring a variable count. This is use to get the prime number

System.out.println("The prime numbers between 2 and 100: ");


for(int i=2;i<=100;i++){ //starting from 2 because 0 and 1 are not prime numbers
count=0; //Initializing the value of count

for(int j=1;j<=i;j++){ //For each value of i the each value of j will be divided to
check whether the the remainder is 0 or not
if(i%j==0){ //if so increment the count
count++;
}
}
if(count==2){
System.out.print(i + " "); //Printing the value each time of i that we had checked
}
}
}
}

Answer to Question 11
No, the use of redundant parenthesis does not affect your output as there is in-between your
code and output and that is compiler it simplified your code.
Answer to Question 12
Yes, a block defines a scope. The variable inside that block will be only limited to that
block.
Part-II
Answer to Question 01
Two ways to declare an array of 12 doubles
1. double[] myarray = new double[12];
2. double myarray[] = new double[12];

Answer to Question 02
//Initializing a one dimensional array of integers from 1 to 5
Int myarray[] = {1,2,3,4,5};

Answer to Question 03
//Code
public class ArrayAverage{
public static void main(String[] args){
System.out.println("Engr.Uzam Haider");

double[] myarray = {1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.10};


double average = 0;

for(int i=0;i<myarray.length;i++){
average = average + myarray[i];
}

System.out.println("The average of 10 double values: " + average/myarray.length);

}
}

Result:
Answer to Question 04

//Code
public class StringSort{
public static void main(String[] args){
System.out.println("Engr.Uzam Haider");

String[] mystring = {"Java", "is", "a", "programming", "language"};

String temp;

System.out.println("Original array: ");


for(int i=0;i<mystring.length;i++){
System.out.println(" " + mystring[i]);
}

//Applying sorting
for(int i=1;i<mystring.length;i++){
for(int j=mystring.length-1;j>=i;j--){
if(mystring[j-1].compareTo(mystring[j])>0){
temp = mystring[j-1];
mystring[j-1] = mystring[j];
mystring[j] = temp;
}
}
}

System.out.println("Original array: ");


for(int i=0;i<mystring.length;i++){
System.out.println(" " + mystring[i]);
}

}
}
Answer to Question 05
indexOf() method finds the first occurrence of your specifies substring while the
lastIndexOf() finds the last occurrence of your specified substring.

Answer to Question 06
You can call it by
System.out.println("I like Java".length());
System.out.println("I like Java".charAt(5));

Answer to Question 07
//Code
public class MatricesMultiply{

public static void main(String args[]){


System.out.println("Engr.Uzam Haider");
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{1,2,3},{4,5,6},{7,8,9}};

//Creating another matrix to store the result


int c[][]=new int[3][3];

//multiplying the matrices


for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++){
c[i][j]+=a[i][k]*b[k][j];
}//end of k loop
System.out.print(c[i][j]+" "); //printing matrix element
}//end of j loop
System.out.println();//new line
}
}
}

Result:

Answer to Question 08
No, they can't.

Answer to Question 09
You can rewrite it as:
y = x < 0 ? 10:20; //same meaning as above.

.
.
Answer to Question 10
It is a logical operator as because it is a type of Boolean(True or False)

Answer to Question 11
Yes, it is an error to overrun an array because where it goes further?
Yes, because all array's index are starts from 0.

Answer to Question 12
>>> is called unsigned right shift operator. The left operand values moves to the right by
the specified bits.

Answer to Question 13
Sir, as there was a limited time for me because I have to analyze all the codes and to
implement so, this question after I understand it will send you tomorrow. Thanks!

Answer to Question 14

//Code
public class Add{
public static void main(String args[]){
System.out.println("Engr.Uzam Haider");

int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{1,2,3},{4,5,6},{7,8,9}};

int c[][]=new int[3][3];

/*add and print addition of m1 and m2 matrices */


for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}

//Subtraction

public class Sub{


public static void main(String args[]){
System.out.println("Engr.Uzam Haider");

int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{1,2,3},{4,5,6},{7,8,9}};

int c[][]=new int[3][3];

/*add and print addition of m1 and m2 matrices */


for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]-b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}

Answer to Question 15
Yes, it can control the switch statement.

Answer to Question 16
Using var keyword.

Answer to Question 17

//Code
public class VarDemo{
public static void main(String[] args){
System.out.println("Engr.Uzam Haider");
// int
var x = 100;

// double
var y = 1.90;

// char
var z = 'a';

// string
var p = "asghdgsahdsahd";

// boolean
var q = false;

//What it actually do it automatically detects the datatype.


System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println(p);
System.out.println(q);
}
}

Result:

Answer to Question 18

Class is not allowed as you can see, but you can use it as a variable because sir you showed
in the class.
Answer to Question 19
var detects the datatype. It itself is not a datatype so you cannot do that.

Answer to Question 20

Not allowed, you can use it separately but not as a compound.

You might also like