Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default Write SumGPA that implement the StudentStatisticCacluator interface,

    I was being told to do the following:

    Write SumGPA that implement the StudentStatisticCacluator interface,


    And I was wondering is this how I start out?

    public interface SumGPA{


    public String StudentStatisticCalculator();

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm guessing that they want you to create a class, SumGPA, and that this class implements the above interface.

    Java Code:
    public class MyClass implements MyInterface {
       // interface methods go here
    }

  3. #3
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    So this is what i have as of right now :

    public class SumGPA implements StudentStatisticCalculator {

    }
    With the following error:


    SumGPA.java:1: SumGPA is not abstract and does not override abstract method computeStatistic(Student[]) in StudentStatisticCalculator
    public class SumGPA implements StudentStatisticCalculator {
    ^
    1 error

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hoangx87 View Post
    So this is what i have as of right now :

    public class SumGPA implements StudentStatisticCalculator {

    }
    With the following error:


    SumGPA.java:1: SumGPA is not abstract and does not override abstract method computeStatistic(Student[]) in StudentStatisticCalculator
    public class SumGPA implements StudentStatisticCalculator {
    ^
    1 error
    Yep. That's why my little example has the comment:

    Java Code:
    // interface methods go here

    Please read your text or the tutorials on interfaces as they're crucial in Java, but the most important point is, if your class implements an interface, if it is not abstract (and yours shouldn't be), it must implement all of the methods defined by that interface.

  5. #5
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    I don't have any error but i was wondering, is this what my instructor want for me to do?


    instruction:2) StudentStatisticCalculator.java
    An interface with two methods:
    getName(), which returns a String
    computeStatistic(Student[] students), which returns a double

    You'll write 4 classes that implement the StudentStatisticCacluator interface,
    to find the sum, min, max, and average gpa in the student array passed to printStatistic.


    public class SumGpa implements StudentStatisticCalculator {

    String name;


    public SumGpa(){
    }

    public String getName(){
    return name;
    }

    public double computeStatistic(Student[] students){
    return 1;
    }


    public static void main(String args[]){
    SumGpa s=new SumGpa();


    }



    }

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    No it isn't, the code works but doesn't do the right thing, did you run it? Does it match what the assignment requires?

  7. #7
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    my instructor want for me to get the sum. So im wondering, what do i have to do to get the sum.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Well let's think about that, he wants the sum of what? That's the first step to take, from there we can work on getting the sum

  9. #9
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    he want the sum of name and gpa of which will be written in another class that will be call test students.

    name gpa
    John 3.0
    Jane 4.0
    Jesse 2.0

  10. #10
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    In HW8, you'll create a number of classes to compute statistic over an array of Student objects.

    Please read over this file carefully before beginning.

    Here are the classes you'll create:

    (1) Student.java
    Instance variables: name (a String), gpa (double)
    Instance methods: getName(), getGPA()

    (2) StudentStatisticCalculator.java
    An interface with two methods:
    getName(), which returns a String
    computeStatistic(Student[] students), which returns a double

    You'll write 4 classes that implement the StudentStatisticCacluator interface,
    to find the sum, min, max, and average gpa in the student array passed to printStatistic.

    (3) SumGPA.java:
    (4) MinGPA.java
    (5) MaxGPA.java
    (6) AverageGPA.java

    Each class above does what it sounds like. E.g., the MaxGPA method returns the highest gpa in the array.
    For the getName method, return a description of the class (e.g., "sum").

    (7) StudentStatisticPrinter.java
    This class has one method:
    void printStatistics(StudentStatisticCalculator[] stats, Student[] students) {
    ...
    }

    For each element of the stats array, call computeStatistic on the students array.
    Print the name of each StudentStatisticCacluator object as well as the computedStatistic.

    Finally:
    (8) TestStudents.java

    This has a main method that creates an array of 3 students:
    name gpa
    John 3.0
    Jane 4.0
    Jesse 2.0

    Additionally, create an array of StudentStatisticCalculators, containing max, min, average, and sum.

    Construct a new StudentStatisticPrinter object, and call printStatistics,
    passing in the statisticCalculator array and the student array.

    This should print out:

    average=3.0
    sum=9.0
    min=2.0
    max=4.0

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How does the compute method get access to this information? How would you manually find the sum for those names.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Thanks for the extra information, however; i understand what is needed to be done, I am trying to force you to think about it.

  13. #13
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    if im not mistaken, i would have to use while loops?

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Any loop can be used actually, if you are more comfortable with whole loops, use a while loop. A for loop works good too.

  15. #15
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    Is this it?

    public class SumGpa implements StudentStatisticCalculator {

    String name;
    double sum=0;
    double number=0;

    public SumGpa(){
    }

    public String getName(){
    return name;
    }

    public double computeStatistic(Student[] students){
    return 1;
    }


    public static void main(String args[]){
    SumGpa s=new SumGpa();

    }


    public double Sum(double num1,double num2,double num3){

    for(double number=0;number<3;number++){}
    System.out.println("number is" + number);
    number++;
    return sum;


    }



    }

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The point of implementing an interface is to have the same method produce different answers. In the sum class computeStatistics should produce sum, in average it should produce the average, etc.

    Here is a similar method that uses loops close to what you want, this one produces the factorial of some number
    Java Code:
    public int factorial(int n){
      int answer = 1;
      for(int j = 1; j <= n; j++){ 
        answer *= j;
      }
      return answer;
    }

  17. #17
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    so are you saying the method i have wasnt correct?

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Yes I am. Each class only needs the two methods from the interface. The compute method from the interface should create the create thing per class.

  19. #19
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    if that's the case, what would be the best way to have it to become a sum.

  20. #20
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    i am actually running out of time. I only have approximately 90 min left to finish on his homework.

Page 1 of 2 12 LastLast

Similar Threads

  1. Steps to write code to implement attachment feature
    By kishan in forum Advanced Java
    Replies: 6
    Last Post: 05-04-2009, 10:47 AM
  2. how to implement interface
    By makpandian in forum New To Java
    Replies: 1
    Last Post: 12-09-2008, 02:39 PM
  3. Can generic types implement an interface?
    By jdgallag in forum New To Java
    Replies: 3
    Last Post: 10-30-2008, 04:07 AM
  4. Replies: 1
    Last Post: 10-20-2008, 12:58 PM
  5. How to implement the Icon interface in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •