Week 2 : Programming Assignment 1
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[]){
Student s=new Student();
s.print();
School s1=new School();
s1.print();
}
}
Week 2 : Programming Assignment 2
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) {
Printer p=new Printer();
p.print("Hi! I am class STUDENT");
p.print();
}
}
Week 2 : Programming Assignment 3
Complete the code segment to call 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!");
}
public void studentMethod()
{
print(this);
}
}
Week 2 : Programming Assignment 4
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.println("You got "+marks+" for an "+ type);
}
}
Week 2 : Programming Assignment 5
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) {
String nptel, space, java;
nptel= "NPTEL";
space= " ";
java= "JAVA";
System.out.print(nptel+space+java);
}
}
No comments