Question

Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program...

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

Thank you.

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<Integer, Integer> values = new TreeMap<Integer, Integer>();
        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:
Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program...
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
  • 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...

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

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

  • In JAVA: Write a method that reverses the array passed the argument and returns this array....

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

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

  • Please help with my java program

    I'm in my first java class at school and really need some help. I've looked online for different programs for the same assignment I'm doing now, and none of them seemto work. Here is the assignment:Write a program that reads integers, finds the largest of them, and counts its occurrences. Assume that the input ends with number 0. Suppose that you entered 3 5 2 55 5 0; the program finds that the largest is 5 and the occurrence count...

  • (Print distinct numbers) C++ Write a program that reads in 10 numbers and displays distinct numbers...

    (Print distinct numbers) C++ Write a program that reads in 10 numbers and displays distinct numbers (i.e., if a number appears multiple times, it is displayed only once). The numbers are displayed in the order of their input and separated by exactly one space. (Hint: Read a number and store it to an array if it is new. If the number is already in the array, discard it. After the input, the array contains the distinct numbers.) Sample Run Enter...

  • Python (Convert milliseconds to hours, minutes, and seconds) Write a function that converts milliseconds to hours,...

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

  • 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