Week 05 : Programming Assignment 1 2 3 4 5

 



Week 05 : Programming Assignment 1

Due on 2024-08-29, 23:59 IST
Write a program to create a method that takes an integer as a parameter and throws an exception if the number is odd.
Your last recorded submission was on 2024-08-17, 23:41 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.*;
 
class IllegalArgumentException extends Exception {
  public IllegalArgumentException(String message) {
    super(message);
  }
}
 
public class W05_P1 {
    public static void main(String[] args) {
     // int n = 18;
      Scanner input = new Scanner(System.in);
      int n=input.nextInt();
      trynumber(n);
    }
// write a function to check an integer as a parameter and throws an exception if the number is odd
public static void trynumber(int n){
  try {
      checkEvenNumber(n);
    } catch (IllegalArgumentException e) {
      System.out.print(e.getMessage());
    }
}
 
 
public static void checkEvenNumber(int number) throws IllegalArgumentException {
  if ( number % 2 == 1)
    throw new IllegalArgumentException("Error: " + number + " is odd.");
  else
    System.out.print(number + " is even.");
}
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
7
Error: 7 is odd.
Error: 7 is odd.
Passed
Test Case 2
8
8 is even.
8 is even.
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 05 : Programming Assignment 2

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

Write a program to create an interface Searchable with a method search(String keyword) that searches for a given keyword in a text document.
Create two classes Document and WebPage that implement the Searchable interface and provide their own implementations of the search() method.

Your last recorded submission was on 2024-08-18, 00:07 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
interface Searchable {
    // Declare the abstract method "search" that classes implementing this interface must provide
    boolean search(String keyword);
}
// Declare the Document class and WebPage class, which implements the Searchable interface
class Document implements Searchable{
  public boolean search(String keyword){
    return true; // dummy statement
  }
}
 
class WebPage implements Searchable{
  
  String url;
  String title;
  
  WebPage(String url, String title){
    this.url = url;
    this.title = title;
  }
  public boolean search(String keyword){
    return title.contains(keyword);
  }
}
public class W05_P2 {
    public static void main(String[] args) {
        // Create an instance of the Document class with a sample content
   Scanner in = new Scanner(System.in);
        String text = in.nextLine();
        String document = text;
        String str = in.nextLine();
 
        // Search for a keyword in the document and store the result
        boolean documentContainsKeyword = document.contains(str);
 
        // Print whether the document contains the keyword
System.out.println("Document contains keyword: " +str+ " "+documentContainsKeyword);
// Create an instance of the WebPage class with a sample URL and HTML content
        WebPage webPage = new WebPage("https://onlinecourses.nptel.ac.in", "This is a NPTEL online course webpage.");
 
        // Search for a keyword in the webpage and store the result
        boolean webPageContainsKeyword = webPage.search("webpage");
 
        // Print whether the webpage contains the keyword
        System.out.print("Webpage contains keyword 'webpage': " + webPageContainsKeyword);
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
You are a hard working person.
student
Document contains keyword: student false\n
Webpage contains keyword 'webpage': true
Document contains keyword: student false\n
Webpage contains keyword 'webpage': true
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 05 : Programming Assignment 3

Due on 2024-08-29, 23:59 IST
Write a  program to create a method that takes a string as input and throws an exception if the string does not contain vowels.
(Note: Check both upper and lower case vowels)
Your last recorded submission was on 2024-08-18, 00:24 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
class NoVowelsException extends Exception {
  public NoVowelsException(String message) {
    super(message);
  }
}
 
public class W05_P3 {
    public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      try {
        String text = input.nextLine();
 
        System.out.println("Original string: " + text);
        checkVowels(text);
        System.out.print("String contains vowels.");
      } catch (NoVowelsException e) {
        System.out.print("Error: " + e.getMessage());
      }
    }
// create a method that takes a string as input and throws an exception if the string does not contain vowels.
public static void checkVowels(String a) throws NoVowelsException {
  boolean flag = false;
  for ( int i = 0 ; i < a.length() ; i++){
        char c = a.charAt(i);
        if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || 
            c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
          flag = true;
      }
  if ( flag == false )
    throw new NoVowelsException("String does not contain any vowels.");
}
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
Fly by my crypt
Original string: Fly by my crypt\n
Error: String does not contain any vowels.
Original string: Fly by my crypt\n
Error: String does not contain any vowels.
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 05 : Programming Assignment 4

Due on 2024-08-29, 23:59 IST
Write a  program to create an interface Flyable with a method called fly_obj().
Create three classes Spacecraft, Airplane, and Helicopter that implement the Flyable interface.
Implement the fly_obj() method for 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)


Your last recorded submission was on 2024-08-18, 00:30 IST
Select the Language for this assignment. 
File name for this program : 
interface Flyable {
    // Declare the abstract method "fly_obj" that classes implementing this interface must provide
    void fly_obj();
}
// Declare the Spacecraft class, Airplane class, Helicopter class which implements the Flyable interface
// Implement the "fly_obj" method required by the Flyable interface
class Spacecraft implements Flyable{
  public void fly_obj(){
    System.out.println("Spacecraft is flying");
  }
}
class Airplane implements Flyable{
  public void fly_obj(){
    System.out.println("Airplane is flying");
  }
}
class Helicopter implements Flyable{
  public void fly_obj(){
    System.out.println("Helicopter is flying");
  }
}
public class W05_P4 {
    public static void main(String[] args) {
        // Create an array of Flyable objects, including a Spacecraft, Airplane, and Helicopter
        Flyable[] flyingObjects = {new Spacecraft(), new Airplane(), new Helicopter()};
 
        // Iterate through the array and call the "fly_obj" method on each object
        for (Flyable obj : flyingObjects) {
            obj.fly_obj();
        }
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Spacecraft is flying\n
Airplane is flying\n
Helicopter is flying
Spacecraft is flying\n
Airplane is flying\n
Helicopter is flying\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed


Week 05 : Programming Assignment 5

Due on 2024-08-29, 23:59 IST
Write a program to create an interface Playable with a method play() that takes no arguments and returns void.
Create three classes Football, Volleyball, and Basketball that implement the Playable interface and override the play() method to play the respective sports.

(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 : 
interface Playable {
    // Declare the abstract method "play" that classes implementing this interface must provide
    void play();
}
// Declare the Volleyball, Basketball, Football class, which implements the Playable interface
class Volleyball implements Playable{
  public void play(){
    System.out.println("Playing volleyball");
  }
}
class Basketball implements Playable{
  public void play(){
    System.out.println("Playing basketball");
  }
}
class Football implements Playable{
  public void play(){
    System.out.println("Playing football");
  }
}
public class W05_P5 {
    public static void main(String[] args) {
        // Create instances of Playable objects for football, volleyball, and basketball
        Playable football = new Football();
        Playable volleyball = new Volleyball();
        Playable basketball = new Basketball();
 
        // Call the "play" method on each Playable object to play different sports
        football.play();
        volleyball.play();
        basketball.play();
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
NA
Playing football\n
Playing volleyball\n
Playing basketball
Playing football\n
Playing volleyball\n
Playing basketball\n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed



netaji gandi Tuesday, August 27, 2024

NPTEL » Programming in Java Week 07 : Programming Assignment 1 2 3 4 and 5

  Week 07 : Programming Assignment 1 Due on 2024-09-12, 23:59 IST Write a Java program to find longest word in given input. Select the Langu...