Results 1 to 11 of 11
  1. #1
    Desmond is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default Creating Methods Assistance

    Hello

    I have been given an assignment task, and I managed to solve the problem my way i.e the logic and the program runs fine. The problem is I must conform to the Method prototype that has been supplied and I am really struggling to adjust my code to the given prototype.

    Here is my code as well as the Task as well as the suggested method prototype, plase can some one adapt my code to the given prototype.


    This method takes an integer as input and it determines if the given integer is odd and is not divisible by 3. The method returns true if the input is an odd integer that is not divisible by 3, otherwise it returns false. For instance, given input 17, the method returns true; given input 14 or 15, the method returns false.
    The following is the method prototype for the method:

    boolean checkNumber(int number)

    class studentSubmission {
    boolean checkNumber(int number) {
    }
    }

    and here is the code that I created

    import java.util.Scanner;
    public class studentSubmission

    {
    public static void main(String[] args)
    {

    boolean checkNumber;
    int number;
    System.out.println("Enter a number: ");
    Scanner keyboard = new Scanner(System.in);
    number = keyboard.nextInt();
    checkNumber = ((number%3!=0) && (number%2!=0));
    System.out.println(checkNumber);
    }
    }


    Please help

  2. #2
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    162
    Rep Power
    6

    Default

    hi,is this you want or please specify how and why you want like that
    import java.util.Scanner;
    public class studentSubmission

    {
    public static void main(String[] args)
    {
    boolean checkNumber;
    int number;
    System.out.println("Enter a number: ");
    Scanner keyboard = new Scanner(System.in);
    number = keyboard.nextInt();
    checkNumber=new studentSubmission1().checkNumber(number);

    System.out.println(checkNumber);
    }
    }

    class studentSubmission1 {
    boolean checkNumber(int number) {
    boolean checkNumber;
    checkNumber = ((number%3!=0) && (number%2!=0));
    return checkNumber;
    }
    }

  3. #3
    Desmond is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default

    Thanx a million, that is exactly what I needed, I am doig an assignment and it requires we use methods like you did, Thax mate.
    Desmond

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    I would be surprised if you get more than a D for that solution.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Unsub's Avatar
    Unsub is offline Member
    Join Date
    Jan 2010
    Posts
    30
    Rep Power
    0

    Default

    I'm not exactly 100% positive, but wouldn't dividing a number by 2 give you the simpelest method of determining if a number is odd or even?
    And then you could simply use the modulous (spelling) of three to determine if the number is infact divisable by three?

    I would do it something like this:

    *code isn't tested

    Java Code:
    public boolean isEvenNumber(int num){
      if(num/2 == 0){
        return true;
      }
      else{
        return false;
      }
    }
    for the divisable by three part, i would do something along the lines of
    Java Code:
    public boolean isDivisableByThree(int num){
      if(num%3 == 0){
         return true;
      }
      else{
        return false;
      }
    }
    You could then compair the two boolean results to determine if the number meets the requirments.

    T&T = T

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Way too complicated. A simple
    Java Code:
    return ((number%3!=0) && (number%2!=0));
    is enough.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    Desmond is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default

    "I would be surprised if you get more than a D for that solution."


    Why do you think so PhHein, please give your opinion or solution.

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Because that solution creates new class which is completely pointless.
    I'm not going to spoon feed you a solution, but I'll give you a skeletton:
    Java Code:
    public class StudentSubmission{
      private XXX input;
      
      public static void main(String[] args){ 
        StudentSubmission stsub = new XXX();
        stsub.getInput();
        System.out.println(stsub.checkInput());
      }
    
      public XXX getInput(){
        XXX
      }
    
      public XXX checkInput(){
        XXX
        return XXX;
      }
    }
    Now you have to replace all the XXXs to make it work.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    Desmond is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default

    Got your point, Thank you

  10. #10
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    162
    Rep Power
    6

    Default

    if had answered as per the requirement that specified
    thats all and not to setup as a larger line of code the same can be as
    import java.util.Scanner;
    public class studentSubmissions
    {
    public static void main(String[] args)
    {
    int number;
    System.out.println("Enter a number: ");
    Scanner keyboard = new Scanner(System.in);
    number = keyboard.nextInt();
    System.out.println(checkNumber(number));
    }
    private static boolean checkNumber(int number){
    return ((number%3!=0) && (number%2!=0));
    }
    }
    please any body tell me the need for forum
    ok thank you

  11. #11
    Desmond is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

Similar Threads

  1. creating objects by methods automatically
    By thedarlington in forum New To Java
    Replies: 13
    Last Post: 02-07-2010, 11:48 PM
  2. Looking for assistance
    By s_dawg101 in forum New To Java
    Replies: 32
    Last Post: 11-04-2009, 03:49 AM
  3. Creating Blocking Methods
    By Singing Boyo in forum Advanced Java
    Replies: 5
    Last Post: 06-11-2009, 11:44 AM
  4. In need of some assistance
    By Boer84 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 05:14 PM
  5. X-Tremely new to this...Need assistance...
    By Johnny562 in forum New To Java
    Replies: 5
    Last Post: 07-01-2008, 10:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •