Results 1 to 10 of 10
  1. #1
    c_giov is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Question Java student, need help

    I am a new Java student and we just started using NetBeans in class using an online textbook for homework/study. Here's my issue:

    I need to define a Coffee class to use for calculation examples. I already defined it and it took :

    Java Code:
    class Coffee {
    String kind;
    int price;
    int weight;
    
    Coffee(String kind, int price, int weight) {
    this.kind = kind; 
    this.price = price;
    this.weight = weight;
    }
    
    // Now, the next thing i needed to do is to compute total cost, i re-read the chapter and tried it over and over again. This is what I have:
    
    
    int cost ()  {
    return (this.price ∗ this.weight);
    }
    }
    My textbook has this as an example in the book but when I try it in netbeans it just gives me errors saying "missing return statement" and "not a statement". What am I doing wrong?

    The error that comes up for the "int cost () {" line is:

    Java Code:
    <identifier expected>
    
    Missing return statement
    And the error that comes up for the "return this.price ∗ this.weight ();" line is :

    Java Code:
    illegal character: /8727
    
    unreachable statement
    
    cannot find symbol
     symbol: method weight ()
    
    Surround with ...
    
    Introduce...
    Last edited by c_giov; 11-24-2012 at 11:02 PM.

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

    Default Re: Java student, need help

    Please re-post the entire class code, but wrap the code in [code] [/code] tags. Then post the actual and entire error message, also wrapped in code tags.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Java student, need help

    In addition to what Fubarable said, please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    c_giov is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Java student, need help

    Quote Originally Posted by Fubarable View Post
    Please re-post the entire class code, but wrap the code in [code] [/code] tags. Then post the actual and entire error message, also wrapped in code tags.
    Done! (I think)

  5. #5
    c_giov is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Java student, need help

    Quote Originally Posted by DarrylBurke View Post
    In addition to what Fubarable said, please go through the Forum Rules -- particularly the third paragraph.

    db
    Just read through it! New to this forum and will take that into consideration next time I post. Is there a way I can change the name of the post now? Or is it not editable? Thank you!

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

    Default Re: Java student, need help

    I think I see your problem. On the line indicated as having an error, you're not using the standard multiplication symbol, *. I'm not sure what symbol you're using other than it's similar to * but not quite it.

  7. #7
    c_giov is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Java student, need help

    Quote Originally Posted by Fubarable View Post
    I think I see your problem. On the line indicated as having an error, you're not using the standard multiplication symbol, *. I'm not sure what symbol you're using other than it's similar to * but not quite it.
    That was the problem! I had copy/pasted it from my online textbook example and I guess it didn't register as the " * " symbol in NetBeans. I retyped it and it went through. I can't believe it was that simple after wasting hours trying to figure out different ways

    I do have one more question, I am now trying to figure out a class for "bulk price" using the weight an prices of the Coffee classes I defined, here is the code I am using:

    Java Code:
    double bulkPrice() {
    if (5000 >= this.weight && this.weight <= 20000) {
        return this.price - .10 * this.price;
    }
    }
    If the weight is equal to or more than 5000 and equal to or less than 20000 then there is a 10% discount. So what I tried to do is have the full price minus .10 of the full price to create the 10% off .Now, everything clears except for the first line "double bulkPrice () {" where the error states :

    Java Code:
    missing return statement
    
    Surround with ...
    Any help appreciated!

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

    Default Re: Java student, need help

    Your problem is that the method returns nothing if the weight is out of range, not even a default value, and that's not allowed. You could put in a return price; at the bottom to give it a default value to return. Please show the actual instructions for this though as I wonder if this is indeed correct.

  9. #9
    c_giov is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Java student, need help

    Quote Originally Posted by Fubarable View Post
    Your problem is that the method returns nothing if the weight is out of range, not even a default value, and that's not allowed. You could put in a return price; at the bottom to give it a default value to return. Please show the actual instructions for this though as I wonder if this is indeed correct.
    Ok so here is the problem from my textbook :

    Java Code:
    // represent a coffee sale:
    // at which price, how much
    // coffee was sold
    
    class Coffee {
    String kind;
    int price;
    int weight;
    
    Coffee(String kind, int price, int weight) {
    this.kind = kind;
    this.price = price;
    this.weight = weight;
    }
    
    // to compute the total cost
    // of this coffee purchase
    
    int cost() {
    return this.price * this.weight;
    }
    }
    
    // collect examples of coffee sales
    
    class CoffeeExamples {
    Coffee kona = new Coffee("Kona",2095,100);
    Coffee ethi = new Coffee("Ethiopian", 800, 1000);
    Coffee colo = new Coffee("Colombian", 950, 20);
    
    boolean testKona = check this.kona.cost() expect 209500;
    boolean testEthi = check this.ethi.cost() expect 800000;
    boolean testColo = check this.colo.cost() expect 19000;
    
    CoffeeExamples() { }
    }
    Now here is my assignment :

    Modify the Coffee class so that cost takes into
    account bulk discounts:
    Develop a program that computes the cost of selling bulk
    coffee at a specialty coffee seller from a receipt that includes
    the kind of coffee, the unit price, and the total amount (weight)
    sold. If the sale is for less than 5,000 pounds, there is no discount.
    For sales of 5,000 pounds to 20,000 pounds, the seller
    grants a discount of 10%. For sales of 20,000 pounds or more,
    the discount is 25%. . . .
    Don’t forget to adapt the examples, too.

    I would also like to mention when I do the boolean check examples it comes up with error :

    Java Code:
    cannot find symbol
    symbol: variable check
    location: class Coffee.CoffeeExamples
    
    ';' expected
    
    package kona does not exist
    
    <identifier expected>
    
    ';' expected

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

Similar Threads

  1. A new student study Java
    By MSs.Java in forum New To Java
    Replies: 11
    Last Post: 06-20-2014, 08:31 AM
  2. student interactions gui : Java
    By klater999 in forum Advanced Java
    Replies: 0
    Last Post: 11-13-2012, 02:21 AM
  3. Need Help : student Java programming Beginner
    By wali in forum New To Java
    Replies: 13
    Last Post: 12-28-2011, 06:59 PM
  4. New Java Student in dire need of help
    By Leonspade in forum New To Java
    Replies: 11
    Last Post: 11-22-2010, 09:28 PM
  5. New java student needs help
    By cmizer in forum New To Java
    Replies: 9
    Last Post: 12-13-2008, 06:16 AM

Tags for this Thread

Posting Permissions

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