d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > @overrite Not Overwriting A Method
12Next
Add Reply New Topic New Poll
Member
Posts: 17,177
Joined: Aug 10 2007
Gold: 125.00
Oct 21 2013 06:37pm
Here's my code, I cannot figure out what I am doing wrong. It is still displaying the output from the DVD class.

Code

public class Inventory_Program {

public static void main (String args [])
{//Start main method
//Instantiate DVD array
final int ARRAY_LENGTH = 5;//Constant to set array size
DVD dvd[] = new DVD[ARRAY_LENGTH];//Create an array of Person objects

DVD dvd1 = new DVD (01, " The Machinist", 25, 12.99, "Brad Anderson");//Creates an instance of the DVD class and initialize class instance variables
DVD dvd2 = new DVD (02, " X-Men:Last Stand", 16, 19.00, "Brett Ratner");//Creates an instance of the DVD class and initialize class instance variables
DVD dvd3 = new DVD (03, " The Dark Knight", 52, 19.99, "Christopher Nolan");//Creates an instance of the DVD class and initialize class instance variables
DVD dvd4 = new DVD (04, " Forrest Gump", 9, 9.99, "Robert Zemeckis");//Creates an instance of the DVD class and initialize class instance variables
DVD dvd5 = new DVD (05, " A Walk To Remember", 12, 5.00, "Adam Shankman");//Creates an instance of the DVD class and initialize class instance variables

dvd[0] = dvd1;//Add The Machinist DVD to the array of DVDs
dvd[1] = dvd2;//Add X-Men:Last Stand DVD to the array of DVDs
dvd[2] = dvd3;//Add The Dark Knight DVD to the array of DVDs
dvd[3] = dvd4;//Add Forest Gump DVD to the array of DVDs
dvd[4] = dvd5;//Add A Walk To Remember DVD to the array of DVDs

DVD.sort_by_name(dvd);//Call the sort_by_name method
System.out.println();

for (int n = 0; n < dvd.length; n++)
{
dvd[n].display_inventory_information();//display output to the console
System.out.println();
}

//Output the total value of the inventory
double total = 0.0;
for (int i = 0; i < 5; i++)
{
total += dvd[i].get_inventory_value();//A running total of get_inventory_value
}
System.out.printf("Total Value of Inventory is: \t$%.2f\n", total);//Outputs the total value of the inventory
}//End output
}//End method


Code
public class DVD
{
//Instance variable declarations
private int item_number; //Item number of product
private String product_name; //Product name
private int units_in_stock; //Number of units in stock
private double price_per_unit; //Price per unit

public DVD (){}
//DVD constructor
public DVD (int item, String name, int stock, double price, String director) {
item_number = item;
product_name = name;
units_in_stock = stock;
price_per_unit = price;
}//End constructor

public void set_item_number (int item)
{//Start Method
this.item_number = item;//Set variable to be passed with the method
} //End method

public int get_item_number()
{//Start Method
return item_number;//Get the value of the instance variable
}//End method

public void set_product_name (String name)
{//Start Method
this.product_name = name;//Set variable to be passed with the method
}//End method

public String get_product_name()
{//Start Method
return product_name;//Get the value of the instance variable
}//End method

public void set_units_in_stock (int stock)
{//Start Method
this.units_in_stock = stock;//Set variable to be passed with the method
}//End method

public double get_units_in_stock()
{//Start Method
return units_in_stock;//Get the value of the instance variable
}//End method

public void set_price_per_unit (double price)
{//Start Method
this.price_per_unit = price;//Set variable to be passed with the method
}//End method

public double get_price_per_unit()
{//Start Method
return price_per_unit;//Get the value of the instance variable
}//End method

//calculate inventory value
public double get_inventory_value()
{//Start Method
return units_in_stock * price_per_unit;//Get the value of the instance variable
}//End method

public double get_inventory_total_value()
{//Start Method
double value = 0;
for(int i = 0; i < 5; i++)
{
value = get_inventory_value();
}
return value;
}//End method

public void display_inventory_information()
{//Displays DVD information to the console
System.out.println("Product Number: \t\t" + this.get_item_number());
System.out.println("Product Name: \t\t\t" + this.get_product_name());
System.out.println("Number of Units in Stock: \t" + this.get_units_in_stock());
System.out.printf("Price per Unit: \t\t$%.2f\n", + this.get_price_per_unit());
System.out.printf("Value of Inventory: \t\t$%.2f\n", + this.get_inventory_value());
System.out.println();
}//End method

public static void sort_by_name(DVD[] dvd)
{//Start Method
DVD temp;
for(int j = dvd.length -2;j>=0;j--)
for(int n=0;n<=j;n++)
if(dvd[n].get_product_name().compareTo(dvd[n+1].get_product_name())>0)
{
temp=dvd[n];
dvd[n]=dvd[n+1];
dvd[n+1]=temp;
}
}//End method
}//End class


Code
class Director extends DVD
{//being Director class
//Instance variable declaration
protected String director;

public Director (){}
//Director constructor
Director(int item_number, String product_name, int units_in_stock, double price_per_unit, String dvd_director)
{//Start Method
super(item_number, product_name, units_in_stock, price_per_unit, dvd_director);
this.director = dvd_director;
}//End constructor

// set release date
public void set_director(String dvd_director)
{//Start Method
this.director = dvd_director;
}//End method

// return release date
public String get_director()
{//Start Method
return director;
}//End method

// add 5% restocking fee
public double get_restocking_fee()
{//Start Method
return get_price_per_unit() * .05;
}//End method

@Override
public void display_inventory_information()
{//Displays DVD information to the console
System.out.println("Product Number: \t\t" + this.get_item_number());
System.out.println("Product Name: \t\t\t" + this.get_product_name());
System.out.println("Product Director: \t\t" + String.valueOf(this.get_director()));
System.out.println("Number of Units in Stock: \t" + this.get_units_in_stock());
System.out.println("Restocking fee: \t\t$%.2f\n" + this.get_restocking_fee());
System.out.printf("Price per Unit: \t\t$%.2f\n", + this.get_price_per_unit());
System.out.printf("Value of Inventory: \t\t$%.2f\n", + this.get_inventory_value());
System.out.println();
}//End method
}//End class
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Oct 21 2013 06:45pm
You're not creating any Director objects in your main class.
Member
Posts: 17,177
Joined: Aug 10 2007
Gold: 125.00
Oct 21 2013 07:00pm
Quote (labatymo @ Oct 21 2013 07:45pm)
You're not creating any Director objects in your main class.


Shouldn't it not matter though since Director is an extension of DVD?


/e

I'm a Java noob and I really dislike it. I took C++ before and it was a breeze for me. For some reason Java is proving to be difficult. -_-

This post was edited by Ayudame on Oct 21 2013 07:02pm
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Oct 21 2013 07:21pm
Quote (Ayudame @ Oct 21 2013 09:00pm)
Shouldn't it not matter though since Director is an extension of DVD?


/e

I'm a Java noob and I really dislike it. I took C++ before and it was a breeze for me. For some reason Java is proving to be difficult. -_-


Nah it has to be a Director. And you'll find java is way easier than c++ once you get the hang of it
Member
Posts: 17,177
Joined: Aug 10 2007
Gold: 125.00
Oct 21 2013 07:28pm
Quote (labatymo @ Oct 21 2013 08:21pm)
Nah it has to be a Director. And you'll find java is way easier than c++ once you get the hang of it


What would actually need to be a Director object? I get what you're saying to do, i'm just unsure exactly how I need to do it.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 21 2013 07:34pm
Quote (Ayudame @ Oct 21 2013 09:28pm)
What would actually need to be a Director object? I get what you're saying to do, i'm just unsure exactly how I need to do it.


i'm not clear what you want to be a director. i assume you dont want DVD's method to be used at all, in which case replace every new DVD(..) with new Director(...)

your classes seem poorly named btw.
Member
Posts: 17,177
Joined: Aug 10 2007
Gold: 125.00
Oct 21 2013 07:37pm
Quote (carteblanche @ Oct 21 2013 08:34pm)
i'm not clear what you want to be a director. i assume you dont want DVD's method to be used at all, in which case replace every new DVD(..) with new Director(...)

your classes seem poorly named btw.


Just replacing DVD wiith Director defeats the point of the assignment, which is to use @Overrite to bypass the output in DVD class and to use the output in Director class.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 21 2013 07:41pm
Quote (Ayudame @ Oct 21 2013 09:37pm)
Just replacing DVD wiith Director defeats the point of the assignment, which is to use @Overrite to bypass the output in DVD class and to use the output in Director class.


where did i say replace "DVD" with "Director"? I said replace "new DVD" with "new Director". changes the object type but not the reference type.

http://stackoverflow.com/questions/13597203/why-is-reference-type-can-be-different-from-object-type-in-java

This post was edited by carteblanche on Oct 21 2013 07:43pm
Member
Posts: 17,177
Joined: Aug 10 2007
Gold: 125.00
Oct 21 2013 07:47pm
Quote (carteblanche @ Oct 21 2013 08:41pm)
where did i say replace "DVD" with "Director"? I said replace "new DVD" with "new Director". changes the object type but not the reference type.

http://stackoverflow.com/questions/13597203/why-is-reference-type-can-be-different-from-object-type-in-java


okay I understand now.
Member
Posts: 17,177
Joined: Aug 10 2007
Gold: 125.00
Oct 21 2013 08:01pm
Thanks for the help. It works now :)
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll