Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7541403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:52:57+00:00 2026-05-30T07:52:57+00:00

I tried looking up tutorials and videos and I understand what implementing does, although

  • 0

I tried looking up tutorials and videos and I understand what implementing does, although I’m a bit confused as to how one would implement a class from the Java Library. In my homework, I’m suppose to utilize the class, DataSet and make it so it accepts Comparable objects. The program is suppose to record the Min and Max values depending on the objects, in this case, I’m suppose to use strings. I wasn’t sure if I needed any classes to implement the Comparable interface, so I made two classes just in case I was suppose to do so. My real question is how do I actually incorperate a String variable in the tester class to actually read and compare the object to another? thanks in advance.

public class Word implements Comparable
{
private String str;

public Word()
{
    str = null;
}

public Word(String s)
{
    str = s;
}

 public int compareTo(Object other)
 {
    String n = (String) other;
    return str.compareTo(n);
 }

}

I wasn’t sure which of the two classes would be suitable for implementing Although i think the String class below would not work at all b/c It’s already a standard class so I wasn’t too sure about using it

public class String implements Comparable
{
    public String s;

    public String()
    {
    s = null;
    }

public String(String str)
{
    s = str;
}
public int compareTo(Object other)
{
    String n = (String) other;
   return s.compareTo(n);
}
}


public interface Comparable
{
public int compareTo(Object other);
}


public class DataSet
{
 private Object maximum;
 private Object least;
 private Comparable compare;
 private int count;

 public DataSet(Comparable s)
 {
   compare = s;
 }

 public void add(Object x)
 {

   if(count == 0)
   least = x;
   if(count == 0 || compare.compareTo(x) >=0)
   maximum = x;
   else if(compare.compareTo(x) <0)
   least = x;
   count++;

 }
 public Object getMaximum()
 {
  return maximum;
 }

 public Object getLeast()
 {
   return least;
 }

 }


public class DataSetTester
{
   public static void main(String[] args)
  {
  Comparable n = new Word("sand");
  DataSet data = new DataSet(n);


  data.add(new Word(man));



  System.out.println("Maximum Word: " + data.getMaximum());
  System.out.println("Least Word: " + data.getLeast());
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:52:58+00:00Added an answer on May 30, 2026 at 7:52 am

    An interface is a contract that showes that your class contain all methodes that are implemented in the interface. In this case the CompareTo(object other). The String class already implements the comparable interface so you don’t need youre own class. I think your data set class should look something like this :

    public class DataSet<T implements Comparable>
    {
     private T maximum;
     private T least;
     private T count;
    
    
     public void add(T x)
     {
    
       if(count == 0){
         least = x;
         maximum = x;
       }
       else if(least.compareTo(x) > 0)
         least = x;
       else if(maximum.compareTo(x) < 0)
         maximum = x;
       count++;
     }
    
     public T getMaximum()
     {
      return maximum;
     }
    
     public T getLeast()
     {
       return least;
     }
    
     }
    

    T is a generic type and in your case it should be String, Here is how you create a new Data set:
    DataSet<String> ds = new DataSet<String>;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a web services newbie and I've tried to learn it looking for tutorials
I've tried looking at similar problems, but could not easily find one that helped
I have been looking around for solutions, and tried to implement what is often
I have been working with Xcode 4.2.1, but following some tutorials and videos from
Im new to wpf, and looking for good tutorials to help understand triggers better
I tried looking up multiple tutorials on how to change the programs icon in
I've tried looking around and there are similar problems, but mine is way more
I know silly question but i tried looking it up on Google with no
This is a fairly involved bug, and I've tried looking around to find other
I tried urllib{2}, pycurl and I'm looking at twisted's new http client. But: I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.