Friday, 10 June 2022

A paper roll manufacturing company offers discount to the dealer and retailer based on the length of the

paper roll as per the following criteria:
Length of the paper Dealer Retailer
Up to 10 metres 10% 8%
11 metres and up to 20 metres 15% 10%
More than 20 metres 20% 15%
Write a program to input the length of paper roll and the amount of purchase. Use menu driven approach
having choices (D) for dealer and (R) for retailer to find and print the amount to be paid to the company after
discount.
For an inappropriate choice, display “Wrong Choice”.



import java.util.*;
public class Assignment_5
{
    public static void main(String args[])
    {
        String ch = ""; 
        int Pay = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("User menu");
        System.out.println("D for Dealer");
        System.out.println("R for Retailer");
        System.out.print("Enter your Choice");
        ch = sc.nextLine();
        System.out.print("Enter the lenght of paper roll");
        int len = sc.nextInt();
        System.out.print("Enter the amount of purchase");
        int pur = sc.nextInt();
        switch(ch)
        {
            case "D":{
                    if(len > 0 && len <= 10)
                    {
                        Pay = pur-((pur * 10) / 100);
                    }
                    else if(len > 10 && len <= 20)
                    {
                        Pay = pur-((pur * 15) / 100);
                    }
                    else if(len > 20)
                    {
                        Pay = pur-((pur * 20) / 100);
                    }
                    System.out.println("Amount to be paid: " + Pay);
            }
            break;
            case "R" :{
                    if(len > 0 && len <= 10)
                    {
                        Pay = pur-((pur * 8) / 100);
                    }
                    else if(len > 10 && len <= 20)
                    {
                        Pay = pur-((pur * 10) / 100);
                    }
                    else if(len > 20)
                    {
                        Pay = pur-((pur * 15) / 100);
                    }
                    System.out.println("Amount to be paid: " + Pay);
            }
            break;
            default : System.out.println("Wrong Choice");
        }
    }
}

Define a class called ‘Mobike’ with the following specifications:

 Instance variable/ Data members:
int bno : to store the bike number
int phno : to store the phone number of the customer
String name : to store the name of the customer
int days : to store the number of days the bike is taken out on rent
int charge : to calculate and store the rental charge
Member methods :
void input ( ) : to input and store the details of the customer
void compute ( ) : to compute the rental charge
The rent for a mobike is charged on the following basis:
For first five days : Rs. 500 per day
For the next five days : Rs. 400 per day
Rest of the days : Rs. 200 per day
void display ( ) : to display the details in the following format:
Bike No. Phone No. Name No. of days Charge
***** ****** **** ******** ****


import java.util.*;
public class Assignment_4
{
    static int bno;
    static long phno;
    static String name;
    static int days;
    static int charge;
    public static void main(String args[])
    {
        input();
        compute();
        display();
    }
    static void input()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the name of the customer: ");
        name = sc.nextLine();
        System.out.print("Enter the mobile number: ");
        phno = sc.nextLong();
        System.out.print("Enter the bike number: ");
        bno = sc.nextInt();
        System.out.print("Enter the number of days: ");
        days = sc.nextInt();
    }
    static void compute()
    {
        if(days > 0 && days <= 5)
        {
            charge = days * 500;
        }
        else if(days > 5 && days <= 10)
        {
            charge = (5 * 500) + ((days - 5) * 400);
        }
        else if(days > 10)
        {
            charge = (5 * 500) + (5 * 400) + ((days - 10) * 200);
        }
    }
    static void display()
    {
        System.out.println("Bike No. \t Phone No. \t\t Name \t \t\tNo. of Days \t Charges");
        System.out.println(bno + " \t  \t " + phno + " \t  \t " + name + " \t  \t " + days + " \t  \t " + charge);
    }
}

Given below is a hypothetical table showing rate of income tax from male citizens below the age of 65 year:

Taxable Income (TI) Income Tax

Up to Rs. 5,00,000 Nil
More than Rs. 5,00,000 and less than or equal to Rs. 10,00,000 (TI – 5,00,000) x 10%
More than R. 10,00,000 and less than or equal to Rs. 20,00,000 (TI – 10,00,000) x 20% + 30,000
More than Rs. 20,00,000 (TI – 20,00,000) x 30% + 90,000
Write a program to input the age, gender (male or female) and taxable income of a person. If the age is more
than 65 years or the gender is female, display “Wrong category”.
If the age is less than or equal to 65 years and the gender is male, compute and display the income tax
payable as per the table given above.




import java.util.*;
public class Assignment3_Tridib
{
    public static void main(String args[])
    {
        String gen="";
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the age of a person: ");
        int age = sc.nextInt();
        System.out.print("Enter the gender: ");
        gen = sc.next();
        
        
        if(age < 65 && gen.equalsIgnoreCase("male"))
        {
            System.out.print("Enter the taxable income: ");
            int TI = sc.nextInt();
        
            if(TI <= 500000)
            {
                System.out.println("Nil");
            }
            else if(TI > 500000 && TI <= 1000000)
            {
                System.out.println("Income tax payable: " + (TI - 500000) * 0.1);
            }
            else if(TI > 1000000 && TI <= 2000000)
            {
                System.out.println("Income tax payable: " + ((TI - 1000000) * 0.2) + 30000);
            }
            else if(TI > 2000000)
            {
                System.out.println("Income tax payable: " + ((TI - 2000000) * 0.3) + 90000);
            }
        }
        else
            System.out.println("Wrong category");
    }
}





Output:




A company announces an increment of their employees on the seniority basis (length of service) as per the given tariff:

Length of service Increment Dearness Allowance
Up to 10 years 20% of basic 10% of basic
Above 10 and up to 20 years 25% of basic 15% of basic
Above 20 years 30% of basic 20% of basic
Write a program to find new basic by using the following class specifications:
Class : Increment
Data members/Instance variables:
String name : name of the employee
double basic : basic pay of the employee
int pds : length of service of the employee
Member methods :
void getdata(String n, int p, : to accept name, length of service, basic
double bs, double d) and DA
void calculate( ) : to find increment and update basic salary.
void display ( ) : to display length of service and update basic salary in
the format given below:




import java.util.*;
public class Increment
{
    static String name = "";
    static double basic;
    static int pds;
    static double DA;
    public static void main(String args[])
    {
        String n = ""; int p; double bs; double d = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("Name: ");
        n = sc.nextLine();
        System.out.print("Enter the basic pay: ");
        bs = sc.nextDouble();
        System.out.print("Enter the length of service: ");
        p = sc.nextInt();
        
        if(p<= 10)
        {
            d = (bs * 10) / 100;
        }
        else if(p > 10 && p <= 20)
        {
            d = (bs * 15) / 100;
        }
        else if(p > 20)
        {
            d = (bs * 20) / 100;
        }
        
        getdata(n, p, bs, d);
    }
    static void getdata(String n, int p, double bs, double d)
    {
        name = n;
        pds = p;
        basic = bs;
        DA = d;
        
        calculate();
    }
    static void calculate()
    {
        double inc = 0;;
        if(pds<= 10)
        {
            inc = (basic * 20) / 100;
        }
        else if(pds > 10 && pds <= 20)
        {
            inc = (basic * 25) / 100;
        }
        else if(pds > 20)
        {
            inc = (basic * 30) / 100;
        }
        //basic = basic + inc;
        
        display();
    }
    static void display()
    {
        System.out.println("Name \t Length of service \t Basic salary \t\t DA");
        System.out.println(name + " \t " + pds + " \t \t " + basic + " \t\t " + DA);
    }
}



Output:




Write a program to accept a word. Check and display whether the word is a palindrome or only a special word or none of them.

Special words are those words which start and end with the same letter.
Example: EXISTENCE, COMIC, WINDOW
Palindrome words are those words which read the same from left to right and vice-versa.
Example: MALYALAM, MADAM, LEVEL, ROTATOR, CIVIC
All palindromes are special words but all special words are not palindromes.


import java.util.Scanner;

public class assign20_Tridib
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String str = in.next();
        str = str.toUpperCase();
        int len = str.length();
        
        if (str.charAt(0) == str.charAt(len - 1)) {
            boolean isPalin = true;
            for (int i = 1; i < len / 2; i++) {
                if (str.charAt(i) != str.charAt(len - 1 - i)) {
                    isPalin = false;
                    break;
                }
            }
            
            if (isPalin) {
                System.out.println("Palindrome");
            }
            else {
                System.out.println("Special");
            }
        }
        else {
            System.out.println("Neither Special nor Palindrome");
        }
        
    }
}




Output:



Write a program to accept a sentence in mixed case. Find the frequency of vowels in each word and print the words along with their frequencies in separate lines.

Sample Input: We are learning scanner class
Sample Output:
We are learning scanner class
Word Frequency of vowels
we 1
are 2
learning 3
scanner 2
class 1



import java.util.Scanner;

public class Assignment19
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a sentence :");
        while (in.hasNext()) {
            String orgWord = in.next();
            String word = orgWord.toUpperCase();
            if (word.equals("."))
                break;
            int vowelFreq = 0;
            for (int i = 0; i < word.length(); i++) {
                if (word.charAt(i) == 'A' ||
                    word.charAt(i) == 'E' ||
                    word.charAt(i) == 'I' ||
                    word.charAt(i) == 'O' ||
                    word.charAt(i) == 'U')
                    vowelFreq++;
            }
            System.out.println(orgWord + "\t\t" + vowelFreq);
        }

    }
}


Output:




Write a program in Java to enter a String/Sentence and display the longest word and the length of the longest word present in the String.

Sample Input: “TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN BAGAN”
Sample Output:
The longest word: FOOTBALL
The length of the word: 8


import java.util.Scanner;

public class assign17
{
    public static void main(String args[]) {
       Scanner in = new Scanner(System.in);
       System.out.println("Enter a word or sentence:");
       String str = in.nextLine();
       
       str += " "; //Add space at end of string
       String word = "", lWord = "";
       int len = str.length();
       
       for (int i = 0; i < len; i++) {
           char ch = str.charAt(i);
           if (ch == ' ') {
               
                if (word.length() > lWord.length())
                    lWord = word;
                    
                word = "";
           }
           else {
               word += ch;
           }
       }
       
       System.out.println("The longest word: " + lWord +
        ": The length of the word: " + lWord.length());
    }
    
}



Output:



Write a program to input a sentence. Convert in Upper case and shift each letter of the English alphabet two steps towards the right. Characters other than alphabets remain the same. Print original and new string in two different lines.

Sample Input: ABACUS12@RYZ
Sample Output: CDCEWU12@TAB


//ABACUS12@RYZ
import java.util.Scanner;
public class Assignment16
{
   public static void main(String args[]) 
   {
       int a,p,i;
       String st,str="";
       char chr;
       Scanner in = new Scanner(System.in);
       System.out.println("Enter a sentence:");
       st=in.next();
       p=st.length();       
        for(a=0;a<p;a++)
        {
            chr=st.charAt(a);
      
                if (chr>='A' && chr<='X')
                { i=chr;
                  i=i+2;
                  char c=(char)i;
                  str=str+c;
                } 
                else if (chr>'X' && chr<='Z')
                { i=chr;
                  i=i-24;
                  char c1=(char)i;
                  str=str+c1;
                }    
               else
               str=str+chr;
        }  
            System.out.print(str); 
}
}

Using the switch statement, write a menu driven program to calculate the maturity amount of a Bank Deposit. The user is given the following options:

  1. Term Deposit
  2. Recurring Deposit

For option 1, accept principal (P), rate of interest(r) and time period in years(n). Calculate and output the maturity amount(A) receivable using the formula:

A = P[1 + r / 100]n

For option 2, accept Monthly Installment (P), rate of interest (r) and time period in months (n). Calculate and output the maturity amount (A) receivable using the formula:

A = P x n + P x (n(n+1) / 2) x r / 100 x 1 / 12

For an incorrect option, an appropriate error message should be displayed.


import java.util.Scanner;

public class BankDeposit_Assign1_Tridib
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Type 1 for Term Deposit");
        System.out.println("Type 2 for Recurring Deposit");
        System.out.print("Enter your choice: ");
        int ch = in.nextInt();
        double p = 0.0, r = 0.0, a = 0.0;
        int n = 0;
        
        switch (ch) {
            case 1:
            System.out.print("Enter Principal: ");
            p = in.nextDouble();
            System.out.print("Enter Interest Rate: ");
            r = in.nextDouble();
            System.out.print("Enter time in years: ");
            n = in.nextInt();
            a = p * Math.pow(1 + r / 100, n);
            System.out.println("Maturity amount = " + a);
            break;
            
            case 2:
            System.out.print("Enter Monthly Installment: ");
            p = in.nextDouble();
            System.out.print("Enter Interest Rate: ");
            r = in.nextDouble();
            System.out.print("Enter time in months: ");
            n = in.nextInt();
            a = p * n + p * ((n * (n + 1)) / 2) * (r / 100) * (1 / 12.0);
            System.out.println("Maturity amount = " + a);
            break;
            
            default:
            System.out.println("Invalid choice");
        }
    }
}

Thursday, 12 May 2022

Write a program to accept a word and convert it into lower case, if it is in upper case. Display the new word by replacing only the vowels with the letter following it.

Sample Input  : computer
Sample Output :  cpmpvtfr


import java.util.Scanner;
public class VowelReplace_Tridib
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a word: ");
String str = in.nextLine();
str = str.toLowerCase();
String newStr = "";
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (str.charAt(i) == 'a' ||
str.charAt(i) == 'e' ||
str.charAt(i) == 'i' ||
str.charAt(i) == 'o' ||
str.charAt(i) == 'u') {
char nextChar = (char)(ch + 1);
newStr = newStr + nextChar;
}
else {
newStr = newStr + ch;
}
}
System.out.println(newStr);
}
}



Output:



Friday, 18 June 2021

Write a program to accept the name and display the initials along with the surname.

Sample Input  : Mohandas Karamchand Gandhi
Sample Output : M.K.Gandhi


import java.util.*;
public class Surname_Tridib
{
public static void main(String args[])
{
String st,sn,st1="",st2="";
int i,p;
char ch;
Scanner in = new Scanner(System.in);
System.out.println("Enter the name:");
st = in.nextLine();
st=' '+st;
p=st.lastIndexOf(' ');
sn=st.substring(p);
for(i=0;i<p;i++)
{
    ch=st.charAt(i);
    if(ch==' ')
    st1=st1+st.charAt(i+1)+'.';
    }
st2=st1+sn;
System.out.println(st2);
}
}



Output:






Accept a sentence and convert first letter of each word in upper case.

Sample Input  : i love my school
Sample Output : I Love My School



import java.util.Scanner;
public class uppconvert_Tridib
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String str1=" ";
int i,p;
char chr,chr1;
str=' '+str;
p=str.length();
for(i=0;i<p;i++)
{
    chr=str.charAt(i);
    if(chr==' ')
    {
        chr1=str.charAt(i+1);
        str1=str1+' '+Character.toUpperCase(chr1);
        i=i+1;
    }
    else
    str1=str1+chr;
}
System.out.println(str1);
}
}



Output:




Program to display the following pattern.

 BLUEJ
   BLUE
      BLU
        BL
          B

import java.util.Scanner;

public class pattern_Tridib
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a,b,p,k=1;
String s;
System.out.println("Enter the word");
s=sc.next();
for(a=s.length();a>=0;a--)
{
    for(p=1;p<=k;p++)
    {
        System.out.print(" ");
    }

System.out.println(s.substring(0,a));
k=k+1;
}
}
}