Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,289 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 985 people online right now. Registration is fast and FREE... Join Now!




Java Sub Class

 
Reply to this topicStart new topic

Java Sub Class, Having problems understanding

evak77
20 Dec, 2008 - 03:38 PM
Post #1

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 9


My Contributions
Here is my code for an assignment, but i need to add a sub class and a 5% restock fee to the program. I was wondering if someone could shoe me an example of adding a sub class to my cd inventory. The restock fee I can worry about later.


CODE

import java.util.Arrays;        

public class Inventory3 {
    
    public static void main(String[] args) {    
        
        CD cd;
        Inventory inventory = new Inventory();    
        
        cd = new CD(001, "Master of Puppets", 2, 27.98f);
        inventory.add(cd);  
        
        cd = new CD(022, "Heaven and Hell", 1, 9.99f);
        inventory.add(cd);  
        
        cd = new CD(013, "Throwing Copper", 10, 17.95f);
        inventory.add(cd);  
        
        cd = new CD(004, "Back in Black", 4, 8.99f);
        inventory.add(cd);  
        
        cd = new CD(006, "Silver Side Up", 5, 10.99f);
        inventory.add(cd);  

        inventory.display();      
        
        System.]out.println("\nEnd CD inventory\n");
    }
        
} // end class CD

CODE

class CD implements Comparable {  // compares array
    private int partID;
    private String name;
    private int copies;        
    private float price;
    
    CD(int partID, String name, int copies, float price) {
        this.partID = partID;
        this.name   = name;
        this.copies = copies;
        this.price  = price;
    }

    public String getName() {
        return name;
    }
    
    public float value() {
        return copies * price;
    }

    public int compareTo(Object o) {
        CD cd = (CD) o;
        return getName().compareTo(cd.getName());
    }

    public String toString() {
        return String.format("  %03d    %-22s  %3d     $%6.2f     $%7.2f",
                             partID, name, copies, price, value());
    }
        
} // end class CD


CODE

class Inventory {
    private CD[] cds;
    private int numCDs;
  
    ///Construct a new inventory for CDs
    Inventory() {
        cds = new CD[25];
        numCDs = 0;
    }
    
    public void add(CD cd) {
        cds[numCDs] = cd;
        ++numCDs; //add cd to collection
        sort();            
    }

    
    private void sort() { //This sorts the array
        Arrays.sort(cds, 0, numCDs);
    }

    
    public double value() {   //This is the value method
        double total = 0;
        for (int i = 0; i < numCDs; i++)
            total += cds[i].value();
        return total;
    }

    public void display() {        
        System.out.println("\nCD Inventory\n");
        for (int i = 0; i < numCDs; i++)     
            System.out.println(cds[i]);
          System.out.printf("\nThe total value of the inventory is $%.2f\n", value());    
    }
    

} // end class Inventory


*Edited to add the [ code] tags. Please code.gif
so DIC can read your code and help you

This post has been edited by pbl: 20 Dec, 2008 - 09:12 PM

User is offlineProfile CardPM
+Quote Post


n8wxs
RE: Java Sub Class
20 Dec, 2008 - 03:51 PM
Post #2

--... ...-- -.. . -. ---.. .-- -..- ...
Group Icon

Joined: 6 Jan, 2008
Posts: 1,609



Thanked: 223 times
My Contributions
Please post your code using tags: code.gif

You can edit your post to add the tags.



User is offlineProfile CardPM
+Quote Post

evak77
RE: Java Sub Class
20 Dec, 2008 - 04:14 PM
Post #3

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 9


My Contributions
I do not understand, I am kinda new to this and I am not very good at it.


QUOTE(n8wxs @ 20 Dec, 2008 - 03:51 PM) *

Please post your code using tags: code.gif

You can edit your post to add the tags.


User is offlineProfile CardPM
+Quote Post

Locke
RE: Java Sub Class
20 Dec, 2008 - 05:20 PM
Post #4

I am *not* a Thief...I *prefer* TREASURE HUNTER!
Group Icon

Joined: 20 Mar, 2008
Posts: 2,808



Thanked: 185 times
Dream Kudos: 325
Expert In: Java

My Contributions
That means when you show us your code in those tags.

When you do that, it organizes the code better, and we can help faster.

code.gif

When you use those tags, you get this.

CODE
I am a codebox


This post has been edited by Locke37: 20 Dec, 2008 - 05:21 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 03:41PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month