Not sure what to do next (Basic java program)
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
Sorry i'm really new at this so bare with me.. this is what i have so far
Code :
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 the prof 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.
Re: Not sure what to do next (Basic java program)
Ok, so if you are wanting to get a random phone number, and we have the area code, you need to get 7 random numbers to start off with. The easiest way to do this, in my mind, would be to modify this statement to run 7 times (I wont do everything for you, but I will help you get there):
Code java:
//Having the number as a String will allow us to Concatenate Numbers
String number = "";
//Make your loop here to run the next few statements 7 times. I will break them up for you to understand better
//Get Random Number between 0 and 9 Inclusively
int num = (int)(Math.random()*9);
//Concatinate that number to the end of our Phone Number String
number += num;
Now that we have the Phone Number, we need to attach the Area Code. You said this was your criteria:
Quote:
Carlisle – 551
Harrisburg – 552
York – 553
Lancaster – 554
So we can do a bunch of IF statements or a SWITCH statement. We know we will get a number back from the User Input, where:
1 = Carlisle
2 = York
3 = Harrisburg
4 = Lancaster
So, for this, I will do a SWITCH statement. Remember that you need to get the int associated with each input before proceeding.
Code java:
//Input from User
int userSelection = //Whatever the value is what was inputted from the user
//Switch Statement
switch(userSelection){
//When userSelection == 1
case 1: number = 551+number;break;
//When userSelection == 2
case 2: number = 553+number;break;
//When userSelection == 3
case 3: number = 552+number;break;
//When userSelection == 4
case 4: number = 554+number;break;
//Incase of Invalid User Input
default: //You need to decide how to handle this
}
Tell me if that helps at all.
Re: Not sure what to do next (Basic java program)
Thanks! that does help.. and sorry for the very stupid question. I'm very new at this and i understand where to put the second part of the code (the switch statements) but where do i exactly put the first part of the code? Like I don't get the String number = ""; part.. is that part of the global variable?
Re: Not sure what to do next (Basic java program)
By saying:
We are create a number variable with the type String, and setting its default value to an empty String. This variable will be the number returned in the end. Now, the important thing here is that we do not want to put this in the loop. There are two reasons for that:
Reason 1:
Each time the loop runs, it will recreate the number variable and erase the previous added number
Reason 2:
Due to scope, if the variable is declared inside the loop, we cannot access it outside the loop.
So, by declaring it before the loop, it allows us to add on to the String (without loosing any of the number we put on beforehand) and it allows us to use that variable later on (like when we need to add the Area Code on and when we need to return it to the user).
You have two options here.
Option 1:
Create String number as a global variable
Option 2:
Create a method to construct our number and return String number to wherever the method was called from. In this situation, the String number variable would be local to the method it is returned from.
EDIT: After reviewing what I wrote, I had a look at your code again and realized you haven't hammered down scope yet.
In this segment of code:
Code java:
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");
}
You create cityName as a private global variable. I believe you then attempted to set the global variable in the getCity() method. However, a little thing called scope makes that not happen. With the statement:
You are create a local variable named cityName that can only be accessed inside the getCity() method. You did this by declaring: String cityName. If you wanted to alter the global variable, you have to say this:
Code java:
cityName = JOptionPane.showInputDialog("Enter\r\n" +...
Notice how String is not in front of cityName? This tells the compiler that there is already a variable named cityName, and you are wanting to set that variable. Now, due to scope, it will always look for the variable with that name that is closes in scope.
In the example below, the Global Variable: Test will not be altered. Instead, the Local Variable: Test will be altered:
Code java:
public class Example
{
String Test = "";
public static void main(String[] args)
{
String Test = "";
Test = "Setting Local Variable";
}
}
Now, in the example below, the Global Variable: Test will be altered, since there is no Local Variable with the same name:
Code java:
public class Example
{
String Test = "";
public static void main(String[] args)
{
Test = "Setting Global Variable";
}
}
Scope is the MOST IMPORTANT thing to understand for really any programming language.