Java_Unit-2_lec-11_NeoColab_COD

Close-up of a computer screen displaying programming code in a dark environment.
Spread the love

  1. Problem Statement


    Samantha wants to develop a program to organize a set of numbers in an array. The program should accept the array size (n) and n integers as input, categorize them as even and odd, and display the grouped numbers.


    It should contain a class called NumberClassifier with attributes including n (array size) and an array to store input numbers.
    Input format :
    The first line consists of an integer n, representing the size of the array.
    The second line consists of n space-separated integers representing the array elements.
    Output format :
    The first line prints “Even numbers:” followed by the even numbers from the given array, separated by a space.
    The second line prints “Odd numbers:” followed by the odd numbers from the given array, separated by a space.


    Refer to the sample output for the formatting specifications.
    Code constraints :
    In this scenario, the test cases fall under the following constraints:
    1 ≤ n ≤ 10
    1 ≤ Each element in the array ≤ 1000
    Sample test cases :
    Input 1 :
    5 9 2 11 4 8
    Output 1 :
    Even numbers: 2 4 8 Odd numbers: 9 11
    Input 2 :
    10 6 2 4 3 7 13 11 10 15 22
    Output 2 :
    Even numbers: 6 2 4 10 22 Odd numbers: 3 7 13 11 15
import java.util.Scanner;
// You are using Java
class NumberClassifier {
    int n;
    //type your code here
    void classifyNumbers(int arr[]){
       System.out.println("");
    }
    
    void printEvenNumbers(int arr[]){
        System.out.print("Even numbers: ");
        for(int i = 0; i < n; i++){
            if(arr[i] % 2 == 0){
                System.out.print(arr[i]+" ");
            }
        }
    
    }
    void printOddNumbers(int arr[]){
        System.out.println("\nOdd numbers: ");
        for(int i = 0; i < n; i++){
            if(arr[i] % 2 != 0){
                System.out.print(arr[i]+" ");
            }
        }
    }
    
    
    
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        NumberClassifier obj = new NumberClassifier();
        obj.n = scanner.nextInt();
        int[] arr = new int[obj.n];
        
        for (int i = 0; i < obj.n; i++) {
            arr[i] = scanner.nextInt();
        }
        
        obj.classifyNumbers(arr);
        obj.printEvenNumbers(arr);
        obj.printOddNumbers(arr);

        scanner.close();
    }
}

2. Problem Statement

Arun is working on a project to convert seconds to a time format. He wants to create a program that accepts a time duration in seconds and converts it into hours, minutes, and seconds.

Help him write a logic under class SecondsToTime with a constructor that gets input in seconds and converts it into hh:mm:ss format.

Input format :

The input consists of a single integer, representing the time duration in seconds.

Output format :

The output prints the converted time in the format ‘hh:mm:ss’ where hh represents hours, mm represents minutes, and ss represents seconds.

Refer to the sample output for formatting specifications.

Code constraints :

In this scenario, the test cases fall under the following constraints:

1 ≤ Time in Seconds ≤ 106

Sample test cases :

Input 1 :

3665

Output 1 :

01:01:05

Input 2 :

7200

Output 2 :

02:00:00

Input 3 :

60

Output 3 :

00:01:00
import java.util.Scanner;
// You are using Java
class SecondsToTime {
   //type your code here
   private int hour;
   private int min;
   private int sec;
   
   public SecondsToTime(int totalseconds){
       this.hour = (totalseconds / 3600);
       this.min = (totalseconds % 3600)/60;
       this.sec = totalseconds % 60;
   }
   
   void displayTime(){
       System.out.printf("%02d:%02d:%02d%n", hour, min, sec);
   }
   
}
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int totalSeconds = scanner.nextInt();
        scanner.close();

        SecondsToTime timeConverter = new SecondsToTime(totalSeconds);
        timeConverter.displayTime();
    }
}

Source: NeoColab(for educational purpose)


Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top