In: Computer Science
In the following class
public class Single
{
private float unique;
...
}
Create constructor, setter and getter methods, and toString method.
Write a program that request a time interval in seconds and display it in hours, minutes, second format.
NEED THESE ASAP
Implement the program as follows:
Program: Single.java
Language: Java
import java.util.Scanner; /* import Scanner class */
public class Single{ /* define class Single */
private float unique; /* define private member unique */
public Single(float unique){ /* define a constructor that accepts float value, unique */
this.unique = unique; /* set float value unique */
}
public void setUnique(float unique){ /* define setter method setUnique() that accepts float value unique */
this.unique = unique; /* set float value unique */
}
public float getUnique(){ /* define getter method getUnique() */
return this.unique; /* return unique value */
}
public String toString(){ /* define the method toString() */
return "Single[unique=" + getUnique() + "]"; /* return string representation of Single class */
}
public static void main(String[] args){ /* define the method main() */
Single single = new Single(4.56f); /* Instantiate Single class */
System.out.println(single); /* print Single class object */
System.out.println("Updating unique..");
single.setUnique(6.78f); /* update unique using setUnique() */
System.out.println(single); /* print Single class object */
int time_seconds, hours, minutes, total_minutes, seconds; /* define variables to store hours, minutes and seconds */
Scanner scnr = new Scanner(System.in); /* initialize Scanner */
System.out.print("Enter time interval in seconds : "); /* ask the user to enter time interval in seconds */
time_seconds = scnr.nextInt(); /* read time interval */
seconds = time_seconds%60; /* calculate seconds as time_seconds%60 */
total_minutes = time_seconds/60; /* calculate total_minutes as time_seconds/60 */
minutes = total_minutes%60; /* calculate minutes as total_minutes%60 */
hours = total_minutes/60; /* calculate hours as total_minutes/60 */
System.out.println(hours + ":" + minutes + ":" + seconds); /* print time interval as hours, minutes and seconds */
}
}
Screenshot:
Output:
Please don't forget to give a Thumbs Up.