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

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};...
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 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
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
(c) Explain TWO (2) differences between int and Integer in the context of Java programming using...
(c) Explain TWO (2) differences between int and Integer in the context of Java programming using suitable example,
Program in Java code Write a program with total change amount in pennies as an integer...
Program in Java code Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. .Ex1: If the input is: 0 the output is:    No change            Ex2: If the input is:    45   the output is:   1 Quarter 2 Dimes
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT