Question

Create and implement a class called clockType with the following data and methods Data: Hours, minutes,...

Create and implement a class called clockType with the following data and methods

  • Data:
    • Hours, minutes, seconds
  • Methods:
    • Set and get hours
    • Set and get minutes
    • Set and get seconds
    • printTime(…) to display time in the form of hh:mm:ss
    • default and overloading constructor

Homework Answers

Answer #1
import java.util.Scanner;
public class clockType {
    private int hours;
    private int minutes;
    private int seconds;
    
    public clockType(){  //Default constructor
        this.hours = 0;
        this.minutes = 0;
        this.seconds = 0;
    }
    
    public clockType(int hours, int minutes, int seconds){  //Overloading constructor
        this.hours = hours;
        this.minutes = minutes;
        this.seconds = seconds;
    }
    
    // Getters       
    public int getHours() { //getter for hours
      return hours;
    }
    public int getMinutes() { //getter for minutes
      return minutes;
    }
    public int getSeconds(){ //getter for seconds
      return seconds;
    }

    // Setters
    public void setHours(int hours) {  //setter for hours
      this.hours = hours;
    }
    public void setMinutes(int minutes) {  //setter for minutes
      this.minutes = minutes;
    }
    public void setSeconds(int seconds) {  //setter for seconds
      this.seconds = seconds;
    }
    
    public void printTime(){  //display time in the form of hh:mm:ss
        System.out.println(this.hours+":"+this.minutes+":"+this.seconds);
    }
    
    public static void main(String[] args) {
        clockType obj = new clockType();        // using default constructor
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Hours: ");
        //using setter to set hours
        obj.setHours(sc.nextInt());
        System.out.print("Enter Minutes: ");
        //using setter to set minutes
        obj.setMinutes(sc.nextInt());
        System.out.print("Enter Seconds: ");
        //using setter to set seconds
        obj.setSeconds(sc.nextInt());
        // displaying Time
        obj.printTime();
    }
}

Sample Output

Know the answer?
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
Similar Questions
Create a class called time that has three data members hours, minutes, and seconds. The class...
Create a class called time that has three data members hours, minutes, and seconds. The class has the following member functions: - Overloaded Constructors - Display function to display it, in 11:59:59 format. - SetTime() - IsValid(): Check if the time is valid or not - AddTime(Time t1, Time t2): Add two time values passed as a parameter leaving the result in third time variable. - Increment(): Tells what will be time after a second - Decrement(): Tells what was...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods....
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define...
Create a C# application You are to create a class object called “Employee” which included eight...
Create a C# application You are to create a class object called “Employee” which included eight private variables: firstN lastN dNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week. regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods:  constructor  properties  CalcPay(): Calculate the regular...
a. The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class...
a. The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class named PhoneCall that includes a String field for a phone number and a double field for the price of the call. Also include a constructor that requires a phone number parameter and that sets the price to 0.0. Include a set method for the price. Also include three abstract get methods - one that returns the phone number, another that returns the price of...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for...
Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for all methods should print simple messages to the screen using System.out.println(). Add an integer speed variable that is changed using the ChangeSpeed method. ChangeSpeed adds 5 to the speed each time it is called. Create a default constructor that sets the initial speed to 0. Don't create other constructors or any setter/getter methods. PLEASE CODE THIS IN JAVA
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT