You must select, copy and paste the output created by your program You must uplo
ID: 3725160 • Letter: Y
Question
You must select, copy and paste the output created by your program
You must upload your source code file to the Assignment submission online
Make sure your program has comments
You must follow the coding conventions and indent your code before submitting otherwise you automatically lose 2 points.
Do not use Arrays
Objectives:
1. Write a subroutine that takes a parameter
2. Write a subroutine that returns a value
3. Modify your solution to previous assignment
Problem
Modify your spellNumber() subroutine from Assignment 6 to make it return a String containing the number spelled out. Your new version of spellNumber() will do NO WRITING TO THE CONSOLE. This subroutine will still have a parameter that tells it what number to spell out. Here is your test program:
class TestSpellNumber {
public static void main(String args []) {
for(int i=-9;i<=9;i++){
System.out.println(UsefulRoutines7.spellDigit(i));
}
System.out.println("------------------");
for(int i=-99;i<=99;i++){
System.out.println(UsefulRoutines7.spellNumber(i));
}
}
}
First get the first three lines on the main() above to work. That entails modifying the method spellDigit() to make it return the String instead of printing it. Then modify the subroutine spellNumber() to make it return the String instead of printing it.
Notes:
This program will have no user input.
The subroutines spellDigit() and spellNumber() will contain no calls to System.out.println()
The subroutine spellNumber() will work for any integer that is between -99 and 99.
You must submit both source code files (TestSpellNumber.java and UsefulRoutines.java)
______________________________________________________________________________________________________________________________________________
Here is my program from my assignment 6 :
=========================================================================================================================
public class UsefulRoutines6 {
/**
* This method spells out a single-digit number, given in the parameter. It
* doesn't return anything or get any user input, but there is output.
*/
public static void spellDigit(int number) {
if (number < -9 || number > 9) {
System.out.println("Error");
} else {
if (number == 0) {
System.out.println("Zero");
} else {
String output = "";
if (number < 0) {
output = "negative ";
number = number * -1;// convert to positive so we have fewer cases
}
switch (number) {
case 1:
System.out.println(output + "one");
break;
case 2:
System.out.println(output + "two");
break;
// ...list the rest
}
}
}
}
/**
* This method spells out a two-digit number, given in the parameter. It
* doesn't return anything or get any user input, but there is output. This
* method calls spellDigit for numbers smaller than 10 and for the ones
* place of numbers larger than 20, unless that digit is a 0.
*/
public static void spellNumber(int number) {
if (number < -99 || number > 99) {
System.out.println("Error");
} else {
String output = "";
if (number < 0) {
output = "negative ";
number = number * -1;
}
System.out.print(output);
if(number<10){
spellDigit(number);
}
else if(number < 20){
switch (number){
case 10:
System.out.println("ten");
break;
case 11:
System.out.println("eleven");
break;
// list the rest thru 19
}
}
else{
//numbers between 20 and 99
int tens = number/10;
int unit = number%10;
//System.out.println("tens="+tens+" units="+unit);
switch(tens){
case 2:
System.out.print("twenty ");
break;
case 3:
System.out.print("thirty ");
break;
//list rest thru 9 = ninety
}
if(unit!=0){
//we do not want to print zero
spellDigit(unit);
}
else{
//goto next line
System.out.println();
}
}
}
}
}
Explanation / Answer
//Code in JAVA
/*package whatever //do not write package name here */
import java.io.*;
public class UsefulRoutines7 {
/**
* This method spells out a single-digit number, given in the parameter. It
* doesn't return anything or get any user input, but there is output.
*/
public static String spellDigit(int number)
{
String output="";
if (number == 0) {
output+="Zero";
}
else
{
if (number < 0)
{
output+= "negative ";
number = number * -1;// convert to positive so we have fewer cases
}
switch (number) {
case 1:
output+=" one";
break;
case 2:
output+=" two";
break;
case 3:
output+=" three";
break;
case 4:
output+=" four";
break;
case 5:
output+=" five";
break;
case 6:
output+=" six";
break;
case 7:
output+=" seven";
break;
case 8:
output+=" eight";
break;
case 9:
output+=" nine";
break;
}
}
return output;
}
/**
* This method spells out a two-digit number, given in the parameter. It
* doesn't return anything or get any user input, but there is output. This
* method calls spellDigit for numbers smaller than 10 and for the ones
* place of numbers larger than 20, unless that digit is a 0.
*/
public static String spellNumber(int number)
{
String output ="";
if (number < 0) {
output = "negative ";
number = number * -1;
}
if(number<10){
output+=" "+spellDigit(number);
}
else if(number < 20)
{
switch (number)
{
case 10:
output+="ten";
break;
case 11:
output+="eleven";
break;
case 12:
output+="twelve";
break;
case 13:
output+="thirteen";
break;
case 14:
output+="fourteen";
break;
case 15:
output+="fifteen";
break;
case 16:
output+="sixteen";
break;
case 17:
output+="seventeen";
break;
case 18:
output+="eighteen";
break;
case 19:
output+="nineteen";
break;
}
}
else{
//numbers between 20 and 99
int tens = number/10;
int unit = number%10;
switch(tens){
case 2:
output+="twenty";
break;
case 3:
output+="thirty";
break;
case 4:
output+="fourty";
break;
case 5:
output+="fifty";
break;
case 6:
output+="sixty";
break;
case 7:
output+="seventy";
break;
case 8:
output+="eighty";
break;
case 9:
output+="ninety";
break;
}
if(unit!=0)
{
//we do not want to print zero
output+=spellDigit(unit);
}
}
return output;
}
}
class TestSpellNumber
{
public static void main(String args [])
{
for(int i=-9;i<=9;i++)
{
System.out.println(UsefulRoutines7.spellDigit(i));
}
System.out.println("------------------");
for(int i=-99;i<=99;i++)
{
System.out.println(UsefulRoutines7.spellNumber(i));
}
}
}
/* OUTPUT :
negative nine
negative eight
negative seven
negative six
negative five
negative four
negative three
negative two
negative one
Zero
one
two
three
four
five
six
seven
eight
nine
------------------
negative ninety nine
negative ninety eight
negative ninety seven
negative ninety six
negative ninety five
negative ninety four
negative ninety three
negative ninety two
negative ninety one
negative ninety
negative eighty nine
negative eighty eight
negative eighty seven
negative eighty six
negative eighty five
negative eighty four
negative eighty three
negative eighty two
negative eighty one
negative eighty
negative seventy nine
negative seventy eight
negative seventy seven
negative seventy six
negative seventy five
negative seventy four
negative seventy three
negative seventy two
negative seventy one
negative seventy
negative sixty nine
negative sixty eight
negative sixty seven
negative sixty six
negative sixty five
negative sixty four
negative sixty three
negative sixty two
negative sixty one
negative sixty
negative fifty nine
negative fifty eight
negative fifty seven
negative fifty six
negative fifty five
negative fifty four
negative fifty three
negative fifty two
negative fifty one
negative fifty
negative fourty nine
negative fourty eight
negative fourty seven
negative fourty six
negative fourty five
negative fourty four
negative fourty three
negative fourty two
negative fourty one
negative fourty
negative thirty nine
negative thirty eight
negative thirty seven
negative thirty six
negative thirty five
negative thirty four
negative thirty three
negative thirty two
negative thirty one
negative thirty
negative twenty nine
negative twenty eight
negative twenty seven
negative twenty six
negative twenty five
negative twenty four
negative twenty three
negative twenty two
negative twenty one
negative twenty
negative nineteen
negative eighteen
negative seventeen
negative sixteen
negative fifteen
negative fourteen
negative thirteen
negative twelve
negative eleven
negative ten
negative nine
negative eight
negative seven
negative six
negative five
negative four
negative three
negative two
negative one
Zero
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty one
twenty two
twenty three
twenty four
twenty five
twenty six
twenty seven
twenty eight
twenty nine
thirty
thirty one
thirty two
thirty three
thirty four
thirty five
thirty six
thirty seven
thirty eight
thirty nine
fourty
fourty one
fourty two
fourty three
fourty four
fourty five
fourty six
fourty seven
fourty eight
fourty nine
fifty
fifty one
fifty two
fifty three
fifty four
fifty five
fifty six
fifty seven
fifty eight
fifty nine
sixty
sixty one
sixty two
sixty three
sixty four
sixty five
sixty six
sixty seven
sixty eight
sixty nine
seventy
seventy one
seventy two
seventy three
seventy four
seventy five
seventy six
seventy seven
seventy eight
seventy nine
eighty
eighty one
eighty two
eighty three
eighty four
eighty five
eighty six
eighty seven
eighty eight
eighty nine
ninety
ninety one
ninety two
ninety three
ninety four
ninety five
ninety six
ninety seven
ninety eight
ninety nine
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.