Guest User

Untitled

a guest
Aug 17th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. How to write a private method that returns True for certain peramaters?
  2. private void isValid(aRating)
  3. {
  4.  
  5. }
  6.  
  7. private void isValid(int hour, int minute)
  8. {
  9. if (hour >= 0 && hour <=23)
  10. {
  11. System.out.println("Hour is valid");
  12. hourIsValid = true;
  13. }
  14.  
  15. else
  16. {
  17. System.out.println("Hour is not valid");
  18. hourIsValid = false;
  19. System.exit(0);
  20. }
  21. }
  22.  
  23. private boolean isValid(int aRating)
  24. {
  25. if (aRating >= 1 && aRating <= 10)
  26. return true;
  27. else
  28. return false;
  29. }
  30.  
  31. private void isValid(int aRating) {}
  32.  
  33. private boolean isValid(int aRating) {
  34. return /* validity test here */;
  35. }
  36.  
  37. public void doStuff() {
  38. System.out.println("Stuff done.");
  39. }
  40.  
  41. private void doOtherStuff() {
  42. System.out.println("Other stuff done.");
  43. }
  44.  
  45. public void showHowItWorks() {
  46. doStuff();
  47. doOtherStuff();
  48.  
  49. // or if you prefer this style:
  50. this.doStuff();
  51. this.doOtherStuff();
  52. }
  53.  
  54. class PublicExample {
  55. public static void doStuff() {
  56. System.out.println("Stuff done.");
  57. }
  58. }
  59.  
  60. class PrivateExample {
  61. private static void doStuff() {
  62. System.out.println("Stuff done.");
  63. }
  64. }
  65.  
  66. class Example {
  67. public static void Main(String[] args) {
  68. PublicExample.doStuff(); // Works
  69. PrivateExample.doStuff(); // Doesn't work (because it's private and defined in a different class)
  70. }
  71. }
  72.  
  73. private void isValid(int hour, int minute) {
  74. if (hour >= 0 && hour <=23) {
  75. System.out.println("Hour is valid");
  76. hourIsValid = true;
  77. } else {
  78. System.out.println("Hour is not valid");
  79. hourIsValid = false;
  80. System.exit(0);
  81. }
  82. }
  83.  
  84. public static boolean trueExample() {
  85. return true;
  86. }
  87.  
  88. public static void main(String[] args) {
  89. boolean returnValue = trueExample();
  90. System.out.println("trueExample() returned " + returnValue);
  91. }
  92.  
  93. /**
  94. * @return the input value minus one.
  95. */
  96. public static int oneLess(int num) {
  97. return num - 1;
  98. }
  99.  
  100. public static void main(String[] args) {
  101. int num = 10;
  102.  
  103. // Print 10
  104. System.out.println(num);
  105.  
  106. // Print 9
  107. num = oneLess(num);
  108. System.out.println(num);
  109.  
  110. // Print 8
  111. num = oneLess(num);
  112. System.out.println(num);
  113. }
  114.  
  115. public boolean isValid(int num) {
  116. return num >= 10 && num <= 20;
  117. }
  118.  
  119. public boolean isValid(int num) {
  120. /* Numbers less than 10 are invalid */
  121. if(num < 10) {
  122. return false;
  123. }
  124. /* Numbers greater than 20 are invalid */
  125. else if (num > 20) {
  126. return false;
  127. }
  128. /* By process of elimination, everything else is valid */
  129. else {
  130. return true;
  131. }
  132. }
Add Comment
Please, Sign In to add comment