Java Help!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kiamari
    New Member
    • Nov 2007
    • 17

    Java Help!!

    I'm new to Java and sort of stuck....

    this is what i have to do:

    Choose a product that lends itself to an inventory (for example, products at your workplace, office supplies, music CDs, DVD movies, or software).
    • Create a product class that holds the item number, the name of the product, the number of units in stock, and the price of each unit.
    • Create a Java application that displays the product number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory (the number of units in stock multiplied by the price of each unit). Pay attention to the good programming practices in the text to ensure your source code is readable and well documented.

    this is what i have so far:

    [CODE=java]// Product class


    public class Product {

    private String title;
    private double item;
    private double stock;
    private double price;

    // Constructor to initialize the product information.
    public Product(String title, double item, double stock,
    double price) {
    setTitle(title) ;
    setItem(item);
    setStock(stock) ;
    setPrice(price) ;

    }

    // Method to get the title
    public String getTitle() {
    return title;
    }

    // Method to get the item number
    public double getItem() {
    return item;
    }

    //Method to get the stock
    public double getStock() {
    return stock;
    }

    //Method to get the price
    public double getPrice() {
    return price;

    }

    // Method to set the title name.
    public void setTitle(String title) {
    this.title = title;
    }

    // Method to set the item number.
    public void setItem(double item) {
    this.item = item;
    }

    // Method to set the stock available.
    public void setStock(double stock) {
    this.stock = stock;
    }

    //Method to set the price.
    public void setPrice(double price) {
    this.price = price;

    }
    // Method to compute the value of inventory.
    public double getInvValue() {
    return this.stock *this.price;
    }

    }[/CODE]

    this is the error i get:

    java.lang.NoSuc hMethodError: main
    Exception in thread "main"
    Process completed.


    how do i fix this? do i need to add more? please help!

    or am i going about this wrong???
    Last edited by Ganon11; Nov 27 '07, 11:53 PM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    It's because you are trying to use the java command on this class - yet you have no main method. You can't say "java myProgram" on a class without a main() method. You should write one that shows that your program works.

    Comment

    • Kiamari
      New Member
      • Nov 2007
      • 17

      #3
      Originally posted by Ganon11
      It's because you are trying to use the java command on this class - yet you have no main method. You can't say "java myProgram" on a class without a main() method. You should write one that shows that your program works.

      ok, so how do i do that?????

      reminder: i am NEW to java

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        main() is just a method that looks like this:

        [CODE=java]public static void main(String[] args) {
        // ...function body goes here...
        }[/CODE]

        If you're so new to Java that you haven't ever made a main() method to make objects and use their methods, then you are diving in way too deep. You should not be learning classes before you learn how to do basic programming statements in main().

        Comment

        • dlb53
          New Member
          • Feb 2008
          • 3

          #5
          Code:
          // Inventory class
          
          
          public class Inventory {
          
              private String title;
              private double item;
              private double stock;
              private double price;
              private double invValue;
          
          	public static void main(String[] args)
          	{
          
             	 // Constructor to initialize the Inventory information.
              	public Inventory(String title, double item, double stock,double price)
          		{
          	setTitle(title);
                  	setItem(item);
                  	setStock(stock);
                  	setPrice(price);
                  	setInvValue(value);
          
          
              // Method to set the title
              	public String setTitle()
              	{
                  	return title;
              	}
          
              // Method to set the item number
              	public double setItem()
              	{
                  	return item;
              	}
          
              //Method to set the stock
              	public double setStock()
              	{
                  	return stock;
              	}
          
              //Method to set the price
              	public double setPrice()
              	{
                  	return price;
              	}
          
              // Method to get the title name.
              	public void gettitle(String title)
              	{
                  	this.title = title;
              	}
          
              // Method to get the item number.
              public void getitem(double item)
              {
                  this.item = item;
              }
          
              // Method to get the stock available.
             	 public void getstock(double stock)
             	 {
                  this.stock = stock;
               }
          
              //Method to get the price.
              	public void getPrice(double price)
              	{
                  this.price = price;
          	    }
          
              // Method to compute the value of inventory.
              	public double getInvValue()
              	{
                  	return this.stock * this.price;
          
            		}
          	}
          }
          I reworked your program above to include the main() method, which it was missing. However, I am now getting the following errors. And cannot figure out the problem, can someone point out my error:

          C:\Users\End User\Documents\ TestPrograms\In ventory.java:16 : illegal start of expression
          public Inventory(Strin g title, double item, double stock,double price)
          ^
          C:\Users\End User\Documents\ TestPrograms\In ventory.java:80 : ';' expected }
          ^
          C:\Users\End User\Documents\ TestPrograms\In ventory.java:80 : '}' expected }
          ^
          3 errors

          Tool completed with exit code 1

          I think we are in the same class.

          Comment

          • sukatoa
            Contributor
            • Nov 2007
            • 539

            #6
            I reworked your program above to include the main() method, which it was missing. However, I am now getting the following errors. And cannot figure out the problem, can someone point out my error:
            C:\Users\End User\Documents\ TestPrograms\In ventory.java:16 : illegal start of expression
            public Inventory(Strin g title, double item, double stock,double price)
            ^
            C:\Users\End User\Documents\ TestPrograms\In ventory.java:80 : ';' expected }
            ^
            C:\Users\End User\Documents\ TestPrograms\In ventory.java:80 : '}' expected }
            ^
            3 errors

            Tool completed with exit code 1

            I think we are in the same class.[/QUOTE]


            I think you forgot this,

            The Inventory Constructor is inside main method?
            You can call it, not like this...

            Is the setItem() has a parameter?
            Is the setTitle() has a parameter?

            hey, when you construct a Set/Get Method, make sure that when you use SetMethod, it should be just to store value.. And the Get Method must return a value...


            You have many getmethods that doesn't return any value....
            Also in set method...

            Try to change it...

            Concerned,
            Sukatoa.... Shadow Shaman..

            Comment

            Working...