Menu


NPTEL - Programming in Java - Week 04 : Programming Assignments Answers- 2025




Programming in Java
NPTEL Week 04 : Programming Assignments Answers- 2025

Week 04 : Programming Assignment 4 - Programming In Java

Week 04 : Programming Assignments - NPTEL >> Programming In Java - 2025


Week 04 : Programming Assignment 1

Solution :


import java.util.Scanner;

public class W04_P1 {

    // Declare a class Student with one member variable of default access
    static class Student {
        int rollNo; // Default access modifier (no keyword written)

        // Constructor to assign rollNo
        Student(int r) {
            rollNo = r;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Read roll number
        int r = sc.nextInt();

        // Create Student object with roll number
        Student s = new Student(r);
		

		// Answer
		// TODO: Print the roll number using the object
		System.out.print("Roll Number is: "+s.rollNo);
		//End
		

		sc.close();
    }
}



Week 04 : Programming Assignment 2

Solution :


import java.util.Scanner;

public class W04_P2 {

    // Declare class Car with a public member variable
    static class Car {
        public int speed; // Public access, can be accessed from outside the class
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Read speed value
        int s = sc.nextInt();

        // Create Car object
        Car c = new Car();

        // Assign speed to the object
        c.speed = s;

		// Answer
		// TODO: Print the speed using the object
		System.out.print("Speed is: "+c.speed);

		//End
		

		sc.close();
    }
}



Week 04 : Programming Assignment 3

Solution :


import java.util.Scanner;

public class W04_P3 {

    // Declare class Account with a private member variable
    static class Account {
        private int balance; // Private member, cannot be accessed directly from outside

        // Method to set balance value
        public void setBalance(int b) {
            balance = b;
        }

		// Answer
		// TODO: Write a public method to return balance value

			public int getBalance() {
			  return balance;
			}
			// End
		

		}

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Read balance value
        int b = sc.nextInt();

        // Create Account object
        Account acc = new Account();

        // Set balance using method
        acc.setBalance(b);

        // Print balance using the method
        System.out.println("Account Balance is: " + acc.getBalance());

        sc.close();
    }
}


Week 04 : Programming Assignment 4

Solution :


import java.util.Scanner;

public class W04_P4 {

    // Declare parent class Employee with a protected member variable
    static class Employee {
        protected int salary; // Protected member
    }

    // Subclass Manager inherits from Employee
    static class Manager extends Employee {
        // No additional members required for this task
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Read salary value
        int s = sc.nextInt();

        // Create Manager object
        Manager m = new Manager();

		// ANSWER
		// TODO: Assign salary using the inherited member and print it
		m.salary = s;
		System.out.print("Salary is: "+m.salary);

		// END

		sc.close();
    }
}


Week 04 : Programming Assignment 5

Solution :


import java.util.Scanner;

public class W04_P5 {

    // Declare Calculator class with overloaded add methods
    static class Calculator {

        // Public method to add two integers
        public int add(int a, int b) {
            return a + b;
        }
		

	//ANSWER
	// TODO: Private method to add three integers

		private int add(int a, int b, int c) {
          return (a + b + c);
  
		}
		//END
		

	// Method to demonstrate private method access within class
        public void printThreeSum(int x, int y, int z) {
            int sum = add(x, y, z); // Call private method within class
            System.out.println("Sum of three numbers: " + sum);
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Read two numbers
        int a = sc.nextInt();
        int b = sc.nextInt();

        // Read three numbers
        int x = sc.nextInt();
        int y = sc.nextInt();
        int z = sc.nextInt();

        Calculator calc = new Calculator();

        // Call public method to add two numbers
        int sumTwo = calc.add(a, b);
        System.out.println("Sum of two numbers: " + sumTwo);

        // Call method that prints sum of three numbers
        calc.printThreeSum(x, y, z);

        sc.close();
    }
}



Relevant blogs :

NPTEL - Programming in Java - QUIZ : Week 12:Assignment 12 Answers- 2025

NPTEL - Programming in Java - Week 12 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 11:Assignment 11 Answers- 2025

NPTEL - Programming in Java - Week 11 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 10:Assignment 10 Answers- 2025

NPTEL - Programming in Java - Week 10 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 9:Assignment 9 Answers- 2025

NPTEL - Programming in Java - Week 09 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 8:Assignment 8 Answers- 2025

NPTEL - Programming in Java - Week 08 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 7:Assignment 7 Answers- 2025

NPTEL - Programming in Java - Week 07 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 6:Assignment 6 Answers- 2025

NPTEL - Programming in Java - Week 06 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 5:Assignment 5 Answers- 2025

NPTEL - Programming in Java - Week 05 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 4:Assignment 4 Answers- 2025

NPTEL - Programming in Java - Week 04 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 3:Assignment 3 Answers- 2025

NPTEL - Programming in Java - Week 03 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 2:Assignment 2 Answers- 2025

NPTEL - Programming in Java - Week 02 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 1:Assignment 1 Answers- 2025

NPTEL - Programming in Java - Week 01 : Programming Assignments Answers- 2025



Other blogs :

NPTEL - Introduction to Programming in C - Week 7:Assignment 7 Answers- 2025

NPTEL - Introduction to Programming in C - Week 6:Assignment 6 Answers- 2025

NPTEL - Introduction to Programming in C - Week 5:Assignment 5 Answers- 2025

NPTEL - Introduction to Programming in C - Week 4:Assignment 4 Answers- 2025

NPTEL - Introduction to Programming in C - Week 3:Assignment 3 Answers- 2025

NPTEL - Introduction to Programming in C - Week 2:Assignment 2 Answers- 2025

NPTEL - Introduction to Programming in C - Week 1:Assignment 1 Answers- 2025