String

 String Question

1.      Write a program to input a sentence and convert it into uppercase and count and display the total number of words starting with the letter ‘A’.

Example:

Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING.

Sample Output: Total number of words starting with letter ‘A’ = 4.

 

SOLUTION

               

import java.util.Scanner;

class String2019{

    public static void main(String args[])

               {   Scanner sc = new Scanner(System.in);

        System.out.print("Please enter the sentence: ");

        String s = sc.nextLine().toUpperCase();

                               s=" "+s;

        int count = 0;

      

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

            char ch = s.charAt(i);

            if(ch==' ')

                                              {

                if(s.charAt(i+1) == 'A')

                    count++;

            }   

        }

        System.out.println("Total number of words starting with letter 'A' = " + count);

    }

}

 

 

2.      Write a program in Java to accept a string in lowercase and change the first letter of every word to uppercase. Display the new string.

 

Sample INPUT: we are in cyber world

Sample OUTPUT: We Are In Cyber World

 

SOLUTION

               

import java.util.Scanner;

class String2018{

    public static void main(String args[])

               {    Scanner sc = new Scanner(System.in);

        System.out.print("Please enter the string: ");

        String s = sc.nextLine().toLowerCase();

                               s=" "+s;

        String snew="";

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

            if(s.charAt(i) == ' ')

                                              { snew = snew + " "+Character.toUpperCase(s.charAt(i+1));

                                              i++;

                                              }

            else

                snew = snew + s.charAt(i);

        }

        System.out.println(snew.trim());

    }

}

 

         

3.      Define a class Electric Bill with the following specifications:

 

Instance Variable/ data member:

 

String n – to store the name of the customer

int units – to store the number of units consumed

double bill – to store the amount to paid

 

Member methods:

 

Void accept() – to accept the name of the customer and number of units consumed

Void calculate() – to calculate the bill as per the following tariff :

 

Number of units               Rate per unit

 

First 100 units                         Rs.2.00

Next 200 units                         Rs.3.00

Above 300 units                      Rs.5.00

 

A surcharge of 2.5% charged if the number of units consumed is above 300 units.

 

Void print() – To print the details as follows :

Name of the customer ………

Number of units consumed ……

Bill amount …….

 

Write a main method to create an object of the class and call the above member methods.

 

SOLUTION

               

import java.util.Scanner;

class ElectricBill {

 

   String n;

   int units;

   double bill;

 

  public void accept() {

    Scanner sc = new Scanner(System.in);

    System.out.print("Please Enter name: ");

    n = sc.next();

    System.out.print("Please Enter units: ");

    units = sc.nextInt();

  }

 

  public void calculate() {

    if (units <= 100) {

      bill = units * 2;

    } else if (units > 100 && units <= 300) {

      bill = 100 * 2 + (units - 100) * 3;

    } else {

      bill = 100 * 2 + 200 * 3 + (units - 300) * 5;

      double surcharge = bill * 2.5 / 100;

      bill = bill + surcharge;

    }

  }

 

  public void print() {

    System.out.println("Name of the customer is" + n);

    System.out.println("Number of units consumed is " + units);

    System.out.println("Bill amount is " + bill);

  }

 

  public static void main(String[] args) {

    ElectricBill obj = new ElectricBill();

    obj.accept();

    obj.calculate();

    obj.print();

  }

}

 

 

4.      Special words are those words which starts and ends with the same letter.

Examples:

EXISTENCE

COMIC

WINDOW

 

Palindrome words are those words which read the same from left to right and vice-versa.

Examples:

MALAYALAM

MADAM

LEVEL

ROTATOR

CIVIC

 

All palindromes are special words, but all special words are not palindromes.

 

Write a program to accept a word and check and print whether the word is a palindrome or only special word.

 

SOLUTION

               

import java.util.Scanner;

public class String2016 {

 

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.print("Please enter any word: ");

    String word = sc.next();

 

    // Check if word is palindrome

    String reverseString = "";

    int l= word.length();

    for (int i = 0; i< l; i++) {

      reverseString = word.charAt(i)+reverseString;

     

    }

    if (word.equalsIgnoreCase(reverseString)) {

      System.out.println("Both Palindrome and special word");

    }

 

    // Check if word is only special word

    else if (word.charAt(0) == word.charAt(l-1)) {

      System.out.println("Special word only");

    }

    else

    {

      System.out.println("Neither Palindrome nor special word");

    }

  }

}

 

 

5.      Define a class ParkingLot with the following description:

 

Instance variables/data members:

 

int vno – To store the vehicle number

int hours – To store the number of hours the vehicle is parked in the parking lot

double bill – To store the bill amount

 

Member methods:

 

void input() – To input and store vno and hours

void calculate() – To compute the parking charge at the rate of Rs.3 for the first hour or part thereof,

and Rs.1.50 for each additional hour or part thereof.

void display() – To display the detail

Write a main method to create an object of the class and call the above methods

 

SOLUTION

               

import java.util.Scanner;

 

  class ParkingLot {

  int vno;

  int hours;

  double bill;

 

  public void input() {

    Scanner sc = new Scanner(System.in);

    System.out.print("Please Enter vehicle number: ");

    vno = sc.nextInt();

    System.out.print("Please Enter hours: ");

    hours = sc.nextInt();

  }

 

  public void calculate() {

    bill = 3 + (hours - 1) * 1.50;

  }

 

  public void display() {

    System.out.println("Vehicle number is: " + vno);

    System.out.println("Hours is: " + hours);

    System.out.println("Bill is: Rs. " + bill);

  }

 

  public static void main(String[] args) {

    ParkingLot object = new ParkingLot();

    object.input();

    object.calculate();

    object.display();

  }

}

 

         

6.      Define a class named movieMagic with the following description:

 

Instance variables/data members:

int year – to store the year of release of a movie

String title – to store the title of the movie.

float rating – to store the popularity rating of the movie.

(minimum rating = 0.0 and maximum rating = 5.0)

 

Member Methods:

 

(i) movieMagic() Default constructor to initialize numeric data members to 0 and String data member to "".

(ii) void accept() To input and store year, title and rating.

(iii) void display() To display the title of a movie and a message based on the rating as per the table below.

 

RATING                                                MESSAGE TO BE DISPLAYED

 

0.0 to 2.0                                                                Flop

2.1 to 3.4                                                                Semi-hit

3.5 to 4.5                                                                Hit

4.6 to 5.0                                                                Super Hit

 

Write a main method to create an object of the class and call the above member methods.

 

SOLUTION

 

import java.util.Scanner;

class movieMagic {

    int year;

    String title;

    float rating;

 

    public movieMagic() {

        year = 0;

        title = "";

        rating = 0;

    }

 

    public void accept() {

        Scanner sc = new Scanner(System.in);

        System.out.print("Please enter title:- ");

        title = sc.nextLine();

        System.out.print("Please enter year:- ");

        year = sc.nextInt();

        System.out.print("Please enter rating between 0.0 to 5.0:- ");

        rating = sc.nextFloat();

    }

 

    public void display() {

        System.out.println("Movie Title:- " + title);

        if (rating >= 0 && rating <= 2.0) {

             System.out.println("Flop");

         } else if (rating >= 2.1 && rating <= 3.4) {

             System.out.println("Semi-hit");

         } else if (rating >= 3.5 && rating <= 4.5) {

             System.out.println("Hit");

         } else if (rating >= 4.6 && rating <= 5.0) {

            System.out.println("Super Hit");

        }

    }

 

    public static void main(String[] args) {

        movieMagic movie = new movieMagic();

        movie.accept();

        movie.display();

    }

 

}

         

 

7.      Write a program that encodes a word into Piglatin. To translate word into Piglatin word, convert the word into uppercase and then place the first vowel of the original word as the start of the new word along with the remaining alphabets. The alphabets present before the vowel being shifted towards the end followed by “AY”.

 

Sample Input 1: London

Output: ONDONLAY

Sample Input 2: Olympics

Output: OLYMPICSAY

 

SOLUTION

               

import java.util.Scanner;

class String2013{

    public static void main(String args[])

  {

        Scanner sc= new Scanner(System.in);

        System.out.print("Please enter any Word: ");

        String str = sc.next().toUpperCase();

        str = str.trim();

        String newstr = "";

        int pos = 0;

       

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

            char ch = str.charAt(i);

            if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')

            {    pos = i;

                break;

      }

            }

       

    newstr = str.substring(pos)+ str.substring(0, pos)+"AY";

        System.out.println(newstr);

    }

}

 

 

8.      Write a program to accept a string. Convert the string to uppercase. Count and output the number of double letter sequences that exist in the string.

 

Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”

Sample Output: 4

 

SOLUTION  

   

import java.util.Scanner;

class String2012{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        System.out.print("Please enter any String: ");

        String s = sc.nextLine().toUpperCase();

        int l = s.length();

        int count = 0;

        if(l > 1){

            for(int i = 0; i < l - 1; i++)

            {

                char ch1 = s.charAt(i);

                char ch2 = s.charAt(i + 1);

                if(Character.isLetter(ch1) && Character.isLetter(ch2))

                {    if(ch1 == ch2)

                        count++;

                }

            }

        }

        System.out.println(count);

    }

}

         

 

9.      Write a program to accept a word and convert it into lowercase if it is in uppercase, and display the new word by replacing only the vowels with the character following it.

 

Example:

Sample Input: computer

Sample Output: cpmpvtfr

 

SOLUTION

               

import java.util.Scanner;

class String2011{

    public static void main(String args[]){

        Scanner sc= new Scanner(System.in);

        System.out.print("Please enter any Word: ");

        String word = sc.next();

        word = word.trim();

       

        word = word.toLowerCase();

        String snew = "";

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

            char ch = word.charAt(i);

                if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')

 

                snew= snew + (char)(ch + 1);

                else

                snew =snew + ch;

            }

       

        System.out.println(snew);

    }

}

         

 

10.  Write a program to input a string in uppercase and print the frequency of each character.

Example:

INPUT: COMPUTER HARDWARE

OUTPUT:

Characters                       Frequency

A                                              2

C                                              1

D                                              1

E                                              2

H                                              1

M                                             1

O                                              1

P                                               1

R                                              3

T                                              1

U                                              1

W                                             1

 

SOLUTION

               

import java.util.Scanner;

class String2010{

    public static void main(String args[])

  {

    Scanner sc = new Scanner(System.in);

     System.out.print("Please Enter any String: ");

        String s = sc.nextLine().toUpperCase();

    System.out.println("Characters\t Frequency");

        for(char alpha = 'A'; alpha <= 'Z'; alpha++){

            int count = 0;

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

                char ch = s.charAt(i);

                if(ch == alpha)

                    count++;

            }

            if(count > 0)

                System.out.println(alpha + "\t\t\t" + count);

        }

    }

}

 

         

11.  An electronics shop has announced the following seasonal discounts on the purchase of certain items:

 

Purchase amount in Rs.            Discount on Laptop         Discount on Desktop PC

 

0 – 25000                                                0.0%                                     5.0%

25001 – 57000                                        5.0%                                     7.6%

57001 – 100000                                      7.5%                                     10%

More than 100000                                 10.0%                                   15.0%

 

Write a program based on the above criteria to input name, address, amount of purchase and the type of purchase (L for Laptop and D for Desktop) by a customer. Compute and print the net amount to be paid by a customer along with his name and address.

 

(Hint: Discount = (discount rate / 100) * amount of purchase, Net amount = amount of purchase – discount)

 

SOLUTION

               

import java.util.Scanner;

class Shop{

    public static void main(String args[]){

        Scanner sc = new Scanner(System.in);

        System.out.print("Name: ");

        String name = sc.nextLine();

        System.out.print("Address: ");

        String address = sc.nextLine();

        System.out.print("Amount of purchase: ");

        double amount = sc.nextDouble();

        System.out.println("Please enter type of purchase:");

        System.out.print("L for Laptop and D for Desktop");

 

        char type = sc.next().charAt(0);

        type=Character.toUpperCase(type);

 

        double discount = 0.0;

        if(amount <= 25000){

            if(type == 'L')

                discount = 0.0;

            else if(type == 'D')

                discount = 5.0;

        }

        else if(amount <= 57000){

            if(type == 'L')

                discount = 5.0;

            else if(type == 'D')

                discount = 7.6;

        }

        else if(amount <= 100000){

            if(type == 'L')

                discount = 7.5;

            else if(type == 'D')

                discount = 10.0;

        }

        else{

            if(type == 'L')

                discount = 10.0;

            else if(type == 'D')

                discount = 15.0;

        }

        discount = discount / 100 * amount;

        double net = amount - discount;

        System.out.println("Customer's name: " + name);

        System.out.println("Address: " + address);

        System.out.println("Net Amount: " + net);

    }

}

 

No comments:

Post a Comment