Results 1 to 20 of 29
Thread: help! implementing an interface
- 11-16-2008, 04:53 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
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 05:52 AM.
-
Yes. Try it first, then come back.any suggestions?
- 11-17-2008, 12:43 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
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+".");
-
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?
- 11-17-2008, 01:10 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
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....
-
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 [code] at the top of your block of code and the tag [/code] at the bottom, like so:
Java Code:[code] // your code block goes here. // note the differences between the tag at the top vs the bottom. [/code]
- 11-17-2008, 01:44 AM #7
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
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
-
That looks excellent. Really, it does. Are you still having problems?
- 11-17-2008, 01:59 AM #9
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
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
-
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-17-2008, 02:15 AM #11
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
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 :
that doesnt seem right ... would it be using feeCalculator instead? but feeCalculator is not a class. ...Java Code:Student x = new Student(); x.getHousingFees(); x.getTuitionFees(totalCredits);
-
Your problem is here:
You never pass the total credits to the Student variable, "x". You must do this first, then do your calculations.Java Code:Student x = new Student(); x.getHousingFees(); x.getTuitionFees(totalCredits);
Also "getTuitionFees" doesn't take a parameter, nor should it.
- 11-17-2008, 02:20 AM #13
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
my calculations are in:
with the interface being :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; }
Java Code:public interface IFeeCalculator { double calculateHousingFees(); double calculateTuitionFees(int totalCredits);
-
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.
- 11-17-2008, 02:27 AM #15
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
i thuoght that totalCredits would be from the loop that adds up all the credits the student has ...
-
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.
- 11-17-2008, 02:40 AM #17
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
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 ....
-
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! } }
- 11-17-2008, 03:01 AM #19
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
what exactly is :
myFoo.setNumber(number); ??
-
it's in the MyFubar class:
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.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 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?
Similar Threads
-
Implementing Interface
By mew in forum New To JavaReplies: 4Last Post: 02-16-2010, 03:33 PM -
Implementing an interface
By bugger in forum Advanced JavaReplies: 1Last Post: 01-09-2008, 01:35 PM -
Implementing Serializable interface
By javaplus in forum Advanced JavaReplies: 4Last Post: 12-18-2007, 12:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks