Java find output programs (Class and Objects) | set 2

Find the output of Java programs | Class and Objects | Set 2: Enhance the knowledge of Java Class and Objects concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 02, 2021

Question 1:

class Sample {
  int var1 = 20;
  int var2 = 40;

  public void set(int v1, int v2) {
    var1 = v1;
    var2 = v2;
  }

  public void print() {
    System.out.println(var1 + "," + var2);
  }
}

public class ClassEx {
  public static void main(String[] args) {
    new Sample().print();
  }
}

Output:

20,40

Explanation:

In the above program, we created a Sample class with two default integer data members initialized with 20 and 40 respectively. We also created two methods set() and print() in the Sample class.

Now look to the main() method, here we created an anonymous object to call the print() method that will print values of data members on the console screen.

Question 2:

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

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

  public void printInfo() {
    System.out.println(sid + "," + name + "," + fees);
  }
}

public class ClassEx {
  public static void main(String[] args) {
    Student S = new Student();

    S.setInfo(101, "Amit", 10000);
    S.printInfo();
  }
}

Output:

101,Amit,10000

Explanation:

In the above program, we created a Student class that contains data member id, name, and fees and we also created two methods setInfo() and printInfo().

The setInfo() is used to set the data members of Student class and the printInfo() method is used to print the information of the Student class.

Now look to the main() method. Here, we created object S of Student class then we set student information using the setInfo() method and then print the values of data members of Student class on the console screen.

Question 3:

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

  public void setInfo(int id, String na, int fee);
  public void printInfo();
}

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

public void Student::printInfo() {
  System.out.println(sid + "," + name + "," + fees);
}

public class ClassEx {
  public static void main(String[] args) {
    Student S = new Student();

    S.setInfo(101, "Amit", 10000);
    S.printInfo();
  }
}

Output:

/ClassEx.java:10: error: class, interface, or enum expected
public void Student::setInfo(int id, String na, int fee) {
       ^
/ClassEx.java:12: error: class, interface, or enum expected
  name = na;
  ^
/ClassEx.java:13: error: class, interface, or enum expected
  fees = fee;
  ^
/ClassEx.java:14: error: class, interface, or enum expected
}
^
/ClassEx.java:16: error: class, interface, or enum expected
public void Student::printInfo() {
       ^
/ClassEx.java:18: error: class, interface, or enum expected
}
^
6 errors

Explanation:

The above program will generate compile-time errors because we cannot define the methods of a class outside the class using '::' operator in Java.

Question 4:

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

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

  public void printInfo() {
    System.out.println(sid + "," + name + "," + fees);
  }
}

public class ClassEx {
  public static void main(String[] args) {
    Student S = new Student();

    S.setInfo(101, "Amit", 10000);
    S.printInfo();
  }
}

Output:

/ClassEx.java:1: error: class, interface, or enum expected
const class Student {
^
1 error

Explanation:

The above program will generate syntax error because we used the const keyword to declare the Student class. We cannot use the const keyword to declare the class in Java.

Question 5:

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

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

  public void printInfo() {
    System.out.println(sid + "," + name + "," + fees);
  }
}

public class ClassEx {
  public static void main(String[] args) {
    Student S = new Student();

    S.setInfo(101, "Amit", 10000);
    S.printInfo();
  }
}

Output:

101,Amit,10000

Explanation:

In the above program, we created a Student class with the final keyword that contains data member id, name, and fees and we also created two methods setInfo() and printInfo().

The setInfo() is used to set the data members of the Student class and the printInfo() method is used to print the information of the Student class.

Now look to the main() method. Here, we created object S of Student class then we set student information using the setInfo() method and then print the values of data members of Student class on the console screen.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.