Week 02 : Programming Assignment 1 to 5 NPTEL Programming In Java Programming Assignment July-2024 Swayam

 

Week 02 : Programming Assignment 1

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

Complete the code segment to call the method  display() of class Former first and then call display() method of class Latter.

Your last recorded submission was on 2024-07-27, 11:52 IST
Select the Language for this assignment. 
File name for this program : 
// This is the class named Former
class Former {
    // This is a method in class Former
    public void display() {
        System.out.println("This is Former Class.");
    }
}
 
// This is the class named Latter
class Latter {
    // This is a method in class Latter
    public void display() {
        System.out.print("This is Latter Class.");
    }
}
 
public class W02_P1 {
    public static void main(String[] args) {
// Write your code to create objects and call methods
Former f = new Former();
f.display();
Latter l = new Latter();
l.display();
}
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
This is Former Class.\n
This is Latter Class.
This is Former Class.\n
This is Latter Class.
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 02 : Programming Assignment 2

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

Create a class Student with private attributes for name and age.

Use a constructor to initialize these attributes and provide public getter methods to access them.

In the main method, an instance of Student is created and the student's details are printed.

 

Guideline to Solve:

§ Define the Student class with private attributes.

§ Use a constructor to initialize the attributes.

§ Implement getter methods for the attributes.

Follow the naming convetion used in the Fixed code.

Your last recorded submission was on 2024-07-27, 11:59 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
class Student {
// Write the definition of the class here
// Create 2 private variables name and age
  String name;
  int age;
// Create a constructor 
  Student(String name, int age){
    this.name = name;
    this.age = age;
  }
// Create getName() and getAge() functions
  public String getName(){
    return name;
  }
  public int getAge(){
    return age;
  }
}
class W02_P2 {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
 
    // Read input
    // System.out.print("Enter name: ");
    String name = scanner.nextLine();
    // System.out.print("Enter age: ");
    int age = scanner.nextInt();
 
    // Create Student object
    Student student = new Student(name, age);
 
    // Print student details
    System.out.println("Student Name: " + student.getName());
    System.out.print("Student Age: " + student.getAge());
 
    scanner.close();
  }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
Alice
20
Student Name: Alice\n
Student Age: 20
Student Name: Alice\n
Student Age: 20
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 02 : Programming Assignment 3

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

Create a class Rectangle with attributes length and width.

Provide two constructors: one with no parameters (default to 1) and

another with parameters to initialize the attributes.

Use the this keyword to avoid name space collision.

Create a getArea() function that returns the area of the rectangle.

 

Guideline to Solve:

§ Define the Rectangle class with attributes and constructors.

§ Define a default Rectangle constructor that inializes length and width to 1.

§ Use the this keyword in the parameterized constructor.

§ Define a getArea() funtion that returns the area of there rectangle

Follow the naming convetion used in the Fixed code.

Your last recorded submission was on 2024-07-27, 12:22 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
class Rectangle {
// Write the definition of the class here
// Create 2 private variables 
  double length;
  double width;
// Create a default constructor 
  Rectangle(){
    length = 1;
    width = 1;
  }
// Create a parameterised constructor
  Rectangle( double length, double width){
    this.length = length;
    this.width = width;
  }
// Create getLength(), getWidth() and getArea() functions
  public double getLength(){
    return length;
  }
  public double getWidth(){
    return width;
  }
  public double getArea(){
    return length*width;
  }
}
class W02_P3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        // Read input
        // System.out.print("Enter length: ");
        double length = scanner.nextDouble();
        // System.out.print("Enter width: ");
        double width = scanner.nextDouble();
 
        // Create Rectangle objects
        Rectangle rect1 = new Rectangle();
        Rectangle rect2 = new Rectangle(length, width);
 
        // Print rectangle dimensions
        System.out.print("Default Rectangle: L, W, A : ");
        System.out.println(rect1.getLength() + ", " + rect1.getWidth() + ", " + rect1.getArea());
        System.out.print("Parameterised Rectangle: L, W, A : ");
        System.out.print(rect2.getLength() + ", " + rect2.getWidth() + ", " + rect2.getArea());
 
        scanner.close();
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2.5 
3.5
Default Rectangle: L, W, A : 1.0, 1.0, 1.0\n
Parameterised Rectangle: L, W, A : 2.5, 3.5, 8.75
Default Rectangle: L, W, A : 1.0, 1.0, 1.0\n
Parameterised Rectangle: L, W, A : 2.5, 3.5, 8.75
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 02 : Programming Assignment 4

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

Create a class Circle that encapsulates the properties of a circle.

The class should have a private field for the radius, a constructor to initialize the radius, and methods to calculate the area and circumference of the circle.

NOTE: use Math.PI for PI calculations (DO NOT USE 22/7)

 

Guideline to Solve:

§ Define the Circle class with attributes and constructors.

§ Use the this keyword in the parameterized constructor.

§ Define a getArea() funtion that returns the area of there Circle (use Math.PI)

§ Define a getCircumference() funtion that returns the circumference of there Circle (use Math.PI)

Follow the naming convetion used in the Fixed code.

Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
// Define the circle class here
class Circle{
  private double radius;
  
  Circle(double radius){
    this.radius = radius;
  }
  public double calculateArea(){
    return( Math.PI * radius * radius);
  }
  public double calculateCircumference(){
    return( 2 * Math.PI * radius);
  }
}
public class W02_P4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // System.out.print("Enter radius: ");
        double radius = scanner.nextDouble();
 
        // Create circle object
        Circle circle = new Circle(radius);
 
        // Calculate and print area
        double area = circle.calculateArea();
        // Print area to 2 decimal places
        System.out.printf("Area: %.2f\n", area);
 
        // Calculate and print circumference
        double circumference = circle.calculateCircumference();
        // Print circumference to 2 decimal places
        System.out.printf("Circumference: %.2f", circumference);
        scanner.close();
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5.0
Area: 78.54\n
Circumference: 31.42
Area: 78.54\n
Circumference: 31.42
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 02 : Programming Assignment 5

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

Complete the code by creating the constructor and the getter functions for a class Dog as defined below.

Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
// Class Declaration
public class Dog {
    // Instance Variables
    private String name;
    private String breed;
    private int age;
    private String color;
// Create the constructor
Dog( String name, String breed, int age, String color){
  this.name = name;
  this.breed = breed;
  this.age = age;
  this.color = color;
}
// Create the getter functions
public String getName(){
  return name;
}
public String getBreed(){
  return breed;
}
public int getAge(){
  return age;
}
public String getColor(){
  return color;
}
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        String name = scanner.nextLine();
        String breed = scanner.nextLine();
        int age = scanner.nextInt();
        String color = scanner.next();
        
        Dog tommy = new Dog(name, breed, age, color);
        
        System.out.println("Hi my name is: " + tommy.getName());
        System.out.println("My breed is: " + tommy.getBreed());
        System.out.println("My age is: " + tommy.getAge());
        System.out.print("My color is: " + tommy.getColor());
        
        scanner.close();
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
Tommy
German Shepard
2
Black
Hi my name is: Tommy\n
My breed is: German Shepard\n
My age is: 2\n
My color is: Black
Hi my name is: Tommy\n
My breed is: German Shepard\n
My age is: 2\n
My color is: Black
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...