Question

In: Computer Science

Problem1: In this problem we will ask the use to enter a value greater than 1,000...

Problem1: In this problem we will ask the use to enter a value greater than 1,000 and less than 1,000,000 without a comma and the program will add the comma.

  • Create a class named Problem1
    • Create an instance variable named value.
    • Create a constructor that accepts an argument as a parameter variable named v and uses it to initialize the instance variable.
    • Create a method named addComma. It will return a String and will not have an argument.
    • The method should implement the following algorithm:
      • Create a String variable named commaNumber;
      • Create an integer variable named thousands.
      • Create an integer variable named hundreds.
      • Assign to thousands the result of dividing value by one thousand.
      • Assign to hundreds the result of using the modulus operator to find the remainder of dividing value by one thousand.
      • Assign to commaNumber thousands + a comma + hundreds
      • Return commaNumber.
    • Create a tester class named Problem1Tester
      • Import Scanner
      • Create a Scanner object
      • Prompt for a number between 1,000 and 1,000,000 (exclusive) without the comma.
      • Get the number from the user and store it in a variable.
      • Create a Problem1 object initialized to the number obtained from the user
      • Call addComma and print the result (this can be done directly in the print statement or you can create a new variable and print it)

Solutions

Expert Solution

import java.util.*;
import java.io.*;
import java.math.*;
class Problem1
{
  
   int value;
  
   public Problem1(int v)
   {
       value = v;
   }
   String addComma()
   {
       String commaNumber;
       int thousands;
       int hundreds;
       thousands = this.value/1000;
       hundreds = this.value%1000;
       commaNumber = thousands + "," + hundreds;
       return commaNumber;
   }
};
class Problem1Tester
{
   public static void main(String[] args)
   {
       Scanner in = new Scanner(System.in);
       System.out.println("enter a number between 1000 and 1000000\n");
       int inputVariable = Integer.parseInt(in.nextLine());
       Problem1 probObj = new Problem1(inputVariable);
       System.out.println("output: " + probObj.addComma());
   }
}


Related Solutions

C++ Programa that: // 1) Ask the user to enter two numbers greater than zero. Store...
C++ Programa that: // 1) Ask the user to enter two numbers greater than zero. Store the //    values into two variables named 'n' and 'm'. //    Additionally, implement a while-loop to keep asking the user to enter //    the numbers again if any of the two numbers are equal or smaller than zero. // 2) If the two numbers (n, m) are equal, display the message "The //    numbers are the same". If the two numbers are different, display...
Use a while(true) loop to ask the user the following 2 values “Enter a value x...
Use a while(true) loop to ask the user the following 2 values “Enter a value x “ “Enter a nonnegative integer n (enter negative integer to quit): “ If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the partial sum for the Riemann zeta series for the geometric series, namely 1 + 2ˆ-x + 3ˆ-x +...
Use a for loop to ask a user to enter the grades of 5 courses. The...
Use a for loop to ask a user to enter the grades of 5 courses. The user should enter character values, e.g., A. Calculate the GPA of the user Hint: Convert the character values entered to numerals, e.g., A to 4 c programming help me please
1a) Explain why we fail to reject the null hypothesis when the p-value is greater than...
1a) Explain why we fail to reject the null hypothesis when the p-value is greater than the level of significance. b) If the null hypothesis is rejected at a level of significance of 5%, does it automatically get rejected at a level of significance of 1%? Explain your reasoning with an example using a p-value(s) that you make up for different scenarios. c) If the null hypothesis is rejected at a level of significance of 1%, does it automatically get...
Code in Java Change the instructionLabel to ask the user to enter a numeric value into...
Code in Java Change the instructionLabel to ask the user to enter a numeric value into the textField and click the button to convert the entered value from kilometers to miles (1.609344 km = 1 mile). When the actionButton on the form is clicked, ActionWindow should take the value entered in the textField, convert it from kilometers to miles, and display the result in the resultField. import java.awt.Container; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class...
Write a C++ program to allow a user to enter in any positive number greater than...
Write a C++ program to allow a user to enter in any positive number greater than or equal to zero. The program should not continue until the user has entered valid input. Once valid input has been entered the application will determine if the number is an abundant number or not and display whether or not the number is an abundant number. If the user enters in a 0 the program should quit. An abundant number is a number n...
write a c++ program . Ask the user to enter a number less than 100. Test...
write a c++ program . Ask the user to enter a number less than 100. Test the input to make sure it is correct, and use a while loop to continuously ask the user for correct input value if they don't follow the input rule. After receiving the correct input, test the number to see if it is even or odd. If it is odd, use a while loop to do the following: Output to the monitor all odd numbers...
Why is the future value of annuity due always greater than the future value of an...
Why is the future value of annuity due always greater than the future value of an ordinary annuity assuming everything equals 2? Why the present value of an annuity due is always greater that than the future value of an ordinary annuity assuming everything else equals?
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer to quit):” and store this into an int named n. If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the sum of the inverse factorials from 0 to n, that is sum = 1/0! + 1/1! + 1/2! + ....
Write a mips assembly code program that ask the user to enter an integer value, and...
Write a mips assembly code program that ask the user to enter an integer value, and then print the result of doubling that number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT