Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    manda147 is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default help! implementing an interface

    Create a new Java interface by selecting New from the File menu and then Interface. For the interface name enter IFeeCalculator and then click the Finish button. A new file will be created with the name IFeeCalculator.java - in this file you will find the declaration of an interface with the name IFeeCalculator. Add the declarations of the calculateTuitionFees and calculateHousingFees method (as described above) to this interface. Note that no other methods or constants are required.

    You will now create two classes that implement this interface. The first is called UndergradFeeCalculator and the other is called GraduateFeeCalculator.

    For undergraduate students, fees are calculated as follows: tuition is $500 base fee plus $375 per credit and housing is $1200. For graduate students fees are calculated as: tuition is $1500 base fee plus $325 per credit and housing is $1500. Use this information to implement the calculateTuitionFees and calculateHousingFees methods in each of the classes.

    ---

    Write a short Java application that:

    * creates an instance of Student that uses an instance of UndergradFeeCalculator to calculate fees (the particular name and student number are not important)
    * adds three courses having credit values 3, 1 and 4 to this student
    * prints the tuition and housing fees payable by this student
    * changes the calculator used by the student object to an instance of GraduateFeeCalculator
    * prints the tuition and housing fees payable by this student





    i think i've figured out the frist part, but i don't know how to write a program that uses the student class. any suggestions? thanks in advance!
    Last edited by manda147; 11-16-2008 at 06:52 AM.

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

    Default

    any suggestions?
    Yes. Try it first, then come back.

  3. #3
    manda147 is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default

    this is what i've done:


    public interface IFeeCalculator {

    double calculateHousingFees();

    double calculateTuitionFees(int totalCredits);


    }
    -----------

    public class GraduateFeeCalculator implements IFeeCalculator{


    public double calculateHousingFees() {
    int housingfees=1500;
    return housingfees;
    }


    public double calculateTuitionFees(int totalCredits) {
    int tuition=1500+325*totalCredits;
    return tuition;
    }

    //tuition is $1500 base fee plus $325 per credit and housing is $1500.

    }
    ----------
    same thing for undergrad with different values, and they've given us the student class which we are supposed to use, and this is how i've written my program, but something's wrong or im missing something ....
    --------
    import java.util.Scanner;


    public class StudentFeeCalculator {

    public static void main (String[]args){

    Scanner kbd = new Scanner(System.in);

    System.out.println("how many courses?");

    int numberofcourses = kbd.nextInt();

    for (int i=0; i<numberofcourses;)
    {
    System.out.println("how many credits per course?");
    int credits = kbd.nextInt();
    int totalCredits = 0;
    totalCredits = totalCredits + credits;
    i++;
    }

    Student x = new Student();
    x.getHousingFees();
    x.getTuitionFees(totalCredits);

    System.out.println("Your housing fee is "+getHousingFees+" and tuition is "+getTuitionFees+".");

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

    Default

    I think that you don't have to worry about Scanner or user interaction here (I could be wrong). The key here is that you have a Student class (not StudentFeeCalculator) that has a "calculator" variable, and this calculator variable needs to act as an undergrad calculator and then change to a graduate calculator. Knowing this, what "type" should this calculator variable be?

  5. #5
    manda147 is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default

    i'm not sure what you mean about the calculator ...

    there is :

    public void setFeeCalculator( IFeeCalculator feeCalculator )
    {
    this.feeCalculator = feeCalculator;
    }

    as part of the student class, but i dont know how to change it between undergrad and grad. my problem right now is that in my program, (using eclispe), it says :

    Student x = new Student();
    x.getHousingFees();
    x.getTuitionFees(totalCredits);

    "new student()" is a undefined constructor, but isnt it defined in the studnet class? and also, "totalCredits" cannot be resolved, but i thought declaring totalCredits in the loop (it is also in the studnet class) would still work....

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

    Default

    You need to post your attempt at a Student class, one that has a feeCalculator variable. This way I can see how you declare this variable and can perhaps point you in the right direction for the other problems.

    Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you can will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

    Java Code:
    &#91;code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    &#91;/code]

  7. #7
    manda147 is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default

    my student class: the student class is:

    Java Code:
    public class Student
    {
      
        private String name;
      
        private int studentNum;
    
        private int totalCredits;
      
        private IFeeCalculator feeCalculator;
        
        public Student( String name, int studentNum,
                IFeeCalculator feeCalculator )
        {
            this.name = name;
            this.studentNum = studentNum;
            this.feeCalculator = feeCalculator;
            totalCredits = 0;
        }
        
        public void addCredits( int creditLoad )
        {
            totalCredits += creditLoad;
        }
    
        public void removeCredits( int creditLoad )
        {
            totalCredits -= creditLoad;
        }
        
        public double getTuitionFees()
        {
            return feeCalculator.calculateTuitionFees( totalCredits );
        }
    
        public double getHousingFees()
        {
            return feeCalculator.calculateHousingFees();
        }
    
        public String getName()
        {
            return name;
        }
     
        public void setName( String name )
        {
            this.name = name;
        }
    
        public int getStudentNum()
        {
            return studentNum;
        }
    
        public void setFeeCalculator( IFeeCalculator feeCalculator )
        {
            this.feeCalculator = feeCalculator;
        }
    }

    thanks for your help

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

    Default

    That looks excellent. Really, it does. Are you still having problems?

  9. #9
    manda147 is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default

    yes. i need to:

    Write a short Java application that:

    * creates an instance of Student that uses an instance of UndergradFeeCalculator to calculate fees (the particular name and student number are not important)
    * adds three courses having credit values 3, 1 and 4 to this student
    * prints the tuition and housing fees payable by this student
    * changes the calculator used by the student object to an instance of GraduateFeeCalculator
    * prints the tuition and housing fees payable by this student

    and waht i've written (above) doens't work .... i am pretty sure my studnet class is correct, but i am not sure how to incorporate the calculator so that it can be interchanged between grad and undergrad

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

    Default

    Yes Student looks like it should work fine. So just do what the instructions tell you to do:

    * Create a Student object
    * Add the credits
    * Calculate and display the fees
    * Change the calculator
    * recalculate and display the fees.

    This program won't take but a few lines.

  11. #11
    manda147 is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default

    what i've done so far is :

    Java Code:
    import java.util.Scanner;
    
    
    public class StudentFeeCalculator {
    
    public static void main (String[]args){
    	
    	Scanner kbd = new Scanner(System.in);
    	
    	System.out.println("how many courses?");
    	
    	int numberofcourses = kbd.nextInt();
    		
    		for (int i=0; i<numberofcourses;)
    		{
    		System.out.println("how many credits per course?");
    		int credits = kbd.nextInt(); 
    		int totalCredits = 0;
    		totalCredits = totalCredits + credits;
    		i++;
    		}
    		
    		Student x = new Student();
    		x.getHousingFees();
    		x.getTuitionFees(totalCredits);
    		
    		System.out.println("Your housing fee is "+getHousingFees+" and tuition is "+getTuitionFees+".");

    and if the scanner isn't required, i would just removed all the user input parts, but its the :

    Java Code:
    		Student x = new Student();
    		x.getHousingFees();
    		x.getTuitionFees(totalCredits);
    that doesnt seem right ... would it be using feeCalculator instead? but feeCalculator is not a class. ...

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

    Default

    Your problem is here:
    Java Code:
    Student x = new Student();
    x.getHousingFees();
    x.getTuitionFees(totalCredits);
    You never pass the total credits to the Student variable, "x". You must do this first, then do your calculations.

    Also "getTuitionFees" doesn't take a parameter, nor should it.

  13. #13
    manda147 is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default

    my calculations are in:

    Java Code:
    public class GraduateFeeCalculator implements IFeeCalculator{
    
    
    	public double calculateHousingFees() {
    		int housingfees=1500;
    		return housingfees;
    	}
    
    
    	public double calculateTuitionFees(int totalCredits) {
    		int tuition=1500+325*totalCredits;
    		return tuition;
    	}
    with the interface being :

    Java Code:
    public interface IFeeCalculator {
    
    	double calculateHousingFees();
    
    	double calculateTuitionFees(int totalCredits);

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

    Default

    I know that. But in order for these calculations to work, the Student object must know how many credit hours it has. The only way it can know this is if you pass the credit hours to this object. It has a method to do this, and you should use it.

  15. #15
    manda147 is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default

    i thuoght that totalCredits would be from the loop that adds up all the credits the student has ...

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

    Default

    but how will the Student object know what credits it has unless it is specifically told? Not only that, your loop is broken in that if you want a number incremented by the loop, it needs to be declared outside of the loop, else it will be reset to zero with each iteration of the loop, and won't be visible and thus not usable outside of the loop.

  17. #17
    manda147 is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default

    okayyyy .. i've tried a couple things and am just completely stuck.
    i cannot move totalCredits = totalCredits + credits; outside the loop becuase then it just doesnt work .. and i dont know how to TELL the object how many credits there are because my rationality was that the totalCredits thing in the loop WOULD tell it ... but aparnetly not ....

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

    Default

    Please look at this in a very simple way. Check out these two classes as they are doing essentially what you are trying to do:
    Java Code:
    public class MyFubar  // This is your Student class
    {
      private int number = 0;
      
      public void setNumber(int number)
      {
        this.number = number;
      }
      
      public int getNumber()
      {
        return number;
      }
    }
    Java Code:
    public class TestMyFubar // this is your StudentFeeCalculator class
    {
      public static void main(String[] args)
      {
        int number = 8;
        MyFubar myFoo = new MyFubar();
        System.out.println(myFoo.getNumber());  // do you think that this should output 8??? nope
        
        myFoo.setNumber(number);  // here we TELL myFoo what its number is.  You have to do the same with your Student object
        System.out.println(myFoo.getNumber());  // NOW it will output 8!
      }
    }

  19. #19
    manda147 is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default

    what exactly is :

    myFoo.setNumber(number); ??

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

    Default

    it's in the MyFubar class:
    Java Code:
    public class MyFubar  // This is your Student class
    {
      private int number = 0;
      
      public void setNumber(int number)
      {
        this.number = number;
      }
      
      public int getNumber()
      {
        return number;
      }
    }
    The bottom line is that the MyFubar object myFoo will not know what its own "number" is based on numbers that are swimming around it. The only way you can change the myFoo number is to specifically call a MyFubar method that allows you to change this number.

    The situation is exactly the same for your class. Please look at your Student class and all of its methods. Is there any method there that you can call to change its credit hours? Surely you can see this, right?

Page 1 of 2 12 LastLast

Similar Threads

  1. Implementing Interface
    By mew in forum New To Java
    Replies: 4
    Last Post: 02-16-2010, 04:33 PM
  2. Implementing an interface
    By bugger in forum Advanced Java
    Replies: 1
    Last Post: 01-09-2008, 02:35 PM
  3. Implementing Serializable interface
    By javaplus in forum Advanced Java
    Replies: 4
    Last Post: 12-18-2007, 01:29 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
  •