Question

Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds


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


0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

Add a comment
Know the answer?
Add Answer to:
Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Count occurrence of numbers) Write a program that reads some integers between 1 and 100 and...

    (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 Programming. Write your own source code with comments. (Print distinct numbers) Write a program that...

    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...

  • java for netbeans Question 2: (Print distinct numbers) Write a program that reads in ten numbers...

    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: Write a program that prompts the user to enter integers in the range 1 to...

    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...

    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...

    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...

  • C++counting occurrence of numbers

    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

  • write a c++ program that prompts a user for a number then attempts to allocate an...

    write a c++ program that prompts a user for a number then attempts to allocate an array of as many integers as the user enters. In other words, the program might prompt the user with a line like: “How many integers would you like to allocate?” It should then allocate as many integers as the user enters. The program should then wait for the user to press enter before deleting the allocated array and quitting. We will use this time...

  • Write a computer program that prompts the user for one number, n for the number of...

    Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 different arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times. In the body of the loop, Create an array of n random integers (Make sure...

  • Write a program that reads the integers between 1 and 100 and counts the occurences of each

    Could someone please help me with the following;Write a program that reads the integers between 1 and 100 and counts the occurences of each. Assume the input ends with 0. You should have two additional methodsbesides main. Their signature is:public static void displayResults(int[] numList) - This method displays the results as shown below.public static int getValidInput() - This method gets the input from the keyboard, makes sure it falls between 1 and 100 and if so returns the number to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT