JAVA • PROGRAMMING • WEEK 12
NPTEL - Programming in Java - Week 12 : Programming Assignments Answers- 2025
NPTEL • Programming in Java • Programming Assignment
Week 12 : Programming Assignments - NPTEL >> Programming In Java - 2025
Week 12 : Programming Assignment 1
Solution :
import java.util.Scanner;
public class W12_P1 {
// Declare Student class
static class Student {
String name;
int roll;
// Answer
public Student(String name,int roll) {
this.name = name;
this.roll = roll;
}
//End
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int roll = sc.nextInt();
// Create object of Student class with given values
Student s = new Student(name, roll);
// Print student details
System.out.println("Student Name: " + s.name);
System.out.println("Roll Number: " + s.roll);
sc.close();
}
}
Week 12 : Programming Assignment 2
Solution :
import java.util.Scanner;
public class W12_P2 {
// Calculator class with overloaded add methods
static class Calculator {
public int add(int a, int b) {
return a + b;
}
// Answer
public int add(int a, int b, int c) {
return a + b + c;
}
//End
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
Calculator c = new Calculator();
int sumTwo = c.add(a, b);
System.out.println("Sum of two numbers: " + sumTwo);
int sumThree = c.add(x, y, z);
System.out.println("Sum of three numbers: " + sumThree);
sc.close();
}
}
Week 12 : Programming Assignment 3
Solution :
// Answer
int number = 10;
if(number > 0) {
System.out.print("Positive Number");
}
else if( number < 0){
System.out.print("Negative Number");
}
else {
System.out.print("Zero");
}
//End
sc.close();
}
}
Week 12 : Programming Assignment 4
Solution :
import java.util.Scanner;
public class W12_P4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
// Answer
for( int i = 1; i<=n;i++)
{
sum = sum + i;
}
//End
System.out.println("Sum is: " + sum);
sc.close();
}
}
Week 12 : Programming Assignment 5
Solution :
import java.util.Scanner;
public class W12_P5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
// Read n numbers into array
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int sum = 0;
int negativeCount = 0;
// Answer
for (int number : arr) {
if (number > 0) {
sum += number;
}
else if (number < 0) {
negativeCount++;
}
}
//End
System.out.println("Sum of positive numbers: " + sum);
System.out.println("Count of negative numbers: " + negativeCount);
sc.close();
}
}