Saturday, July 17, 2021

Methods in Java

What is method?

What is the advantages of using methods?

What is method prototype?

What is method signature?

What is formal parameter?

What is actual parameter?

Code 01


  1. import java.util.Scanner;
  2. public class ParkingLot 
  3. {
  4. int vno;
  5. int hours;
  6. double bill;
  7. public static void main(String args[])
  8. {
  9. ParkingLot ob = new ParkingLot();
  10. ob.input();
  11. ob.calculate();
  12. ob.display();
  13. }
  14. void input()
  15. {
  16. Scanner sc = new Scanner(System.in);
  17. System.out.print("Enter the vehicle number: ");
  18. vno = sc.nextInt();
  19. System.out.print("Enter the hours vehicle is parked: ");
  20. hours = sc.nextInt();
  21. }
  22. void calculate()
  23. {
  24. if(hours<=1)
  25. bill = 3;
  26. else
  27. bill = 3+(hours-1)*1.5;
  28. }
  29. void display()
  30. {
  31. System.out.println("Vehicle Number = "+vno);
  32. System.out.println("Hours Parked = "+hours);
  33. System.out.println("Bill Amount = "+bill);
  34. }
  35. }

-------------------------------------------------------------------------------------------

Code 02:

  1. import java.util.Scanner;
  2. public class FunctionOverload 
  3. {
  4. void compare(int a,int b)
  5. {
  6. if(a>b)
  7. System.out.println(a+" is greater.");
  8. else if(a<b)
  9. System.out.println(b+" is greater.");
  10. else
  11. System.out.println("Both are equal.");
  12. }
  13. void compare(char a,char b)
  14. {
  15. if((int)a>(int)b)
  16. System.out.println(a+" is greater.");
  17. else if((int)a<(int)b)
  18. System.out.println(b+" is greater.");
  19. else
  20. System.out.println("Both are equal.");
  21. }
  22. void compare(String a,String b)
  23. {
  24. int l1 = a.length();//6
  25. int l2 = b.length();//7
  26. if(l1>l2)
  27. System.out.println(a+" is greater in length");
  28. else if(l1<l2)
  29. System.out.println(b+" is greater in length");
  30. else
  31. System.out.println("Both are equal");
  32. }
  33. public static void main(String args[])
  34. {
  35. FunctionOverload ob = new FunctionOverload();
  36. Scanner sc = new Scanner(System.in);
  37. int p,q;
  38. p = sc.nextInt();
  39. q = sc.nextInt();
  40. ob.compare(p,q);
  41. char ch1,ch2;
  42. ch1 = sc.next().charAt(0);
  43. ch2 = sc.next().charAt(0);
  44. ob.compare(ch1,ch2);
  45. String s1,s2;
  46. s1 = sc.next();
  47. s2 = sc.next();
  48. ob.compare(s1,s2);
  49. }
  50. }






No comments:

Post a Comment

Check whether the word is Special word or Palindrome word.

Special words are those words which starts and ends with the same letter.  Examples: EXISTENCE COMIC WINDOW Palindrome words are those words...