473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help for a total beginner

3
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(String)
public boolean isEvenNumber(int)
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.getRandom();
}

public void getRandom(){
Random randomNumbers = new Random();
int generateNum = randomNumbers.nextInt(100);
System.out.println("\n\n\nThe number generated is " + generateNum);
if (generateNum % 2 == 0){
System.out.println("The number generated is an even number");
}
else{
System.out.println("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
Oct 20 '06 #1
4 2524
r035198x
13,262 8TB
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(String)
public boolean isEvenNumber(int)
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.getRandom();
}

public void getRandom(){
Random randomNumbers = new Random();
int generateNum = randomNumbers.nextInt(100);
System.out.println("\n\n\nThe number generated is " + generateNum);
if (generateNum % 2 == 0){
System.out.println("The number generated is an even number");
}
else{
System.out.println("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.

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class Homework06{
  3.     Random randomNumbers;
  4.     public Homework06() {
  5.         randomNumbers = new Random();
  6.     }
  7.     public Random getRandomNumbers () {
  8.         return randomNumbers;
  9.     }
  10.     public boolean isEvenNumber(int a) {
  11.         return (a % 2) == 0;
  12.     }
  13.  
  14.     public void execute() {
  15.         Homework06 myHomework06 = new Homework06();
  16.         int num1 = myHomework06.getRandom();
  17.         int num2 = myHomework06.getRandom(myHomework06.getRandomNumbers());
  18.         String result1 = "";
  19.         String result2 = "";
  20.         if (isEvenNumber(num1)){
  21.             result1 = "The number generated by method 1 is an even number:"+num1;
  22.         }
  23.         else{
  24.             result1 = "The number generated by method 1 is an odd number"+num1;
  25.         }
  26.         if (isEvenNumber(num2)){
  27.             result2 = "The number generated by method 2 is an even number"+num2;
  28.         }
  29.         else{
  30.             result2 = "The number generated by method 2 is an odd number"+num2;
  31.         }
  32.  
  33.         displayResult(result1);
  34.         displayResult(result2);
  35.     }
  36.     public static void main(String args[]){
  37.  
  38.         new Homework06().execute();
  39.     }
  40.     public int getRandom() {
  41.         return (int)(Math.random() * 100);
  42.  
  43.     }
  44.     //overloading
  45.     public int getRandom(Random r) {
  46.         return r.nextInt(100);
  47.     }
  48.     public int getNextNumber() {
  49.         return randomNumbers.nextInt(100);
  50.     }
  51.     public void displayResult(String s) {
  52.         System.out.println(s);
  53.     }
  54.  
  55. }
Oct 21 '06 #2
Orphic
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(String) - method without return with input

public boolean isEvenNumber(int) - 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!
Oct 25 '06 #3
r035198x
13,262 8TB
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(String) - method without return with input

public boolean isEvenNumber(int) - 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
Oct 25 '06 #4
Orphic
3
After looking back through my notes and the books, I understand!! Thank you so much for all your help and feedback!!
Oct 25 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Philip Mette | last post by:
I am begginner at best so I hope someone that is better can help. I have a stored procedure that updates a view that I wrote using 2 cursors.(Kind of a Inner Loop) I wrote it this way Because I...
16
by: ranger | last post by:
Hi, I'm a beginner with C++, studying on my own from a book and I've encoutered quite some problems.. If possible, someone out there might help me out.. The problem is the following.. I've...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
2
by: Michael Keene | last post by:
I'm pretty much a beginner with Access but do have some experience but I'm stuck on this problem. Any suggestions would be appreciated. I have a very simple database that tracks the number of...
6
by: kaosyeti | last post by:
is there a way to create simple help file that i plan on linking to a command button on a form using what's already in access? i will probably be giving out this db that i've written to a number...
3
by: Mark Reed | last post by:
I am trying to have two fields, one being a total, the next being a decimal factor for pecentage. I then want it to calculate the total plus the percentage factor... for example, 100 is the...
1
by: Phil | last post by:
Hi I admit it, I am a total vb beginner. I thought I'd get hold of a copy to see if I could resolve a simple problem we have. I am looking to search AD for user objects for reporting purposes,...
7
by: strsury | last post by:
Hi all, I have a page with 5 or 6 drop down boxes, all ranging from 1 to 100. After each is selected, I need a box that will show a total of all the results. So basically, dropdown 1 +...
1
by: clairelee0322 | last post by:
I am a c++ beginner and i am working on a dice loop lab. This program should roll two dice (each dice is from 1 to 6) and then total them together. The user should be able to input how many times...
3
by: ALKASER266 | last post by:
Hey guyz I am a java beginner and I have problem with part of the code. The thing that I don't know how to do it in this code is how to show the user that he/she has input an invalid input In my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.