NPTEL Programming In Java Programming Assignment July-2024 SwayamWeek-3

 

Week 03 : Programming Assignment 1

Due on 2024-08-15, 23:59 IST

Create a class Department having a method getCourses that prints "These are the department's courses". It will have two subclasses, ComputerScience and MechanicalEngineering, each having a method with the same name that prints specific courses for the respective departments.Call the method by creating an object of each of the three classes.

(Remember to match the output given exactly, including the spaces and new lines)

(passed with presentation error means you will get full marks)

Select the Language for this assignment. 
File name for this program : 
class Department {
  public void getCourses() {
    System.out.println("These are the department's courses");
  }
}
// Create two subclasses, ComputerScience and MechanicalEngineering, each having a method with the same name getCourse() that prints specific courses for the respective departments.
class ComputerScience extends Department{
  public void getCourses() {
    System.out.println("Courses: Data Structures, Algorithms, Operating Systems");
  }
}
 
class MechanicalEngineering extends Department{
  public void getCourses() {
    System.out.println("Courses: Thermodynamics, Fluid Mechanics, Heat Transfer");
  }
}
class W03_P1 {
  public static void main(String[] args) {
    ComputerScience cs = new ComputerScience();
    MechanicalEngineering me = new MechanicalEngineering();
    cs.getCourses();
    me.getCourses();
  }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Courses: Data Structures, Algorithms, Operating Systems\n
Courses: Thermodynamics, Fluid Mechanics, Heat Transfer
Courses: Data Structures, Algorithms, Operating Systems\n
Courses: Thermodynamics, Fluid Mechanics, Heat Transfer\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 03 : Programming Assignment 2

Due on 2024-08-15, 23:59 IST

There are two class cls1 and cls2 which is subclass of cls1.  cls1 having a method "add" which add two numbers. Create two method inside cls2 which will take 2 parameters as input i.e. a and b and print the sum , multiplication and sum of their squares i.e (a^2) + (b2).

(Remember to match the output given exactly, including the spaces and new lines)

(passed with presentation error means you will get full marks)

Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
class cls1
{
    void add(int p,int q)
    {
        System.out.println(p+q);
    }
}
//Create subclass cls2 of cls1 which perform task of multiplication and sum of squares of passing two parameters.
class cls2 extends cls1{
  void mul(int p,int q)
    {
        System.out.println(p*q);
    }
  void task(int p,int q)
    {
        add(p*p,q*q);
    }
}
public class W03_P2{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
    
        cls2 obj=new cls2();
        int a=sc.nextInt();
        int b=sc.nextInt();
        //String tilde=sc.next();
        obj.add(a,b);
        obj.mul(a,b);
        obj.task(a,b);
    
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
4
6\n
8\n
20
6\n
8\n
20\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 03 : Programming Assignment 3

Due on 2024-08-15, 23:59 IST

Write a program to print the factorial of a number by defining a recursive method named 'Factorial'.

Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.-

4! = 1*2*3*4 = 24

3! = 3*2*1 = 6

2! = 2*1 = 2

Also,

1! = 1

0! = 0

Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
class W03_P3{
//Create recursive method to find factorial of a number
 
static long factorial(int x){
  if(x == 0 || x == 1 )
    return 1;
  else 
    return x * factorial(x -1);
}
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x;
        x = in.nextInt();
        System.out.print(factorial(x));   
  }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
120
120
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 03 : Programming Assignment 4

Due on 2024-08-15, 23:59 IST

Write a program to take integer inputs from user until he/she presses q ( Ask to press q to quit after every integer input ). Print average and product of all numbers.

(Remember to match the output given exactly, including the spaces and new lines)

(passed with presentation error means you will get full marks)

Your last recorded submission was on 2024-08-06, 12:39 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.*;
 
public class W03_P4{
public static void main(String[] args) {
 
String choice = "";
Scanner input = new Scanner(System.in);
// Add your code
int x;
int product = 1;
float avg = 0;
int count = 0;
do{
  if(input.hasNextInt()) {
    x = input.nextInt();
    product*=x;
    avg +=x;
    count++;
  }
  else
    choice="q";
}while(choice!="q");
System.out.println("Product is: " + product);
System.out.print("Average is: " + avg/count);
}
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
4
5
6
8
1
q
Product is: 1920\n
Average is: 4.3333335
Product is: 1920\n
Average is: 4.3333335
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 03 : Programming Assignment 5

Due on 2024-08-15, 23:59 IST

Write a Java program to create a class called Employee with methods called work() and getSalary(). Create a subclass called HRManager that overrides the work() method and adds a new method called addEmployee().

(Remember to match the output given exactly, including the spaces and new lines)

(passed with presentation error means you will get full marks)

Your last recorded submission was on 2024-08-06, 13:20 IST
Select the Language for this assignment. 
File name for this program : 
class Employee {
    private final int salary;
 
    public Employee(int salary) {
        this.salary = salary;
    }
 
    public void work() {
        System.out.println("working as an employee!");
    }
 
    public int getSalary() {
        return salary;
    }
}
// Create a subclass called HRManager that overrides the work() method and adds a new method called addEmployee().
class HRManager extends Employee{
  public HRManager(int salary) {
        super(salary);
    }
  public void work() {
        System.out.println("\nManaging employees");
    }
  public void addEmployee() {
        System.out.print("\nAdding new employee!");
    }
}
class W03_P5 {
    public static void main(String[] args) {
        Employee emp = new Employee(40000);
        HRManager mgr = new HRManager(70000);
 
        emp.work();
        System.out.println("Employee salary: " + emp.getSalary());
 
        mgr.work();
        System.out.println("Manager salary: " + mgr.getSalary());
        mgr.addEmployee();
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
working as an employee!\n
Employee salary: 40000\n
\n
Managing employees\n
Manager salary: 70000\n
\n
Adding new employee!
working as an employee!\n
Employee salary: 40000\n
\n
Managing employees\n
Manager salary: 70000\n
\n
Adding new employee!
Passed





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...