Beruflich Dokumente
Kultur Dokumente
Max Marks: 20
10
4. Syntax:
public static void arraycopy(Object s, int spos, Object d, int dpos, int length)
s : Source Array
d : Destination Array
This method returns no values.
5. Example Program:
public class SystemDemo {
1
public static void main(String[] args) {
int arr1[] = { 0, 1, 2, 3, 4, 5 };
int arr2[] = { 5, 10, 20, 30, 40, 50 };
System.arraycopy(arr1, 0, arr2, 0, 1); // copies an array from the
System.out.print("array2 = ");
// specified source array
3. Any machine with Virtual Machine installed on it, can run Java Programs.
4. Java Runtime Environment has Just in Time (JIT) Compiler for translation of
bytecode to native machine code. This makes execution of java code with high
performance.
An array of objects is created just like an array of primitive type data items in the
following way.
Student[] studentArray = new Student[7];
The above statement creates the array which can hold references to seven Student
objects. It doesn't create the Student objects themselves. They have to be created
separately using the constructor of the Student class.
The studentArray contains seven memory spaces in which the address of seven
Student objects may be stored. If we try to access the Student objects even before
creating them, run time errors would occur. For instance, the following statement
throws a NullPointerException during runtime which indicates that studentArray[0]
isn't yet pointing to a Student object.
studentArray[0].marks = 100;
The Student objects have to be instantiated using the constructor of the Student
class and their references should be assigned to the array elements in the following
way.
studentArray[0] = new Student();
The above for loop creates seven Student objects and assigns their reference to the
array elements. Now, a statement like the following would be valid.
studentArray[0].marks=100;
3. To pass three argument from command line to main function of Add class the
command is given as java Add 1 2 3
4. After executing the above command an string array of size 3 is created and
given arguments 1,2,3 are stored in args[0],args[1],args[2] respectively.
5.
Example:
public class Add {
public static void main(String[] args) {
int sum = 0;
4. class Calculation{
void sum(int a,int b){
System.out.println(a+b);
}
void sum(int a,int b,int c){
System.out.println(a+b+c);
}
}
5. If subclass (child class) has the same method as declared in the parent class, it
is known as method overriding in java.
6. Method overriding is used to provide specific implementation of a method that
is already provided by its super class.
7. Method overriding is used for runtime polymorphism
8. Rules for method overloading
a. method must have same name as in the parent class
b. Method must have same parameter as in the parent class.
1.
class Student{
int rollno;
String name;
static String college = "ITS";
2.
static void change(){
college = "BBDIT";
}
}
Any One
A
//Parameterized Constructor
a=x;
}
}
class B extends A
{
private int b,m;
B(int x, int y)
{
super(x);
b=y;
m=super.c;
}
void display()
//Subclass
//Parameterized Constructor
3
{
System.out.println("a="+a+" b="+b+"c="+m);
}
}
class Main
{
public static void main(String args[])
{
B objb = new B(4,5);
objb.display();
}
}
OR
B
Write a program to display area of square, rectangle and circle using abstract class.
Import java.util.Scanner;
abstract class Shape
{
public static final float PI = 3.14f;
area=l*b;
}
void display()
{
System.out.println("Area of Rectangle ="+area));
}
}
class Circle extends Shape
{
float r;
Circle(float r)
{
this.r=r;
}
void calculateArea()
{
area=PI*(float)Math.pow(r,2);
}
void display()
{
System.out.println("Area of Circle ="+area);
}
}
class Square extends Shape
{
float s;
Square(float s)
{
this.s=s;
}
void calculateArea()
{
area=(float)Math.pow(s,2);
}
void display()
{
System.out.println("Area of Square ="+area));
} }
class CalculateArea
{
public static void main(String args[])
{
System.out.println(Select any one option);
System.out.println( 1. Circle \n 2. Square \n 3. Rectangle);
Scanner sc = new Scanner(System.in);
int ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println(Enter Radius of circle);
float r=sc.nextFloat();
Circle c=newCircle(r);
c.claculateArea();
c.display();
break;
case 2:
System.out.println(Enter side of Square);
float s=sc.nextFloat();
Square sq=new Square(s);
sq.claculateArea();
sq.display();
break;
case 3:
System.out.println(Enter two sides of Rectangle);
float l=sc.nextFloat();
float b=sc.nextFloat();
Rectangle r=new Rectangle(l,b);
r.claculateArea();
r.display();
break;
default:
Syastem.out.println(Invalid Choice);
}
}
}
3
Any one
A
Draw class diagram for following scenario, a bank maintains two kinds of accounts
for its customers, saving and current account. Saving account provides withdrawal
and compound interest on deposit facility. Current account provides facility to
issue cheque book but no interest on deposit.
OR
B
Draw class diagram for following scenario, Library maintains books and
magazines. Student can issue a book or return a book. Fine is charged if a book is
returned after 8 days. The magazines are not issued, but student can read it in
library.