Results 1 to 2 of 2
- 04-28-2011, 07:48 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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!
- 04-28-2011, 08:16 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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
-
Multiple images from different classes
By superbriggs in forum Java 2DReplies: 1Last Post: 03-20-2011, 08:59 AM -
Multiple classes
By Lund01 in forum New To JavaReplies: 1Last Post: 03-16-2011, 02:58 PM -
help with GUI and multiple classes
By sssss in forum Advanced JavaReplies: 14Last Post: 01-16-2011, 10:08 PM -
Problem with multiple string in classes
By sjaakie in forum New To JavaReplies: 3Last Post: 10-10-2010, 02:48 PM -
Help with multiple frames/classes
By Celletti in forum AWT / SwingReplies: 1Last Post: 04-28-2010, 03:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks