Professional Documents
Culture Documents
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
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
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:
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");
//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;
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
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");
for(int i=0;i<myarray.length;i++){
average = average + myarray[i];
}
}
}
Result:
Answer to Question 04
//Code
public class StringSort{
public static void main(String[] args){
System.out.println("Engr.Uzam Haider");
String temp;
//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;
}
}
}
}
}
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{
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}};
//Subtraction
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{1,2,3},{4,5,6},{7,8,9}};
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;
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