Results 1 to 11 of 11
Thread: Creating Methods Assistance
- 03-11-2010, 08:58 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
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
- 03-11-2010, 09:21 AM #2
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;
}
}
- 03-11-2010, 10:01 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
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
- 03-11-2010, 10:12 AM #4
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
- 03-11-2010, 10:28 AM #5
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
for the divisable by three part, i would do something along the lines ofJava Code:public boolean isEvenNumber(int num){ if(num/2 == 0){ return true; } else{ return false; } }
You could then compair the two boolean results to determine if the number meets the requirments.Java Code:public boolean isDivisableByThree(int num){ if(num%3 == 0){ return true; } else{ return false; } }
T&T = T
- 03-11-2010, 10:32 AM #6
Way too complicated. A simple
is enough.Java Code:return ((number%3!=0) && (number%2!=0));
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-11-2010, 10:37 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
"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.
- 03-11-2010, 10:54 AM #8
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:Now you have to replace all the XXXs to make it work.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; } }Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-11-2010, 11:02 AM #9
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
Got your point, Thank you
- 03-12-2010, 04:22 AM #10
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
- 03-15-2010, 03:21 PM #11
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
creating objects by methods automatically
By thedarlington in forum New To JavaReplies: 13Last Post: 02-07-2010, 10:48 PM -
Looking for assistance
By s_dawg101 in forum New To JavaReplies: 32Last Post: 11-04-2009, 02:49 AM -
Creating Blocking Methods
By Singing Boyo in forum Advanced JavaReplies: 5Last Post: 06-11-2009, 10:44 AM -
In need of some assistance
By Boer84 in forum New To JavaReplies: 2Last Post: 07-08-2008, 04:14 PM -
X-Tremely new to this...Need assistance...
By Johnny562 in forum New To JavaReplies: 5Last Post: 07-01-2008, 09:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks