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

    Question Need one class to run a second class

    I need advice on exactly how to do what I need to here. I have two Java programs, that are supposed to be connected with each other. That has proven to be mind-numbingly difficult to accomplish, but the two programs just need to both execute for my goal to be accomplished. I do not need to pass values between them or anything, I just need the first one to run, and at the end, I need the first one to send a call to the second one to run it. So, the first program runs, and then the second program runs, data does not need to be transferred between them, I just need them both to run, but I need the first one to send a call to the second one to run after the first one finishes running. Anyway, here is my code:
    Java Code:
    package Annual;
    import java.util.Scanner;
    public class Pay {
        public static void main(String[] args)
        {   Scanner percent = new Scanner(System.in);
            System.out.println("Please enter annual sales:");
            double yearly = percent.nextDouble();
            double T = 50000;                 
            double Amt;
            double YrPrcnt = yearly * .05;
            Amt = T + YrPrcnt;
            System.out.print("The Annual Payrate for employee is:");
            System.out.print(Amt);
        }}
    Somewhere at the end of this program, I need to insert code to call and run this one:
    Java Code:
    // Calculates base salary 
    // plus appropriate sales commission based on percentage of sales goals met.
    package Annual;
    public class CommissionSales
    {    public static void main(String[] args) {
           //Base salary is $50,000 annually....        
           System.out.println ("Salesperson regular wages = $50,000");
           // 80% of sales target is computed
           // Target = 100000 * 80%.
           System.out.println ("$100,000 * 80% = $80,000");
           // Return 5% commission of sales target
           // commission = 5% * $100,000;
            System.out.println ("100,000 * 5% = $5000");
           // Return commission with acceleration factor
           // Commission = 5% + 1.25 acceleration factor;
            System.out.println ("5% + 1.25 = 6.25%");
            // Return potential.
            // Sales with 5% interest increment at 1.25 acceleration
            // factor until annual sales reach 50% of salesperson annaul sales
            // total sales = 50% + 6.25%;
            System.out.println ("50% + 6.25% = 56.25% ");        
            // Calculate potential compensation = 100,000 / 56.25%;
            System.out.println ("$100,000 / 56.25% = $56,250");
            // Calculate potential compensation by sales;
            // Return potential payrates with commissions;
            System.out.println ("$100,000 * 5% = $5,000");
            System.out.println ("$100,000 + $5,000 = Total Sales $105,000");
            System.out.println ("$105,000 / 56.25% = $56,562.50");
            System.out.println ("$110,000 / 56.25% = $61,875");
            System.out.println ("$115,000 / 56.25% = $64,687.50");
            System.out.println ("$120,000 / 56.25% = $67,500");
            System.out.println ("$125,000 / 56.25% = $70,312.50");
            System.out.println ("$130,000 / 56.25% = $73,125");
            System.out.println ("$135,000 / 56.25% = $75,937.50");
            System.out.println ("$140,000 / 56.25% = $78,750");
            System.out.println ("$145,000 / 56.25% = $81,562.50");
            System.out.println ("$150,000 / 56.25% = $84,375");}}
    I do not need to pass data between them, just need the first one to run, and then the second one to run, but I need the first one to call the second one to execute. Please explain how I can do that??

  2. #2
    java.java is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need one class to run a second class

    All you have to do is run the main method of your second class from your first class. Add a statement at the end of your first class with the below sytax.
    Secondclassname.main(args). Args can be any argument that you wish to pass to the secondprogram. This should work , let me know if you face any problem.

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Need one class to run a second class

    When you have an application you can create one class as the entry point of your program. This class is the one that should have the declaration of the main(String[] args) method. Then define the tasks your program want to accomplish in a method of classes. For instance your Pay class may have the method doSomething() and the CommisionSales class also have the doSomething() method. In the end you can have something like this:

    Java Code:
    public class Program {
        public static void main(String[] args) {
            Pay pay = new Pay();
            pay.doSomething();
    
            CommisionSales commisionSales = new CommisionSales();
            commisitonSales.doSomething();
        }
    }
    
    public class Pay {
        public void doSomething() {
        }
    }
    
    public class CommisionSales {
        public void doSomething() {
        }
    }
    Last edited by wsaryada; 11-19-2012 at 08:21 AM.

  4. #4
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Question Re: Need one class to run a second class

    Quote Originally Posted by java.java View Post
    All you have to do is run the main method of your second class from your first class. Add a statement at the end of your first class with the below sytax.
    Secondclassname.main(args). Args can be any argument that you wish to pass to the secondprogram. This should work , let me know if you face any problem.
    That is the thing, I don't need to pass anything between the classes, they are independent of each other completely. One does the prompt to the users for input, and calculates it and returns it to the users, and the other just prints out a preformed list of potential values that is not related at all to the user input. I just really need the first program to run, do its thing, and then run the second one to produce the list for the user, after presenting the results of their calculations. So, could I just add the line Secondclassname.main(); with no arguments, to run the second program??

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

    Default Re: Need one class to run a second class

    Was actually curious myself, and the answer is yes... that is really easy.

    Java Code:
    package testing;
    
    public class MainOne {
    	
    	public static void main(String args[]){
    		System.out.println("Program One");
    		
    		MainTwo.main();
    	}
    	
    	
    }
    Java Code:
    package testing;
    
    public class MainTwo {
    
    	public static void main(){
    		
    		System.out.println("Program Two");
    		
    	}
    	
    }

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2011, 11:55 AM
  2. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  3. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  4. Replies: 8
    Last Post: 04-14-2010, 05:49 AM
  5. Replies: 7
    Last Post: 05-06-2009, 12:42 PM

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
  •