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

    Question Need to split this class into methods

    Here is my program code:
    Java Code:
    package zackfinal;
    import java.util.*;
    public class ZackFinal {@SuppressWarnings("empty-statement")
        public static void main(String[] args)
        {   double T = 50000; //set value for base salary                
            double Amt; //declare variable for final pay
            int x = 1;
         do{
          try{ //validate user input
            Scanner percent = new Scanner(System.in);
            System.out.println("Please enter annual sales:");//request user input
            System.out.println("Please enter valid dollar value:");
            double yearly = percent.nextDouble(); //store user input
               if (yearly >= 0 && yearly <= 500000){ //if over $500,000.00 in sales
               if (yearly >80000){ //if 80% sales goal was not reached
            double YrPrcnt = yearly * .05; //value used for compensation set next
            Amt = T + YrPrcnt * 1.25; //set Amt to pay plus commissions
            System.out.print("The Annual Payrate for employee is: $");
            System.out.println(Amt);}
               else {
                 System.out.print("The Annual Payrate for employee is: $"); 
                 System.out.println(T);}}
                  else {
               System.err.print("Sales too high, ");
               System.err.println("please see Human Resources.");}
               x=2; break;
          }
            catch(Exception e){ //if user input throws exception
                System.out.println("Try again please");}}while(x==1);
    
           System.out.println("-------------------------------------------------");
           System.out.println ("Salesperson regular wages = $50,000");
           // 80% of sales target is computed
           // Target = 100000 * 80%.
           System.out.println("Sales Target is:");
           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("Potential compensations based on sales:");
            System.out.println("-------------------------------------");
            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");}}
    //end program....
    and I need to split it into two methods, with one being the main class, and the second method starting after the user input validation catch line so that the list of calculated salaries will display correctly if I move that code to a second file, and create it's own class.
    I tried this, but the second class doesn't run anymore, and so the list never displays....
    Java Code:
    package zackfinal;
    import java.util.*;
    public class ZackFinal {@SuppressWarnings("empty-statement")
        public static void main(String[] args)
        {   double T = 50000; //set value for base salary                
            double Amt; //declare variable for final pay
            int x = 1;
         do{
          try{ //validate user input
            Scanner percent = new Scanner(System.in);
            System.out.println("Please enter annual sales:");//request user input
            System.out.println("Please enter valid dollar value:");
            double yearly = percent.nextDouble(); //store user input
               if (yearly >= 0 && yearly <= 500000){ //if over $500,000.00 in sales
               if (yearly >80000){ //if 80% sales goal was not reached
            double YrPrcnt = yearly * .05; //value used for compensation set next
            Amt = T + YrPrcnt * 1.25; //set Amt to pay plus commissions
            System.out.print("The Annual Payrate for employee is: $");
            System.out.println(Amt);}
               else {
                 System.out.print("The Annual Payrate for employee is: $"); 
                 System.out.println(T);}}
                  else {
               System.err.print("Sales too high, ");
               System.err.println("please see Human Resources.");}
               x=2; break;
          }
            catch(Exception e){ //if user input throws exception
                System.out.println("Try again please");}}while(x==1);}
    
    
    
    class Chart extends ZackFinal{
        
        public void chart(String[] args){
           System.out.println("-------------------------------------------------");
           System.out.println ("Salesperson regular wages = $50,000");
           // 80% of sales target is computed
           // Target = 100000 * 80%.
           System.out.println("Sales Target is:");
           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("Potential compensations based on sales:");
            System.out.println("-------------------------------------");
            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");}}}
    //end program....
    and this compiles without errors and runs, but only the top half of the code actually executes??
    PLEASE, anyone tell me what I can do here to break this program into separate classes, and still have it work!!

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

    Default Re: Need to split this class into methods

    I see that in your code that you've never call the chart() method in the Chart class. Then how can this method get executed? You don't have to extends the first class on your Chart class. You just need to call it. In the main() method of the ZackFinal class create an instance of the Chart class and call the chart() method. Then it will get executed.

Similar Threads

  1. A class calling methods from external class
    By Aaron5466 in forum New To Java
    Replies: 4
    Last Post: 04-16-2012, 11:36 AM
  2. Using the split() method in the string class
    By Jdelacroix in forum New To Java
    Replies: 2
    Last Post: 03-27-2011, 10:39 AM
  3. how to split class into two classes?
    By ruud00000 in forum Java Applets
    Replies: 0
    Last Post: 12-31-2010, 03:45 PM
  4. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 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
  •