Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

NPTEL Programming in Java Jan 2024 Week 12

 


Week 12 : Programming Assignment 1

Due on 2024-04-18, 23:59 IST

Write a program that calculates the letter grade based on a numerical score entered by the user.

§ >=80 - A

§ 70-79 - B

§ 60-69 - C

§ 50-59 - D

§ 40-49 - P

§ <40 - F

Follow the naming convention as given in the main method of the suffix code.

Your last recorded submission was on 2024-04-06, 14:41 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W12_P1 {
4
// Create a static function calculateGrade() to calculate the grade as needed and return to main() where it will automatically get printed
5
public static char calculateGrade(int score){
6
  if (score >= 80)
7
    return 'A';
8
  if (score >= 70 && score < 80)
9
    return 'B';
10
  if (score >= 60 && score < 70)
11
    return 'C';
12
  if (score >= 50 && score < 60)
13
    return 'D';
14
  if (score >= 40 && score < 50)
15
    return 'P';
16
  if (score < 40)
17
    return 'F';
18
  else
19
    return 'e';
20
}
0
public static void main(String[] args) {
1
        Scanner scanner = new Scanner(System.in);
2
        // System.out.print("Enter the numerical score: ");
3
        int score = scanner.nextInt();
4
        scanner.close();
5
        // If score is greater than 100 or less than 0, print "Invalid Input"
6
        if (score > 100 || score < 0) {
7
            System.out.println("Invalid Input");
8
            return;
9
        }
10
        char grade = calculateGrade(score);
11
        System.out.println("Grade: " + grade);
12
    }
13
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
80
Grade: A
Grade: A\n
Passed after ignoring Presentation Error
Test Case 2
40
Grade: P
Grade: P\n
Passed after ignoring Presentation Error



Week 12 : Programming Assignment 2

Due on 2024-04-18, 23:59 IST

Write a program that validates a user's password based on the following criteria (in the following order):

§ 1. The password must be at least 8 characters long.

§ 2. The password must contain at least one uppercase letter (A-Z).

§ 3. The password must contain at least one lowercase letter (a-z).

§ 4. The password must contain at least one digit (0-9).

Take the following assumptions regarding the input:

§ The input will not contain any spaces

Your output should print the rule that it violates exactly as defined above.

If the password violates multiple rules then the first rule it violates should take priority.

 

For example:

Input:        

pass

Output:    

Your password is invalid.
The password must be at least 8 characters long.

Follow the naming convention as given in the main method of the suffix code.

Your last recorded submission was on 2024-04-06, 18:47 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
public class W12_P2 {
4
// create a static function validatePassword() to validate the password and print the messages as needed
5
public static void validatePassword(String pass){
6
  String rules = ""; boolean Uc = false; boolean Lc = false; boolean dig = false;
7
  if ( pass.length() < 8 )
8
    rules += "The password must be at least 8 characters long.\n";
9
  for ( int i = 0 ; i < pass.length(); i++ ){
10
    if ( pass.charAt(i) >= 65 && pass.charAt(i) <= 90 )
11
      Uc = true;
12
    if ( pass.charAt(i) >= 97 && pass.charAt(i) <= 122 )
13
      Lc = true;
14
    if ( pass.charAt(i) >= 48 && pass.charAt(i) <= 57 )
15
      dig = true;
16
    
17
  }
18
  
19
  if ( !Uc )
20
    rules += "The password must contain at least one uppercase letter (A-Z).\n";
21
  if ( !Lc )
22
    rules += "The password must contain at least one lowercase letter (a-z).\n";
23
  if ( !dig )
24
    rules += "The password must contain at least one digit (0-9).\n";
25
  
26
  if ( rules == "" )
27
    rules = "Your password is valid." + rules;
28
  else
29
    rules = "Your password is invalid.\n" + rules;
30
  
31
  System.out.print(rules);
32
}
0
public static void main(String[] args) {
1
        Scanner scanner = new Scanner(System.in);
2
3
        // System.out.print("Enter your password: ");
4
        String password = scanner.nextLine();
5
        scanner.close();
6
7
        validatePassword(password);
8
    }
9
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
Password123
Your password is valid.
Your password is valid.
Passed
Test Case 2
password123
Your password is invalid.\n
The password must contain at least one uppercase letter (A-Z).
Your password is invalid.\n
The password must contain at least one uppercase letter (A-Z).\n
Passed after ignoring Presentation Error




Week 12 : Programming Assignment 3

Due on 2024-04-18, 23:59 IST

Write a program that analyzes a given text and provides the following information:

§ Number of words: The total number of words in the text.

§ Longest word: The longest word found in the text.

§ Vowel count: The total number of vowels (a, e, i, o, u) in the text.

Take the following assumptions regarding the input:

§ The input will only contain lowerspace characters[a-z] and spaces.

§ The last word will not end with a space nor with any special character.

You need to print exactly as shown in the test case.

Do not print anyting else while taking input.

Follow the naming convention as given in the main method of the suffix code.

Your last recorded submission was on 2024-04-08, 20:39 IST
Select the Language for this assignment. 
File name for this program : 
1
// This is where you will write the code:
2
// 1. Split the text into words using a suitable delimiter (e.g., space).
3
// 2. Initialize variables to store the word count, longest word, and vowel count.
4
// 3. Loop through each word:
5
//     - Update the word count.
6
//     - Check if the current word is longer than the current longest word and update if necessary.
7
//     - Count the vowels in the current word and add it to the total vowel count.
8
// 4. After the loop, print the calculated results (word count, longest word, vowel count).
9
import java.util.Scanner;
10
11
public class W12_P3 {
12
  public static void main(String[] args) {
13
    Scanner scanner = new Scanner(System.in);
14
    String text = scanner.nextLine();
15
    scanner.close();
16
    int wc = 0, vc = 0; String longest = "";
17
    
18
    String[] arrOfStr = text.split(" ");
19
    wc = arrOfStr.length;
20
    
21
    for (String a : arrOfStr){
22
      if ( a.length() >= longest.length() )
23
        longest = a;
24
      for ( int i = 0 ; i < a.length() ; i++){
25
        char c = a.charAt(i);
26
        if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'I' )
27
          vc++;
28
      }
29
    }
30
    System.out.println("Number of words: " + wc );
31
    System.out.println("Longest word: " + longest);
32
    System.out.print("Vowel count: " + vc );
33
    }
34
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
this is a sample text for analysis
Number of words: 7\n
Longest word: analysis\n
Vowel count: 10
Number of words: 7\n
Longest word: analysis\n
Vowel count: 10
Passed
Test Case 2
lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
Number of words: 19\n
Longest word: consectetur\n
Vowel count: 45
Number of words: 19\n
Longest word: consectetur\n
Vowel count: 45
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed





Week 12 : Programming Assignment 4

Due on 2024-04-18, 23:59 IST

Write a Java program to calculate the area of different shapes using inheritance.

Each shape should extend the abstact class Shape

Your task is to make the following classes by extending the Shape class:

§ Circle (use Math.PI for area calculation)

§ Rectangle

§ Square

Each Shape subclass should have these private variables apart from the private variables for storing sides or radius.

private String shapeType;
private double area;

Follow the naming convention as given in the main method of the suffix code.

Your last recorded submission was on 2024-04-06, 22:00 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
abstract class Shape {
4
    protected abstract void calcArea();
5
    public abstract void printArea();
6
}
7
// Implement 3 classes here:
8
class Circle extends Shape {
9
  private String shapeType;
10
  private double area;
11
  private double radius;
12
  
13
  Circle(String shapeType, double arg1){
14
    this.shapeType = shapeType;
15
    radius = arg1;
16
  }
17
  
18
  protected void calcArea(){
19
    area = Math.PI * radius * radius;
20
  }
21
  
22
  public void printArea(){
23
    System.out.print("Area of Circle: " + area );
24
  }
25
}
26
27
class Rectangle extends Shape {
28
  private String shapeType;
29
  private double area;
30
  private double length;
31
  private double breadth;
32
  
33
  Rectangle(String shapeType, double arg1, double arg2){
34
    this.shapeType = shapeType;
35
    length = arg1;
36
    breadth = arg2;
37
  }
38
  
39
  protected void calcArea(){
40
    area = length * breadth;
41
  }
42
  
43
  public void printArea(){
44
    System.out.print("Area of Rectangle: " + area );
45
  }
46
}
47
48
class Square extends Shape {
49
  private String shapeType;
50
  private double area;
51
  private double side;
52
  
53
  Square(String shapeType, double arg1){
54
    this.shapeType = shapeType;
55
    side = arg1;
56
  }
57
  
58
  protected void calcArea(){
59
    area = side * side;
60
  }
61
  
62
  public void printArea(){
63
    System.out.print("Area of Square: " + area );
64
  }
65
}
0
public class W12_P4 {
1
2
    public static void main(String[] args) {
3
        Scanner scanner = new Scanner(System.in);
4
        // System.out.print("Enter shape type (Circle, Rectangle or Square): ");
5
        String shapeType = scanner.nextLine();
6
        // System.out.print("Enter first dimension: ");
7
        double arg1 = scanner.nextDouble();
8
        Shape shape = null;
9
        if (shapeType.equalsIgnoreCase("Circle")) {
10
            shape = new Circle(shapeType, arg1);
11
        } else if (shapeType.equalsIgnoreCase("Rectangle")) {
12
            double arg2 = scanner.nextDouble();
13
            shape = new Rectangle(shapeType, arg1, arg2);
14
        } else if (shapeType.equalsIgnoreCase("Square")) {
15
            shape = new Square(shapeType, arg1);
16
        } else {
17
            System.out.println("Invalid shape type");
18
            scanner.close();
19
            return;
20
        }
21
        shape.calcArea();
22
        shape.printArea();
23
        scanner.close();
24
    }
25
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
Circle
10.0
Area of Circle: 314.1592653589793
Area of Circle: 314.1592653589793
Passed
Test Case 2
Square
10.0
Area of Square: 100.0
Area of Square: 100.0
Passed
Test Case 3
Rectangle
10.0 20.0
Area of Rectangle: 200.0
Area of Rectangle: 200.0
Passed





Week 12 : Programming Assignment 5

Due on 2024-04-18, 23:59 IST

Implement two classes, Car and Bike, that implement the Vehicle interface.
Each class should have appropriate attributes and constructors.
Ensure that the startaccelerate, and brake methods are implemented correctly for each vehicle.

Follow the naming convention as given in the main method of the suffix code.

Your last recorded submission was on 2024-04-06, 22:36 IST
Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
interface Vehicle {
3
    void start();
4
    void accelerate();
5
    void brake();
6
}
7
// Implement two classes, Car and Bike, that implement the Vehicle interface. 
8
// Each class should have appropriate attributes and constructors. 
9
// Ensure that the start, accelerate, and brake methods are implemented correctly for each vehicle.
10
class Car implements Vehicle{
11
    private int numDoors;
12
    Car(int numDoors){
13
      this.numDoors = numDoors;
14
    }
15
    public void start() {
16
      System.out.println("Car with " + numDoors + " doors started");
17
    }
18
  public void accelerate() {
19
      System.out.println("Accelerating to 60 mph");
20
    }
21
  public void brake() {
22
      System.out.print("Applying brakes");
23
    }
24
  
25
}
26
27
class Bike implements Vehicle{
28
    private int numWheels;
29
    Bike(int numWheels){
30
      this.numWheels = numWheels;
31
    }
32
    public void start() {
33
      System.out.println("Bike with " + numWheels + " wheels started");
34
    }
35
  public void accelerate() {
36
      System.out.println("Accelerating to 40 mph");
37
    }
38
  public void brake() {
39
      System.out.print("Applying brakes");
40
    }
41
  
42
}
0
public class W12_P5 {
1
    public static void main(String[] args) {
2
        Scanner scanner = new Scanner(System.in);
3
        String vehicleType = scanner.nextLine();
4
5
        // Fixed code: Prefix
6
        Vehicle vehicle = null;
7
        switch (vehicleType) {
8
            case "Car":
9
                int numDoors = scanner.nextInt();
10
                vehicle = new Car(numDoors);
11
                break;
12
            case "Bike":
13
                int numWheels = scanner.nextInt();
14
                vehicle = new Bike(numWheels);
15
                break;
16
            default:
17
                System.out.println("Invalid vehicle type");
18
                scanner.close();
19
                System.exit(1);
20
        }
21
        if (vehicle != null) {
22
            vehicle.start();
23
            vehicle.accelerate();
24
            vehicle.brake();
25
        }
26
        scanner.close();
27
    }
28
29
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
Car
4
Car with 4 doors started\n
Accelerating to 60 mph\n
Applying brakes
Car with 4 doors started\n
Accelerating to 60 mph\n
Applying brakes
Passed
Test Case 2
Bike
2
Bike with 2 wheels started\n
Accelerating to 40 mph\n
Applying brakes
Bike with 2 wheels started\n
Accelerating to 40 mph\n
Applying brakes
Passed



No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.