Difference between revisions of "Return Array"
Line 1: | Line 1: | ||
{{1010PrAD|ProblemName=Return Array | {{1010PrAD|ProblemName=Return Array | ||
− | |Problem= Write a method which will take | + | |Problem= Write a method which will take a three digit area code and a phone number and combine these into one value. Print this value in main. |
<BR><BR> | <BR><BR> | ||
Example: | Example: | ||
<BR> | <BR> | ||
− | + | combineNumber(204, 4881234); | |
− | + | ||
− | + | ||
<BR><BR> | <BR><BR> | ||
Output: | Output: | ||
<BR> | <BR> | ||
− | + | 2044881234 | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
Line 26: | Line 21: | ||
|Solution=The solution... | |Solution=The solution... | ||
− | To solve this problem we are going to create a skeleton structure. We know that we will have two methods: main & | + | To solve this problem we are going to create a skeleton structure. We know that we will have two methods: main & combineNumber. We know that combineNumber will accept two integers and return an integer: |
<BR> | <BR> | ||
<pre> | <pre> | ||
Line 32: | Line 27: | ||
{ | { | ||
} | } | ||
− | public static int | + | public static int returnArray( int areaCode, int phoneNumber ) |
{ | { | ||
} | } | ||
</pre> | </pre> | ||
<BR> | <BR> | ||
− | Now that we have the skeleton we can start | + | Now that we have the skeleton we can start filling in main. We know that main will pass an area code and a number to combineNumber. combineNumber will return a integer which we will store in a temporary place holderand print: |
<pre> | <pre> | ||
public static void main() | public static void main() | ||
{ | { | ||
− | int | + | int temp; |
− | + | ||
− | + | temp = combineNumber(204, 4881234); | |
− | + | System.out.print(temp); | |
− | + | ||
− | + | ||
− | + | ||
} | } | ||
</pre> | </pre> | ||
<BR> | <BR> | ||
− | Now we will fill in | + | Now we will fill in combineNumber. We know that it will be passed two numbers and we will need to combine them into one number. We can't just add them together though. We will need to change the area code to be area code + 0000000. This will cause it to be added onto the start of the phone number: |
<pre> | <pre> | ||
− | public static int | + | public static int combineNumber( int areaCode, int phoneNumber ) |
{ | { | ||
− | + | int returnValue; | |
− | + | ||
− | + | areaCode = areaCode * 10000000 | |
− | + | ||
− | return | + | returnValue = areaCode + phoneNumber; |
+ | |||
+ | return returnValue; // Return the changed combined values | ||
} | } | ||
</pre> | </pre> | ||
Line 72: | Line 65: | ||
public static void main() | public static void main() | ||
{ | { | ||
− | int | + | int temp; |
− | + | ||
− | + | temp = combineNumber(204, 4881234); | |
− | + | System.out.print(temp); | |
− | + | ||
− | + | ||
− | + | ||
} | } | ||
− | public static int | + | public static int combineNumber( int areaCode, int phoneNumber ) |
{ | { | ||
− | + | int returnValue; | |
− | + | ||
− | + | areaCode = areaCode * 10000000 | |
− | + | ||
− | return | + | returnValue = areaCode + phoneNumber; |
+ | |||
+ | return returnValue; // Return the changed combined values | ||
} | } | ||
</pre> | </pre> | ||
}} | }} |
Revision as of 12:22, 1 April 2010
Back to the Program-A-Day homepage
ProblemWrite a method which will take a three digit area code and a phone number and combine these into one value. Print this value in main.
|
SideSectionTitle
An image or By Students section | |
---|---|---|
SolutionThe solution...
To solve this problem we are going to create a skeleton structure. We know that we will have two methods: main & combineNumber. We know that combineNumber will accept two integers and return an integer:
public static void main() { } public static int returnArray( int areaCode, int phoneNumber ) { }
public static void main() { int temp; temp = combineNumber(204, 4881234); System.out.print(temp); }
public static int combineNumber( int areaCode, int phoneNumber ) { int returnValue; areaCode = areaCode * 10000000 returnValue = areaCode + phoneNumber; return returnValue; // Return the changed combined values }
public static void main() { int temp; temp = combineNumber(204, 4881234); System.out.print(temp); } public static int combineNumber( int areaCode, int phoneNumber ) { int returnValue; areaCode = areaCode * 10000000 returnValue = areaCode + phoneNumber; return returnValue; // Return the changed combined values } | ||
CodeSolutionCode goes here. Please DO NOT put your code in <pre> tags! |