Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Smile 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());

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default lets see...

    Some comments:
    Java Code:
    public Inventory(int size) { // Here is where the actual arrays is created
    		list = new Product[size];
    	}
    Your comment is almost correct, but not complete. This is the class constructor and it is invoked when the class is instantiated. What this constructor is doing is instantiating the Product array.
    Java Code:
    public void add(Product d, int p) {  // adds an item to the array
    		list[p] = d;
    	}
    uh... not really.. I would change the name of the method. I would rename this method as "assign" or "populate", because it is not really adding elements to the array, it's assigning them.
    Java Code:
    public Product get(int i) { // gets an item from product class to add to array
    		return list[i];
    	}
    Nope... it gets or retreives an element from the Product array...period.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    Some comments:
    Java Code:
    public Inventory(int size) { // Here is where the actual arrays is created
    		list = new Product[size];
    	}
    Your comment is almost correct, but not complete. This is the class constructor and it is invoked when the class is instantiated. What this constructor is doing is instantiating the Product array.
    Java Code:
    public void add(Product d, int p) {  // adds an item to the array
    		list[p] = d;
    	}
    uh... not really.. I would change the name of the method. I would rename this method as "assign" or "populate", because it is not really adding elements to the array, it's assigning them.
    Java Code:
    public Product get(int i) { // gets an item from product class to add to array
    		return list[i];
    	}
    Nope... it gets or retreives an element from the Product array...period.

    CJSL
    Thanks for the input.:)

Similar Threads

  1. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM
  2. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-17-2008, 12:02 AM
  3. Replies: 0
    Last Post: 05-13-2008, 07:16 AM
  4. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:12 PM
  5. Replies: 0
    Last Post: 04-02-2008, 08:04 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
  •