Java programming help. Using modulo operator, I need to convert the numbers inpu
ID: 3558048 • Letter: J
Question
Java programming help.
Using modulo operator, I need to convert the numbers input in to the program into degrees, minutes, and seconds. For example: If the program asks to enter a number of seconds: 61, number of minutes: 61, and number of degrees: 200, how can I make the program calculate this to say: 201 degrees, 2 minutes, and 1 seconds. I'm very confused and this is the program:
package hw1rodriguezk.pkg1;
//importing to get user input
import java.util.Scanner;
/**
* This program takes 2 sets of user input of degrees, minutes and seconds
* It sums them and shows the result
* @author xyz
*/
public class HW1RodriguezK1 {
public static void main(String[] args) {
//Declraing two arrays to get user inputs and third one to store the sum
int coordinatesArray1[] = new int[3];
int coordinatesArray2[] = new int[3];
int sumOfCoordinatesArrays1And2[] = new int[3];
//array to display unit while taking user input in loop or we will need to use if conditions
String units[] = {"seconds", "minutes", "degree"};
//creating object of scanner class to get user input
Scanner keyBoardInput = new Scanner(System.in);
//loop till the legth of array and get first set of user input
for (int i = 0; i < coordinatesArray1.length; i++) {
System.out.print("Enter a number of " + units[i] + ": ");
coordinatesArray1[i] = keyBoardInput.nextInt();
//e. After each prompt and entry of a value, display on a separate line a text that contains
//"The value you entered is " and then the value entered.
System.out.println("The value you entered is: " + coordinatesArray1[i]);
}// for loop ends after getting first set of values from user
//displaying blank line
System.out.println(" ");
//loop till the legth of array and get second set of user input
for (int i = 0; i < coordinatesArray2.length; i++) {
System.out.print("Enter a number of " + units[i] + ": ");
coordinatesArray2[i] = keyBoardInput.nextInt();
//e. After each prompt and entry of a value, display on a separate line a text that contains
//"The value you entered is " and then the value entered.
System.out.println("The value you entered is: " + coordinatesArray2[i]);
}// for loop ends after getting second set of values from user
//storing sum of both sets in thord array
sumOfCoordinatesArrays1And2[0] = coordinatesArray2[0] + coordinatesArray1[0];
sumOfCoordinatesArrays1And2[1] = coordinatesArray2[1] + coordinatesArray1[1];
sumOfCoordinatesArrays1And2[2] = coordinatesArray2[2] + coordinatesArray1[2];
//calculating seconds and minutes using modulo operator
sumOfCoordinatesArrays1And2[1] = sumOfCoordinatesArrays1And2[1] + (sumOfCoordinatesArrays1And2[0] / 60);
sumOfCoordinatesArrays1And2[0] = sumOfCoordinatesArrays1And2[0] % 60;
//calculating minutes and degrees
sumOfCoordinatesArrays1And2[2] = sumOfCoordinatesArrays1And2[2] + (sumOfCoordinatesArrays1And2[1] / 60);
sumOfCoordinatesArrays1And2[1] = sumOfCoordinatesArrays1And2[1] % 60;
//displaying the results
System.out.println(" The first coordinate is: " + coordinatesArray1[2] + " degrees, " + coordinatesArray1[1] + " minutes, and " + coordinatesArray1[0] + " seconds.");
System.out.println("The second coordinate is: " + coordinatesArray2[2] + " degrees, " + coordinatesArray2[1] + " minutes, and " + coordinatesArray2[0] + " seconds.");
//separate the previous two lines from this new line:
System.out.println("");
/**
* checking that the number of degrees does not exceed
* 359. If the degrees exceed the threshold number, an error message
* displays stating
* the number of degrees over 359.
*/
if (sumOfCoordinatesArrays1And2[2] >= 359) {
System.out.println("Error! The number of degrees over 359");
}//if ends after checking the condition that sum of degrees is over 359
//displaying sum of the coordinates entered
System.out.println("The sum of the two coordinates is: " + sumOfCoordinatesArrays1And2[2] + " degrees, " + sumOfCoordinatesArrays1And2[1] + " minutes, and " + sumOfCoordinatesArrays1And2[0] + " seconds.");
//displaying end of the program message and purpose of the code
System.out.println("");
System.out.println("***End of the program***");
System.out.println("This program takes 2 sets of user input of degrees, minutes and seconds");
System.out.println("It sums them and shows the result");
}//main ends
}//class ends
Explanation / Answer
Note: The given code is correct, I just added comments to logic of the program.
//importing to get user input
import java.util.Scanner;
/**
* This program takes 2 sets of user input of degrees, minutes and seconds
* It sums them and shows the result
*/
public class HW1RodriguezK1 {
public static void main(String[] args) {
//Declare two arrays to get user inputs and third one to store the sum
int input1[] = new int[3];
int input2[] = new int[3];
int result[] = new int[3];
//array to display unit while taking user input in loop
String units[] = {"seconds", "minutes", "degree"};
//creating object of scanner class to get user input
Scanner keyBoardInput = new Scanner(System.in);
//loop till the length of array and get first set of user input
for (int i = 0; i < input1.length; i++)
{
System.out.print("Enter a number of " + units[i] + ": ");
input1[i] = keyBoardInput.nextInt();
//After each prompt and entry of a value, display on a separate line a text that contains
//"The value you entered is " and then the value entered.
System.out.println("The value you entered is: " + input1[i]);
}// for loop ends after getting first set of values from user
//displaying blank line
System.out.println(" ");
//loop till the length of array and get second set of user input
for (int i = 0; i < input2.length; i++)
{
System.out.print("Enter a number of " + units[i] + ": ");
input2[i] = keyBoardInput.nextInt();
//After each prompt and entry of a value, display on a separate line a text that contains
//"The value you entered is " and then the value entered.
System.out.println("The value you entered is: " + input2[i]);
}// for loop ends after getting second set of values from user
//storing sum of both sets in third array
result[0] = input2[0] + input1[0];
result[1] = input2[1] + input1[1];
result[2] = input2[2] + input1[2];
/*
* 1 minute = 60 seconds
* convert the seconds to minutes, when the second's value is greater than
* 60. If the second's value is 60, then it is converted to 1 minute. If the second's value is 61, then it is converted to 1 minute and
* 1 second. Mathematically minute's value is calculated by applying divison ( / ) by 60 and second's value is calculated by finding the remainder( % ) after divison by 60.
* If the minutes value is 10 and second's value is 100, then it is converted as below:
* new minutes value = 100 / 60
* = 1 (Quotient)
* Hence minutes value = 10 + 1 (old + new values)
* = 11
* Seconds value = 100 % 60
* = 40
* Now, seconds value = 40
*
* Hence new value is 11 minutes and 40 seconds
*/
//calculate minutes
result[1] = result[1] + (result[0] / 60);
//calculate seconds
result[0] = result[0] % 60;
/*
* 1 hour = 60 minutes
* convert the minutes to hours, when the minute's value is greater than
* 60. If the minute's value is 60, then it is converted to 1 hour. If the minute's value is 61, then it is converted to 1 hour and
* 1 minute. Mathematically hour's value is calculated by applying divison ( / ) by 60 and minute's value is calculated by finding the remainder( % ) after divison by 60.
* If the hours value is 10 and minute's value is 100, then it is converted as below:
* new hours value = 100 / 60
* = 1 (only Quotient value)
* Hence hours value = 10 + 1 (old + new values)
* = 11
* minutes value = 100 % 60
* = 40 (Remainder value)
* Now, minutes value = 40
*
* Hence new value is 11 hours and 40 minutes
*/
//calculate degrees
result[2] = result[2] + (result[1] / 60);
//calculate minutes
result[1] = result[1] % 60;
//displaying the results
System.out.println(" The first coordinate is: " + input1[2] + " degrees, " + input1[1] + " minutes, and " + input1[0] + " seconds.");
System.out.println("The second coordinate is: " + input2[2] + " degrees, " + input2[1] + " minutes, and " + input2[0] + " seconds.");
//separate the previous two lines from this new line:
System.out.println("");
/**
* checking that the number of degrees does not exceed
* 359. If the degrees exceed the threshold number, an error message
* displays stating
* the number of degrees over 359.
*/
if (result[2] >= 359) {
System.out.println("Error! The number of degrees over 359");
}//if ends after checking the condition that sum of degrees is over 359
//displaying sum of the coordinates entered
System.out.println("The sum of the two coordinates is: " + result[2] + " degrees, " + result[1] + " minutes, and " + result[0] + " seconds.");
//displaying end of the program message and purpose of the code
System.out.println("");
System.out.println("***End of the program***");
System.out.println("This program takes 2 sets of user input of degrees, minutes and seconds");
System.out.println("It sums them and shows the result");
}//main ends
}//class ends
Output:
Enter a number of seconds: 33
The value you entered is: 33
Enter a number of minutes: 30
The value you entered is: 30
Enter a number of degree: 5
The value you entered is: 5
Enter a number of seconds: 27
The value you entered is: 27
Enter a number of minutes: 29
The value you entered is: 29
Enter a number of degree: 5
The value you entered is: 5
The first coordinate is: 5 degrees, 30 minutes, and 33 seconds.
The second coordinate is: 5 degrees, 29 minutes, and 27 seconds.
The sum of the two coordinates is: 11 degrees, 0 minutes, and 0 seconds.
***End of the program***
This program takes 2 sets of user input of degrees, minutes and seconds
It sums them and shows the result
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.