This article will help you with the solutions of the Programming in Java NPTEL 2022 Week 2 Assignment.
Programming in Java NPTEL Week 2 Programming Assignment 1 Solutions
Complete the code segment 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 Question21{
public static void main(String args[]){
// Create an object of class Student
Student stu_Obj = new Student();
// Call 'print()' method of class Student
stu_Obj.print();
// Create an object of class School
School sch_Obj = new School();
// Call 'print()' method of class School
sch_Obj.print();
}
}
Week 2: Programming Assignment 2 Solution
Complete the code segment to call the method print() of class given class Printer to print the following.
——————————–
Hi! I am class STUDENT
Hi! I class SCHOOL.
——————————–
// This is the class named Printer
class Printer {
// This are the methods in class Printer
public void print() {
System.out.println("Hi! I class SCHOOL.");
}
public void print(String s) {
System.out.println(s);
}
}
public class Question22{
public static void main(String[] args) {
// Create an object of class Printer
Printer prt = new Printer();
// Call 'print()' methods for desired output
prt.print("Hi! I am class STUDENT");
prt.print();
}
}
Week 2: Programming Assignment 3 Solution
Complete the code segment tocall print() method of class Question by creating a method named ‘studentMethod()’.
// This is the main class Question
public class Question23{
public static void main(String[] args) {
// Object of the main class is created
Question23 q = new Question23();
// Print method on object of Question class is called
q.studentMethod();
}
// 'print()' method is defined in class Question
void print(Question23 object){
System.out.print("Well Done!");
}
// Define a method named 'studentMethod()' in class Question
public void studentMethod() {
// Call the method named 'print()' in class Question
print(this);
}
}
Week 2: Programming Assignment 4 Solution
Complete the code segment to call default constructor first and then any other constructor in the class.
// 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.print("You got "+marks+" for an "+ type);
}
}
Week 2: Programming Assignment 5 Solution
Complete the code segment to debug / complete 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);
}
}
The above question set contains all the correct answers. But in any case, you find any typographical, grammatical or any other error in our site then kindly inform us. Don’t forget to provide the appropriate URL along with error description. So that we can easily correct it.
Thanks in advance.
For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.