Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Need help using interfaces

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Need help using interfaces

    The instructions for this program are to find a way to make it display both the letter grade and the score but I have no idea how to do it without completely defeating the point of the interface. If anyone knows a way to do this I would really appreciate the help.
    public class Quiz implements Measurable
    {
        public Quiz(double initialScore, String initialLetter)
        {
            score = initialScore;
            letter = initialLetter;
        }
        public Object measure()
        {
            return score;
        }
        public Object measure2()
        {
            return letter;
        }
        private double score;
        private String letter;
    }
     
    public interface Measurable
    {
        Object measure();
    }
     
    public class DataSet
    {
       public DataSet()
       {
           sum = 0;
           i = 0;
           maximum = null;
       }
       public void add(Measurable x)
       {
           sum += (double) x.measure();
           if (i == 0 || (double) maximum.measure() < (double) x.measure())
           {
               maximum = x;
           }
           i++;
       }
       public double getAverage()
       {
           return sum / i;
       }
       public Measurable getMaximum()
       {
           return maximum;
       }
       private Measurable maximum; 
       private double sum;
       private int i; 
    }
     
    public class DataSetTester
    {
        public static void main(String[] args)
        {
            DataSet data = new DataSet();
     
            data.add(new Quiz(.98, "A"));
            data.add(new Quiz(.90, "A"));
            data.add(new Quiz(.85, "B"));
            data.add(new Quiz(.28, "F"));
            data.add(new Quiz(.91, "A"));
     
            System.out.println(data.getAverage());
            System.out.println(data.getMaximum().measure());
        }
    }
    Last edited by Niceout; January 13th, 2021 at 09:38 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,555
    Thanks
    53
    Thanked 2,517 Times in 2,468 Posts

    Default Re: Need help using interfaces

    What happens when you compile the program? If there are errors, copy the full text and paste it here.
    What is the program supposed to do? Where are the values to be displayed? What code is supposed to display them? Why does the method in the interface return an Object? What is in the object?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help using interfaces

    The program works this is the output:
    0.784
    0.98

    I just need to figure out how to get the letter grade into the output through the dataset. The exact instructions are as follows: Define a class Quiz that implements the Measurable interface. A quiz has a score and a letter grade (such as B+). Use the first implementation of the DataSet class to process a collection of quizzes. Display the average score and the quiz with the highest score (both letter grade and score).
    Last edited by Niceout; January 13th, 2021 at 09:45 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,555
    Thanks
    53
    Thanked 2,517 Times in 2,468 Posts

    Default Re: Need help using interfaces

    What is supposed to be in the Object returned by measure? Why does the method return an Object instead of a specific class that has useful data and methods?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help using interfaces

    So the point of the Measurable interface is to make the code reusable. The idea being that you could place a bank account class, coin class, quiz class, ect... (in place of the quiz class) and it would work the same. I made it return an Object because it was trying to think of a way to make the letter come through that but ended up just casting it to a double so it probably makes no sense to someone who understands it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,555
    Thanks
    53
    Thanked 2,517 Times in 2,468 Posts

    Default Re: Need help using interfaces

    Yes, I have a reasonable understanding of what an interface is and how to use it.

    My question is about the Measurable interface. What is it supposed to do? How and why would it be used?
    Method names should be verbs describing what the method does. What is being measured by the measure method?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help using interfaces

    Ok so the Measurable interface is supposed to make the code resuable. The book has the method in the interface named "getMeasure" so that is the purpose of it. The measure method is measuring the fundamental data that is inputted rather than looking at specifics of the measurement. It's basically just looking at what all measurable objects have in common for the sake of reducing coupling and making the code reusable.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,555
    Thanks
    53
    Thanked 2,517 Times in 2,468 Posts

    Default Re: Need help using interfaces

    measure method is measuring the fundamental data that is inputted rather than looking at specifics of the measurement. It's basically just looking at what all measurable objects have in common for the sake of reducing coupling and making the code reusable.
    How does it do that? Your description is generic and could apply to lots of methods.
    What does it return after doing all that?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Re: Interfaces
    By meenal in forum The Cafe
    Replies: 0
    Last Post: September 22nd, 2018, 04:41 AM
  2. Interfaces
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 14
    Last Post: July 13th, 2018, 12:57 AM
  3. Interfaces...
    By eggpulusu in forum Java Theory & Questions
    Replies: 1
    Last Post: September 17th, 2013, 10:52 AM
  4. Interfaces
    By leyland in forum Java Theory & Questions
    Replies: 4
    Last Post: April 5th, 2011, 08:51 PM
  5. Importance of interfaces
    By jyothishey in forum Object Oriented Programming
    Replies: 9
    Last Post: March 12th, 2010, 07:11 AM