Question

In: Computer Science

Java Language The int t contains an integer between 1 and 50 (inclusive). Write code that...

Java Language

The int t contains an integer between 1 and 50 (inclusive). Write code that outputs the number in words and stores the result in the String inwords. For example, if t is 35 then inwords should contain "thirty five".

Test Cases

Test case #1

Expected result: When t is 2, your code sets inwords to "two"

Test case #2

Expected result: When t is 50, your code sets inwords to "fifty"

Test case #3

Expected result: When t is 37, your code set inwords to "thirty seven"

Test case #4

Expected result: When t is 16, your code sets inwords to "sixteen"

Solutions

Expert Solution

JAVA CODE :

import java.util.*;

public class Main
{
public static String singles(int n) // return singles
{
if(n == 1) return "one";
if(n == 2) return "two";
if(n == 3) return "three";
if(n == 4) return "four";
if(n == 5) return "five";
if(n == 6) return "six";
if(n == 7) return "seven";
if(n == 8) return "eight";
return "nine";
}
  
public static String doubles2(int n) // returns after twnetys
{
if(n == 3) return "thirty";
if(n == 4) return "forty";
return "fifty";
}
  
public static String doubles1(int n) // returns elevenes, twelves, ..
{
if(n == 11) return "eleven";
if(n == 12) return "twelve";
if(n == 13) return "thirteen";
if(n == 14) return "forteen";
if(n == 15) return "fifteen";
if(n == 16) return "sixteen";
if(n == 17) return "seventeen";
if(n == 18) return "eighteen";
if(n == 19) return "nineteen";
return "twenty";
  
}
  
   public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       System.out.print("Enter the number (1 to 50): ");
       int n = s.nextInt(); // read teh integer
       if(n == 10)// if it is ten
       System.out.println("Ten");
       }
       else if(n > 10 && n <= 20)
       {
       System.out.println(doubles1(n));
       }
       else{
       int n1 = n % 10;
       int n2 = (n / 10) % 10;
       if(n2 == 0)
       {
       System.out.println(singles(n1));
       }
       else{
       if(n2 == 2)
       {
       System.out.println(doubles1(n2) + " " + singles(n1));
       }
       else{
       System.out.println(doubles2(n2) + " " + singles(n1));
       }
       }
       }
   }
}

SAMPLE OUTPUTS :


Related Solutions

C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The...
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The program prompts the user to enter a number continuously until the number matches the randomly generated number. For each user input, the program tells the user whether the input is too low or too high, so the user can choose the next input intelligently. Here is a sample run:
Write the BNF for mini Java language based of the following information: Data Types Integer Int...
Write the BNF for mini Java language based of the following information: Data Types Integer Int Long Double Boolean Char References Complex Data Structures Arrays int v[30]; Classes member variables class Name { int a; char b; char name[25]; } Methods Return data type Primitive data type Void Method Name Parameter list Could be empty Statement Block { Variable declarations Executable Statements } Program Variable Declarations Class Definitions Methods Only one method named "main" but must have one method named...
Write the EBNF for mini Java language based of the following information: Data Types Integer Int...
Write the EBNF for mini Java language based of the following information: Data Types Integer Int Long Double Boolean Char References Complex Data Structures Arrays int v[30]; Classes member variables class Name { int a; char b; char name[25]; } Methods Return data type Primitive data type Void Method Name Parameter list Could be empty Statement Block { Variable declarations Executable Statements } Program Variable Declarations Class Definitions Methods Only one method named "main" but must have one method named...
Java language (a) Write code segments to perform the following: (i) declare and create an integer...
Java language (a) Write code segments to perform the following: (i) declare and create an integer array freqArray of size 8 (ii) declare and initialize an array weight (with suitable type) which contains 48.5, 80 and 68 (iii) declare a Mouse array of size 2 with name mouse and initialize it with Mouse objects using one statement (b) A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3};...
**********Java language************ 1-      (Conversions between Celsius and Fahrenheit) Write a class that contains the following two methods:...
**********Java language************ 1-      (Conversions between Celsius and Fahrenheit) Write a class that contains the following two methods: /** Convert from Celsius to Fahrenheit */ public static double celsiusToFahrenheit(double celsius) /** Convert from Fahrenheit to Celsius */ public static double fahrenheitToCelsius(double fahrenheit) The formula for the conversion is: Celsius = (5.0 / 9) * (Fahrenheit – 32) Fahrenheit = (9.0 / 5) * Celsius + 32 Program output should be like: If you want to convert from Fahrenheit To Celsius press 0...
JAVA programing language: What is printed when the following code is executed? int columns; int rows;...
JAVA programing language: What is printed when the following code is executed? int columns; int rows; for(rows = 1; rows < 2; ++rows) { for(columns = 1; columns < 3; ++columns) { System.out.print("x"); } System.out.println(): } select one: A) xx B) xxx xxx C) x x D) xx xx xx
Write a MIPS assembly language to transpose a square integer matrix in code
Write a MIPS assembly language to transpose a square integer matrix in code
Write a code fragment in C language that tests the value of an integer num1. If...
Write a code fragment in C language that tests the value of an integer num1. If the value is 10, square num1. If it is 9, read a new value into num1. If it is 2 or 3, multiply num1 by 99 and print out the result. Implement your code using SWITCH statements
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a java code that: 1) Takes as an argument an integer number, say N 2)...
Write a java code that: 1) Takes as an argument an integer number, say N 2) Creates an array of size N 3) Populates the array with random numbers between 0 and 10 * N. This is, the values of the elements of the array should be random numbers between 0 and 10 * N. 4) Finally, the code outputs the index of the smallest element and the index of the largest element in the array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT