Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Not sure what to do next (Basic java program)

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

    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.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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):
    //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:
    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.

    //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.

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    desi22601 (September 21st, 2010)

  4. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Not sure what to do next (Basic java program)

    By saying:
    String number = "";
    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:
    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:
    String cityName = JOptionPane.showInputDialog("Enter\r\n" +...
    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:
    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:
    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:
    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.
    Last edited by aussiemcgr; September 21st, 2010 at 09:16 AM.

Similar Threads

  1. Basic program which gets cost, adds tax, gets payment then calculates change.
    By bibboorton in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 25th, 2010, 10:31 AM
  2. A couple of basic java assignments, need help!
    By barnabus in forum Paid Java Projects
    Replies: 1
    Last Post: August 18th, 2010, 10:18 AM
  3. Basic Java Encryption
    By BronxBomber in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2010, 10:50 PM
  4. Few very basic Java questions.
    By 01001010 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 13th, 2010, 01:14 PM
  5. Basic Java Program Help
    By roaster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 6th, 2009, 10:28 PM