Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1-3 Computer Programming: Programming Problem Using the Java Development Kit and

ID: 664446 • Letter: 1

Question

1-3 Computer Programming: Programming Problem

Using the Java Development Kit and jGrasp, develop a program in response to the following prompt:Write a program that prompts the user to input a decimal number and ouputs the number rounded to the nearest integer. Remember the rules around proper development style and form, including adding comments. A software developer should always add comments to their code so other developers (or in this case the instructor) understand what the intent of the program is. Submit your compiled Java code for this assignment, compress the .java file into a single .zip file.

Explanation / Answer

// import the Scanner class from util library import java.util.Scanner; // Only one public class is allowed per java file public class RoundToNearestInteger { // create a class method convert public void convert() { // initialize a Scanner object from Scanner class Scanner getUserInput = new Scanner(System.in); // Access the system standard output and print to console System.out.println("~~~~~ Decimal to Rounded Integer Converter ~~~~~ Enter a decimal number to round to the nearest integer:"); // define and initialize two variables, one to handle decimals(float) and regular integers(int) float userDecimalNumber = getUserInput.nextFloat(); int roundedNumber = Math.round(userDecimalNumber); // Use the Math library and access round via method access operator (.) // print to console a string concatenated with both variables, also using escaped return characters System.out.println(" You entered: " + userDecimalNumber + " The rounded number is: " + roundedNumber); } // the main method which runs automatically when the program is executed public static void main(String []args) { // instantiate RoundToNearestInteger object RoundToNearestInteger decimalToInteger = new RoundToNearestInteger(); // call convert method on new object decimalToInteger.convert(); } }