Question

In: Computer Science

In the following class public class Single { private float unique; ... } Create constructor, setter...

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

Solutions

Expert Solution

Implement the program as follows:

  1. import Scanner class
  2. define class Single
  3. define private member unique
  4. define a constructor that accepts float value, unique
  5. set float value unique
  6. define setter method setUnique() that accepts float value unique
  7. set float value unique
  8. define getter method getUnique()
  9. return unique value
  10. define the method toString()
  11. return string representation of Single class
  12. define the method main()
  13. Instantiate Single class
  14. print Single class object
  15. update unique using setUnique()
  16. print Single class object
  17. define variables to store hours, minutes and seconds
  18. initialize Scanner
  19. ask the user to enter time interval in seconds
  20. read time interval
  21. calculate seconds as time_seconds%60
  22. calculate total_minutes as time_seconds/60
  23. calculate minutes as total_minutes%60
  24. calculate hours as total_minutes/60
  25. print time interval as hours, minutes and seconds

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.


Related Solutions

In the following class: public class Truth { private boolean yes_no; ... } Create constructor, setter...
In the following class: public class Truth { private boolean yes_no; ... } Create constructor, setter and getter methods, and toString method.
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d,...
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d, int y) { dealerPrice = d; year = y; } public float getDealerPrice() { return dealerPrice; } public int getYear() { return year; } public abstract float getStickerPrice(); } In the space below write a concrete class Car in the dealership package that is a subclass of Vehicle class given above. You will make two constructors, both of which must call the superclass constructor....
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public float getPrice() { return price; } } package compstore; public class DeskTopDeals{ // assume proper variables and other methods are here public void dealOfTheDay(Desktop[] items, int numItems){ /**************************** * your code would go here * ****************************/ } } Given the above Desktop class, write code that should go...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price)...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price) { this.contractor = contractor; this.price = price; } public String getContractor() { return contractor; } public float getPrice() { return price; } } package construction; public class ContractorBids{ // assume proper variables and other methods are here public void winningBid(Bid[] bids, int numBids){ /**************************** * your code would go here * ****************************/ } } You are doing renovations on your building, and multiple contractors...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
public class Square {    public static final int NUM_OF_SIDES = 4;    private float length;...
public class Square {    public static final int NUM_OF_SIDES = 4;    private float length;       public Square(float l) {        setLength(l);    }       public void setLength(float l) {        if(l >= 0) {            length = l;        }    }       public float getLength() {        return length;    }           //TODO - Add method to calculate add return area          //TODO -...
public class GroceryCart { private static final int DEFAULT_CAPACITY = 10; /* * Default constructor with...
public class GroceryCart { private static final int DEFAULT_CAPACITY = 10; /* * Default constructor with zero arguments. This constructs a grocery * cart with a default capacity of ten items. */ public GroceryCart() { } /* * Alternate constructor which takes in a maxCapacity. This maxCapacity * determines how many items can fit inside of this groceryCart. */ public GroceryCart(int maxCapacity) { } /* * Adds an item to the grocery cart. Returns true if the item was added...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor function or ES6 class for a Book object. The Book object should store the following data in attributes: title and author. Library Class Create a constructor function or ES6 class for a Library object that will be used with the Book class. The Library object should be able to internally keep an array of Book objects. B. Methods to add Library Class The class...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT