Results 1 to 12 of 12

Thread: Job Class

  1. #1
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default Job Class

    I have been trying to run the following code and it compiles but somehow it does not show the cost as stored. So here is the code below;

    public Job(int ref, String nam, int pago, int copo, int hour, int minute, boolean doubleSide)
    {

    reference = ref;
    name = nam;
    pageno = pago;
    copyno = copo;
    doubleSides = doubleSide;


    setTime = new Time(hour, minute);
    Date = null;

    }

    /**
    *
    */

    public void calCost(boolean doubleSide, int pago, int copo)
    {
    if (doubleSides == true)
    {
    int c = pago * copo * 2;


    }
    else
    {
    int c = pago * copo * 3;
    }
    }


    and this is what i have been requiring to do;

    The Job class must store the following information about a job: the job's reference number as an int, the name of the member of staff submitting the job as a String, the number of pages and the number of copies to be made as int, whether the copies are to be double sided or not.
    &
    Write an accessor getCost which returns the cost of the job as specified above

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    I think this might be what you are looking for.

    Java Code:
    public Job(int ref, String nam, int pago, int copo, int hour, int minute, boolean doubleSide)
    {
    
    reference = ref;
    name = nam;
    pageno = pago;
    copyno = copo;
    doubleSides = doubleSide;
    
    setTime = new Time(hour, minute);
    Date = null;
    
    //add cost field.  May need to be something besides an int.
    int cost = this.calCost(doubleSides, pageno, copyno);  
    }
    
    /**
    *
    */
    
    //change this to return cost.
    public int calCost(boolean doubleSide, int pago, int copo)
    {
    if (doubleSide == true)
    {
    int c = pago * copo * 2;
    
    
    }
    else
    {
    int c = pago * copo * 3;
    }
    
    return c;
    }
    Disclaimer: I didn't compile it. There is a chance I mistyped something.
    Last edited by ShoeNinja; 11-29-2007 at 07:36 PM.

  3. #3
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by ShoeNinja View Post
    I think this might be what you are looking for.

    Java Code:
    public Job(int ref, String nam, int pago, int copo, int hour, int minute, boolean doubleSide)
    {
    
    reference = ref;
    name = nam;
    pageno = pago;
    copyno = copo;
    doubleSides = doubleSide;
    
    setTime = new Time(hour, minute);
    Date = null;
    
    //add cost field.  May need to be something besides an int.
    int cost = this.calCost(doubleSides, pageno, copyno);  
    }
    
    /**
    *
    */
    
    //change this to return cost.
    public int calCost(boolean doubleSide, int pago, int copo)
    {
    if (doubleSides == true)
    {
    int c = pago * copo * 2;
    
    
    }
    else
    {
    int c = pago * copo * 3;
    }
    
    return c;
    }
    Disclaimer: I didn't compile it. There is a chance I mistyped something.

    I have corrected the mistypes, but still it does not store the cost

  4. #4
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    I don't know what you mean by store the cost? In the above code, there is a field in the job class where the cost is stored.

    Write an accessor getCost which returns the cost of the job as specified above.


    Is this what you need?

    If so:
    Java Code:
    public int getCost(){
    return this.cost;
    }

  5. #5
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by ShoeNinja View Post
    I don't know what you mean by store the cost? In the above code, there is a field in the job class where the cost is stored.

    Write an accessor getCost which returns the cost of the job as specified above.


    Is this what you need?

    If so:
    Java Code:
    public int getCost(){
    return this.cost;
    }
    ye later on i will return the cost, but before that it should calculate the cost. As a result, when i return the cost, it must show me the cost which i have entred not as 0.

  6. #6
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    That should be what it's doing.

    Java Code:
    this.cost = this.calCost(doubleSides, pageno, copyno);
    Here the calCost method uses the data passed to the constructor to calculate and return the cost. Therefore, the cost is stored in cost (imagine that?).

    Again I didn't run this, but I am confident that the concept is correct. A little troubleshooting on your part and you should be there.
    Last edited by ShoeNinja; 11-29-2007 at 07:23 PM.

  7. #7
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Is that all of your code up top? I assume you have something like this before that.

    Java Code:
    public class Job{
    
    int reference, pageno, copyno, cost;
    String name;
    boolean doubleSides;

  8. #8
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by ShoeNinja View Post
    That should be what it's doing.

    Java Code:
    this.cost = this.calCost(doubleSides, pageno, copyno);
    Here the calCost method uses the data passed to the constructor to calculate and return the cost. Therefore, the cost is stored in cost (imagine that?).

    Again I didn't run this, but I am confident that the concept is correct. A little troubleshooting on your part and you should be there.
    it compiles, but still shows the cost as 0.

  9. #9
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Did you make the changes to the calCost method that I posted?

    Java Code:
    public int calCost(boolean doubleSide, int pago, int copo)
    {
    if (doubleSide == true)
    {
    int c = pago * copo * 2;
    
    
    }
    else
    {
    int c = pago * copo * 3;
    }
    
    return c;
    }
    How are you instantiating your Job object?

  10. #10
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by ShoeNinja View Post
    Did you make the changes to the calCost method that I posted?

    Java Code:
    public int calCost(boolean doubleSide, int pago, int copo)
    {
    if (doubleSide == true)
    {
    int c = pago * copo * 2;
    
    
    }
    else
    {
    int c = pago * copo * 3;
    }
    
    return c;
    }
    How are you instantiating your Job object?

    Finally. i have just got it worked. thanks a lot buddy

  11. #11
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Sweet man. What was wrong?

  12. #12
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by ShoeNinja View Post
    Sweet man. What was wrong?
    I should have put the if statement into constructor not making it as a method
    Last edited by kizilbas1; 12-12-2007 at 04:22 PM.

Similar Threads

  1. Replies: 0
    Last Post: 04-02-2008, 07:04 AM
  2. Replies: 0
    Last Post: 02-17-2008, 09:03 AM
  3. Replies: 0
    Last Post: 02-17-2008, 09:01 AM
  4. Inner class accessing outer class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 08:59 AM
  5. Replies: 5
    Last Post: 01-03-2008, 07:54 AM

Posting Permissions

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