Header Ads Widget

Java Programming (3350703) Assignment -3

_Java Programming

(3350703) 

Assignment -3


1. Write a short note on class in java with example. 

answer:- 

A class is declared using class class keyword.

A class contain both data and code that operate on that data.

The data or variable defined within a class are called instance variables and the code that operates on this data is known as methods. 

A class describes objects that has common properties.

A class in java contains:

-Data Member
 -Method
-Constructor

Example:-

public class Student
{
    private int rollno;
    private int marks;
    private String name;
    public void run()
    {
        //Run method implementation 
    }
}


2. Write two steps to create an object. 

answer:- 

There are three steps to create an object. 

1) Declaration 
2) Instantiation 
3) Initialization

Instantiation:- 

The new keyword is used to create the object.

Syntax is :- ClassName objName = new ClassName();  

 Instantiation mean to call the constructor of a class that creates an object of the type of that class.

Initialization:- Using a call to a constructor. this call initializes the new object.

The new operator is followed by a call to a constructor, which initializes the new object.



3. Explain Access control rules of Java with example. 

answer:- 

Accessing control is the process of controlling visibility of a variable or method.

Each object has member which can be declared to have specific access.

Java has 4 level and 3 access modifiers.

Access levels are listed below in the least to most restrictive order.

Public:- Public members are available anywhere outside the class. 

Protected:- Accessible by subclases and form classes of same package.

Default:- Accessible only by classes in the same package.

Private:- Strictly controlled, which means they cannot be accessed by anywhere outside the enclosing class.

4. Explain this keyword with example. 

answer:- 

"this" keyword is used to refer to current object.

"this" keyword is always a reference to the object on which method was invoked.

"this" keyword can be used to invoke current class constructor.

"this" keyword can be passed as an argument to another method.

Example:- 

Student.java  

public class Student
{
    static 
   {
        int rollno;
        Student(int rollno)
        {
            this.rollno = rollno;
        }
        public static void main(String[] args)
        {
            Student s1=new Student(34);
            system.out.println(s1.rollno);
        }
   }


5. Explain static variable, method and block in java. 

answer:- 

Static variable:-

Instance variables declared as static are, essentially, global variables.

When objects of its class are declared, no copy of a static variable is created.

Data stored in static variables is common for all the objects of that class.

Static Method:-

Static Methods can access class variables without using object of the class.

It can access non-static methods and non-static variables by using objects.

Static methods can be accessed directly in static and non-static methods.

Static Block:-

Static Block gets executed when the class is loaded in the memory.

A class can have multiple Static Blocks, which will execute in the same sequence in which they have been written into the program.

Static block is a set of statements, which will be executed by the JVM before execution of main method.


6. Explain final variable, method and class in java. 

answer:- 

Final keyword is used to make data, method or class as final.

it means it can not be modified further in the program.

final at variable level:-

Final keyword is used to make a variable as a constant.

A variable declared with the final keyword cannot be modified by the program after initialization.

final at method level:-

It makes a method final, means sub classes can not override this method.

The compiler checks and gives an error if you try to override the method.

final at class level:-

It makes a class final, meaning that the class can not be inheriting by other class.

When we want to restrict inheritance then make class as a final.


7. Explain method overloading with example. 

answer:- 

If two or more methods in a class has same name but different parameters, it is known as  method overloading.

Method overloading is a type of polymorphism.

If two or more method have same name and same parameter list but differs in return type are not said to be overloaded method.

Example:-

class Adder
{
    static int add(int a,int b){return a+b;}
    static int add(int a,int b,int c){return a+b+c;}
}
class Student
{
    public static void main(String[] args)
    {
        system.out.println(Adder.add(11,11));
        system.out.println(Adder.add(11,11,11));
    }
}


8. What is constructor? Write its properties. List different types of constructor. 

answer:- 

A constructor initializes an object when it is created. 
It has the same name as its class and do not have any return type.
All classes have constructors, whether you define one or not, because Java automatically provides a default constructor that initializes all member variables to zero. 
However, once you define your own constructor, the default constructor is no longer used. 
A constructor is used to initialize a new object.
Constructor is automatically called immediately when the object is created using "new" key-word.
 Constructors are not methods, they aren't inherited.
There are Three Types Of Constructors:-

-Default Constructor
-Parameterized Constructor
-Copy Constructor


9. Explain default constructor with example. 

answer:- 

If you don't define any constructor in class, Java will generate a constructor by default 

which is called as Default Constructor.

Example:-

Student.java

class StudentDemo
{
    int rollno;
    StudentDemo()
   {
    system.out.println("I an a default constructor");
    rollno=10;
    marks=100;
    system.out.println("value of rollno is :"+rollno);
    system.out.println("value of marks is: "+marks);
    }
};
class Student
{
    public static void main(String[] args)
    {
        StudentDemo s=new StudentDemo ();
    }
}






10. Explain parameterized constructor with example. 

answer:- 

The constructors with arguments is known as parameterized constructor.

Example:-

Student.java 


public class Student
{}
    public Student (String str)
    {
        System.out.println("Hello:" + str);
    }
};


Student2.java


public class Student2
{
    public static void main(String[] args)
    {
        Student s1 = new Student("Students");
    }
};


11. Explain copy constructor with example. 

answer:- 

There are many ways to copy the values of one object into another. They are:

-By constructor
-By assigning the values of one object into another 
-By clone () method of Object class

A copy constructor is used to initialize an object from another object, Copy constructor takes object as argument.

Example: 

Student.java

class Student
{
    int rollno;
    String name;
    Student(int i,String n)
    {
        rollno = i;
        name = n;
    }
    Student(Student s)
    {
        rollno s.rollno;
        name =s.name;
    }
    void display (System.out.println(rollno+" "+name);} 
    public static void main(String args[])
    {
        Student s1 = new Student(61,"Krunal");
        Student s2 = new Student(s1); //Copy Constructor invoked
        s1.display();
        $2.display();
    }
}



12. What is constructor overloading? Demonstrate with example. 

answer:- 

Constructors can also be overloaded.

Overloaded constructor are differentiated on the basis of their type of parameters or number of parameters.

Whereas in Constructor overloading you have multiple constructor with different signature but only difference is that constructor doesn't have return type in java .

Example:

class Student1
{
    String name;
    int rollno;
    Student1()
    {
        name = "";
        rollno = 0;
    }
    Student1 (String nint a)
    {
        name = n;
        rollno = a;
    }
    Student1 (Student1 s1)
    {
        name=s1.name;
        rollno=s1.rollno;
    }
    public String toString()
    {   
        return "this is " + name + " of "+rollno;
    }
}
class Student
{
    public static void main (String[] args)
    {
        Student1 s1new Student1(0);
        Student1 s2 = new Student1 ("parth"32);
        Student1 s3 = new Studentl (s2);
        System.out.println(s2); 
        System.out.println(s3);
        s1.name = "Virat";
        s1.rollno = 23;
        System.out.println(s1);
    }
}


13. How an object can be passed as parameter? 

answer:- 

We can also pass object as a parameter to method in Java programming.

 We learn Passing object as parameter conepts by below example:

 Example of Passing Object as Parameter

Student.java
class Student Demo
{
    int rollno;
    Student Demo(int i)
    {
        rollno = i;
    }
    boolean equals (StudentDemo s)
    {

        if (rollno == s.rollno)
        return true;
        else
        return false;
    }
}
public class Student
{
    public static void main(String args[]) 
    {
        Student Demo s1 = new StudentDemo(10);
        Student Demo s2 = new StudentDemo(10);
        StudentDemo s3 = new StudentDemo(20);
        System.out.println("s1 == $2:"+s1.equals($2)); 
        System.out.println("s1 == s3:"+s1.equals(s3));
    }
}



14. Write a program in JAVA that implements a class with instance variable and method as class member. 

answer:- 




15. Write a program in JAVA that implements the class Box with attributes length, width, height and calculate the volume of the Box using class method. 

answer:- 




16. Write a program in JAVA that implements the class Box with attributes length, width, height and method setdata to assign the value to these attributes and also calculate the volume of the Box using class method volume.

answer:- 

public class Box {
    double h,w,d;

     Box(double width,double height,double depth)
    {
        h=height;
        w=width;
        d=depth;
    }
    double volume()
    { double v;
    v=h*w*d;
    return v;
    }
   
    public static void main(String[] args) {
       
    Box bc = new Box(8.5,80.3,9.6);
    System.out.println(bc.volume());

    }

}


Post a Comment

2 Comments

  1. Replies
    1. The answer may be wrong so that why i not uploaded that
      apology for that.

      Delete