Results 1 to 3 of 3
- 11-20-2008, 12:49 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
Help interpreting what a class is supposed to do
I am supposed to determine what various sections of the code are supposed to do. My comments are in red. The first class is the one I am interpreting. The others are provided for reference. All constructive criticism is welcome. Thanks in advance for your help...
Java Code:public class Inventory { private Product[] list; [COLOR="Red"]// Here where the array is declared[/COLOR] public Inventory(int size) { [COLOR="Red"]// Here is where the actual arrays is created[/COLOR] list = new Product[size]; } public int size() { return list.length; } public double totalValue() { [COLOR="Red"]//This area computes the total value of the inventory[/COLOR] double val = 0.0; for (int i = 0; i < list.length; i++) { val += list[i].value(); } return val; } public void add(Product d, int p) { [COLOR="Red"]// adds an item to the array[/COLOR] list[p] = d; } public Product get(int i) { [COLOR="Red"]// gets an item from product class to add to array[/COLOR] return list[i]; } [COLOR="Red"]// sort by name using a bubble sort[/COLOR] public void sort() { int n = list.length; for (int search = 1; search < n; search++) { for (int i = 0; i < n-search; i++) { if (list[i].getdvdName().compareToIgnoreCase(list[i+1].getdvdName()) > 0) { Product temp = list[i]; list[i] = list[i+1]; list[i+1] = temp; } } } }
Java Code:public class InventoryProgram { public static void main(String args[]) { //make products Product product1 = new Product(1,"House", 11,16.99); Product product2 = new Product(2,"Heroes", 25,21.99); Product product3 = new Product(3,"24", 2,14.99); // inventory object Inventory obj = new Inventory(3); obj.add(product1, 0); obj.add(product2, 1); obj.add(product3, 2); obj.sort(); /// output for (int i = 0; i < obj.size(); i++) { System.out.println(obj.get(i)); System.out.println(); } // total val System.out.printf("Total value: $%.2f", obj.totalValue());
Java Code:public class Product { // variables private int itemNumber; private String dvdName; private int units; private double price; // constructor for the product public Product(int itemNumber, String dvdName, int units, double price) { this.itemNumber = itemNumber; this.dvdName = dvdName; this.units = units; this.price = price; } // total value public double value() { return units*price; } // getters and setters public int getitemNumber() { return itemNumber; } public void setitemNumber(int itemNumber) { this.itemNumber = itemNumber; } public String getdvdName() { return dvdName; } public void setdvdName(String dvdName) { this.dvdName = dvdName; } public int getUnits() { return units; } public void setUnits(int units) { this.units = units; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String toString () { return "Item number: " + itemNumber + "\nName: " + dvdName+ "\nUnits: " + units + "\nPrice: " + String.format("$%.2f", price) + "\nValue: " + String.format("$%.2f", value());
- 11-20-2008, 01:15 AM #2
lets see...
Some comments:
Java Code:public Inventory(int size) { // Here is where the actual arrays is created list = new Product[size]; }
Java Code:public void add(Product d, int p) { // adds an item to the array list[p] = d; }
Java Code:public Product get(int i) { // gets an item from product class to add to array return list[i]; }
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-20-2008, 01:19 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 01:25 AM -
How to create main class link to another two class?
By pearllymary78 in forum New To JavaReplies: 6Last Post: 07-17-2008, 12:02 AM -
Able to find class file in WEB-INF/classes but not after add sub folders in class dir
By vitalstrike82 in forum Web FrameworksReplies: 0Last Post: 05-13-2008, 07:16 AM -
Class Reflection: Finding super class names
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 09:12 PM -
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 08:04 AM
Bookmarks