Formatting page ...
BASIC JAVA PROGRAMS 1. 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.0% More than 100000 10.0% 15.0% Write a program based on the above criteria to input name, address, amount of purchase and 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.io.*; public class Electronics { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter name: "); String name = br.readLine(); System.out.print("Enter address: "); String address = br.readLine(); System.out.print("Enter type of purchase: "); String type = br.readLine(); System.out.print("Enter amount of purchase: "); int amount = Integer.parseInt(br.readLine()); double discountRate = 0.0; if (type.equals("L")) { if (amount <= 25000) discountRate = 0; else if (amount >= 25001 && amount <= 57000) discountRate = 5.0; else if (amount >= 57001 && amount <= 100000) discountRate = 7.5; else if (amount > 100000) discountRate = 10.0; } else if (type.equals("D")) { if (amount <= 25000) discountRate = 5.0; else if (amount >= 25001 && amount <= 57000) discountRate = 7.6; else if (amount >= 57001 && amount <= 100000) discountRate = 10.0; else if (amount > 100000) discountRate = 15.0; } double discount = (discountRate / 100) * amount; double netAmount = amount - discount; System.out.println("Name: " + name); System.out.println("Address: " + address); System.out.println("Net Amount: " + netAmount); } } 2. Write a program to input a sentence and print the number of characters found in the longest word of the given sentence. For example is S = India is my country then the output should be 7. Solution: import java.io.*; public class LongestWord { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a sentence: "); String sentence = br.readLine(); int longest = 0; int currentWordLength = 0; for (int i = 0; i < sentence.length(); i++) { char ch = sentence.charAt(i); if (ch == ' ') { if (currentWordLength > longest) { longest = currentWordLength; } currentWordLength = 0; } else { currentWordLength++; } } if (currentWordLength > longest) { longest = currentWordLength; } System.out.println("The longest word has " + longest + " characters"); } } 3. Shasha Travels Pvt. Ltd. gives the following discount to its customers: [15] Ticket amount Discount Above Rs 70000 18% Rs 55001 to Rs 70000 16% Rs 35001 to Rs 55000 12% Rs 25001 to Rs 35000 10% less than Rs 25001 2% Write a program to input the name and ticket amount for the customer and calculate the discount amount and net amount to be paid. Display the output in the following format for each customer : Sl no Name ticket charges Discount Net amount ------- ---------- ---------------- ---------- ---------- (Assume that there are 15 customers, first customer is given the serial no (SlNo.) 1, next customer 2 and so on. Solution: import java.io.*; public class Travels { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter sno: "); int sno = Integer.parseInt(br.readLine()); System.out.print("Enter name: "); String name = br.readLine(); System.out.print("Enter ticket charges: "); int charges = Integer.parseInt(br.readLine()); int discountPercentage = 0; if (charges > 70000) discountPercentage = 18; else if (charges >= 55001 && charges <= 70000) discountPercentage = 16; else if (charges >= 35001 && charges <= 55000) discountPercentage = 12; else if (charges >= 25001 && charges <= 35000) discountPercentage = 10; else if (charges < 25001) discountPercentage = 2; double discount = discountPercentage * charges / 100.0; double netAmount = charges - discount; System.out.println("Sl. No. \t Name \t Ticket Charges \t Discount \t Net Amount"); System.out.println(sno + "\t\t" + name + "\t\t"+charges + "\t\t" + discount + "\t\t" + netAmount); } } 4. 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.io.*; public class StringConversion { public static void main(String arg[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a String: "); String input = br.readLine(); input = input.toLowerCase(); String answer = ""; for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') answer = answer + (char) (c + 1); else answer = answer + c; } System.out.println(answer); } } 5. 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.io.*; public class StringOperations { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a String: "); String input = br.readLine(); input = input.toUpperCase(); int count = 0; for (int i = 1; i < input.length(); i++) { if (input.charAt(i) == input.charAt(i - 1)) count++; } System.out.println(count); } } 6. A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original two-digit number. Example: Consider the number 59. Sum of digits = 5 + 9 = 14 Product of its digits = 5 x 9 = 45 Sum of the sum of digits and product of digits= 14 + 45 = 59 Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, output the message Special 2-digit number otherwise, output the message Not a Special 2-digit number . Solution: import java.io.*; public class SpecialNumber { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter number: "); int number =Integer.parseInt(br.readLine()); int rightDigit = number % 10; int leftDigit = number / 10; int sumOfDigits = rightDigit + leftDigit; int productOfDigits = rightDigit * leftDigit; int sumOfProductAndSum = sumOfDigits + productOfDigits; if (sumOfProductAndSum == number) { System.out.println("Special 2-digit number"); } else { System.out.println("Not a Special 2-digit number"); } } } 7. Using a switch statement, write a menu driven program to convert a given temperature from Fahrenheit to Celsius and vice versa. For an incorrect choice, an appropriate error message should be displayed. HINT: C = 5/9 x (F 32) and F = 1.8 x C + 32 Solution: import java.io.*; class Temperature { public static void main (String args[ ]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("1. Fahrenheit to Celsius"); System.out.println("2. Celsius to Fahrenheit"); System.out.print("Enter choice: "); int choice=Integer.parseInt(br.readLine()); switch(choice) { case 1: System.out.print("Enter the temperature in Fahrenheit to be converted: "); int n=Integer.parseInt(br.readLine()); double C = (n-32) *5/9; System.out.println("Temperature in Celsius " + C); break; case 2: System.out.print("Enter Temperature in Celsius to be converted: "); int n1=Integer.parseInt(br.readLine()); double F = (1.8*n1) + 32; System.out.println("Temperature in Fahrenheit: " + F); break; default: System.out.println("Incorrect Choice"); } } } 8. Write a program that encodes a word into Piglatin. To translate a word into a 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, Sample Output (1) : ONDONLAY Sample Input (2) : Olympics, Sample Output (2) : OLYMPICSAY Solution: import java.io.*; class Piglatin { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); System.out.print("Enter any word: "); String s=br.readLine(); s=s.toUpperCase(); //converting the word into Uppercase int l=s.length(); int pos=-1; char ch; for(int i=0; i<l; i++) { ch=s.charAt(i); if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { pos=i; //storing the index of the first vowel break; } } if(pos!=-1) //printing piglatin only if vowel exists { String a=s.substring(pos); //extracting all alphabets in the word beginning from the 1st vowel String b=s.substring(0,pos); //extracting the alphabets present before the first vowel String pig=a+b+"AY"; //adding "AY" at the end of the extracted words after joining them System.out.println("The Piglatin of the word = "+pig); } else System.out.println("No vowel, hence piglatin not possible"); } } 9. Write a menu driven program for the following conditions. For an incorrect choice, an appropriate error message should be displayed. a) Addition of 2 numbers b) Subtraction of 2 numbers c) Multiplication of 2 numbers d) Division of 2 numbers e) Modulus of 2 numbers Solution: import java.io.*; public class menuExample { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int choice; int value1, value2; double number; do { System.out.println("*** Calculator v1.0***"); System.out.println("1, Addition"); System.out.println("2, Subtration"); System.out.println("3, Multiplication"); System.out.println("4, Division"); System.out.println("5, Modulus"); System.out.println("6. Exit"); System.out.println("**********************"); System.out.println("Please enter your choice:"); choice = Integer.parseInt(br.readLine()); switch(choice) { case 1: System.out.println("addition"); System.out.println("Please enter two values to be added"); value1 = Integer.parseInt(br.readLine()); value2 = Integer.parseInt(br.readLine()); System.out.println(value1 + " + " + value2 + " = " + (value1+value2)); break; case 2: System.out.println("subraction"); System.out.println("Please enter two values to be subtracted"); value1 = Integer.parseInt(br.readLine()); value2 = Integer.parseInt(br.readLine()); System.out.println(value1 + " - " + value2 + " = " + (value1-value2)); break; case 3: System.out.println("multiplication"); System.out.println("Please enter two values to be multiplicated"); value1= Integer.parseInt(br.readLine()); value2 = Integer.parseInt(br.readLine()); System.out.println(value1 + " * " + value2 + " = " + (value1*value2)); break; case 4: System.out.println("division"); System.out.println("Please enter two values to be divided"); value1= Integer.parseInt(br.readLine()); value2 = Integer.parseInt(br.readLine()); number = (double)value1 / value2; System.out.println(value1 + " / " + value2 + " = " + number); break; case 5: System.out.println("Modulus"); System.out.println("Please enter two values to find modulus"); value1= Integer.parseInt(br.readLine()); value2 = Integer.parseInt(br.readLine()); System.out.println(value1 + " % " + value2 + " = " + (value1%value2)); break; case 6: System.out.println("You have chose exit!"); break; default: System.out.println("You entered an invalid choice"); } }while(choice != 6); } } 10. Write a menu driven program that outputs the results of the following evaluations based on the number entered by the user. Print appropriate message for a wrong input. i) Natural logarithm of the number ii) Absolute value of the number iii) Square root of the number iv) Random number between 0 and 1 solution: import java.io.*; public class menuEx { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int choice; int num; do { System.out.println("1. Natural Logarithm of a number"); System.out.println("2. Absolute Value of a number"); System.out.println("3. Square Root of a number"); System.out.println("4. Random values from 0 to 1"); System.out.println("5. Exit"); System.out.println("Please enter your choice:"); choice = Integer.parseInt(br.readLine()); switch(choice) { case 1: System.out.println("Natural Logarithm"); System.out.println("Enter a number"); num=Integer.parseInt(br.readLine()); double a=Math.log(num); System.out.println("The value is:"+a); break; case 2: System.out.println("Absolute value"); System.out.println("Enter a number"); num=Integer.parseInt(br.readLine()); double b=Math.abs(num); System.out.println("The value is:"+b); break; case 3: System.out.println("Square Root"); System.out.println("Enter a number"); num=Integer.parseInt(br.readLine()); double c=Math.sqrt(num); System.out.println("The value is:"+c); break; case 4: System.out.println("Random values from 0 to 1"); double d=Math.random(); System.out.println("The value is:"+d); break; case 5: System.out.println("you chose to exit!"); break; default: System.out.println("Invalid Input"); } }while(choice != 5); } } 11. Write a program to convert a given string to lowercase and print the number of vowels in a given string. Solution: import java.io.*; class Vowel { public static void main(String arg[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter a string"); String s=br.readLine(); int count=0; s=s.toLowerCase(); int x=s.length(); char t; for(int i=0;i<x;i++) { t=s.charAt(i); if(t=='a'||t=='e'||t=='i'||t=='o'||t=='u') count++; } System.out.println("the number of vowels:"+count); System.out.println(s); } } 12. Write a program to check whether the given string is palindrome or not. Solution: import java.io.*; class Palin { public static void main(String arg[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter a string"); String s=br.readLine(); int x=s.length(); String rev=""; for(int i=(x-1);i>=0;i--) { rev=rev+s.charAt(i); } if(rev.equalsIgnoreCase(s)) { System.out.println("String is Palindrome"); } else { System.out.println("Not a palindrome"); } } } 13. Write a program to count total number of upper case and lower case characters in a string entered by the user. Solution: import java.io.*; class Up_Low { public static void main(String arg[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter a string"); String s=br.readLine(); int x=s.length(); int c=0; for(int i=0;i<x;i++) { if(s.charAt(i)>=97 && s.charAt(i)<=122) c++; } System.out.println("number of lower case character:"+c); int c1=0; for(int j=0;j<x;j++) { if(s.charAt(j)>=65 && s.charAt(j)<=90) c1++; } System.out.println("number of upper case character:"+c1); } }
|