6 Replies - 267 Views - Last Post: 02 December 2017 - 11:55 AM Rate Topic: -----

#1 chloeCodes   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Implementing an Interface question

Posted 02 December 2017 - 07:13 AM

Hello!

I am practicing the use Interface types.

I have written a generic interface which has the method check in it.
The method check takes an object of the class's type argument and returns a boolean
public interface Checking<T>
{
   public boolean check(Checking<T> check);
}



Then I want to write some classes which implement the above interface. I want to write a class which implement the interface type.
I want to write a class whose type argument is Integer, and the method check returns true if the integer argument is odd, false otherwise.
I have written:
public class WillCheck implements Checking<Integer>
{
   public boolean check(Checking<Integer> check)
   {
     //Integer convertCheck = (Integer)check;
     //return true if the Integer argument is odd
     if[b](check %2 !=0)[/b]     {
         return true;
     }
     else
     {
         return false;
     }
   }
}



The problem is in the line of code that I have made bold, I don't know what else to do? I tried type casting, however, that also gives me an error. I would appreciate some advice.

Many Thanks.

This post has been edited by chloeCodes: 02 December 2017 - 07:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Implementing an Interface question

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: Implementing an Interface question

Posted 02 December 2017 - 08:18 AM

The % operator can only be used on numerical primitive data types. Should Checking objects have some associated value? Note that the parameter check only has access to the methods and constants specified by the interface.

Note that an interface cannot force an implementing class to have a specific instance variable, which seems to be what you want.

You might want to give more thought to the design here and what you want to accomplish.
Was This Post Helpful? 0
  • +
  • -

#3 chloeCodes   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Re: Implementing an Interface question

Posted 02 December 2017 - 08:25 AM

Hi,

I have to write a generic interface which defines the class of objects which have the following method: check in them which takes an object of the class's type argument and returns a boolean.

And then I have to write some classes which implement the interface. A class whose type argument is an integer, whose check method returns true if its integer argument is odd. That is the practice question that I have been given, and the code I have posted in the solution.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: Implementing an Interface question

Posted 02 December 2017 - 08:31 AM

Your instructions tell you how to define the check() method. Re-read them- specifically for the parameter type of the check() method. The instructions tell you how to fix your problem.

Quote

I have to write a generic interface which defines the class of objects which have the following method: check in them which takes an object of the class's type argument and returns a boolean.

This post has been edited by macosxnerd101: 02 December 2017 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7197
  • View blog
  • Posts: 15,004
  • Joined: 16-October 07

Re: Implementing an Interface question

Posted 02 December 2017 - 09:31 AM

Frankly, that interface seems kind of nonsensical.

How does a implementer of Checking<T> know the particulars of any other Checking<T> implementation?

Rather, one would expect something like this:
public interface Checking<T> {
   boolean check(); // only the instance knows if this is true or not
}

class WillCheck implements Checking<Integer> {
   public boolean check() {
     // your code here
   }
}



If you want to do more than that, your interface needs to offer more to play with:
public interface Checking<T> {
   T getAmount();
   boolean check(Checking<T> that);
}

class WillCheck implements Checking<Integer> {
   public boolean check(Checking<Integer> that) {
     Integer thatValue = that.getAmount();
     // ...



Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#6 chloeCodes   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 178
  • Joined: 05-January 17

Re: Implementing an Interface question

Posted 02 December 2017 - 10:20 AM

Thank you! You are fantastic!
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: Implementing an Interface question

Posted 02 December 2017 - 11:55 AM

As per the instructions:

Quote

I have to write a generic interface which defines the class of objects which have the following method: check in them which takes an object of the class's type argument and returns a boolean.


Your instructor is looking for:
public boolean check(T other);



Rather than:
public boolean check(Checking<T> other);


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1