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};...
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
Java programming language Write a Java application that simulates a test. The test contains at least...
Java programming language Write a Java application that simulates a test. The test contains at least five questions. Each question should be a multiple-choice question with 4 options. Design a QuestionBank class. Use programmer-defined methods to implement your solution. For example: - create a method to simulate the questions – simulateQuestion - create a method to check the answer – checkAnswer - create a method to display a random message for the user – generateMessage - create a method to...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT