Help for a total beginner

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Orphic
    New Member
    • Oct 2006
    • 3

    #1

    Help for a total beginner

    I'm hoping someone can help me with my homework assignment, or at least steer me in the right direction. I've got ZERO programming experience and am struggling bigtime, so I'm sorry if my questions seem dumb.

    The assignment is: Create a simple Java program to generate a random number that is smaller than 100 and divide the number by 2. If the number is an odd number, display a message that this number is an odd number. Otherwise, display this number is an even number. Use the different types of method to develop this program:

    public void displayResult(S tring)
    public boolean isEvenNumber(in t)
    public int getNextNumber()
    public void execute()
    public static void main()

    This is what I have so far:

    import java.util.*;

    public class Homework06{

    public static void main(String args[]){

    Homework06 myHomework06 = new Homework06();
    myHomework06.ge tRandom();
    }

    public void getRandom(){
    Random randomNumbers = new Random();
    int generateNum = randomNumbers.n extInt(100);
    System.out.prin tln("\n\n\nThe number generated is " + generateNum);
    if (generateNum % 2 == 0){
    System.out.prin tln("The number generated is an even number");
    }
    else{
    System.out.prin tln("The number generated is an odd number");
    }
    }
    }

    So at least I know this much works. But I have no idea what to do now. The lecture preceding this assignment was on method overload, if that makes sense. The books are not helping me at all. Any help or tips would save my sanity!! Thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Orphic
    I'm hoping someone can help me with my homework assignment, or at least steer me in the right direction. I've got ZERO programming experience and am struggling bigtime, so I'm sorry if my questions seem dumb.

    The assignment is: Create a simple Java program to generate a random number that is smaller than 100 and divide the number by 2. If the number is an odd number, display a message that this number is an odd number. Otherwise, display this number is an even number. Use the different types of method to develop this program:

    public void displayResult(S tring)
    public boolean isEvenNumber(in t)
    public int getNextNumber()
    public void execute()
    public static void main()

    This is what I have so far:

    import java.util.*;

    public class Homework06{

    public static void main(String args[]){

    Homework06 myHomework06 = new Homework06();
    myHomework06.ge tRandom();
    }

    public void getRandom(){
    Random randomNumbers = new Random();
    int generateNum = randomNumbers.n extInt(100);
    System.out.prin tln("\n\n\nThe number generated is " + generateNum);
    if (generateNum % 2 == 0){
    System.out.prin tln("The number generated is an even number");
    }
    else{
    System.out.prin tln("The number generated is an odd number");
    }
    }
    }

    So at least I know this much works. But I have no idea what to do now. The lecture preceding this assignment was on method overload, if that makes sense. The books are not helping me at all. Any help or tips would save my sanity!! Thanks
    I wonder if this is want you want.

    Code:
    import java.util.*;
    public class Homework06{
    	Random randomNumbers;
    	public Homework06() {
    		randomNumbers = new Random();
    	}
    	public Random getRandomNumbers () {
    		return randomNumbers;
    	}
    	public boolean isEvenNumber(int a) {
    		return (a % 2) == 0;
    	}
    
    	public void execute() {
    		Homework06 myHomework06 = new Homework06();
    		int num1 = myHomework06.getRandom();
    		int num2 = myHomework06.getRandom(myHomework06.getRandomNumbers());
    		String result1 = "";
    		String result2 = "";
    		if (isEvenNumber(num1)){
    			result1 = "The number generated by method 1 is an even number:"+num1;
    		}
    		else{
    			result1 = "The number generated by method 1 is an odd number"+num1;
    		}
    		if (isEvenNumber(num2)){
    			result2 = "The number generated by method 2 is an even number"+num2;
    		}
    		else{
    			result2 = "The number generated by method 2 is an odd number"+num2;
    		}
    
    		displayResult(result1);
    		displayResult(result2);
    	}
    	public static void main(String args[]){
    
    		new Homework06().execute();
    	}
    	public int getRandom() {
    		return (int)(Math.random() * 100);
    
    	}
    	//overloading
    	public int getRandom(Random r) {
    		return r.nextInt(100);
    	}
    	public int getNextNumber() {
    		return randomNumbers.nextInt(100);
    	}
    	public void displayResult(String s) {
    		System.out.println(s);
    	}
    
    }

    Comment

    • Orphic
      New Member
      • Oct 2006
      • 3

      #3
      Thanks so much for the reply!! I did not really understand what you did, though. This may sound dumb, but in class we had a similar exercise where we had to identify and use four methods: one with input and return, one with input without return, one without input with return, and one without input or return. Is that what you've done? Again, sorry if my question doesn't make sense. I don't expect anyone to do my homework for me, I really want to learn, but this stuff has been so difficult to grasp. I'm sure this is baby stuff for you guys, but bear with me. Maybe if you can tell me if I have understood this much correctly I can look back through the code you've written to understand how you came up with what you did:

      public void displayResult(S tring) - method without return with input

      public boolean isEvenNumber(in t) - method with return and input

      public int getNextNumber() - method with return without input

      public void execute() - method without return or input

      I hope I got this right. Again, thanks so much for the help!

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Orphic
        Thanks so much for the reply!! I did not really understand what you did, though. This may sound dumb, but in class we had a similar exercise where we had to identify and use four methods: one with input and return, one with input without return, one without input with return, and one without input or return. Is that what you've done? Again, sorry if my question doesn't make sense. I don't expect anyone to do my homework for me, I really want to learn, but this stuff has been so difficult to grasp. I'm sure this is baby stuff for you guys, but bear with me. Maybe if you can tell me if I have understood this much correctly I can look back through the code you've written to understand how you came up with what you did:

        public void displayResult(S tring) - method without return with input

        public boolean isEvenNumber(in t) - method with return and input

        public int getNextNumber() - method with return without input

        public void execute() - method without return or input

        I hope I got this right. Again, thanks so much for the help!
        5/5 in my class!

        As you may now have grasped, if a method is declared to be void, it does not return anything otherwise, it returns something.All methods written
        as method() (With empty brackets at the end) do not take anything otherwise the method takes whatever has been supplied in the brackets

        Comment

        • Orphic
          New Member
          • Oct 2006
          • 3

          #5
          After looking back through my notes and the books, I understand!! Thank you so much for all your help and feedback!!

          Comment

          Working...