Week 9 NPTEL Programming In Java Programming Assignment Sep-2024 Swayam

 


Week 09 : Programming Assignment 1

Due on 2024-09-26, 23:59 IST

Write a Java program to display the number rhombus structure.

Input: n=2
Output:
  1
212
  1

(the output shown with the public test case has no spaces in the beginning,
its a fault of the swayam portal as it removes whitespaces before and after the output
you can write your program normally including spaces and it will be correct)

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

Your last recorded submission was on 2024-09-14, 10:21 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
class W09_P1 {
  public static void main(String args[]) 
    {
        Scanner in = new Scanner(System.in);
        //System.out.print("Input the number:  ");
        int n = in.nextInt();
// program to display the number rhombus structure
for ( int i = 1; i <= n ; i++)
{
  for ( int j = n; j >= 1 ; j-- )
  {
    if ( j > i )
      System.out.print(" ");
    else
      System.out.print(j);
  }
  for ( int j = 2; j <= n ; j++ )
  {
    if ( j > i )
      System.out.print("");
    else
      System.out.print(j);
  }
  System.out.print("\n");
}
for ( int i = n-1; i >= 1 ; i--)
{
  for ( int j = n; j >= 1 ; j-- )
  {
    if ( j > i )
      System.out.print(" ");
    else
      System.out.print(j);
  }
  for ( int j = 2; j <= n ; j++ )
  {
    if ( j > i )
      System.out.print("");
    else
      System.out.print(j);
  }
  System.out.print("\n");
}
}
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4
1\n
  212\n
 32123\n
4321234\n
 32123\n
  212\n
   1
   1\n
  212\n
 32123\n
4321234\n
 32123\n
  212\n
   1\n
Passed after ignoring Presentation Error



Week 09 : Programming Assignment 2

Due on 2024-09-26, 23:59 IST

Write a Java program to create an abstract class Person with abstract methods eat(), sleep() and exercise().
Create subclasses Athlete and LazyPerson that extend the Person class and implement the respective methods to describe how each person eats, sleeps and exercises.
Override the respective method in each subclass.

Your last recorded submission was on 2024-09-14, 10:50 IST
Select the Language for this assignment. 
File name for this program : 
abstract class Person {
    public abstract void eat();
    public abstract void sleep();
    public abstract void exercise();
  }
// Create subclasses Athlete and LazyPerson that extend the Person class and implement the respective methods to describe how each person eats, sleeps and exercises.
class Athlete extends Person {
  public void eat() {
    System.out.println("Athlete: Include foods full of calcium, iron, potassium, and fiber.");
  }
  public void sleep() {
    System.out.println("Athlete: sleeps for 8 hours.");
  }
  public void exercise() {
    System.out.println("Athlete: Training allows the body to gradually build up strength and endurance, improve skill levels and build motivation, ambition and confidence.");
  }
}
class LazyPerson extends Person {
  public void eat() {
    System.out.println("Couch Potato: Eating while watching TV also prolongs the time period that we're eating.");
  }
  public void sleep() {
    System.out.println("Couch Potato: sleeps for 12 hours.");
  }
  public void exercise() {
    System.out.println("Couch Potato: Rarely exercising or being physically active.");
  }
}
public class W09_P2 {
    public static void main(String[] args) {
      Person athlete = new Athlete();
      Person lazyPerson = new LazyPerson();
      athlete.eat();
      athlete.exercise();
      athlete.sleep();
      lazyPerson.eat();
      lazyPerson.exercise();
      lazyPerson.sleep();
    }
  }
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Athlete: Include foods full of calcium, iron, potassium, and fiber.\n
Athlete: Training allows the body to gradually build up strength and endurance, improve skill levels and build motivation, ambition and confidence.\n
Athlete: sleeps for 8 hours.\n
Couch Potato: Eating while watching TV also prolongs the time period that we're eating.\n
Couch Potato: Rarely exercising or being physically active.\n
Couch Potato: sleeps for 12 hours.
Athlete: Include foods full of calcium, iron, potassium, and fiber.\n
Athlete: Training allows the body to gradually build up strength and endurance, improve skill levels and build motivation, ambition and confidence.\n
Athlete: sleeps for 8 hours.\n
Couch Potato: Eating while watching TV also prolongs the time period that we're eating.\n
Couch Potato: Rarely exercising or being physically active.\n
Couch Potato: sleeps for 12 hours.\n
Passed after ignoring Presentation Error



Week 09 : Programming Assignment 3

Due on 2024-09-26, 23:59 IST

Write a Java program to create a base class Shape with methods draw() and calculateArea().
Create two subclasses Circle and Cylinder.
Override the draw() method in each subclass to draw the respective shape.
In addition, override the calculateArea() method in the Cylinder subclass to calculate and return the total surface area of the cylinder.

(Remember to match the output given exactly, including the spaces and new lines)
(passed with presentation error means you will always get full marks)

Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
abstract class Shape {
    public abstract void draw();
  
    public abstract double calculateArea();
  }
// Create two subclasses Circle and Cylinder and implement required method.
class Circle extends Shape {
  private double radius;
  
  Circle(double radius){
    this.radius = radius;
  }
  
  public double calculateArea(){
    return( Math.PI * radius * radius);
  }
  
  public void draw(){
    System.out.println("Drawing a circle");
  }
}
class Cylinder extends Shape {
  private double radius;
  private double height;
  
  
  Cylinder(double radius, double height){
    this.radius = radius;
    this.height = height;
  }
  
  public double calculateArea(){
    return( 2 * Math.PI * radius * ( radius + height) );
  }
  
  public void draw(){
    System.out.println("Drawing a cylinder");
  }
}
public class W09_P3{
    public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      int radius = in.nextInt();
      int height = in.nextInt();
 
      Shape circle = new Circle(radius);
      Shape cylinder = new Cylinder(radius, height);
  
      drawShapeAndCalculateArea(circle);
      drawShapeAndCalculateArea(cylinder);
    }
  
    public static void drawShapeAndCalculateArea(Shape shape) {
      shape.draw();
      double area = shape.calculateArea();
      System.out.printf("Area: %.4f%n", area);
    }
  }
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
3
Drawing a circle\n
Area: 12.5664\n
Drawing a cylinder\n
Area: 62.8319
Drawing a circle\n
Area: 12.5664\n
Drawing a cylinder\n
Area: 62.8319\n
Passed after ignoring Presentation Error



Week 09 : Programming Assignment 4

Due on 2024-09-26, 23:59 IST

Write a Java program to create a class called "ElectronicsProduct" with attributes for product ID, name, and price.
Implement methods to apply a discount and calculate the final price.
Create a subclass " WashingMachine" that adds a warranty period attribute and a method to extend the warranty.

Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
class ElectronicsProduct {
    // Attributes for the product ID, name, and price
    private String productId;
    private String name;
    private double price;
 
    // Constructor to initialize the ElectronicsProduct object
    public ElectronicsProduct(String productId, String name, double price) {
        this.productId = productId;
        this.name = name;
        this.price = price;
    }
 
    // Method to apply a discount to the product price
    public void applyDiscount(double discountPercentage) {
        // Calculate the discount amount
        double discountAmount = price * discountPercentage / 100;
        // Subtract the discount amount from the original price
        price -= discountAmount;
    }
 
    // Method to calculate the final price after discount
    public double getFinalPrice() {
        // Return the current price which may have been discounted
        return price;
    }
 
    // Getter for product ID
    public String getProductId() {
        return productId;
    }
 
    // Getter for name
    public String getName() {
        return name;
    }
 
    // Getter for price
    public double getPrice() {
        return price;
    }
}
//Define the WashingMachine subclass that extends ElectronicsProduct
class WashingMachine extends ElectronicsProduct {
  public int warrantyPeriod;
  
  WashingMachine ( String productId, String name, double price, int warrantyPeriod) {
    super(productId,name,price);
    this.warrantyPeriod = warrantyPeriod;
  }
  
  public void applyDiscount(double discountPercentage) {
    super.applyDiscount(discountPercentage);
    System.out.println("Discount applied to Washing Machine: " + getName());
  }
  
  public int getWarrantyPeriod() {
    return warrantyPeriod;
  }
  public void extendWarranty(int months) {
    warrantyPeriod += months;
  }
}
public class W09_P4{
    public static void main(String[] args) {
        // Create an ElectronicsProduct object
        ElectronicsProduct product = new ElectronicsProduct("WM123", "Washing Machine", 1.00);
        // Apply a discount and display the final price
        product.applyDiscount(10);
        //System.out.println("Product ID: " + product.getProductId());
        //System.out.println("Name: " + product.getName());
        //System.out.println("Price after discount: $" + product.getFinalPrice());
        //System.out.println();
 
        // Create a WashingMachine object
        Scanner in = new Scanner(System.in);
 
        String productId = in.nextLine();     
        String name = in.nextLine();
        int price = in.nextInt();
        int warrantyPeriod = in.nextInt();
        
        int discountPercentage = in.nextInt();
 
        WashingMachine washingMachine = new WashingMachine(productId,name,price,warrantyPeriod);
        // Apply a discount and display the final price
        washingMachine.applyDiscount(discountPercentage);
        System.out.println("Product ID: " + washingMachine.getProductId());
        System.out.println("Name: " + washingMachine.getName());
        System.out.println("Price after discount: $" + washingMachine.getFinalPrice());
        // Display the warranty period
        System.out.println("Warranty period: " + washingMachine.getWarrantyPeriod() + " months");
 
        // Extend the warranty period and display the new warranty period
        washingMachine.extendWarranty(12);
        System.out.print("Warranty period after extension: " + washingMachine.getWarrantyPeriod() + " months");
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
w34
whirlpool
800
12
10
Discount applied to Washing Machine: whirlpool\n
Product ID: w34\n
Name: whirlpool\n
Price after discount: $720.0\n
Warranty period: 12 months\n
Warranty period after extension: 24 months
Discount applied to Washing Machine: whirlpool\n
Product ID: w34\n
Name: whirlpool\n
Price after discount: $720.0\n
Warranty period: 12 months\n
Warranty period after extension: 24 months
Passed



Week 09 : Programming Assignment 5

Due on 2024-09-26, 23:59 IST

Write a Java program to find the length of the longest sequence of zeros in binary representation of an integer.

Your last recorded submission was on 2024-09-14, 11:43 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
public class W09_P5{
// find the length of the longest sequence of zeros in binary representation of an integer.
public static int maxZeros(int n) {
  int maxlength = 0;
  String s = Integer.toBinaryString(n);
  for (int i = 0; i < s.length(); i++) {
    int local_length = 0;
    if (s.charAt(i) == '0'){
      local_length++;
      for (int j = i+1; j < s.length(); j++) {
        if (s.charAt(j) == '0')
          local_length++;
        else {
          i = j - 1;
          break;
        }
      }
    }
    if (local_length > maxlength)
      maxlength = local_length;
  }
  return maxlength;
}
public static void main(String args[]) {
                Scanner in = new Scanner(System.in);
 
                int n = in.nextInt();
                System.out.print(maxZeros(n));
 
        }
  
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
12546
6
6
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...