Results 1 to 2 of 2
  1. #1
    trakke is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Java project with multiple classes

    Alright, so I'm writing a Java project where I need the following:

    -Use of a local variables and parameters
    -Use of a constant
    -More than one class
    -Interaction between their objects
    -Methods from both the same class and another class

    My current project is a "Sales Tax" project which mainly utilizes one class, but I'm trying to add more to another class to fulfill the requirements. What it does is allow you to input item prices into an array and it calculates the sales tax cost and gives you a total cost. How can I fulfill:

    -Interaction between their objects
    -Methods from both the same class and another class

    These? Here is my code:
    Java Code:
    public class TaxClass
    {
        private Input newList;
        /**
         * Constructor for objects of class Tax
         */
        public TaxClass(int anyAmount)
        {
            newList = new Input(anyAmount);
        }
        /**
         * Mutator method to add items and their cost
         */
        public void addItems(){
            newList.setArray();
        }
    }
    Java Code:
    public class Input
    {
        private Scanner keybd;
        private String[] costArray;
        private String[] itemArray;
    
        /**
         * Constructor for objects of class Scanner
         */
        public Input(int anyAmountofItems)
        {
            keybd = new Scanner(System.in);
            costArray = new String[anyAmountofItems];
            itemArray = new String[anyAmountofItems];
        }
        /**
         * Mutator method to set the item names and costs
         */
        public void setArray(){
            System.out.println("Enter the sales tax percentage: ");
            double salesTax = keybd.nextDouble();
            double totalTax=0.0;
            double total=0.0;
            for(int indexc=0; indexc < costArray.length; indexc++){
               System.out.println("Enter the item cost: ");
               double cost = Double.valueOf(keybd.next()).doubleValue();
               totalTax = totalTax + (cost * salesTax);
               total = total + cost;
            }
            System.out.println("Total tax: " + totalTax);
            System.out.println("Total cost pre-tax: " + total);
            System.out.println("Total cost including tax: " + (total+totalTax));
        }
    }

    Thank you!

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    So what is the constant going to be? You don't have many choices really, with only sales tax and the item prices; the sales tax seems like the obvious choice here... but it's up to you.

    Before you write any code, you need to sort out your design - what tasks and roles are there? what data is required? what objects will perform those tasks or roles, what data will they use, and how will they communicate and how will they need to pass the data around?

    In a good design, each component has a single, clearly defined task or role, and its name should reflect that. For example, if you're going to have an Input class, then it should deal with input only. It can get the input items and make them available to the rest of the application. The rest of the application can then process the input data, e.g. do the calculations. Maybe you'll have a separate class to do the calculations, e.g. TaxCalculator or TaxAdjuster - maybe it will take an array of items and calculate the total cost and maybe it could have a method to get the adjusted cost for an item and call that for every item... I don't know, it's up to you, but you need to decide such general things before you start writing any code. Spend some time thinking about the different options and how they could work - try different ideas on paper, and play around until you're confident you've got something that will work.

    Once you have a well thought-out design and you can step through it on paper to check that it will all work the way you want, then you can move on to translating it into Java code.

    Doing the design takes a fair bit of thought deciding how you want it to work. Writing the code takes a fair bit of thought when you're unfamiliar with the language. Trying to do both at once will have you tearing your hair out over a long and frustrating time.

Similar Threads

  1. Multiple images from different classes
    By superbriggs in forum Java 2D
    Replies: 1
    Last Post: 03-20-2011, 09:59 AM
  2. Multiple classes
    By Lund01 in forum New To Java
    Replies: 1
    Last Post: 03-16-2011, 03:58 PM
  3. help with GUI and multiple classes
    By sssss in forum Advanced Java
    Replies: 14
    Last Post: 01-16-2011, 11:08 PM
  4. Problem with multiple string in classes
    By sjaakie in forum New To Java
    Replies: 3
    Last Post: 10-10-2010, 03:48 PM
  5. Help with multiple frames/classes
    By Celletti in forum AWT / Swing
    Replies: 1
    Last Post: 04-28-2010, 04:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •