Question

answer the questions throughout this program public class Day implements Comparable {    Private Boolean atWork;...

answer the questions throughout this program

public class Day implements Comparable {
   Private Boolean atWork;
   Private String Weather;
   Private int fuNumber;

}
public Day ( boolean Atwork, String Weather, int funNumber) {
   Atwork = Atwork;
   Weather = Weather;
   funNumber = funNumber;
}
//question 1: implement the getter and setter (mutatto of the funNumber data member. if the number is outside the range 0-10 make the fuNumebr=5;
public boolean equals (Day rhs) {
   // question 2: implement the following method. it determines if two Day objects are equalby comparing their three private data members. if all are the same, then they are equal
}
public int compareTo(Day rhs) {
   //question 3: compare this Day with the parameter Day first by funNumber and if two days have the same fuNumber, then by atWork. a day with the same funNumber butatworkis less than a non-work day.
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Day.java

public class Day implements Comparable {

   // Data members
   private Boolean atwork;
   private String weather;
   private int funNumber;

   // Parameterized constructor
   // Set value of funNumber a/c to given condition
   public Day(boolean atwork, String weather, int funNumber) {
       this.atwork = atwork;
       this.weather = weather;
       setFunNumber(funNumber);
   }

   // Getter and setter for data members
   public Boolean getAtwork() {
       return atwork;
   }

   public void setAtwork(Boolean atwork) {
       this.atwork = atwork;
   }

   public String getWeather() {
       return weather;
   }

   public void setWeather(String weather) {
       this.weather = weather;
   }

   public int getFunNumber() {
       return funNumber;
   }

   public void setFunNumber(int funNumber) {
       if (funNumber < 0 || funNumber > 10) {
           this.funNumber = 5;
       } else {
           this.funNumber = funNumber;
       }
   }

   // In this method, compare all data members
   // if all are equal return true else false
   public boolean equals(Day rhs) {
       if (this.atwork == rhs.getAtwork() && this.weather.equals(rhs.getWeather())
               && this.funNumber == rhs.getFunNumber()) {
           return true;
       } else {
           return false;
       }
   }

   @Override
   public int compareTo(Object obj) {
       // Cast the object to Day
       Day rhs = (Day)obj;
       // -1 or a negative value means this day < rhs
       // 0 means both values are equal
       // 1 or a positive means this day > rhs
       if (this.funNumber == rhs.getFunNumber()) {
           if (this.atwork==true && rhs.getAtwork()==false) {
               return -1;
           } else if(this.atwork==false && rhs.getAtwork()==true){
               return 1;
           }else {
               return 0;
           }
       } else {
           return this.funNumber-rhs.funNumber;
       }
   }
}

DayTest.java - Class with the Main method to check Day class methods

public class DayTest {

   public static void main(String[] args) {
       // Create 3 day objects
       Day d1 = new Day(true,"cloudy",5);
       Day d2 = new Day(false,"hot",11);
       Day d3 = new Day(true,"cloudy",5);
      
       // Check some methods
       System.out.println("FunNumber of d2: "+d2.getFunNumber());
       System.out.println("Compare d1 to d2 result: "+d1.compareTo(d2));
      
       if(d1.equals(d3)) {
           System.out.println("d1 and d3 are equal");
       }else {
           System.out.println("d1 and d3 are not equal");
       }
   }
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
answer the questions throughout this program public class Day implements Comparable {    Private Boolean atWork;...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • public class Item implements Comparable { private String name; private double price; /** * Constructor for...

    public class Item implements Comparable { private String name; private double price; /** * Constructor for objects of class Item * @param theName name of item * @param thePrice price of item */ public Item(String theName, double thePrice) { name = theName; price = thePrice; }    /** * gets the name * @return name name of item */ public String getName() { return name; }    /** * gets price of item * @return price price of item */...

  • JAVA The Comparable interface is defined as follows: public interface Comparable<T> {         int compareTo(T other);...

    JAVA The Comparable interface is defined as follows: public interface Comparable<T> {         int compareTo(T other); } A Film class is defined as public class Film {      private String title;      private int yearOfRelease;           public Film(String title, int yearOfRelease) {           super();           this.title = title;           this.yearOfRelease = yearOfRelease;      }           public void display()      { System.out.println("Title " + title +". Release" + yearOfRelease);      } } Rewrite the Film class so that it...

  • public class NumStack implements Comparable{ private Integer[] data; private int index; public NumStack(int cap){ data=new Integer[cap];...

    public class NumStack implements Comparable{ private Integer[] data; private int index; public NumStack(int cap){ data=new Integer[cap]; index =-1; } public boolean isEmpty(){ return index == -1; } public boolean isFull(){ return index==data.length -1; } public NumStack pop(){ if(!isEmpty()) data[index--]=null; return this; } public int size(){ return index+1; } public Integer top(){ if(isEmpty()) return null; return data[index]; } public NumStack push(int num){ if(index < data.length-1) data[++index]=num; return this; } public int compareTo(NumStack s){} } public int compareTo(NumStack s) compares two stack...

  • Adapt your Rational class : public class Rational { private int num; private int denom; public...

    Adapt your Rational class : public class Rational { private int num; private int denom; public Rational() { num = 0; denom = 1; } public Rational(int num, int denom) { this.num = num; this.denom = denom; } int getNum() { return num; } int getDenom() { return denom; } public Rational add(Rational rhs) { return new Rational(num * rhs.denom + rhs.num * denom, denom * rhs.denom); } public Rational subtract(Rational rhs) { return new Rational(num * rhs.denom - rhs.num...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • Define a public method that is called isPrime() that returns a boolean and implements the Sieve...

    Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public method that is called numberOfPrimes() that returns the number of prime numbers between 2 and the private attribute value. Show this object in a main method that allows the user to interact with all the public methods of your class. Make a class called MyNumber with an integer private attribute. Make a constructor that defines an integer parameter...

  • import java.util.*; public class Movie {    private String movieName; private int numMinutes; private boolean isKidFriendly;...

    import java.util.*; public class Movie {    private String movieName; private int numMinutes; private boolean isKidFriendly; private int numCastMembers; private String[] castMembers;    // default constructor public Movie() { movieName = "Flick"; numMinutes = 0; isKidFriendly = false; numCastMembers = 0; castMembers = new String[10]; }    // overloaded parameterized constructor public Movie(String movieName, int numMinutes, boolean isKidFriendly, String[] castMembers) { this.movieName = movieName; this.numMinutes = numMinutes; this.isKidFriendly = isKidFriendly; numCastMembers = castMembers.length; this.castMembers = new String[numCastMembers];    for(int i=0;i<castMembers.length;i++)...

  • You are to create a class Appointment.java that will have the following: 5 Instance variables: private...

    You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...

  • Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The...

    Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The clone method must allow a deep copy on the students field. In addition, add the equals(Object o) and the toString() methods. Write a test program to invoke the compareTo, clone, equals, and toString methods in a meaningful way. Below is the Listing from the book that needs to be rewritten public class Course {    private String courseName;    private String[] students = new...

  • public class Fish { private String species; private int size; private boolean hungry; public Fish() {...

    public class Fish { private String species; private int size; private boolean hungry; public Fish() { } public Fish(String species, int size) { this.species = species; this.size = size; } public String getSpecies() { return species; } public int getSize() { return size; } public boolean isHungry() { return hungry; } public void setHungry(boolean hungry) { this.hungry = hungry; } public String toString() { return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species; } }Define a class called Lake that defines the following private...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT