First of all, I'm not asking anyone to do this for me. I really just wanted to be pointed at the right direction. Alright well i have to program in java using the object oriented design style... I'm not sure how to start this exactly.
You have to create a phone number generator for a cell phone company. The program should present a menu with the options to generate a phone number for Harrisburg, Carlisle, York or Lancaster. Use the random number generator to produce the last four numbers of the phone number. Upon successful generation of the cell phone number, the user should be given the option to generate another cell phone number.
The first three numbers of the phone number should be as follows:
Carlisle – 551
Harrisburg – 552
York – 553
Lancaster – 554
Heres an example of what it should look like
http://i53.tinypic.com/24b7p7b.jpg
http://i55.tinypic.com/2qltwe0.jpg
Heres really what i have so far and it isn't much at all
import java.util.*;
import java.io.*;
public class PhoneNumberGenerator {
If anyone can point me in the right direction i'd be very thankful.. the professor seems to like assign programs to things we haven't even gone for in class.. the main question i have is how do i generate a random number thats 4 digits for the phone number? Thanks.
How do i go about doing this basic Java program?
Started by desi22601, Sep 19 2010 05:20 PM
7 replies to this topic
#1
Posted 19 September 2010 - 05:20 PM
|
|
|
#2
Posted 20 September 2010 - 12:14 AM
The start of your program is by asking the user's input as the first image. How to do so can be found here -->Dialogs in Java
The input will return a String.
You can then do
if ..else if else if else. To determine which number is chosen and pick the correct first 3 numbers. -> store these in a string
Then all you must do is add a random value, using the Random class.
Then again an input is required. On the site i've given are various types of dialogboxes. Choose the right one.
To start over. put all this in a while(true) loop and leave the loop when the final dialogbox is negative.
The input will return a String.
You can then do
if ..else if else if else. To determine which number is chosen and pick the correct first 3 numbers. -> store these in a string
Then all you must do is add a random value, using the Random class.
Then again an input is required. On the site i've given are various types of dialogboxes. Choose the right one.
To start over. put all this in a while(true) loop and leave the loop when the final dialogbox is negative.
#3
Posted 20 September 2010 - 12:31 AM
Okay, so you just have to generate the number and display it, and not do anything with it, right?
So, I don't think you'll need very many methods in this class. You can do it as one, but I'd expect that your professor would want more than one method to conform to the constraints of the 'object oriented design' style (actually, I'd assume that would mean to create a program that creates instances of other classes, but that sounds needlessly complicated for a task you could theoretically use one method to solve).
I'd start by making a constructor (or main method if you're using the command line) that starts by asking what area they live in, 1 2 3 or 4. Then, use a Switch command to determine if the number was 1, 2, 3, 4 or something else, to get the area code. Have a method that generates the final four digits radnomly (I assume that your professor has shown you how to do this), and returns these as a String. The constructor would then tack the returned string on to the string holding the area code, ask if this is okay, and if not, take the final 4 numbers off and run the number generator again.
I pretty much have an idea of how I would make a complete version of this; feel free to ask if you have any more questions!
So, I don't think you'll need very many methods in this class. You can do it as one, but I'd expect that your professor would want more than one method to conform to the constraints of the 'object oriented design' style (actually, I'd assume that would mean to create a program that creates instances of other classes, but that sounds needlessly complicated for a task you could theoretically use one method to solve).
I'd start by making a constructor (or main method if you're using the command line) that starts by asking what area they live in, 1 2 3 or 4. Then, use a Switch command to determine if the number was 1, 2, 3, 4 or something else, to get the area code. Have a method that generates the final four digits radnomly (I assume that your professor has shown you how to do this), and returns these as a String. The constructor would then tack the returned string on to the string holding the area code, ask if this is okay, and if not, take the final 4 numbers off and run the number generator again.
I pretty much have an idea of how I would make a complete version of this; feel free to ask if you have any more questions!
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)
#4
Posted 20 September 2010 - 11:03 PM
Sorry i'm really new at this so bare with me.. this is what i have so far
import java.util.*;
import javax.swing.JOptionPane;
import java.io.*;
import java.util.Random.*;
public class PhoneNumberGenerator
{
private String cityName;
private int carlisleNumber;
private int harrisburgNumber;
private int yorkNumber;
private int lancasterNumber;
public void getCity()
{
String cityName = JOptionPane.showInputDialog("Enter\r\n" +
"1 - Carlisle\r\n" +
"2 - York\r\n" +
"3 - Harrisburg\r\n" +
"4 - Lancaster\r\n");
}
public static void main(String[] args)
{
PhoneNumberGenerator x = new PhoneNumberGenerator();
x.getCity();
}
}
The bottom is basically the object oriented style she wants done... so when i compile this a dialog box comes up and its exactly like picture 1.. now where do i go from here exactly? Like when the dialog box comes up and they put 1 in it how do i make it so the number generated for Carlisle is 551 - **** (whatever the random number is). Thanks for the help.
import java.util.*;
import javax.swing.JOptionPane;
import java.io.*;
import java.util.Random.*;
public class PhoneNumberGenerator
{
private String cityName;
private int carlisleNumber;
private int harrisburgNumber;
private int yorkNumber;
private int lancasterNumber;
public void getCity()
{
String cityName = JOptionPane.showInputDialog("Enter\r\n" +
"1 - Carlisle\r\n" +
"2 - York\r\n" +
"3 - Harrisburg\r\n" +
"4 - Lancaster\r\n");
}
public static void main(String[] args)
{
PhoneNumberGenerator x = new PhoneNumberGenerator();
x.getCity();
}
}
The bottom is basically the object oriented style she wants done... so when i compile this a dialog box comes up and its exactly like picture 1.. now where do i go from here exactly? Like when the dialog box comes up and they put 1 in it how do i make it so the number generated for Carlisle is 551 - **** (whatever the random number is). Thanks for the help.
#5
Posted 21 September 2010 - 12:50 AM
You don't really need the cityName and the cityNumbers as global variables... after you take the input, you could do:
Alternatively, you could use the 'if' statement, but this is less efficient:
Hope that helps, anything else, feel free to ask :)
String numberContainer; //This is just here for completeness; I suggest making this a global variable (ie - a variable not defined in a method so all methods can access it)
switch (input) {
case 1: numberContainer = "551"; break;
case 2: numbercontainer = "552"; break;
case 3: numberContainer = "553"; break;
case 4: numberContainer = "554"; break;
Alternatively, you could use the 'if' statement, but this is less efficient:
if (input == 1) {numberContainer = "551";}
if (input == 2) {numberContainer = "552";}
Hope that helps, anything else, feel free to ask :)
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)
#6
Posted 21 September 2010 - 06:55 AM
String phonenumber = "";
String cityName = JOptionPane.showInputDialog("Enter\r\n" +
"1 - Carlisle\r\n" +
"2 - York\r\n" +
"3 - Harrisburg\r\n" +
"4 - Lancaster\r\n");
int input= Integer.parseInt(cityName ); //turns the string into an integer
//Note that Fae's switch and if statements are working with integers. Switch statements can't work with Strings, and for ifs you need cityName .equals("1");
//i think The switch statement is easiest in use. Just copy Fae's code here. Or in this case the following would also work:
phonenumber = 550 + input;
Then all you need to do is put random numbers behind it
Random random = new Random(); int randomNumber = random.nextInt(10) //nextInt(10) will generate a number from 0 to 10 (10 not included, so maximum 9.)
I think you can finish it now. Just do the random 4 times and add the randomnumber to the String 4 times.
Use a loop for this, don't write it down 4 times.
DRY - Don't Repeat Yourself
Good luck.
#7
Posted 21 September 2010 - 08:14 AM
Thanks! I finished it. Wouldn't have done it without both of your help. I asked this at a another java forum and they basically lol'd at my face. Good to know this forum is helpful.
#8
Posted 21 September 2010 - 11:58 PM
We're all newbies sometime :) Happy to know it helped.
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)


Sign In
Create Account

Back to top









