Programming Assignment 1
Write a class called Clock. Your class should have 3 instance variables, one for the hour, one for the minute and one for the second. Your class should have the following methods:
I am including a main program for you to use to test your class, and also a screenshot of what the expected output should be. (Note this main program doesn’t explicitly test all of your methods, but they should still be provided.)
General notes that apply to all programming assignments:
Here is the main program, just copy and paste:
public class clockTest
{
/**
* A main program to test the Clock class
*/
public static void main (String[] args)
{
Clock myClock = new Clock(23, 59, 59);
System.out.println ("The time is " + myClock.toString());
myClock.tick(1);
System.out.println ("After adding one second, the time is " +
myClock.toString());
myClock.tick(-1);
System.out.println ("After subtracting one second, the time is " +
myClock.toString());
myClock.tick(86400);
System.out.println ("After adding a full day, the time is " +
myClock.toString());
myClock.tick(43200);
System.out.println ("After adding a half day, the time is " +
myClock.toString());
myClock.tick(60);
System.out.println ("After adding a minute, the time is " +
myClock.toString());
myClock.tick(-3600);
System.out.println ("After subtracting an hour, the time is " +
myClock.toString());
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Clock.java
public class Clock {
// attributes
private int hour;
private int minute;
private int second;
// default constructor, initializes time to 12:00:00 AM or 0 hours 0 minutes
// and 0 seconds
public Clock() {
hour = 0;
minute = 0;
second = 0;
}
// constructor taking values for each field
public Clock(int hour, int minute, int second) {
// validating and assigning values
setHour(hour);
setMinute(minute);
setSecond(second);
}
// accessors and mutators for each field
public int getHour() {
return hour;
}
public void setHour(int hour) {
// validating hour (in 24hr format) before assigning
if (hour >= 0 && hour <= 23)
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
if (minute >= 0 && minute <= 59)
this.minute = minute;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
if (second >= 0 && second <= 59)
this.second = second;
}
// method to advance or withdraw given number of seconds from the current
// time
public void tick(int seconds) {
// checking if seconds is positive
if (seconds > 0) {
// looping for seconds number of times
for (int i = 0; i < seconds; i++) {
// advancing second
this.second++;
// if second go out of range, wrapping from 0 and advancing
// minute
if (this.second > 59) {
this.second = 0;
this.minute++;
}
// if minute is out of range, advancing hour
if (this.minute > 59) {
this.minute = 0;
this.hour++;
}
// if hour is out of range, wrapping around from 0
if (this.hour > 23) {
this.hour = 0;
}
}
} else {
// looping for -seconds number of times
for (int i = 0; i < -seconds; i++) {
// decrmenting second
this.second--;
// wrapping around second, minute or hour if necessary. this
// time, checking for the lower bounds
if (this.second < 0) {
this.second = 59;
this.minute--;
}
if (this.minute < 0) {
this.minute = 59;
this.hour--;
}
if (this.hour < 0) {
this.hour = 23;
}
}
}
}
public String toString() {
// initially assuming time is "AM"
String amPM = "AM";
// if hour field is 12 or above, setting amPM to "PM"
if (hour >= 12) {
amPM = "PM";
}
// taking hour value
int h = hour;
// if hour is 0, setting h to 12 (0 hour in 24 hr format is 12 hour in
// 12 hr format)
if (h == 0) {
h = 12;
}
// otherwise if h exceeds 12, subtracting 12 from time to convert 24 hr
// time into 12 hr time
else if (h > 12) {
h = h - 12;
}
//returning a String in HH:MM:SS AM/PM format
return String.format("%02d:%02d:%02d %s", h, minute, second, amPM);
}
}
/*OUTPUT*/
The time is 11:59:59 PM
After adding one second, the time is 12:00:00 AM
After subtracting one second, the time is 11:59:59 PM
After adding a full day, the time is 11:59:59 PM
After adding a half day, the time is 11:59:59 AM
After adding a minute, the time is 12:00:59 PM
After subtracting an hour, the time is 11:00:59 AM
Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one...
In this practical task, you need to implement a class called MyTime, which models a time instance. The class must contain three private instance variables: hour, with the domain of values between 0 to 23. minute, with the domain of values between 0 to 59. second, with the domain of values between 0 to 59. For the three variables you are required to perform input validation. The class must provide the following public methods to a user: MyTime() Constructor. Initializes...
Write a class called Book. Here are the relevant attributes: title author yearPublished bookPriceInCAD Provide a constructor that takes parameters to initialize all the instance variables. The constructor calls the mutator (set) methods to initialize the instance variables. Provide an accessor (get) and mutator (set) for each instance variable. The mutators all validate their parameters appropriately and use them only if valid. If the passed parameter was invalid an IllegalArgumentException will be thrown with a proper error message A...
C++ Assignment: Create a class called FitnessMember that has only one variable, called name. The FitnessMember class should have a constructor with no parameters, a constructor with a parameter, a mutator function and an accessor function. Also, include a function to display the name. Define a class called Athlete which is derived from FitnessMember. An Athlete record has the Athlete's name (defined in the FitnessMember class), ID number of type String and integer number of training days. Define a class...
In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...
Exercise #3: Create the “MathTest” class. It will have two class variables: 1) a question and 2) the answer to that question. Exercise #4: Please create an accessor and mutator method for both of those variables, without which we would not be able to see or change the question or the answer. Exercise #5: There should be a constructor method. We will also have a “ToString” method, which will print the question followed by its answer. The constructor method has...
Create a class called Retangle.java that contains two double-precision instance variables named width and height. The class should include all kinds of overloaded constructors. Additionally, there should be two accessor methods, mutator methods, class method named area() that returns the area of a Rectangle object. Inside main(),fully test all methods.
Create a class called DuplicateRemover. Create an instance method called remove that takes a single parameter called dataFile (representing the path to a text file) and uses a Set of Strings to eliminate duplicate words from dataFile. The unique words should be stored in an instance variable called uniqueWords. Create an instance method called write that takes a single parameter called outputFile (representing the path to a text file) and writes the words contained in uniqueWords to the file pointed...
You will write a class called Shoe.java Shoe.java should have Three instance variables String brand (cannot be blank) double size (from 5 to 12) int color (a number from 1 to 5 representing one of 5 colors) This code is to control the amount of colors. the colors represented are as follows 1. red 2. green 3. blue 4. black 5. grey One constructor, one get method per instance variable, one set method per instance variable. You will need a...
*Python* INTRODUCTION: The goal of this programming assignment is to enable the student to practice object-oriented programming using classes to create objects. PROBLEM DEFINITION: Write a class named Employee that holds the following data about an employee in attributes: name, IDnumber, department, jobTitle The class should have 8 methods as follows: For each attribute, there should be a method that takes a parameter to be assigned to the attribute. This is known as a mutator method. For each...
Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...