Question

In: Computer Science

Must be written in Java: After completing this lab, you should be able to: Write a...

Must be written in Java:

After completing this lab, you should be able to:

  1. Write a Java class to represent Time.

  2. Create default and parameterized constructors.

  3. Create accessor and mutator methods.

  4. Write a toString method.

This project will represent time in hours, minutes, and seconds.

Part 1:

  1. Create a new Java class named Time that has the following instance fields in the parameterized constructor:

    1. hours

    2. minutes

    3. seconds

  2. Create a default constructor that sets the time to that would represent the following time: 12:00:00.

Part 2:

  1. Create getter methods.

    1. getHours() : returns the current hours of Time

    2. getMinutes() : returns the current minutes of Time

    3. getSeconds() : returns the current seconds of Time.

  2. Create setter methods. Do they return anything?:

    1. setHours() : method allows user to change the hours of Time

      1. Should have 1 input parameter.

    2. setMinutes() : method allows user to change the minutes of Time

      1. Should have 1 input parameter.

    3. setSeconds() : method allows user to change the seconds of Time

      1. Should have 1 input parameter.

    4. setTime() method with 3 parameters -- to change the hours, minutes, and seconds of Time.


DON’T FORGET TO JAVADOC


Part 3:

  1. In your TimeTester class you should have a main method that uses your Time class.

    1. Create 2 Time objects;

      1. One object that will use the default constructor.

      2. One that uses the parameterized constructor.

    2. Print the just hours of both Time objects.

    3. Print the just minutes of both Time objects.

    4. Print the just seconds of both Time objects.

    5. Change the parameterized Time object.

      1. Using setTime() method, change to 12:34:56.

    6. Change the default Time object.

      1. Hours to 4 using setHours().

      2. Minutes to 2 using setMinutes().

      3. Seconds to 3 using setSeconds().

    7. Print both Time objects. What do you notice?

  2. In Time Class

    1. Create a toString() method that, when called, returns the state of the Time object in the format hh:mm:ss i.e. 12:23:33.

  3. In your timeTester class

    1. Print both Times objects. What do you notice?

Don’t forget to check for errors! Make sure to ask questions if you have any.

Compile/build your code, there should be no errors.

Run your code because you just created your main/tester class.

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

Time.java

package c15;

public class Time {
   private int hours;
   private int minutes;
   private int seconds;
   Time(){
       seconds=0;
       minutes=0;
       hours=12;
   }

   public Time(int hours, int minutes, int seconds) {
       super();
       this.hours = hours;
       this.minutes = minutes;
       this.seconds = seconds;
   }

   public void setTime(int hours, int minutes, int seconds) {
       this.hours = hours;
       this.minutes = minutes;
       this.seconds = seconds;
   }

   public int getHours() {
       return hours;
   }
   public void setHours(int hours) {
       this.hours = hours;
   }
   public int getMinutes() {
       return minutes;
   }
   public void setMinutes(int minutes) {
       this.minutes = minutes;
   }
   public int getSeconds() {
       return seconds;
   }
   public void setSeconds(int seconds) {
       this.seconds = seconds;
   }

   @Override
   public String toString() {
       return String.format("Time[%02d:%02d:%02d]", hours,minutes,seconds);
   }

   public static void main(String[] args) {
       Time defaultTime = new Time();
       System.out.println(defaultTime);

       Time valTime = new Time(12,34,30);
       System.out.println(valTime);

       System.out.println("Hour of first object : "+defaultTime.getHours());
       System.out.println("Hour of second object : "+valTime.getHours());

       System.out.println("Minutes of first object : "+defaultTime.getMinutes());
       System.out.println("Minutes of second object : "+valTime.getMinutes());

       System.out.println("Seconds of first object : "+defaultTime.getSeconds());
       System.out.println("Seconds of second object : "+valTime.getSeconds());

       valTime.setTime(12, 34, 56);
       System.out.println(valTime);

       defaultTime.setHours(4);
       defaultTime.setMinutes(2);
       defaultTime.setSeconds(3);
       System.out.println(defaultTime);
   }
}

output


Related Solutions

Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Must be written in C++ in Visual studios community In this lab, you will modify the...
Must be written in C++ in Visual studios community In this lab, you will modify the Student class you created in a previous lab. You will modify one new data member which will be a static integer data member. Call that data member count. You also add a static method to the Student class that will display the value of count with a message indicating what the value represents, meaning I do not want to just see a value printed...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
Program should be written in Java b) The computer program should prompt the user (You are...
Program should be written in Java b) The computer program should prompt the user (You are the user) to enter the answers to the following questions: What is your city of birth? What is your favorite sport? If you could live anywhere in the world, where would you like to live? What is your dream vacation? Take this information and create a short paragraph about the user and output this paragraph. You may use the Scanner class and the System.out...
Lab Objectives Be able to declare a new class Be able to write a constructor Be...
Lab Objectives Be able to declare a new class Be able to write a constructor Be able to write instance methods that return a value Be able to write instance methods that take arguments Be able to instantiate an object Be able to use calls to instance methods to access and change the state of an object Introduction Everyone is familiar with a television. It is the object we are going to create in this lab. First we need a...
MUST BE WRITTEN IN ASSEMBLY LANGUAGE ONLY AND MUST COMPILE IN VISUAL STUDIO You will write...
MUST BE WRITTEN IN ASSEMBLY LANGUAGE ONLY AND MUST COMPILE IN VISUAL STUDIO You will write a simple assembly language program that performs a few arithmetic operations. This will require you to establish your programming environment and create the capability to assemble and execute the assembly programs that are part of this course. Your \student ID number is a 7-digit number. Begin by splitting your student ID into two different values. Assign the three most significant digits to a variable...
• This lab, you will write a Java program to determine if a given Sudoku puzzle...
• This lab, you will write a Java program to determine if a given Sudoku puzzle is valid or not. • You determine if the puzzle is complete and valid, incomplete, or is invalid. • A puzzle is a 2-dimensional array 9x9 array. Each element contains the numbers 1 – 9. A space may also contain a 0 (zero), which means the spot is blank. • If you don’t know how a Sudoku Puzzle works, do some research, or download...
Starbucks after Schultz This activity is important because, as a manager, you must be able to...
Starbucks after Schultz This activity is important because, as a manager, you must be able to identify your company’s core competency and select an appropriate business-level strategy to optimize its competitive value. The goal of this exercise is to demonstrate your understanding of core competency and business-level strategies by applying these concepts to Starbucks’ recent experience in identifying and regaining its competitive advantage. Read the case below and answer the questions that follow. Case Inspired by Italian coffee bars, Starbucks...
This program is written in Java and should be modularized in methods in one class. This...
This program is written in Java and should be modularized in methods in one class. This program will calculate the Gross Earnings, FICA tax (Medicare and Social Security taxes), Federal Tax Withheld, and Net Amount of the payroll check for each employee of a company. The output must contain numbers with 2 decimal places. The user input must be validated – if incorrect data is entered, send an error message to the user. INPUT The application must be able to...
JAVA Generic versions of allOf() and anyOf() In this lab, you will write generic versions of...
JAVA Generic versions of allOf() and anyOf() In this lab, you will write generic versions of the allOf() and anyOf() methods you wrote in an earlier lab. Then you will take those generic versions and create versions that work with a predicate rather than direct comparison of values. Part 1 - Create generic versions of allOf() and anyOf() Recall that the integer-only versions of allOf() and anyOf() looked like this: // Returns true if any element of 'a' is equal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT