Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------
- Post Title 'OOC Assignment-5 Answers'
- Post by user 'Adusumilli-Uday-Kumar'
- Post encryped with secure 256-bit MD5 Encryption
- Post accesable only VIA link
- Post will Expire on '3-OCT-2017'
- --------------------------
- Q1.
- An array is a group of like-typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more dimensions. A specific element in an array is accessed by its index. Arrays offer a convenient means of grouping related information.
- i.
- One-Dimensional Arrays
- A one-dimensional array is, essentially, a list of like-typed variables. The general form of a one-dimensional array declaration is....
- type var-name[ ];
- Here, 'type' declares the base type of the array. The following declares an array named month_days with the type “array of int”....
- int month_days[];
- To link month_days with an actual, physical array of integers, you must allocate one using 'new' and assign it to 'month_days'. 'new' is a special operator that allocates memory. The general form of new as it applies to one-dimensional arrays appears as follows....
- array-var = new type[size];
- This example allocates a 12-element array of integers and links them to month_days....
- month_days = new int[12];
- Once you have allocated an array, you can access a specific element in the array by specifying its index within square brackets. All array indexes start at zero. For example, this statement assigns the value 28 to the second element of month_days....
- month_days[1] = 28;
- The next line displays the value stored at index 3....
- System.out.println(month_days[3]);
- ii. Multidimensional Arrays
- In Java, multidimensional arrays are actually arrays of arrays. To declare a multidimensional array variable, specify each additional
- index using another set of square brackets. For example, the following declares a two-dimensional array variable called twoD....
- int twoD[][] = new int[4][5];
- This allocates a 4 by 5 array and assigns it to twoD. Internally this matrix is implemented as an array of arrays of 'int'.
- Q2.
- Java provides a rich operator environment. Most of its operators can be divided into the following four groups: arithmetic, bitwise, relational, and logical. Java also defines some additional operators that handle certain special situations. A list of all the Operators include....
- 1. Basic Arithmetic Operators
- 2. Modulus Operators
- 3. Airthmatic Coumpound Assignment Operators
- 4. Increment and Decrement Operators
- 5. Bitwise Operators
- 6. Bitwise Logical Operators
- 7. Left Shift Operators
- 8. Right Shift Operators
- 9. Unsigned Right Operators
- 10. Bitwise Operator Compound Assignments
- 11. Relational Operators
- 12. Boolean Logical Operators
- 13. Short-Circuit Logical Operators
- 14. The Assignment Operator
- 15. The Ternary Operator
- Q3.
- i. The Unsigned Right Shift Operator
- the >> operator automatically fills the high-order bit with its previous contents each time a shift occurs. This preserves the sign of the value. However, sometimes this is undesirable. In these cases, you will generally want to shift a zero into the high-order bit no matter what its initial value was. This is known as an unsigned shift. To accomplish this, you will use Java’s unsigned, shift-right operator, >>>, which always shifts zeros into the high-order bit.
- ii. The Right Shift
- The right shift operator, >>, shifts all of the bits in a value to the right a specified number of times. the >> moves all of the bits in the specified value to the right, by the number of bit positions specified to it's left.
- The following code fragment shifts the value 32 to the right by two positions, resulting in a being set to 8....
- int a = 32;
- a = a >> 2; // a now contains 8
- iii. Short-Circuit Logical Operators
- Java provides two interesting Boolean operators not found in many other computer languages. These are secondary versions of the Boolean AND and OR operators, and are known as short-circuit logical operators. For example, the following code fragment shows how you can take
- advantage of short-circuit logical evaluation to be sure that a division operation will be valid before evaluating it....
- if ((denom != 0)
Add Comment
Please, Sign In to add comment