Results 1 to 10 of 10
  1. #1
    sinista is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Cool No main method, constructor/object not working

    can someone help me with my code:
    It is saying a need a main method for salesperson, but i do,
    constructors(MAIN) not working,
    objects not working(MAIN)






    import javax.swing.JOptionPane;
    public class salesperson
    {
    private int SalesPersonId;
    public Float SalesAmount;
    public String SalesPersonname;
    protected Float commission;
    int num;


    public void setid(int id)
    {
    SalesPersonId=id;
    while((num<1)||(num>100))
    System.out.println("Id entered must be between 1 and 100");

    }
    public void setname(String name)
    {
    SalesPersonname=name;
    }
    public void setamount(Float amount)
    {
    SalesAmount=amount;
    if((amount<5000)&&(amount>30000))
    System.out.println("Amount entered must be between 5000 and 30000");
    }
    void commissioncalc(){
    if((SalesAmount>=5000)&&(SalesAmount<=10000))
    commission=SalesAmount*5/100;
    else
    { if((SalesAmount>1000)&&(SalesAmount<20000))
    commission=SalesAmount*6/100;
    else
    commission=SalesAmount*5/100*3*100;

    }}

    public Float getcomm(){
    commissioncalc();
    return commission;
    }
    }
    class salespersonMain{
    String SalesPersonId;
    Float SalesAmount;
    String SalesPersonname;
    Float commission;


    public static void main(String arg[]){

    }



    public void salesperson(String amount, String name, int id){


    id=JOptionPane.showInputDialog("Enter your id");
    SalesPersonId=Integer.parse.Int(id);
    JOptionPane.showInputDialog("Enter amount");
    JOptionPane.showInputDialog("Enter name");


    SalesPerson spn;
    spn = new SalesPerson();
    spn.id="55";
    spn.amount="15000.20";
    spn1.id="1";
    spn1.amount="18000.00";
    spn1.name="Samuel";
    spn2=null;
    System.out.println("Hello,"+spn2.name);
    System.out.println("Hello your id is"+spn.id);
    System.out.println("spn.amount");
    System.out.println("spn1.id");
    System.out.println("spn1.name");
    System.out.println("spn1.amount");
    }
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    1.) Please use code tags when posting code.
    2.) Don't use == for comparing strings, use the equals method instead.
    3.) To run a class, it must have the main method. Whatever class it is that you are trying to run, make sure it has that main method. In this case you should be running the class salespersonMain because that's where your main method is. Realize though that main is the starting point. You have nothing in the main method, the code will not do anything.

  3. #3
    sinista is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default constructor

    should i then make a seperate file for salespersonmain, because i currently have everything in one folder. another thing in my main my constructor and objects is not correct, maybe one simple thing. I'm not able to get it. can you help.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    If they are in the same package keep them in the same folder. The name of the package should match the name of the folder.
    If you haven't learned about packages yet then keep them in the same folder as well (but make a note to learn about them later).
    You can have many classes in the same .java file but only one of the classes can be public.
    If you do have a public class then its name must match the name of the .java file.

    The way your code is currently structured, I can't tell what problems you have with the constructor because I can't see the constructor.
    Format the code and post the parts you need help with wrapped around code tags.

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is online now Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    805
    Rep Power
    6

    Default

    I think u are very much confused with the basics.Go thru Sun tutorial.

    I think u are trying to return something from constructor.Constructor will not have return type and have implicit return type as class.

    remove void in the below statement and


    public void salesperson(String amount, String name, int id){

    create object
    like this below in main by passing values for the amount ,name and id.
    salesperson obj = new salesperson(amount, name, id);
    Ramya:cool:

  6. #6
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    public static void main(String arg[]){

    }
    The problem is you put nothing inside the main method!
    Write a program to achieve anything you want in your life!

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Willi View Post
    The problem is you put nothing inside the main method!
    Yeah but you don't get an error for that. See the replies already posted.

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Willi, there is no main method in salesperson. It's hard to see as the OP didn't use code tags. An empty main method is fine for the compiler/ java.exe

    Slow again.
    Last edited by PhHein; 10-14-2009 at 02:20 PM. Reason: my slowliness.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by PhHein View Post
    ..

    Slow again.
    Change your keyboard.

  10. #10
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    I won't even think about blaming anything else, but myself.

Similar Threads

  1. Method and Constructor
    By swikar.java in forum New To Java
    Replies: 7
    Last Post: 10-21-2008, 02:41 PM
  2. Method & Constructor
    By swikar.java in forum New To Java
    Replies: 4
    Last Post: 10-21-2008, 01:31 PM
  3. Replies: 0
    Last Post: 04-23-2008, 08:14 PM
  4. Java constructor method
    By carl in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 02:15 PM
  5. Replies: 2
    Last Post: 07-09-2007, 05:01 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
  •