Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to write a private method that returns True for certain peramaters?
- private void isValid(aRating)
- {
- }
- private void isValid(int hour, int minute)
- {
- if (hour >= 0 && hour <=23)
- {
- System.out.println("Hour is valid");
- hourIsValid = true;
- }
- else
- {
- System.out.println("Hour is not valid");
- hourIsValid = false;
- System.exit(0);
- }
- }
- private boolean isValid(int aRating)
- {
- if (aRating >= 1 && aRating <= 10)
- return true;
- else
- return false;
- }
- private void isValid(int aRating) {}
- private boolean isValid(int aRating) {
- return /* validity test here */;
- }
- public void doStuff() {
- System.out.println("Stuff done.");
- }
- private void doOtherStuff() {
- System.out.println("Other stuff done.");
- }
- public void showHowItWorks() {
- doStuff();
- doOtherStuff();
- // or if you prefer this style:
- this.doStuff();
- this.doOtherStuff();
- }
- class PublicExample {
- public static void doStuff() {
- System.out.println("Stuff done.");
- }
- }
- class PrivateExample {
- private static void doStuff() {
- System.out.println("Stuff done.");
- }
- }
- class Example {
- public static void Main(String[] args) {
- PublicExample.doStuff(); // Works
- PrivateExample.doStuff(); // Doesn't work (because it's private and defined in a different class)
- }
- }
- private void isValid(int hour, int minute) {
- if (hour >= 0 && hour <=23) {
- System.out.println("Hour is valid");
- hourIsValid = true;
- } else {
- System.out.println("Hour is not valid");
- hourIsValid = false;
- System.exit(0);
- }
- }
- public static boolean trueExample() {
- return true;
- }
- public static void main(String[] args) {
- boolean returnValue = trueExample();
- System.out.println("trueExample() returned " + returnValue);
- }
- /**
- * @return the input value minus one.
- */
- public static int oneLess(int num) {
- return num - 1;
- }
- public static void main(String[] args) {
- int num = 10;
- // Print 10
- System.out.println(num);
- // Print 9
- num = oneLess(num);
- System.out.println(num);
- // Print 8
- num = oneLess(num);
- System.out.println(num);
- }
- public boolean isValid(int num) {
- return num >= 10 && num <= 20;
- }
- public boolean isValid(int num) {
- /* Numbers less than 10 are invalid */
- if(num < 10) {
- return false;
- }
- /* Numbers greater than 20 are invalid */
- else if (num > 20) {
- return false;
- }
- /* By process of elimination, everything else is valid */
- else {
- return true;
- }
- }
Add Comment
Please, Sign In to add comment