Question

CSCI 161 - Lab 2: "Give Me a Second!" Overview This lab will have you developing...

CSCI 161 - Lab 2: "Give Me a Second!"

Overview

This lab will have you developing a program that asks the user to input a time period as a number of seconds and in turn calculates the whole number of days, hours and minutes that are contained within the entered time period.

Refer to the "Output Format" below for an example of what your program must do.

The objectives of this lab are to reinforce and help teach:

proper Java program class construction, purpose of the main method and use of other methods that each perform a smaller part of the overall problem

use of different data types, in particular long

methods with parameters

methods that return values

handling of user input (use of the Scanner class)

conditional statements

indefinite loops

Instructions

Following are the instructions and you should follow them carefully:

Using Eclipse create a new Java Project and name it: Lab2

As with all your labs, add a class of the same name, Lab2, and include in that class a main method.

The main method and therefore your program should use an indefinite loop and within that loop:

Prompt the user to enter a whole number of seconds or 0 to exit the program.

Get input from the user as a single long integer.

Check if 0 was entered and if so then...

...Print a departing message and terminate the loop and the program

else if 0 not entered then....

...Print out the number of seconds entered and, ...

...Calculate and print out the total whole number of days contained within said seconds and, ...

...Calculate and print out the total whole number of hours contained within said seconds and, ...

...Calculate and print out the total whole number of minutes contained within said seconds and, ...

...Lastly, calculate and print out the days, hours, minutes and seconds within said seconds in the D:HH:MM:SS format.

Your program should output according to the "Output Format" below.

The program must break down the task at hand into small operations, each one performed by a different method.

Iteratively develop the lab (get one method working, test it, fix syntax errors, continue on to the next).

When the lab is complete and working submit it as Lab2

Things to Consider & Remember

Utilize a commenting style like the one shown in class (here).

Method names and variable names should have the proper camelCase.

Use proper indentation for your curly braces, conditional statements, loops.

You should be able to find some code to re-use (with some modifications required) in the last lecture code sample (here.)

Your program should store the seconds entered by the user in a variable of the long data type.

Your program should have many methods, ideally one method for each of the different feature steps about (approximately 8 in total for i. through x. above).

Test with many different seconds values (a few digits, many digits, 0, ...).

**Please, look at the output format below; I need the program to look exactly like the output format; including the spaces, format and everyhtin** If you want a good rating, I will give it to you, but I need the program to be exactly like it shows below.... thanks!

Output Format!!!!!!!!

Following is an example of the desired output format:



Enter a number of seconds as a whole number or 0 to quit: 3600

Number of seconds entered is 3600 and that equates to:
   0 days
   1 hours
   60 minutes
   0:01:00:00 days, hours, minutes and seconds (D:HH:MM:SS:)

Enter a number of seconds as a whole number or 0 to quit: 123456

Number of seconds entered is 123456 and that equates to:
   1 days
   34 hours
   2057 minutes
   1:10:17:36 days, hours, minutes and seconds (D:HH:MM:SS:)
   
Enter a number of seconds as a whole number or enter 0 to exit: 723672

Number of seconds entered is 723672 and that equates to:
   8 days
   201 hours
   12061 minutes
   8:09:01:12 days, hours, minutes and seconds (D:HH:MM:SS:)    

Enter a number of seconds as a whole number or 0 to quit: 0

0 entered. Goodbye!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class DayCalculator {

    public static void printDetails(int seconds) {
        int daysInSec = 24 * 60 * 60;
        int hoursInSec = 60 * 60;
        int minsInSec = 60;

        int days = 0, hours = 0, mins = 0;
        while (seconds >= daysInSec) {
            days++;
            seconds = seconds - daysInSec;
        }
        while (seconds >= hoursInSec) {
            hours++;
            seconds = seconds - hoursInSec;
        }

        while (seconds >= minsInSec) {
            mins++;
            seconds = seconds - minsInSec;
        }
        String time = String.format("%02d:%02d:%02d:%02d", days, hours, mins, seconds);
        System.out.println(time + " " + "days, hours, minutes and seconds (D:HH:MM:SS:)");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int seconds;

        do {
            System.out.printf("Enter a number of seconds as a whole number or 0 to quit :: ");
            seconds = scanner.nextInt();
            if (seconds == 0) {
                break;
            }
            System.out.println("Number of seconds entered is "+seconds +" and that equates to:\n");
            System.out.println("Days : " + seconds / (24 * 60 * 60));
            System.out.println("Hours : " + seconds / (60 * 60));
            System.out.println("Minutes : " + seconds / (60));
            printDetails(seconds);
        } while (seconds != 0);
        System.out.println("0 entered. Goodbye!");
    }
}

↓ | Objc[959]: Class JavaLaunchHelper îs implemented in both /Library/Java/JavaVirtualMachines/jdk1 .8.0-151.jdk/Contents/Hom
Add a comment
Know the answer?
Add Answer to:
CSCI 161 - Lab 2: "Give Me a Second!" Overview This lab will have you developing...
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
  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • PROBLEM 2 MATLAB It is often important to validate if the user entered an acceptable input...

    PROBLEM 2 MATLAB It is often important to validate if the user entered an acceptable input to a program. For this problem, you may safely assume that the user only enters positive integer inputs (ie.do not consider the case where the user inadvertently enters letters casos hovond thase in the sample user has entered negative numbers). Emulate the output format below: your code should print the autput exactly as shown in the sample runs, Your code should also he generalized...

  • How do I program this problem using c++. Create a process to prompt the user to...

    How do I program this problem using c++. Create a process to prompt the user to enter the number of seconds and determine the whole number of days, hours, minutes, and seconds corresponding to the entered number of seconds. For example if the number of seconds entered was 183945, then 2 days, 3 hours, 5 minutes and 45 seconds should be displayed. There are 86400 seconds in a day, 3600 seconds in an hour, and 60 seconds in a minute....

  • Part 3 (Lab2a) In this exercise you will: a. Write a method called secondTime that takes...

    Part 3 (Lab2a) In this exercise you will: a. Write a method called secondTime that takes as argument an integer corresponding to a number of seconds, computes the exact time in hours, minutes and seconds, then prints the following message to the screen: <inputseconds> seconds corresponds to: <hour> hours, <minute> minutes and <second> seconds Write another method called in Seconds that takes as arguments three integers: hours, minutes and seconds, computes the exact time in seconds, then returns the total...

  • 3. (a) Outline any four features of Object-Oriented Programming OOP, giving examples in each case. [16...

    3. (a) Outline any four features of Object-Oriented Programming OOP, giving examples in each case. [16 marks]      (b) Consider the following code fragments: If   a = 10; Evaluate the new value of “b” in the following:                (i)   b =   ++ a;               (ii) b = a ++;             What value would a and b store in (i) and (ii) after program execution?                                                                                                            [4 marks] 4. Create a C++ program that makes use of three arrays; name, mark, grade. The program should accept...

  • Lab Exercise #11 Assignment Overview You will work with a partner on this exercise during your la...

    help with cse problem Lab Exercise #11 Assignment Overview You will work with a partner on this exercise during your lab session. Two people should work at one computer. Occasionally switch the person who is typing. Talk to each other about what you are doing and why so that both of you understand each step Part A: Class Date . Download the files for this laboratory exercise, then run the Python shell and enter the following commands: >>>import date >>help(...

  • C++ Question 6 (10pt): Convert seconds Write a program that takes a number of seconds as...

    C++ Question 6 (10pt): Convert seconds Write a program that takes a number of seconds as user input (as an integer) and converts it to hours, minutes, and seconds as shown below. You should convert the amount of time in such a way that maximizes the whole numbers of hours and minutes. Expected output 1 (bold is user input) Enter a number of seconds: 60 0 hour(s) 1 minute(s) 0 second(s) Expected output 2 (bold is user input) Enter a...

  • This is done in c programming and i have the code for the programs that it wants at the bottom i ...

    This is done in c programming and i have the code for the programs that it wants at the bottom i jut dont know how to call the functions Program 2:Tip,Tax,Total int main(void) {    // Constant and Variable Declarations    double costTotal= 0;    double taxTotal = 0;    double totalBill = 0;    double tipPercent = 0;    // *** Your program goes here ***    printf("Enter amount of the bill: $");    scanf("%lf", &costTotal);    printf("\n");    // *** processing ***    taxTotal = 0.07 * costTotal;    totalBill...

  • Write a program that asks the user to enter a number ofseconds.• There are...

    Starting Out with C++ (9th Edition)  Chapter 4, Problem 7PCWrite a program that asks the user to enter a number of seconds.• There are 86400 seconds in a day. If the number of seconds entered by the user is greater than or equal to 86400, the program should display the number of days in that many seconds.• There are 3600 seconds in an hour. If the number of seconds entered by the user is less than 86400, but is greater...

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
ADVERTISEMENT