Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PLEASE - help With subclass and method

807601Dec 14 2007 — edited Dec 15 2007
ok to be honest I am posting here because I am completely lost... I am looking for any help. I have to create a subclass of CD. In this subclass I have to create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product. I will post the code as far as I have come. I do not even know if it compiles it will do anything like I asked it to, my knowledge of java is still growing and I feel I am a little over my head on this.


Inventory2.java:21: '.class' expected
((mName) cd[]).sameName(cd, cd);
^
1 error


here is the code... any help is greatly appreciated

import java.text.DecimalFormat;

public class Inventory2 {
      public CD[] cd;
      
       public static void main(String[] args) {
         
         System.out.println("\nCheckPoint: Inventory Part 2");
         Inventory2 inv = new Inventory2();
         CD[] cd = new CD[5];
         DecimalFormat df1 = new DecimalFormat(".00");

      		//Creates new cd

       		cd[0] = new CD("Lily Allen - Alright Still",1,12.50,267);
       		cd[1] = new CD("Daft Punk - Alive 2007",2,12.50,148);
       		cd[2] = new CD("Michael Jackson - Bad",3,9.50,8);
       		cd[3] = new CD("Daft Punk - Alive 2007",2,12.50,148);
       		cd[4] = new CD("Madonna - Immaculate Collection",4,19.50,18);
       		
       		((mName) cd[]).sameName(cd[1], cd[3]);
       			
       	
       		
   				for (int i=0; i < cd.length; i++)
   				{
   					System.out.println(cd);
}
System.out.println("Total Inventory Value: $" +df1.format(inv.invValue(cd)));


System.out.println("\n Sorted Order By Name:" );
inv.sortCd(cd);
for (int i=0; i < cd.length; i++)
{
System.out.println(cd[i]);
}

}




public double invValue(CD[] cd)
{
double inval = 0;
for (int i=0; i < cd.length; i++)
{
inval += cd[i].itemValue();
}
return inval;
}



public static void sortCd(CD[] cd)
{
int a,b;
CD[] tempcd = new CD[1];
for (b = 0; b < cd.length-1; b++)
if(cd.getName().compareTo(cd[b + 1].getName()) > 0)
{
tempcd[0] = cd[b];
cd[b] = cd[b + 1];
cd[b + 1] = tempcd[0];
}

}


}


and the classes
import java.text.DecimalFormat;

class CD
{
private String aName;//Initializes name
private int inum;//Initializes number
private double cost;//Initializes cost
private int stock;//Initializes stock
private double value;//Initializes value


//Constructs CD
public CD ()
{
aName = null;
inum = 0;
cost = 0;
stock = 0;
}
public CD (String name, int itemNumber, double price, int inv)
{
aName = name;
inum = itemNumber;
cost = price;
stock = inv;
}

public String getName()
{
return aName;
}

public double itemValue()
{
value = stock * cost;//computes item value
return value;
}

//toString when cd is printed it sends
public String toString()
{

DecimalFormat df1 = new DecimalFormat(".00");
return "CD: No: " + inum + " Copies: " + stock + " Price: $"
+ df1.format(cost) + " Value: $" + df1.format(itemValue()) + " \n " + aName;//returns the cd information


}



}

class mName extends CD {


double sameName(CD cd1, CD cd2) {

double b;
if(cd1.getName().equalsIgnoreCase(cd2.getName()) == true)
{
b = cd1.itemValue();
b += cd2.itemValue();
b = b * .5;
return b;
}

}




}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 12 2008
Added on Dec 14 2007
6 comments
225 views