Java Week 2:Q1 To call the method print() in class Student following the concept of inner class.
// This is the outer class named School
class School {
// This is the inner class named Student
class Student {
// This is a method in inner class Student
public void print() {
System.out.println("Hi! I am inner class STUDENT of outer class SCHOOL.");
}
}
}
public class Question211{
public static void main(String[] args) {
// Create an object of inner class Student
School.Student s1= new School().new Student();
// Access the 'print()' method of the inner class Student using the inner class object
s1.print();
}
}Java Week 2:Q2 To call the method print() of class Student first and then call print() method of class School.
// This is the class named School
class School {
// This is a method in class School
public void print() {
System.out.println("Hi! I class SCHOOL.");
}
}
// This is the class named Student
class Student {
// This is a method in class Student
public void print() {
System.out.println("Hi! I am class STUDENT");
}
}
public class Question212{
public static void main(String[] args) {
// Create an object of class Student
Student stu = new Student();
// Call 'print()' method of class Student
stu.print();
// Create an object of class School
School sch = new School();
// Call 'print()' method of class School
sch.print();
}
}Java Week 2:Q3 To call print() method of class Question by creating a method named ‘studentMethod()’.
// This is the main class Question
public class Question213{
public static void main(String[] args) {
// Object of the main class is created
Question213 q = new Question213();
// Print method on object of Question class is called
q.studentMethod();
}
// 'print()' method is defined in class Question
void print(Question213 object){
System.out.print("Well Done!");
}
// Define a method named 'studentMethod()' in class Question
void studentMethod(){
// Call the method named 'print()' in class Question
Question213 q = new Question213();
q.print(q);
}
}Java Week 2:Q4 To call default constructor first and then any other constructor in the class Answer.
// This is the main class Question
public class Question214{
public static void main(String[] args){
Answer a = new Answer(10,"MCQ");
}
}
class Answer{
Answer(){
System.out.println("You got nothing.");
}
Answer(int marks, String type){
this();
System.out.println("You got "+marks+" for an "+ type);
}
} Java Week 2:Q5 To debug the program which is intended to print 'NPTEL JAVA'.
public class Question215{
public static void main(String[] args) {
//Declare variable with name 'nptel', 'space' and 'java' and proper datatype.
String nptel,space,java;
//Initialize the variables with proper input
nptel="NPTEL";
space=" ";
java="JAVA";
System.out.print(nptel+space+java);
}
}
Comments
Post a Comment