Unit-2_Lec-13_NeoColab_Java

Spread the love

Problem Statement

Meet Alex, an enthusiastic developer working on a project. Alex needs to create a program that keeps track of button presses. The challenge is to utilize the “this” keyword to increment the count every time the button is pressed. The program should allow Sarah to input the number of button presses and, using “this,” display the final count.

Input format :

The input contains an integer ‘n’, representing the number of button presses.

Output format :

The output displays “Final Count: ” <incremented final value>

If the negative value is provided as an input, it displays the default output “Final Count: 0 “

Refer to the sample output for formatting specifications.

Code constraints :

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

-100000000 ≤ n ≤ 100000000

Sample test cases :

Input 1 :

20

Output 1 :

Final Count: 20

Input 2 :

-100

Output 2 :

Final Count: 0
import java.util.Scanner;
class IncrementalCounter {
    //type your code here
        private int count;

    // Constructor using "this" keyword
    public IncrementalCounter() {
        // Ensure count is not negative
        this.count = 0;
    }
    public void increment(){
        this.count++;
    }
    // Method to display the final count
    public int getCount() {
        return this.count;
    }
}
class MainApp {
    public static void main(String[] args) {
        IncrementalCounter counter = new IncrementalCounter();
        Scanner scanner = new Scanner(System.in);
        int numIncrements = scanner.nextInt();

        for (int i = 0; i < numIncrements; i++) {
            counter.increment();
        }

        System.out.println("Final Count: " + counter.getCount());
    }
}

Problem Statement

Bob is curious about palindrome numbers and wants a program to check if a given integer is a palindrome. The program should take an integer input, use a class with a parameterized constructor and an initializer block to initialize values, and implement a method to check and display whether the given number is a palindrome or not.

Assist Bob to create the program.

Input format :

The input consists of a single integer representing the input number.

Output format :

The output prints whether the given integer is a palindrome or not.

Refer to the sample output for formatting specifications.

Code constraints :

The given test cases fall under the following constraints:

11 ≤ input number ≤ 109

Sample test cases :

Input 1 :

121

Output 1 :

121 is a Palindrome

Input 2 :

1000

Output 2 :

1000 is not a Palindrome

Input 3 :

123321

Output 3 :

123321 is a Palindrome
import java.util.Scanner;
// You are using Java
class PalindromeChecker{
    private int number;
    
    public PalindromeChecker(int number){
        this.number = number;
    }
    
    public boolean isPalindrome(){
        int orgNum = number;
        int revNum = 0;
        int temp = orgNum;
        
        while(temp > 0){
            int digit = temp % 10;
            revNum = revNum*10 + digit;
            temp /= 10;
        }
        
        return orgNum == revNum;
    }
    
    public void displayPalindromeCheckResult(){
        if(isPalindrome()){
            System.out.println(number + " is a palindrome");
        }else{
            System.out.println(number + " is not a palindrome");
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int inputNumber = scanner.nextInt();

        PalindromeChecker palindromeChecker = new PalindromeChecker(inputNumber);

        palindromeChecker.displayPalindromeCheckResult();

        scanner.close();
    }
}

Spread the love

Leave a Comment

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

Scroll to Top