Results 1 to 4 of 4
  1. #1
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

    Default need help with Java program

    Hello,
    I'm brushing up on my Java programming skills and I'm having trouble with a text book problem. Here is the problem:
    a. Create a class named Invoice containing fields for an item number, quantity, price, and total cost. Create instance methods that set the item number, quantity, and price. Also include a displayLine() method that calculates the total cost for the item as price times quantity, then displays the item number, quantity, price, and total cost.

    b. Create a class named TestInvoice whose main() method declares three Ivoice items. Provide values for each and display them.

    Here's what I have so far:

    public class Invoice {
    private int item;
    private int quantity;
    private int price;
    private int cost = item * quantity;

    public void setItem(int item) {
    this.item = item;
    }

    public void setQuantity(int quantity) {
    this.quantity = quantity;
    }

    public void setPrice(int price) {
    this.price = price;
    }

    public int getItem() {
    return item;
    }

    public int getQuanity() {
    return quantity;
    }

    public int getPrice() {
    return price;
    }

    public void displayLine() {
    System.out.println(cost);
    }
    }

    public class TestInvoice {
    public static void main (String[] args) {
    Invoice oneInvoice = new Invoice();
    Invoice twoInvoice = new Invoice();
    Invoice threeInvoice = new Invoice();

    oneInvoice.setItem(1);
    oneInvoice.setQuantity(2);
    oneInvoice.setPrice(2);

    twoInvoice.setItem(2);
    twoInvoice.setQuantity(2);
    twoInvoice.setPrice(3);

    threeInvoice.setItem(3);
    threeInvoice.setQuantity(2);
    threeInvoice.setPrice(4);

    System.out.println(oneInvoice.getItem());
    System.out.println(oneInvoice.getQuantity());
    System.out.println(oneInvoice.getPrice());

    System.out.println(twoInvoice.getItem());
    System.out.println(twoInvoice.getQuantity());
    System.out.println(twoInvoice.getPrice());

    System.out.println(threeInvoice.getItem());
    System.out.println(threeInvoice.getQuantity());
    System.out.println(threeInvoice.getPrice());

    Invoice.displayLine();
    }
    }

    I'm getting the following error. Could you please help me troubleshoot this?

    TestInvoice.java:31: error: non-static method displayLine() cannot be referenced from a static context
    Invoice.displayLine();

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

    Default Re: need help with Java program

    That method is an instance method and must be called off of an Invoice object (such as oneInvoice, twoInvoice, or threeInvoice), not statically off of the Invoice class as you're trying to do.

  3. #3
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

    Default Re: need help with Java program

    Got it. Thanks!

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

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 05:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 01:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 06:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 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
  •