ADVERTISEMENT
ADVERTISEMENT

Java find output programs (Constructor & Destructor) | set 2

Find the output of Java programs | Constructor & Destructor | Set 2: Enhance the knowledge of Java Constructor & Destructor concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 03, 2021

Question 1:

class Student {
  private int sid;
  private String name;
  private int fees;

  Student() {
    sid = 101;
    name = "virat";
    fees = 8000;
  }

  Student(int sid, String name, int fees) {
    sid = sid;
    name = name;
    fees = fees;
  }

  void printInfo() {
    System.out.println("S-ID: " + sid);
    System.out.println("Name: " + name);
    System.out.println("Fees: " + fees);
  }
}

public class CtorEx {
  public static void main(String[] args) {
    Student S1 = new Student();
    Student S2 = new Student(102, "Rohit", 5000);

    S1.printInfo();
    S2.printInfo();
  }
}

Output:

S-ID: 101
Name: virat
Fees: 8000
S-ID: 0
Name: null
Fees: 0

Explanation:

In the above program, we created a class Student that contains data member sid, name, and fees. Here, we defined a default constructor and a parameterized constructor to initialize the data members with specified values, and we also defined a printInfo() method to print the values of data members.

The default constructor initialized data member with 101, "virat", and 8000 respectively.

Student(int sid, String name, int fees)
{
    sid  = sid;
    name = name;
    fees = fees;
}

In the parameterized constructor we tried initialized data members but we used local parameters with the same name that's why here data members were not initialized because sid, name, and fees are local variables not data members inside the parameterized constructor.

Question 2:

class Student {
  private int sid;
  private String name;
  private int fees;

  Student() {
    sid = 101;
    name = "virat";
    fees = 8000;
  }

  Student(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public setInfo(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void printInfo() {
    System.out.println("S-ID: " + sid);
    System.out.println("Name: " + name);
    System.out.println("Fees: " + fees);
  }
}

public class CtorEx {
  public static void main(String[] args) {
    Student S1 = new Student();
    Student S2 = new Student(102, "Rohit", 5000);
    Student S3 = new Student();

    S3.setInfo(103, "Rahul", 6000);

    S1.printInfo();
    S2.printInfo();
    S3.printInfo();
  }
}

Output:

/CtorEx.java:18: error: invalid method declaration; return type required
  public setInfo(int id, String na, int fee) {
         ^
1 error

Explanation:

The above program will generate syntax error because the setInfo() method of the Student class does not have a return type.

Question 3:

class Student {
  private int sid;
  private String name;
  private int fees;

  Student() {
    sid = 101;
    name = "virat";
    fees = 8000;
  }

  Student(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void setInfo(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void printInfo() {
    System.out.println("S-ID: " + sid);
    System.out.println("Name: " + name);
    System.out.println("Fees: " + fees);
  }
}

public class CtorEx {
  public static void main(String[] args) {
    Student S1 = new Student();
    Student S2 = new Student(102, "Rohit", 5000);
    Student S3 = new Student();

    S3.setInfo(103, "Rahul", 6000);

    S1.printInfo();
    S2.printInfo();
    S3.printInfo();
  }
}

Output:

S-ID: 101
Name: virat
Fees: 8000
S-ID: 102
Name: Rohit
Fees: 5000
S-ID: 103
Name: Rahul
Fees: 6000

Explanation:

In the above program, we created a class Student that contains data member sid, name, and fees. Here, we defined default and parameterized constructor to initialize the data members, and we also defined setInfo() and printInfo() method to set and print the values of data members.

Now look to the CtorEx class, the CtorEx class contains the main() method, which is the entry program of the program. Here, we created objects S1 and S2. Object S1 initialized using the default constructor and S2 initialized using the parameterized constructor.

Student S3 = new Student();

We set values of S3 object using the setInfo() method and then print the student details using the printInfo() method on the console screen.

Question 4:

class Student {
  private int sid;
  private String name;
  private int fees;

  Student() {
    sid = 101;
    name = "virat";
    fees = 8000;
  }

  Student(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void setInfo(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void printInfo() {
    System.out.println("S-ID: " + sid);
    System.out.println("Name: " + name);
    System.out.println("Fees: " + fees + "\n");
  }
}

public class CtorEx {
  public static void main(String[] args) {
    Student S1 = new Student();
    Student S2 = new Student(102, "Rohit", 5000);
    Student S3 = S1;

    S3.setInfo(103, "Rahul", 6000);

    S1.printInfo();
    S2.printInfo();
    S3.printInfo();
  }
}

Output:

S-ID: 103
Name: Rahul
Fees: 6000

S-ID: 102
Name: Rohit
Fees: 5000

S-ID: 103
Name: Rahul
Fees: 6000

Explanation:

In the above program, we created a class Student that contains data member sid, name, and fees. Here, we defined default and parameterized constructor to initialize the data members, and we also defined setInfo() and printInfo() method to set and print the values of data members.

Now look to the CtorEx class, the CtorEx class contains the main() method, which is the entry program of the program. Here, we created objects S1 and S2. Object S1 initialized using the default constructor and S2 initialized using the parameterized constructor.

Here, we assigned the reference of an S1 object to the S3, then S1 and S3 will point the same object. Then we set the values of S3 object using the setInfo() method, these new values will be reflected in both S1 and S3.

Then print the student detail for all objects using the printInfo() method on the console screen.

Question 5:

class Student {
  private int sid;
  private String name;
  private int fees;

  Student() {
    sid = 101;
    name = "virat";
    fees = 8000;
  }

  Student(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void setInfo(int id, String na, int fee) {
    sid = id;
    name = na;
    fees = fee;
  }

  public void printInfo() {
    System.out.println("S-ID: " + sid);
    System.out.println("Name: " + name);
    System.out.println("Fees: " + fees + "\n");
  }
}

public class CtorEx {
  public static void main(String[] args) {
    new Student().printInfo();
    new Student(102, "Rohit", 5000).printInfo();
  }
}

Output:

S-ID: 101
Name: virat
Fees: 8000

S-ID: 102
Name: Rohit
Fees: 5000

Explanation:

In the above program, we created a class Student that contains data member sid, name, and fees. Here, we defined default and parameterized constructor to initialize the data members, and we also defined setInfo() and printInfo() method to set and print the values of data members.

Now look to the CtorEx class, the CtorEx class contains the main() method, which is the entry program of the program. Here, we created two anonymous objects and call the printInfo() method that will print student details on the console screen.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.