Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help splitting into two different classes.

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help splitting into two different classes.

    Hello I am new here and to Java. I have a code I typed up and found out I need to use two originally written classes. I do not know where to split the code and how to call one class from the next. Can someone here help me?

    package annualcompensation;

    import java.util.Scanner; // import Scanner

    public class AnnualCompensation { // initiate class AnnualCompensation


    public static void main(String[] args) {//main method

    Scanner input = new Scanner (System.in);// calls new scanner input

    String FirstName;// holds first name
    String LastName;// holds last name
    Double BasePay = 50000.00;// holds base pay
    Double CommPerc = .05;// holds commision percentage
    Double CommAmou;// holds commision amount
    Double AnnSales;//holds annual sales
    Double TotalPay;// holds total pay
    Double SalesTarget = 100000.00;// holds sales target

    System.out.print("Enter First Name ");// calls for first name
    FirstName = input.nextLine(); // accepts first name

    System.out.print("Enter Last Name ");// calls for last name
    LastName = input.nextLine();// accepts last name

    System.out.print("Enter Annual Sales ");// calls for annual sales
    AnnSales = input.nextDouble();// accepts annual sales amount

    if (AnnSales >= 0.8 * SalesTarget && AnnSales <= SalesTarget)
    {
    CommAmou = AnnSales * CommPerc;
    }
    else if(AnnSales >= SalesTarget)
    {
    CommAmou = 1.25 * CommPerc * AnnSales;
    }
    else {
    CommAmou = 0.00;
    }// declares value for CommAmou

    TotalPay = BasePay + CommAmou;// declares value for TotalPay

    System.out.printf(FirstName + " " + LastName + " " +
    "has earned a commision of $" + "%.2f\n", CommAmou);
    // outputs name and commision amount

    System.out.printf(FirstName + " " + LastName + " " +
    "has earned an annual pay of $" + "%.2f\n\n", TotalPay);
    // outputs name and total pay

    System.out.println("Potential Salary Amounts\n");// outputs text
    System.out.println("Total Sales \tTotal Compensation"); // outputs text

    double Display = 5000 + AnnSales;// holds display value

    while (Display <= 1.50 * AnnSales)// initiate loop to find display value
    {
    double Display2 = (Display * CommPerc)*1.25 + BasePay;
    System.out.printf("%.2f\t%.2f\n", Display, Display2);
    Display = Display + 5000;// diplay value increase by 5000

    }// close while loop

    }// close main method
    }// close class AnnualCompensation


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,665
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Help splitting into two different classes.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    All the code in your post is in one method: main(). The first step would be to define the functions of the code so it can be separated into methods.

    Why do you think the code needs to be in two separate classes?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Help splitting into two different classes.

    it's a good idea to seperate a model where you put the busisness logic (in your example : how you count the annualcompensation),
    the I/O of your program (where you ask the data) and a place where you startup the program (in a console app where you
    can find the main). Then you have also some of domain classes. In your example e.g the class Person.

    When you want to use your program later in a other architecture (e.g Swing) you can still use your model and when
    you want to make changes in your code it's easier to locate where you had to make the changes.
    Maybe you think all these classes for a simple program. But in the view of maintaince the program it's the best way to do.

Similar Threads

  1. Splitting text into several components
    By clydefrog in forum File I/O & Other I/O Streams
    Replies: 31
    Last Post: February 23rd, 2012, 11:50 AM
  2. Splitting String
    By JuLiAnc in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 17th, 2011, 09:23 AM
  3. [SOLVED] Splitting String by Multiple Delimiters
    By relixus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2011, 07:54 PM
  4. Splitting an Array?
    By ThatGuy1234 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 19th, 2011, 07:20 AM
  5. Splitting File into Array
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 3rd, 2010, 10:48 AM