Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header:
public static String convertMillis(long millis)
For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, andconvertMillis(555550000) returns the string154:19:10.
Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array). Output should be in ascending order. Assume the input ends when the user enters a 0. Here is a sample run of the program:
Enter integers between 1 and 100 (enter 0 to stop): 2 5 6 5 4 3 23 43 2 0
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time
Note that if the number occurs more than one time, the plural word “times” is used in the output.
Problem 3. (Print distinct numbers)Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by one space (i.e. if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is an example run of the program:
Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
The number of distinct numbers is 6
The distinct numbers are: 1 2 3 6 4 5
Solution to Problem 1:
public class MilliToHrsMinsSecs {
public static void main(String[] args) {
System.out.println(convertMillis(5500));
System.out.println(convertMillis(100000));
System.out.println(convertMillis(555550000));
}
public static String convertMillis(long millis) {
String result;
long hrs = 0, mins = 0, secs = 0;
while (millis != 0) {
if (millis >= 3600000) {
hrs = millis / 3600000;
millis = millis - hrs * 3600000;
} else if (millis >= 60000) {
mins = millis / 60000;
millis = millis - mins * 60000;
} else {
secs = millis / 1000;
millis = 0;
}
}
result = hrs + ":" + mins + ":" + secs;
return result;
}
}
Output:
Solution to Problem
2:
import java.util.*;
public class Occurrences {
public static void main(String[] args) {
int[] numbers = new int[200];
int i = 0, n;
Scanner in = new Scanner(System.in);
System.out.print("Enter integers between 1 and 100 (enter 0 to stop):");
int val;
val = in.nextInt();
while (val != 0) {
numbers[i] = val;
val = in.nextInt();
i = i + 1;
}
n = i;
TreeMap values = new TreeMap();
for (i = 0; i < n; i++) {
values.put(numbers[i], 0);
}
Object ob;
for (i = 0; i < n; i++) {
ob = values.get(numbers[i]);
if (ob != null) {
values.put(numbers[i], values.get(numbers[i]) + 1);
}
}
Set set = values.entrySet();
Iterator x = set.iterator();
int a, b;
while (x.hasNext()) {
Map.Entry current = (Map.Entry) x.next();
a = (int) current.getKey();
b = (int) current.getValue();
if (b == 1)
System.out.println(a + " occurs 1 time");
else
System.out.println(a + " occurs " + b + " times");
}
}
}
Output:
Solution to Problem
3:
import java.util.Scanner;
public class PrintDistinct {
public static void main(String[] args) {
System.out.print("Enter ten numbers:");
Scanner in = new Scanner(System.in);
int i, distinctnumbers = 0, val;
int[] distincts = new int[10];
for (i = 0; i < 10; i++) {
val = in.nextInt();
if (!isThere(distincts, distinctnumbers, val)) {
distincts[distinctnumbers] = val;
distinctnumbers = distinctnumbers + 1;
}
}
System.out.println("The number of distinct numbers is " + distinctnumbers);
System.out.print("The distinct numbers are:");
for (i = 0; i < distinctnumbers; i++)
System.out.print(" " + distincts[i]);
}
public static boolean isThere(int[] distincts, int distinctnumbers, int val) {
int i;
for (i = 0; i < distinctnumbers; i++) {
if (val == distincts[i])
return true;
}
return false;
}
}
Output:

Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds
Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header: public static String convertMillis(long millis) For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, and convertMillis(555550000) returns the string 154:19:10. Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1...
Python (Convert milliseconds to hours, minutes, and seconds) Write a function that converts milliseconds to hours, minutes, and seconds using the following header: def convertMillis(millis): The function returns a string as hours:minutes:seconds. For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, and convertMillis(555550000) returns the string 154:19:10. Write a test program that prompts the user to enter a value for milliseconds and displays a string in the format of hours:minutes:seconds. Sample Run Enter time in milliseconds: 555550000...
(Count occurrence of numbers) Write a program that reads some integers between 1 and 100 and counts the occurrences of each. Here is a sample run of the program: Enter integers between 1 and 100: 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time 5 occurs 2 times 6 occurs 1 time 23 occurs 1 time 43 occurs 1 time 2 5 6 5 4 3 23 43 2 Note that if a number occurs more than...
java for netbeans
Question 2: (Print distinct numbers) Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it. After the input, the array contains the distinct numbers. Here is the...
Java Programming. Write your own source code with comments.
(Print distinct numbers) Write a program that reads in ten
numbers and displays the number of distinct numbers and the
distinct numbers separated by exactly one space (i.e., if a number
appears multiple times, it is displayed only once). (Hint: Read a
number and store it to an array if it is new. If the number is
already in the array, ignore it.) After the input, the array
contains the distinct...
In JAVA: Write a method that reverses the array passed the argument and returns this array. Write a test program that prompts the user to enter ten numbers, invokes the method to reverse the numbers and display the numbers. Write a method that returns a new array by eliminating the duplicate values in the array using following method header: a. Public static int[] eliminateDuplicates(int list) b. Write a test program that reads in ten integers and invoke the method, the...
Java: Write a program that prompts the user to enter integers in the range 1 to 50 and counts the occurrences of each integer. The program should also prompt the user for the number of integers that will be entered. As an example, if the user enters 10 integers (10, 20, 10, 30, 40, 49, 20, 10, 25, 10), the program output would be: 10 occurs 4 times 20 occurs 2 times 25 occurs 1 time 30 occurs 1 time...
Write a program that asks the user to enter 1000 integers to be stored in an array called "numbers". Since the same integer might occur (exist) in the array multiple times, your program needs to fill a second array, called "Isolate" that contains all the integers from the first array but NOT REPAPTED (every integer will exist only once). Finally, your program will print the integers of the second array sorted by occurrence (occurrence is: the number of times the...
HW Help? I cannot seem to figure this one out.. --Write a program that reads the integers between 1 and 100 and counts the occurrences of each. assume the input ends with 0. Example: 2 5 6 5 4 3 23 43 2 0 ENTER 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time 5 occurs 2 times 6 occurs 1 time 23 occurs 1 time 43 occurs 1 time Note that if a number occurs more...
write a program that read the integers between 1 and 100 and count the occurrence of each number. Assume the input end with 0. here is samole of the program:Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0 Enter2 occurs 2 times3 occurs 1 time4 occurs 1 time5 occurs 2 times6 occurs 1 time23 occurs 1 time43 occurs 1 time