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
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...
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.
In C# When the user enters an invalid value, ask the user to repeatedly enter the...
In C# When the user enters an invalid value, ask the user to repeatedly enter the value until a valid value has been entered. Gender must be ‘M’ or ‘F’. Residency must be ‘I’ or ‘O’. Existing Code: using System; public class Student {   public int credit;   public String firstname, lastname, gender, residency, edate;   public void input()   {     Console.WriteLine("\nWelcome to the Continental University Registration System!"); Console.WriteLine("\nEnter data about a student"); Console.Write("First Name: "); firstname = Console.ReadLine(); Console.Write("Last Name: "); lastname...
Write a program and ask a user to enter a numeric value between 0 - 99....
Write a program and ask a user to enter a numeric value between 0 - 99. Your program will spell out the numbers into words. You must use at least three switch cases in addition to multiple if statements. Each missing switch statement will reduce your grade for this problem by 20%. Note: The total number of if statements and switch cases should not exceed 28. Additional if/case statement will further reduce your grade by 5%. in c++ please
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 PYTHON. Create a program that will ask a user to enter whole positive numbers. The...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The program will use a while loop to let the program keep running. The user will be allowed to use the program as long as the quitVariable is equal to 'y' or 'Y'. When the user decides to quit playing, say "Thanks for playing!". The program will use a for loop to test if the number are even or odd. If even print "number is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT