Skip to main content

Post content has been hidden

To unblock this content, please click here

Doughnut
Beginner June 2008

Java (programming) heeeelp

Doughnut, 30 May, 2009 at 18:40 Posted on Off Topic Posts 0 16

Anyone? ?

I was 'getting it' until I had to write my own code. Now I can't make my constructor work. Does anyone speak Java?

I study online at home, and I have no-one to ask. I have Googled but I can't work out what I'm doing wrong; it looks OK to me ?

16 replies

Latest activity by Doughnut, 31 May, 2009 at 21:40
  • raspberryjam
    raspberryjam ·
    • Report
    • Hide content

    Pm me the code my H will make it work.

    hth

    • Reply
  • raspberryjam
    raspberryjam ·
    • Report
    • Hide content

    If you can't pm me let me know will briefly post my email ad

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    Thanks. I've not had much luck with the PM so I'll just post it here if it's OK?

    I have an empty class wrapper for a class called StockItem which will be a stock management system, and this is the instructions I've been given:

    This class StockItem is part of a stock management system, and is to have the following fields:

    • description: a String describing the StockItem type
    • quantity: an int giving the number of the StockItem in stock
    • price: an int giving the current (sale) price (in pence)

    Declare these fields, making them all private, and check that StockItem still compiles without any errors.

    When a StockItem object is created, its description and price are provided by its creator, but the quantity always starts at zero.

    Okaaay, so I have written this:

    public class StockItem
    {
    private String description;
    private int quantity;
    private int price;

    public StockItem(String item, int pence)
    {
    description = item;
    quantity = 0;
    price = pence;
    }

    }

    It compiles OK but when I create a new object of the class, it doesn't do what I expect and doesn't let me enter my own item of stock or price. It says Error - cannot find symbol variable Baked beans (or whatever item I entered).

    I'm off to get a curry in a min so will check back in a bit. Thank yooou kind people. I am finding studying on my own a bit lonely and frustrating - if you get stuck, you're just stuck, so there's no point thinking "Right, I'll get a good 3 hours done tonight" because you just can't. Sorry, I'm ranting on now.

    • Reply
  • raspberryjam
    raspberryjam ·
    • Report
    • Hide content

    The constructer is fine, the code creating the object looks as though it may be wrong if you send me the whole code I will have a look. Also how does your code retrieve the values, you need methods to retrieve the data (because they are private)

    MrJam

    • Reply
  • raspberryjam
    raspberryjam ·
    • Report
    • Hide content

    This is an example of a working self standing application, but it depends on the rest of your code as to whether this solves your problem so if you send the code I will take a look.


    public class StockItem
    {
    private String description;
    private int quantity;
    private int price;
    public StockItem(String description, int price)
    {
    this.description = description;
    this.quantity = 0;
    this.price = price;
    }
    String getDescription() {
    return description;
    }
    int getQuantity() {
    return quantity;
    }
    int getPrice() {
    return price;
    }
    // Main program to instanciate and call StockItem object
    public static void main(String[] args) {
    System.out.println("Creating StockItem Object");
    StockItem myItem = new StockItem("Baked Beans", 127);
    System.out.println("Retrieving Item");
    System.out.println("Item:" + myItem.getDescription());
    System.out.println("Price:" + myItem.getPrice());
    System.out.println("Quantity:" + myItem.getQuantity());
    }
    }

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    Thanks. This is as far as I have got - I haven't started on the methods yet; I didn't want to go any further when I couldn't even get the first bit right.

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    The tutor for this module hasn't taught online before so replies to you and stuff, which is great and completely different to my last two modules. The forum is pretty quiet and people seem to be really far ahead of the learning schedule, so if you're working on what you're meant to be, everyone else did it ages ago and has moved on. Bloody keeners ?

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    Thanks for this raspberryjam, I'll take a closer look and try and work out where I'm going wrong.

    • Reply
  • raspberryjam
    raspberryjam ·
    • Report
    • Hide content

    Feel free to email if you need any help, my H is happy to help.

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content
    View quoted message

    ?

    Thanks. You may be hearing from me a lot over the coming months ?

    • Reply
  • raspberryjam
    raspberryjam ·
    • Report
    • Hide content

    No problem am going to edit my email address now though!

    • Reply
  • summer solstice
    Beginner June 2008
    summer solstice ·
    • Report
    • Hide content

    When you're writing the code to create he new object - do you have bakedbeans inside quotation marks to denote that it is your string, and not a reference to a variable?

    i.e

    StockItem myItem = new StockItem("baked beans", 21);

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content
    View quoted message

    No, I didn't have. I do now, and it works. I love you! ?

    • Reply
  • summer solstice
    Beginner June 2008
    summer solstice ·
    • Report
    • Hide content

    Any time - my Java is a little rusty, but feel free to give me a shout if you need someone to cast an eye over code in future!

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    Thank you! ?

    And thank you all kind Java hitchers and their husbands!

    • Reply

You voted for . Add a comment 👇

×


Premium members

  • Q
    Qa Test I got married in August - 2022 North Yorkshire

General groups

Hitched article topics

Contest icon

Win £3,000 for your wedding

Join Hitched Rewards, where you can win £3,000 simply by planning your wedding with us. Start collecting entries, it's easy and free!

Enter now