NPTEL Programming In Java Programming Week 2 : Programming Assignment 1,2,3,4 and 5 August-2023 Swayam

 

Week 2 : Programming Assignment 1

Due on 2023-08-10, 23:59 IST
Complete the code segment to call the method  print() of class Student first and then call print() method of class School.

NOTE: Don't provide any INPUT in Sample Test Cases

Your last recorded submission was on 2023-07-31, 22:42 IST
Select the Language for this assignment. 
File name for this program : 
// 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[]){
        // Creating object of class Student
        Student student = new Student();
 
        // Creating object of class School
        School school = new School();
 
        // Call 'print()' method of class School
        student.print();
 
        // Call 'print()' method of class Student 
        school.print();
 
} 
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Hi! I am class STUDENT\n
Hi! I class SCHOOL.
Hi! I am class STUDENT\n
Hi! I class SCHOOL.\n
Passed after ignoring Presentation Error




Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 2 : Programming Assignment 2

Due on 2023-08-10, 23:59 IST
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.
--------------------------------

NOTE: Don't provide any INPUT in Sample Test Cases

Your last recorded submission was on 2023-07-31, 22:47 IST
Select the Language for this assignment. 
File name for this program : 
// 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 p = new Printer();
 
// Call 'print()' methods for desired output
 
p.print("Hi! I am class STUDENT");
p.print();
}
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Hi! I am class STUDENT\n
Hi! I class SCHOOL.
Hi! I am class STUDENT\n
Hi! I class SCHOOL.\n
Passed after ignoring Presentation Error




Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 2 : Programming Assignment 3

Due on 2023-08-10, 23:59 IST
Complete the code segment to call print() method of class Question by creating a method named ‘studentMethod()’.
Your last recorded submission was on 2023-07-31, 22:56 IST
Select the Language for this assignment. 
File name for this program : 
// 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
    void studentMethod(){
// Calling a method named 'print()' in class Question23
        print(this);
    }
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Well Done!
Well Done!
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 2 : Programming Assignment 4

Due on 2023-08-10, 23:59 IST
Complete the code segment to call default constructor first and then any other constructor in the class.
Your last recorded submission was on 2023-07-31, 23:01 IST
Select the Language for this assignment. 
File name for this program : 
// This is the main class Question
public class Question214{
    public static void main(String[] args){
        Answer a = new Answer(10,"MCQ");
    }
}
// This is the class Answer
class Answer{
    // This is the default constructor of the class Answer
    Answer(){
        System.out.println("You got nothing.");
    }
    // This is a parameterized constructor of the class Answer
    Answer(int marks, String type){
        //The 'this()' referene variable is able to call the default constructor of the class.
        this();     
        //Print marks and type of the question
        System.out.print("You got "+marks+" for an "+ type);
    }
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
You got nothing.\n
You got 10 for an MCQ
You got nothing.\n
You got 10 for an MCQ
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 2 : Programming Assignment 5

Due on 2023-08-10, 23:59 IST
Complete the code segment to debug / complete the program which is intended to print 'NPTEL JAVA'.
Your last recorded submission was on 2023-07-31, 23:05 IST
Select the Language for this assignment. 
File name for this program : 
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);
   }
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
NPTEL JAVA
NPTEL JAVA
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed


---------------------------------------------------------------------------


Week 3 : Programming Assignment 1

Due on 2023-08-17, 23:59 IST
This program is related to the generation of Fibonacci numbers.

For example: 0,1, 1,2, 3,5, 8, 13,… is a Fibonacci sequence where 13 is the 8th Fibonacci number.

A partial code is given and you have to complete the code as per the instruction given .
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner; //This package for reading input
public class Fibonacci { 
 
public static void main(String args[]) { 
     Scanner sc = new Scanner(System.in);
    int n=sc.nextInt(); //Read an integer
System.out.println(fib(n)); //Generate and print the n-th Fibonacci                
                                     //number
    } 
static int fib(int n) {
//complete the code segment to find the nth Fibonacci number in the Fibonacci sequence and return the value. Write the function recursively.
           if (n==1)      //Terminal condition
            return 0;
        else if(n==2)
            return 1;           
return fib(n - 1) + fib(n - 2); //Recursive call of function
}
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
8
13
13\n
Passed after ignoring Presentation Error




Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed


Week 3 : Programming Assignment 2

Due on 2023-08-17, 23:59 IST

Define a class Point with two fields x and y each of type double. Also, define a method distance(Point p1, Point p2) to calculate the distance between points p1 and p2 and return the value in double.

Complete the code segment given below. Use Math.sqrt( ) to calculate the square root.

Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
public class Circle extends Point{
    
public static void main(String[] args) {
 
     Scanner sc = new Scanner(System.in);
     Point p1=new Point();
     p1.x=sc.nextDouble();
     p1.y=sc.nextDouble();
     Point p2=new Point();
     p2.x=sc.nextDouble();
     p2.y=sc.nextDouble(); 
     Circle c1=new Circle();
    c1.distance(p1,p2);
    
  }
 
}
//Complete the code segment to define a class Point with parameter x,y and method distance()for calculating distance between two points.
//Note: Pass objectsof type class Point as argument in distance() method.
class Point{
  double x;
  double y;
 
public static void distance(Point p1,Point p2){
        double d;
      d=Math.sqrt((p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y));
      System.out.print(d);
  } 
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2.0  3.0
1.0  2.0
1.4142135623730951
1.4142135623730951
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 3 : Programming Assignment 3

Due on 2023-08-17, 23:59 IST
A class Shape is defined with two overloading constructors in it. Another class Test1 is partially defined which inherits the class Shape. The class Test1 should include two overloading constructors as appropriate for some object instantiation shown in main() method. You should define the constructors using the super class constructors. Also, override the method calculate( ) in Test1 to calculate the volume of a Shape.
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
class Shape{
double length, breadth;
Shape(double l, double b){ //Constructor to initialize a Shape object  
length = l;
breadth= b;
}
Shape(double len){    //Constructor to initialize another Shape object  
length = breadth = len;
}
double calculate(){// To calculate the area of a shape object
return length * breadth ;
}
}
public class Test1 extends Shape{
//Create a derived class constructor which can call the one parametrized constructor of the base class
//Create a derived class constructor which can call the two parametrized constructor of the base class
//Override the method calculate() in the derived class to find the volume of a shape instead of finding the area of a shape
//Template code:
    double height;
    Test1(double length,double h) {
//base class constructor with one parameter is called
        super(length);
        height=h;
    }
    
    Test1(double length,double breadth,double h) {
//base class constructor having two argument is called
        super(length,breadth);
        height=h;
    }
 
    double calculate()  {
        return length*breadth*height;
    }
public static void main(String args[]){
    Scanner sc = new Scanner(System.in);//Create an object to read                                                               
                                          //input
    double l=sc.nextDouble(); //Read length
    double b=sc.nextDouble(); //Read breadth    
    double h=sc.nextDouble(); //Read height
    Test1 myshape1 = new Test1(l,h);
    Test1 myshape2 = new Test1(l,b,h);
    double volume1;
    double volume2;
    volume1 = myshape1.calculate();
    volume2=myshape2.calculate();
    System.out.println(volume1);
    System.out.println(volume2);
    }
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2.0 3.0 4.0
16.0\n
24.0
16.0\n
24.0\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 3 : Programming Assignment 4

Due on 2023-08-17, 23:59 IST
This program to exercise the call of static and non-static methods. A partial code is given defining two methods, namely sum( ) and multiply ( ). You have to call these methods to find the sum and product of two numbers. Complete the code segment as instructed.
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
class QuestionScope {
int sum(int a, int b){ //non-static method
        return a + b;
    }
static int multiply(int a, int b){ //static method
        return a * b;
    }
}
public class Exercise3_4{
public static void main( String[] args ) {
        Scanner sc = new Scanner(System.in);
        int n1=sc.nextInt();
        int n2=sc.nextInt();
//Called the method sum() to find the sum of two numbers.
//Called the method multiply() to find the product of two numbers.
QuestionScope st = new QuestionScope(); // Create an object to call non-  
                                                //static method 
      int result1=st.sum(n1,n2); // Call the method
      int result2=QuestionScope.multiply(n1,n2);    // Create an object to call 
                                                //static method 
        
        System.out.println(result1);
        System.out.print(result2);
    
 
}
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
3 5
8\n
15
8\n
15
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 3 : Programming Assignment 5

Due on 2023-08-17, 23:59 IST
Complete the code segment to swap two numbers using call by object reference.
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
class Question {  //Define a class Question with two elements e1 and e2.
  Scanner sc = new Scanner(System.in);
int e1 = sc.nextInt();  //Read e1
int e2 = sc.nextInt();  //Read e2
}
public class Exercise3_5{
//Template code 
//Define static method swap()to swap the values of e1 and e2 of class T. 
public static void swap(Question t) {
int temp = t.e1;
    t.e1 = t.e2;
    t.e2 = temp;
  }
public static void main(String[] args) {
//Create an object of class Question
    Question t = new Question ();
  //Call the method swap()
swap(t);
 
System.out.println(t.e1+" "+t.e2);    
  }
 
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
10 20
20 10
20 10\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed





No comments

File Uploading in PHP

  File Uploading in PHP PHP allow you to upload any type of a file i.e. image, binary or text files.etc..,PHP has one in built global variab...