d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > @overrite Not Overwriting A Method
Prev12
Add Reply New Topic New Poll
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 21 2013 08:13pm
Do not PM me. stick to the topic.

Quote (Ayudame @ Oct 21 2013 10:01pm)
What did you mean by poorly named? How should I change it?


two things. first, notice how DVD's constructor has a parameter for director but it's not used? Get rid of it.

Quote
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


second, think about the names of the classes: DVD and Director. would any normal person say "oh yeah, directors are obviously a kind of DVD"? I don't think so. and looking at the name Director doesn't suggest any relation to DVD. a name like DVDWithDirector or DirectorDVD would be much more meaningful since they both suggest a relationship with DVD and also indicate that they are different from DVDs in the sense it has something to do with a director.
Member
Posts: 17,177
Joined: Aug 10 2007
Gold: 125.00
Oct 21 2013 08:15pm
Quote (carteblanche @ Oct 21 2013 09:13pm)
Do not PM me. stick to the topic.



two things. first, notice how DVD's constructor has a parameter for director but it's not used? Get rid of it.



second, think about the names of the classes: DVD and Director. would any normal person say "oh yeah, directors are obviously a kind of DVD"? I don't think so. and looking at the name Director doesn't suggest any relation to DVD. a name like DVDWithDirector or DirectorDVD would be much more meaningful since they both suggest a relationship with DVD and also indicate that they are different from DVDs in the sense it has something to do with a director.


Okay makes sense. Thanks again for the help. :)

This post was edited by Ayudame on Oct 21 2013 08:22pm
Member
Posts: 17,177
Joined: Aug 10 2007
Gold: 125.00
Oct 25 2013 04:41pm
Ran into the same problem, but this time I cannot figure out what i'm doing wrong. I added text boxes this time to display information instead of the console, and now it cannot find the information. I'm getting "cannot find symbol" for DirectorField.setText(dvd[ArrayIndex].get_director()); and RestockingFeeField.setText(String.valueOf(dvd[ArrayIndex].get_restocking_fee())); It's probably something silly I am overlooking like before.

Code
/** Program: Inventory Program Part 4
* File: Inventory_Program.java
* Summary: Sets, retrieves and displays DVD stock and value.
* Author: Dale R Murray
* Date: October 25 , 2013
**/
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Inventory_Program extends JFrame implements ActionListener
{
private JPanel gridPanel = new JPanel();
private JPanel panel = new JPanel();

//Declare buttons
JButton nextButton;

//Declare Text Fields
JTextField DirectorField;
JTextField ProductNumberField;
JTextField ProductNameField;
JTextField UnitsInStockField;
JTextField PricePerUnitField;
JTextField RestockingFeeField;
JTextField ValueOfInventoryField;
JTextField ValueOfInventoryTotal;

//Declare Labels
JLabel lblDirector;
JLabel lblProductNumber;
JLabel lblProductName;
JLabel lblUnitsInStock;
JLabel lblPricePerUnit;
JLabel lblRestockingFee;
JLabel lblValueOfInventory;
JLabel lblValueOfInventoryTotal;

DVD dvd1;
DVD dvd2;
DVD dvd3;
DVD dvd4;
DVD dvd5;

final int ARRAY_LENGTH = 5;

DVD dvd[] = new DVD[ARRAY_LENGTH];//Create an array of Person objects

private static int ArrayIndex = 0;

public Inventory_Program()
{
DVD dvd1 = new DVD_Director (01, " The Machinist", 25, 12.99, "Brad Anderson");//Creates an instance of the DVD class and initialize class instance variables
DVD dvd2 = new DVD_Director (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_Director (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_Director (04, " Forrest Gump", 9, 9.99, "Robert Zemeckis");//Creates an instance of the DVD class and initialize class instance variables
DVD dvd5 = new DVD_Director (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

gridPanel.setLayout(new BorderLayout()); // create a border layout
gridPanel.add(this.createLabelPanel(), BorderLayout.WEST); // add label panel
gridPanel.add(this.createTextPanel(), BorderLayout.CENTER); // add field panel
gridPanel.add(this.createButtonPanel(), BorderLayout.SOUTH); // add button panel
add(gridPanel);
}

private JPanel createButtonPanel()
{
nextButton = new JButton("Display Inventory");
nextButton.setActionCommand("Next");
nextButton.addActionListener(this);

// create panel object
JPanel panel = new JPanel();

panel.add(nextButton);

return panel;
}

private JPanel createLabelPanel()
{
lblDirector = new JLabel("Director:");
lblProductNumber = new JLabel("Product Number:");
lblProductName = new JLabel("Product Name:");
lblUnitsInStock = new JLabel("Units in Stock:");
lblPricePerUnit = new JLabel("Price per Unit:");
lblRestockingFee = new JLabel ("Restocking Fee:");
lblValueOfInventory = new JLabel("Value of Inventory:");
lblValueOfInventoryTotal = new JLabel("Total Value:");

panel = new JPanel();
panel.setLayout(new GridLayout(8, 1));

// add labels to the panel
panel.add(lblDirector);
panel.add(lblProductNumber);
panel.add(lblProductName);
panel.add(lblUnitsInStock);
panel.add(lblPricePerUnit);
panel.add(lblRestockingFee);
panel.add(lblValueOfInventory);
panel.add(lblValueOfInventoryTotal);

return panel;
}

private JPanel createTextPanel()
{
DirectorField = new JTextField();//Director text field
DirectorField.setEditable(false);//Set field to read-only

ProductNumberField = new JTextField();//Product Number text field
ProductNumberField.setEditable(false);//Set field to read-only

ProductNameField = new JTextField();//Product Name text field
ProductNameField.setEditable(false);//Set field to read-only

UnitsInStockField = new JTextField();//Units in stock text field
UnitsInStockField.setEditable(false);//Set field to read-only

PricePerUnitField = new JTextField();//Price per unit text field
PricePerUnitField.setEditable(false);//Set field to read-only

RestockingFeeField = new JTextField();//Restocking fee unit text field
RestockingFeeField.setEditable(false);//Set field to read-only

ValueOfInventoryField = new JTextField();//Value of inventory field
ValueOfInventoryField.setEditable(false);//Set field to read-only

ValueOfInventoryTotal = new JTextField();//Total value of entire inventory field
ValueOfInventoryTotal.setEditable(false);//Set field to read-only

panel = new JPanel();
panel.setLayout(new GridLayout(8, 1));

panel.add(DirectorField);
panel.add(ProductNumberField);
panel.add(ProductNameField);
panel.add(UnitsInStockField);
panel.add(PricePerUnitField);
panel.add(RestockingFeeField);
panel.add(ValueOfInventoryField);
panel.add(ValueOfInventoryTotal);

return panel; // return the panel
}

public void actionPerformed(ActionEvent e)
{
// add button functions
if (e.getActionCommand() == "Next")
if (ArrayIndex < dvd.length)
{
DirectorField.setText(dvd[ArrayIndex].get_director());
ProductNumberField.setText(String.valueOf(dvd[ArrayIndex].get_item_number()));
ProductNameField.setText(dvd[ArrayIndex].get_product_name());
UnitsInStockField.setText(String.valueOf(dvd[ArrayIndex].get_units_in_stock()));
PricePerUnitField.setText(String.valueOf(dvd[ArrayIndex].get_price_per_unit()));
RestockingFeeField.setText(String.valueOf(dvd[ArrayIndex].get_restocking_fee()));
ValueOfInventoryField.setText(String.valueOf(dvd[ArrayIndex].get_inventory_value()));

double total = 0.0;
for (int i = 0; i < 5; i++)
{
total += dvd[i].get_inventory_value();//A running total of get_inventory_value
}
ValueOfInventoryTotal.setText(String.valueOf(+total));

ArrayIndex = ArrayIndex + 1;
}
}


public static void main(String[] args)
{
JFrame frame = new Inventory_Program();

frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Inventory Application");
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 25 2013 05:15pm
Quote (Ayudame @ Oct 25 2013 06:41pm)
Ran into the same problem, but this time I cannot figure out what i'm doing wrong. I added text boxes this time to display information instead of the console, and now it cannot find the information. I'm getting "cannot find symbol"  for DirectorField.setText(dvd[ArrayIndex].get_director()); and RestockingFeeField.setText(String.valueOf(dvd[ArrayIndex].get_restocking_fee())); It's probably something silly I am overlooking like before.



If you're referencing a superclass, pretend the subclass doesn't exist. If you cannot find the method, then the compiler won't either. keep in mind that anyone can subclass DVD. if i altered your code to call a method getHorrorMovieName(), would you expect your code to work? I might have a subclass HorrorDVD with that method defined. but how will your code know it's there if it only knows about DVD?

Go back to the book and really understand the difference between reference type and object type.
Member
Posts: 17,177
Joined: Aug 10 2007
Gold: 125.00
Oct 25 2013 05:46pm
Quote (carteblanche @ Oct 25 2013 06:15pm)
If you're referencing a superclass, pretend the subclass doesn't exist. If you cannot find the method, then the compiler won't either. keep in mind that anyone can subclass DVD. if i altered your code to call a method getHorrorMovieName(), would you expect your code to work? I might have a subclass HorrorDVD with that method defined. but how will your code know it's there if it only knows about DVD?

Go back to the book and really understand the difference between reference type and object type.


Okay thanks man. I understand now. My book for my class is somewhat useless. I really need to find a different one for learning this. It doesn't really explain things the best way.
Member
Posts: 17,177
Joined: Aug 10 2007
Gold: 125.00
Oct 25 2013 06:14pm
Are there any websites that you would recommend, or even books? I'm willing to pay for books if it will help me learn this better than my online text. I understand completely what I did wrong and I feel like it's something dumb I shouldn't have done wrong.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 26 2013 03:49am
For java, I really REALLY liked this book

Introduction to Java Programming, Comprehensive Version (9th Edition) (by Liang)

This post was edited by Eep on Oct 26 2013 03:50am
Member
Posts: 17,177
Joined: Aug 10 2007
Gold: 125.00
Oct 26 2013 05:21pm
Quote (Eep @ Oct 26 2013 04:49am)
For java, I really REALLY liked this book

Introduction to Java Programming, Comprehensive Version (9th Edition) (by Liang)


I'll check it out. I'll upload a chapter of my book to, just incase it's just me not comprehending this for some reason. I had no problem with C++ though so I don't see why I'm struggling with simple stuff in Java like using my classes wrong. I've learned all this before >.<
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll