Results 1 to 5 of 5
- 11-18-2012, 11:04 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 57
- Rep Power
- 0
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:
Somewhere at the end of this program, I need to insert code to call and run this one: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); }}
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??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");}}
- 11-19-2012, 04:11 AM #2
Member
- Join Date
- Jul 2012
- Posts
- 9
- Rep Power
- 0
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.
- 11-19-2012, 04:27 AM #3
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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.
Website: Learn Java by Examples
- 11-19-2012, 08:12 AM #4
Member
- Join Date
- Nov 2012
- Posts
- 57
- Rep Power
- 0
Re: Need one class to run a second class
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??
- 11-19-2012, 09:12 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 14
- Rep Power
- 0
Re: Need one class to run a second class
Was actually curious myself, and the answer is yes... that is really easy.
.gif)
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
-
How to get a compatible class of a template class? Return type of method is AClass<E>
By SKuypers in forum Advanced JavaReplies: 0Last Post: 12-07-2011, 11:55 AM -
Eclipse Compile Error: Call validateValue(Class<T>, String, Object, Class<?>...)
By Tomshi in forum EclipseReplies: 0Last Post: 03-27-2011, 05:49 AM -
super class reference variable accesses overriding sub class method
By subith86 in forum New To JavaReplies: 5Last Post: 01-26-2011, 06:38 PM -
Dynamic loading of a class (passing class definition over the network)
By eddie-w in forum Advanced JavaReplies: 8Last Post: 04-14-2010, 05:49 AM -
[SOLVED] How to pass information from child class to parent class
By pellebye in forum New To JavaReplies: 7Last Post: 05-06-2009, 12:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks