java exp 13 to 19
java exp 13 to 19
*;
public class MultidimensionalArray
{
static int N = 4;
static void mul(int a[][],int b[][], int c[][])
{
int i, j, k;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
c[i][j] = 0;
for (k = 0; k < N; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
}
public static void main (String[] args)
{
int a[][] = { {9, 7, 2, 3},{9, 7, 2, 3},{9, 7, 2, 3},{9, 7, 2, 3}};
int b[][] = {{ 9, 7, 2, 3}, {9, 7, 2, 3},{9, 7, 2, 3},{9, 7, 2, 3}};
int c[][] = new int[N][N] ;
int i, j;
mul(a, b, c);
System.out.println("First Matrix is : \n ");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
System.out.print( a[i][j] + " ");
System.out.println();
}
System.out.println("Second Matrix is: \n ");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
System.out.print( b[i][j] + " ");
System.out.println();
}
System.out.println("Multiplication result matrix is \n");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
System.out.print( c[i][j] + " ");
System.out.println();
}
}
}
Ans 1:
Instantiating an Array in Java
• When an array is declared, only a reference of array is created.
• To allocates memory to array, new keyword is used.
• Syntax:
array-var-name = new type [size];
• Here,
type specifies the type of data being allocated,
size specifies the number of elements in the array,
var-name is the name of array variable that is linked to the array.
• Example:
int intArray[]; //declaring array
intArray = new int[20]; // allocating memory to array
OR
int[] intArray = new int[20]; // combining both statements in one
Ans 2:
• Second dimension in 2D-array is optional in Java.
• We can create a two dimensional array without specifying second dimension e.g.
int[4][] is valid array declaration.
• The scenario where you want every row to a different number of columns, in such
cases this type of array declaration is useful.
• A multi-dimensional array is an array of array.
• For example two dimensional array in Java is simply an array of one dimensional
array like int[][] is an array of array of int[] or "array of array of int".
• This diagram shows how exactly two dimensional arrays are stored in Java :
• Example:
class Demo
{
public static void main(String[] args)
{
int a[][] = new int[2][];
a[0] = new int[2];
a[1] = new int[4];
}
}
Ans 3:
Ans 4:
class ArrayExample
{
public static void main(String[] args)
{
int[] age = new int[5];
System.out.println(age[0]);
System.out.println(age[1]);
System.out.println(age[2]);
System.out.println(age[3]);
System.out.println(age[4]);
System.out.println(age[5]);
}
}
• Line #11. Save, Compile & Run the code, then an exception will raised by java
compiler saying ware trying to access array index which is out of bound, So when
we try to access array element out of index java throws exception.
• Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
ArrayDemo.main(ArrayExample.java:11)
Ans 3:
Ans1:
• capacity() method gives the capacity of the vector, means it returns the maximum
number of element a vector can hold.
• size() method returns the number of elements currently hold by vector.
Ans 2:
• addElement() method adds specified element at the end of the vector also
increasing its size by 1.
• insertElementAt() method inserts the specified element at specified position and
moves all the elements one step further.
Ans 3:
import java.util.*;
class VectorClassMethods
{
public static void main(String[] arg)
{
Vector v = new Vector(); // capacity-->10
System.out.println("\n Capacity of Vector v is " + v.capacity());
System.out.println("\n Size of Vector v is " + v.size());
v.add(new Integer(1)); //v=[1]
v.add(2); //v[1,2]
v.add(1,"thakur"); //v=[1,"thakur",2]
v.add(2,"Polytechnic"); //v=[1,"thakur","Polytechnic",2]
v.add(new Integer(1)); //v=[1,"thakur","Polytechnic",2,1]
v.add(2); //v=[1,"thakur","Polytechnic",2,1,2]
System.out.println("\n Capacity of Vector v is " + v.capacity());
System.out.println("\n Size of Vector v is " + v.size());
System.out.println("\n Elements of Vector v are : " + v);
v.add(1,"thakur"); //v=[1,"thakur","thakur","Polytechnic",2,1,2]
v.add(2,"Polytechnic"); //v=[1,"thakur","Polytechnic","thakur",
"Polytechnic",2,1,2]
v.add(new Integer(1)); //v=[1,"thakur","Polytechnic","thakur",
"Polytechnic",2,1,2,1]
v.add(2); //v=[1,"thakur","Polytechnic","thakur",
"Polytechnic",2,1,2,1,2]
v.add(22); //v=[1,"thakur","Polytechnic","thakur",
"Polytechnic",2,1,2,1,2,22]
//v.addElement(new Character('A'));
System.out.println("\n Vector v is " + v);
v.setElementAt(1,0);
System.out.println("\n Vector v is after adding 1 at index 0 :" + v);
//v=[1,"Polytechnic","thakur", "Polytechnic",2,1,2,1,2,22]
System.out.println("\n Is vector v contains elemnent 2? :" +v.contains(2)); //true
// parseInt(): return primitive int value an overloaded method takes radix as well
int zz = Integer.parseInt(bb);
System.out.println("parseInt(bb) = " + zz);
zz = Integer.parseInt(bb, 6);
System.out.println("parseInt(bb,6) = " + zz);
// decode() : decodes the hex,octal and decimal string to corresponding int values.
String decimal = "45";
String octal = "005";
String hex = "0x0f";
System.out.println("decode(45) = " + Integer.decode(decimal));
System.out.println("decode(005) = " + Integer.decode(octal));
System.out.println("decode(0x0f) = " + Integer.decode(hex));
Ans 1:
1)valueOf() method :
• We can use valueOf() method to create Wrapper object for given primitive or String.
• There are 3 types of valueOf() methods:
1) Wrapper valueOf(String s) :
2) Not supported by Character wrapper class.
Syntax: public static Wrapper valueOf(String s);
2) Wrapper valueOf(String s, int radix) :
• All Wrapper class supports ValueOf() method to create wrapper object for the given
String with specified radix (radix is 2 to 36).
Syntax: public static Wrapper valueOf(String s, int radix);
3) Wrapper valueOf(primitive p) :
• All Wrapper class supports ValueOf() method, used to create a Wrapper object for
the given primitive type.
Syntax: public static Wrapper valueOf(primitive p);
Ans 2:
xxxValue():Converts the value of this Number object to the xxx data type and returns it.
1. byte byteValue()
2. short shortValue()
3. int intValue()
4. long longValue()
5. float floatValue()
6. double doubleValue()
Ans 3:
boolean Boolean
byte Byte
char Character
int Integer
float Float
double Double
long Long
short Short
Ans 1:
class StringToInteger
{
public static void main(String[] args)
{
Integer I = Integer.valueOf("10");
System.out.println(I);
Double D = Double.valueOf("10.0");
System.out.println(D);
Integer I1 = Integer.valueOf("1111", 2); // 8 + 4 + 2 +1=15
System.out.println(I1);
Integer I2 = Integer.valueOf("1111", 4);//
System.out.println(I2);
}
}
Ans 2:
Character Wrapper Class Methods:
• boolean isLetter(char ch) : This method is used to determine whether the specified
char value(ch) is a letter or not.
• boolean isDigit(char ch) : This method is used to determine whether the specified
char value(ch) is a digit or not.
• boolean isWhitespace(char ch) : It determines whether the specified char value(ch)
is white space. A whitespace includes space, tab, or new line.
• boolean isUpperCase(char ch) : It determines whether the specified char value(ch) is
uppercase or not.
• boolean isLowerCase(char ch) : It determines whether the specified char value(ch)
is lowercase or not.
• char toUpperCase(char ch) : It returns the uppercase of the specified char value(ch).
If an ASCII value is passed, then the ASCII value of it’s uppercase will be returned.
• char toLowerCase(char ch) : It returns the lowercase of the specified char value(ch)
System.out.println("\nisDigit('A'):"+Character.isDigit('A'));
System.out.println("isDigit('0'):"+Character.isDigit('0'));
System.out.println("\nisWhitespace('A'):"+Character.isWhitespace('A'));
System.out.println("isWhitespace(' '):"+Character.isWhitespace(' '));
System.out.println("isWhitespace('\\n'):"+Character.isWhitespace('\n'));
System.out.println("isWhitespace('\t'):"+Character.isWhitespace('\t'));
System.out.println("\nisWhitespace(9):"+Character.isWhitespace(9));
System.out.println("isWhitespace(9):"+Character.isWhitespace('9'));
System.out.println("\nisUpperCase('a'):"+Character.isUpperCase('A'));
System.out.println("isUpperCase('a'):"+Character.isUpperCase('a'));
System.out.println("\nisLowerCase('a'):"+Character.isLowerCase('A'));
System.out.println("isLowerCase('a'):"+Character.isLowerCase('a'));
System.out.println("\ntoUpperCase(97):"+Character.toUpperCase('a'));
System.out.println("toUpperCase(97):"+Character.toUpperCase(97));
System.out.println("\noLowerCase('A'):"+Character.toLowerCase('A'));
System.out.println("toLowerCase('65'):"+Character.toLowerCase(65));
}
}
Ans 3:
class IntegerObjectToPrimitive
{
public static void main(String[] args)
{
Integer I = new Integer(130);
System.out.println(I.byteValue());
System.out.println(I.shortValue());
System.out.println(I.intValue());
System.out.println(I.longValue());
System.out.println(I.floatValue());
System.out.println(I.doubleValue());
}
}
class Base
{
public void display()
{
System.out.println("Base Class");
}
}
class Derived extends Base
{
public void display()
{
System.out.println("Derived Class");
}
public static void main( String args[])
{
Base b = new Base();
System.out.println("______________________________________________\n");
System.out.println("Base class object is stored in Base Class reference variable:");
b.display();
System.out.println("________________________________________\n");
System.out.println("Derived class object is stored in Base Class reference variable:");
Base b1 = new Derived();
b1.display();
System.out.println("______________________________________\n");
}
}
Ans 1:
Method overloading is
Type of a compile time Method overriding is a run
2
polymorphism polymorphism. time polymorphism.
Method overloading
5 Inheritance may or may not require While method overriding
inheritance. always needs inheritance.
Private/Static/Final
6 Can be overloaded. Cannot be overridden.
method
Ans 4:
Super Keyword in Java
1. Use of super with variables:
• When a derived class and base class has data members with same name.
• In example, both base class and subclass have a member maxSpeed. We could
access maxSpeed of base class in subclass using super keyword.
class Vehicle {
int maxSpeed = 120;
}
class Car extends Vehicle {
int maxSpeed = 180;
void display()
{
System.out.println("Maximum Speed: "
+ super.maxSpeed);
}
}
class Test {
public static void main(String[] args)
{
Car small = new Car();
small.display();
}
}
class SBI
{
void rateOfInterest(float roi)
{
System.out.println("Rate Of Interest (SBI):"+roi);
}
}
class SBM extends SBI
{
void rateOfInterest(float roi)
{
System.out.println("Rate Of Interest (SBM):"+roi);
}
public static void main(String[] args)
{
SBI s=new SBI();
s.rateOfInterest(6.5f);
SBI s1=new SBM();
s1.rateOfInterest(7.5f);
}
}
Ans 3:
class Animal
{
void move()
{
System.out.println("Animal:Method");
}
}
class one
{
public void print_thakur()
{
System.out.println("Thakur");
}
}
class two extends one
{
public void print_poly()
{
System.out.println("Polytechnic");
}
}
class three extends two
{
public void print_kandivli()
{
System.out.println("Kandivli");
}
}
public class Main
{
public static void main(String[] args)
{
three t = new three();
t.print_thakur();
t.print_poly();
t.print_kandivli();
}
}
Ans 1:
• When one class extends more than one classes then this is called multiple inheritance.
• For example: Class C extends class A and B then this type of inheritance is known as
multiple inheritance. Java doesn’t allow multiple inheritance.
• Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it.
• One of the example of such problem is the diamond problem that occurs in multiple
inheritance.
Ans 2:
Conditions when can we use super keyword:
• When a derived class and base class has data members with same name.
• Whenever a parent and child class have same named methods then to resolve
ambiguity we use super keyword.
• super keyword can also be used to access the parent class constructor.
Ans 3:
Final variables
• When a variable is declared with final keyword, its value can’t be modified,
essentially, a constant.
• This also means that you must initialize a final variable.
• Example:
final int THRESHOLD = 5;
Ans 4:
1. It can have abstract methods(methods without body) as well as concrete methods
(regular methods with body).
2. A normal class(non-abstract class) cannot have abstract methods.
3. We can not create object of it abstract class as these classes are incomplete, they
have abstract methods that have no body so java wont allows you to create object of
abstract class.
4. It can have final methods.
5. It can have constructor and static methods also.
Ans 1:
import java.util.Scanner;
class student
{
int rollno;
String name;
}
Ans 2:
import java.util.Scanner;
class RoomDimensions
{
float length;
float width;
float height;
}
class AreaVolume extends RoomDimensions
{
void getDimensions()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter Length , Width and Height of Room (in feets):\t");
length=in.nextFloat();
width=in.nextFloat();
height=in.nextFloat();
}
void displayVolumeArea()
{
System.out.println("Volume of Room="+length*width*height);
System.out.println("Area of Room="+ (2*length*width + 2*width*height +
2*height*length));
}
public static void main(String[] args)
{
AreaVolume av=new AreaVolume();
av.getDimensions();
av.displayVolumeArea();
}
}
Multiple inheritance Using interface:
• Multiple inheritance is not supported in the case of class because of ambiguity.
• However, it is supported in case of an interface because there is no ambiguity.
• It is because its implementation is provided by the implementating class.
• If a class implements multiple interfaces, or an interface extends multiple
interfaces, it is known as multiple inheritance.
Ans 2:
Ans 3:
Constructor A class can have constructor Interface can not have a constructor.
9 methods.
Ans 2:
interface Area
{
public int areaOfRectangle(int l, int b);
public float areaOfCircle(float r);
}
public class AreaOfShape implements Area
{
public int areaOfRectangle(int l, int b)
{
return l*b;
}
public float areaOfCircle(float r)
{
return 3.14f*r*r ;
}
public static void main(String args[])
{
AreaOfShape obj = new AreaOfShape();
System.out.println("Area Of Rectangle:"+obj.areaOfRectangle(2,4));
AreaOfShape obj1 = new AreaOfShape();
System.out.println("Area Of Circle:"+obj.areaOfCircle(2.5f));
}
}
nextFloat() reads a float value form the user
nextInt()