I am having trouble were to declare the abstract classes for Widget, Sport and Grommet. They are not being read in the main part of the program could anyone tell me where to declare them so that they will be read.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication1;

/**
 *
 * @author Torbecire
 */
import java.io.*;
import java.util.StringTokenizer;

/***********************************
 *
 * A program to maintain a simple inventory
 * consisting of three items.
 *
 * Written by Bob Matthews and <Your name here>
 * 
 ***********************************/

public class Project3
{
    abstract class Part{
        
         public int getQuantity (int Quantity) 
         {
             return Quantity;
         }
         
         public void setQuantity(int Quantity) 
         {
             
         }
         
         public int increaseQuantity(int Quantity) 
         {
             return Quantity;
         }
         public int decreaseQuanity(int Quantity) 
         {
             return Quantity;
             
         }
         
          
          private int printInventory(int Widget, int Spork, int Grommet) 
          {
             System.out.print(toString());
          
          }
          /*private String quit()
          {
              exit;
          }*/
          
          public String toString() 
          {
              return (" who" + "ahh");
          }
 
          public abstract int Widget();
          public abstract int Spork();
          public abstract int Grommet();
           
        
    }
    // String constants for the four commands
    //   that the inventory control system
    //   understands.
    static final String ADD_COMMAND = "add";
    static final String REMOVE_COMMAND = "remove";
    static final String QUIT_COMMAND = "quit";
    static final String PRINT_COMMAND = "print";

    // The number of different items in the
    //   inventory.
    public static final int N_INVENTORY_ITEMS = 3;

    // Convert a string containing an item name
    //   into its position in the inventory array.
    public static int positionOf (String item)
     {
	// Convert the string to upper case, and
	//   then compare it to one of the items.
	if (item.toUpperCase().equals("WIDGET"))
	    return 0;
	else if (item.toUpperCase().equals("SPORK"))
	    return 1;
	else if (item.toUpperCase().equals("GROMMET"))
	    return 2;
	else
	    return -1;
    }

    // Read the next line from the user and return it as
    //   as string.
    //
    // Lines will have the form:
    //   <command> <item name> <quantity> for add and remove
    //   <command> for print and quit
    public static String readCommandLine (BufferedReader keyboard)
    {
	String oneLine = "";

	try
	    {
		oneLine = keyboard.readLine();
	    }
	catch (IOException e)
	    {
		System.out.println ("Unexpected end of input.");
	    }

	return oneLine;
    }

	
    public static void main (String [] args)
    {	
        // The list of inventory items.
	Part [] inventory = new Part [N_INVENTORY_ITEMS];

	// The three pieces of data we read from
	//    the user.
	String command;   // The command
	String item;      // The number of items to manipulate
	int quantity;     // The quantity to manipulate
 NOT READ HERE NOT READ BELOW      
	// Setup and initialize the inventory
	inventory[0] = new Widget();
	inventory[1] = new Spork();
	inventory[2] = new Grommet();
 ends here
	// Setup the input.
	BufferedReader keyboard = new BufferedReader
	    (new InputStreamReader (System.in));
	StringTokenizer commandLine = 
	    new StringTokenizer (readCommandLine(keyboard));

	// Extract the first command from the command line.
	command = commandLine.nextToken();
	
	// Loop until the quit command is given.
	while (! command.equals (QUIT_COMMAND))
	    {
		
		// Add to the inventory
		if (command.equals (ADD_COMMAND))
		    {
			// Add items to inventory
			item = commandLine.nextToken();
			quantity = Integer.parseInt(commandLine.nextToken());
			
			inventory[positionOf(item)]
			    .increaseQuantity(quantity);

		    }
		// Remove from the inventory
		else if (command.equals (REMOVE_COMMAND))
		    {
			/* Insert commands here to remove items 
			   from the inventory (if possible). */
		    }
			
		// Print the inventory
		else if (command.equals (PRINT_COMMAND))
		    {
			/* Insert commands here to print
			   the inventory of all items.
			   Use a loop. */
		    }
		
		// User entered a bad command
		else
		    System.out.println ("Bad command.");
		
		// Read the next line of input and extract
		//   the next command.
		commandLine = new StringTokenizer (readCommandLine(keyboard));
		command = commandLine.nextToken();
	    }

    }

}

Recommended Answers

All 11 Replies

do you mean, "it doesn't compile"? there are no classes named Widget, Spork, or Grommet; how do you expect to create new objects of those classes?

not declaring them is the ultimate in abstraction, but I'm sure your course material doesn't mean that when it talks about abstract classes and does mention what they actually are.

Sorry forgot to put the subclasses. Here they are

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication2;

/**
 *
 * @author Torbecire
 */
public class Widget extends Part
{
   public Widget(String nm)
   {
       super(nm);
   }
    @Override
   public int getQuantity (int Quantity) 
         {
             return Quantity;
         }
         
   public void setQuantity() 
         {
            int Quantity = N_INVENTORY_ITEMS;
         }
         
    @Override
   public int increaseQuantity(int Quantity) 
         {
             //int 
                    return Quantity;// = N_INVENTORY_ITEMS;
         }
         
    public void decreaseQuantity()
    {
        int Quantity = N_INVENTORY_ITEMS;

        System.out.print("Decrease");
    }
    public static void main(String [] args)
    {
        Widget ahh = new Widget("daaaad");
        ahh.decreaseQuantity();
        
    }       
}

SPORK SUBCLASS

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication2;

public class Spork extends Part 
{
    
    public Spork(String nm)
    {
 super(nm);

    }
     @Override
   public int getQuantity (int Quantity) 
         {
             return Quantity;
         }
         
   public void setQuantity() 
         {
            int Quantity = N_INVENTORY_ITEMS;
         }
         
    @Override
   public int increaseQuantity(int Quantity) 
         {
             //int 
                    return Quantity;// = N_INVENTORY_ITEMS;
         }
         
    public void decreaseQuantity()
    {
        int Quantity = N_INVENTORY_ITEMS;

        System.out.print("Spork");
    }
   
    public static void main (String [] args)
    {
        Spork please = new Spork("corkj");
        please.decreaseQuantity();
    }
   
}

GROMMET SUBCLASS

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication2;

/**
 *
 * @author Torbecire
 */
public class Grommet extends Part {
    
    public Grommet(String nm)
    {
            super(nm);
    }
     @Override
   public int getQuantity (int Quantity) 
         {
             return Quantity;
         }
         
   public void setQuantity() 
         {
            int Quantity = N_INVENTORY_ITEMS;
         }
         
    @Override
   public int increaseQuantity(int Quantity) 
         {
             //int 
                    return Quantity;// = N_INVENTORY_ITEMS;
         }
   
         
    public void decreaseQuantity()
    {
        int Quantity = N_INVENTORY_ITEMS;

        System.out.print(N_INVENTORY_ITEMS);
    }
   public static void main (String [] args)
    {
       Grommet Dakro = new Grommet("THE KINGDOM");
       Dakro.decreaseQuantity();
    }
}

In the Project three part above (first listing) it reads Widget when I declare withe inventory but not Spork or Grommet.

inventory[0] = new Widget();
inventory[1] = new Spork(); // GIVES ERROR CANNOT FIND SYMBOL
inventory[2] = new Grommet();
WHY?

They are in different packages, so you need to import those classes or put them in the same package as the inventory class.

I put them in the same package, but now it's says. Can not find constructor Widget(); Is it because in the Widget file I have Widget(String nm)?

Yes, you don't have a no-argument constructor defined in the class.

Question. How can I pass data from the main project to one of the other files like Widget. Lets say I want to add a widget. Is it automatically done since it's abstract

Your question is not clear. Perhaps if you restate it with a little more detail it would help.

I want to change a value in the main program( add subtract) . Then pass this value to the subclass. I am thinking I should use super(int add). Is that right? If it is how where should i put the super?

Could you disregard the last question.

I have added

abstract void overiden(int access)

to the part class. So that I can use I can overide overiden in the subclasses of widget, Spork and Grommet.

How do I overide them in Widget, Spork, and Grommet?

You just write a new method in each of those classes called

void overiden( int access )

Then when an object of type Widget calls the overiden method, it will use the implementation in the Widget class. Similarly, a Spork object uses Spork's implementation and a Grommet will use Grommet's.

Where it gets really interesting is when Part's overiden method is not declared as abstract and the subclasses each override the method. They can call super.overiden(access) in order to call Part's version of the method, and then go on to do some more things as needed. This allows for code reuse, efficient maintenance of code and is a common theme in object oriented programming called polymorphism.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.