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...
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...
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...
Create a class for working with complex numbers. Only 2 private float data members are needed,...
Create a class for working with complex numbers. Only 2 private float data members are needed, the real part of the complex number and the imaginary part of the complex number. The following methods should be in your class:a. A default constructor that uses default arguments in case no initializers are included in the main.b. Add two complex numbers and store the sum.c. Subtract two complex numbers and store the difference.d. Multiply two complex numbers and store the product.e. Print...
How do you write the constructor for the three private fields? public class Student implements Named...
How do you write the constructor for the three private fields? public class Student implements Named { private Person asPerson; private String major; private String universityName; @Override public String name() { return asPerson.name(); }
Define the following class: class XYPoint { public: // Contructors including copy and default constructor //...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor // Destructors ? If none, explain why double getX(); double getY(); // Overload Compare operators <, >, ==, >=, <=, points are compare using their distance to the origin: i.e. SQR(X^2+Y^2) private: double x, y; };
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT