0% found this document useful (0 votes)
42 views

Assignment 3

The document contains 9 programming questions and their solutions. Each question asks the user to write a Java method or program to perform tasks such as counting letters in a string, validating a password, finding common prefixes/substrings between strings, checking for consecutive duplicate values in an array, and determining if two arrays contain identical elements. For each question, the document provides the full code for the method/program and displays sample output.

Uploaded by

Aayush Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Assignment 3

The document contains 9 programming questions and their solutions. Each question asks the user to write a Java method or program to perform tasks such as counting letters in a string, validating a password, finding common prefixes/substrings between strings, checking for consecutive duplicate values in an array, and determining if two arrays contain identical elements. For each question, the document provides the full code for the method/program and displays sample output.

Uploaded by

Aayush Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ASSIGNMENT-2

Name:- Aayush Kumar

Roll no.:- 12112126

Section:- CSB-06

Q1)

//Write a method that counts the number of letters in a string using the
following header:
// public static int countLetters(String s)
// Write a test program that prompts the user to enter a string and displays
the number of letters in the
// string.
import java.util.*;
public class Q1 {
    public static int countLetters(String s) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (Character.isLetter(s.charAt(i)))
            count++;
        }
        System.out.println("Your String contains:- "+ count + " number of
letters.");
        return count;
    }

    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);
        System.out.println("Input String:- ");
        String s = input.nextLine();
        countLetters(s);
    }
}

Output:-

Q2)
// Some websites impose certain rules for passwords. Write a method that
checks whether a string is a
// valid password. Suppose the password rules are as follows:
// ■ A password must have at least eight characters.
// ■ A password consists of only letters and digits.
// ■ A password must contain at least two digits.
// Write a program that prompts the user to enter a password and displays
Valid Password if the rules
// are followed or Invalid Password otherwise.
import java.util.*;

public class Q2 {
    public static void checkPassword(String s) {
        int count = 0;
        if(s.length() < 8) {
            System.out.println("Password too short.");
        } else {
            for(int i = 0;i<s.length();i++) {
                if (Character.isDigit(s.charAt(i)))
                    count++;
                if(Character.isLetterOrDigit(s.charAt(i)) == false) {
                    System.out.println("Invalid Password");
                }
               
            }
            if(count < 2)
                    System.out.println("Invalid Password");
            else
                System.out.println("Valid Password");

           
        }
       
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Input String:- ");
        String s = input.nextLine();
        checkPassword(s);
    }
}

Output:-
Q3)

//Write a method that counts the number of letters in a string using the
following header:
// public static int countLetters(String s)
// Write a test program that prompts the user to enter a string and displays
the number of letters in the
// string.
import java.util.*;
public class Q1 {
    public static int countLetters(String s) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (Character.isLetter(s.charAt(i)))
            count++;
        }
        System.out.println("Your String contains:- "+ count + " number of
letters.");
        return count;
    }

    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);
        System.out.println("Input String:- ");
        String s = input.nextLine();
        countLetters(s);
    }
}

Output:-

Q4)

//Write a program that prompts the user to enter two strings and displays the
largest common prefix
// of the two strings. Here are some sample runs:
// Enter the first string: Welcome to C++
// Enter the second string: Welcome to programming
// The common prefix is Welcome to
// Enter the first string: Atlanta
// Enter the second string: Macon
// Atlanta and Macon have no common prefix

import java.util.*;

public class Q4 {
    public static void commonPrefix(String s1, String s2) {
        String small, large;
        if (s1.length() > s2.length()) {
            small = s2;
            large = s1;
        } else {
            small = s1;
            large = s2;
        }
        int index = 0;
        for (char c : large.toCharArray()) {
            if (index == small.length())
                break;
            if (c != small.charAt(index))
                break;
            index++;
        }
        if (index == 0)
            System.out.println("" + s1 + " and " + s2 + " have no common
prefix");
        else
            System.out.println(large.substring(0, index));
    }

    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);
        System.out.println("Input String:- ");
        String s1 = input.nextLine();
        String s2 = input.nextLine();

        commonPrefix(s1, s2);
    }
}

Output:-
Q5)

// Write a program that prompts the user to enter a Social Security


// number in the format DDD-DD-DDDD, where D is a digit. Your program should
check whether
// the input is valid.

import java.util.Scanner;

public class Q5 {
    public static boolean isSSNValidFormat(String s) {

        for (int i = 0; i < s.length(); i++) {

            char ch = s.charAt(i);
            if (i == 3 && ch != '-')
                return false;
            if (i == 6 && ch != '-')
                return false;
            if (i != 6 && i != 3 && !Character.isDigit(ch))
                return false;
        }
        return true;

    }

    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);
       
        System.out.println("Enter a SSN:- ");
       
        String s = input.nextLine();

        input.close();
        if(isSSNValidFormat(s))
            System.out.println(s + " is a valud social securty number.");
        else
            System.out.println(s + " is a INVALID social security number.");
    }
}

Output:-
Q6)

// Write a program that prompts the user to enter two strings and reports
whether the second string is a substring of the first String

import java.util.*;

public class Q6 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Input String s1:- ");
        String s1 = input.nextLine();
        System.out.println("Input String s2:- ");
        String s2 = input.nextLine();

        if (s1.contains(s2))
            System.out.println(s2 + " is a substring of " + s1);
        else
            System.out.println(s2 + "Not a substring of " + s1);
    }    
}

Output:-

Q7)

// Write the following method that tests whether the array has four
consecutive numbers with the
// same value.
import java.util.*;

public class Q7 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of values:");

        int[] values = new int[input.nextInt()];

        System.out.print("Enter the number of values:");


        for (int i = 0; i < values.length; i++) {
            values[i] = input.nextInt();
        }

        if (isConsecutiveFour(values)) {
            System.out.println("The list has consecutive fours");
        } else {
            System.out.println("The list has no consecutive fours");
        }

    }

    public static boolean isConsecutiveFour(int[] values) {

        int start = values[0];


        int count = 1;
        for (int i = 1; i < values.length; i++) {
            if (start != values[i]) {
                start = values[i];
                count = 1;
            } else {

                count++;
            }

            if (count == 4)
                break;

        }

        if (count == 4)
            return true;

        return false;

    }
}

Output:-

Q8)
import java.util.Scanner;
public class Q8 {

    static final int SIZE = 10;

    public static void main(String[] args) {

        int[] numbers = new int[SIZE];


        Scanner input = new Scanner(System.in);

        System.out.print("Enter " + SIZE + " numbers: ");


        for (int i = 0; i < numbers.length; i++) numbers[i] = input.nextInt();

        printArray(eliminateDuplicates(numbers), 10);

    }

    public static int[] eliminateDuplicates(int[] list) {

        int[] temp = new int[list.length];


        int tempIndex = 0;
        for (int i = 0; i < list.length; i++) {

            boolean isDuplicate = false;


            for (int k = 0; k < list.length; k++) {
                if (temp[k] == list[i]) {
                    isDuplicate = true;

                }
            }
            if (!isDuplicate) {
                temp[tempIndex++] = list[i];
            }

        }
        int[] trimmedArray = new int[tempIndex];
        for (int i = 0; i < tempIndex; i++) {
            trimmedArray[i] = temp[i];
        }

        return trimmedArray;
    }

    public static void printArray(int[] array, int numberPerLine) {

                for (int i = 0; i < array.length; i++) {

                    System.out.printf("%2d ", array[i]);


                    if ((i + 1) % numberPerLine == 0) System.out.println("");
                }
    }
}

Output:-

Q9)

// The arrays list1 and list2 are identical if they have the same contents.
Write a method that
// returns true if list1 and list2 are identical, using the following header:

import java.util.*;

public class Q9 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter list1: ");


        int[] list1 = new int[input.nextInt()];
        for (int i = 0; i < list1.length; i++)
            list1[i] = input.nextInt();    

        System.out.print("Enter list2: ");


        int[] list2 = new int[input.nextInt()];
        for (int i = 0; i < list2.length; i++)
            list2[i] = input.nextInt();

        System.out.println("Two lists are" + (equals(list1, list2) ? " " : "


not ")
            + "identical");
    }

    public static boolean equals(int[] list1, int[] list2) {


        if (list1.length != list2.length)
            return false;

        sort(list1);
        sort(list2);
        for (int i = 0; i < list1.length; i++) {
            if (list1[i] != list2[i])
                return false;
        }
        return true;
    }

    public static void sort(int[] list) {


        for (int i = 0; i < list.length - 1; i++) {
            int min = list[i];
            int minIndex = i;

            for (int j = i + 1; j < list.length; j++) {


                if (list[j] < min) {
                    min = list[j];
                    minIndex = j;
                }
            }

            if (minIndex != i) {
                list[minIndex] = list[i];
                list[i] = min;
            }
        }
    }
}

Output:-

You might also like