I am working on this assignment in my java class, and am running into a few problems.
Basically there are 2 classes and a main/client program that tests the classes.

there is 1 superclass and a subclass that extends the superclass..

I have 2 constructors in my superclass but when i try to access them from my subclass I receive some errors, I believe I am supposed to create a constructor in my subclass and call the constructor using

super(arguments);

If someone could please explain what exactly I am doing wrong it would be greatly appreciated..

Here are my 3 files total..

public class GroceryItemOrder {

	 private String myName;  //name of grocery item
	 private int myQuantity; //number of items
	 private double myPrice; //price of each item
     private double total;   //total price of items

	//default constructor to handle normal data
	public GroceryItemOrder (String name, int quantity, double unitPrice){

		myName = name;
		myQuantity = quantity;
        myPrice = unitPrice;
	}

	//extra constructor to work with the setQuantity
	// and setUnitPrice methods when only name is available
	public GroceryItemOrder(String name){

		myName = name;
	}

	// Precondition: myPrice and myQuantity > 0
	// Returns: total cost
	public double getCost(){

		//calculates final price of item(s)
		total = myPrice * myQuantity;
		//returns the total price of item(s)
		return total;
	}

	// Parameter: quantity
	// Precondition: quantity > 0
	public void setQuantity(int quantity){

		myQuantity = quantity;
	}

	// Parameter: unitPrice
	// Precondition: unitPrice > 0
	public void setUnitPrice(double unitPrice){

		myPrice = unitPrice;
	}

	public void setName(String name){

		myName = name;
	}

	// Returns: string output
	public String toString(){

		//returns the final output string, formatted
		return String.format("%s: %d @ $%.2f",myName,myQuantity,getCost());
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.io.*;
import java.util.*;


public class GroceryList extends ArrayList<GroceryItemOrder>{

	//my fail attempt at using constructor from GroceryItemOrder class
	public GroceryList(String name, int quantity, double unitPrice){

	  super(name,quantity,unitPrice);
    }

   public void add()
   {
      Scanner inp = new Scanner(System.in);

      GroceryItemOrder tempGIO = new GroceryItemOrder();

      System.out.print("Enter item name: ");
      tempGIO.setName(inp.next());

      System.out.print("Enter item price (for 1): ");
      tempGIO.setUnitPrice(inp.nextDouble());

      System.out.print("Enter item quantity: ");
      tempGIO.setQuantity(inp.nextInt());

      this.add(tempGIO);
   }

   public double getTotalCost()
   {
      double dblTotalCost = 0;

      for(int i=0; i< this.size(); i++)
      {
         dblTotalCost += this.get(i).getCost();
      }

      return dblTotalCost;
   }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.util.*;
public class TestGroceryList{

public static void main (String [] args) {

GroceryList list = new GroceryList();

// The empty grocery list should print out as a blank
// and its total cost should be $0
System.out.println("Grocery List:");
System.out.println(list);
System.out.printf("Total Cost: $%.2f\n",list.getTotalCost());
System.out.println();

// fill the list with items 10 items
fillList(list,10);

// This next item should not be added
list.add(new GroceryItemOrder("celery", 2, 1.67));
System.out.println("Grocery List:");
System.out.print(list);
System.out.printf("Total Cost: $%.2f\n",list.getTotalCost());
System.out.println();

// make a new list that is not "full"
list = new GroceryList();
fillList(list, new Random().nextInt(7)+1);
System.out.println("Grocery List:");
System.out.print(list);

// just the items should print, no NULLs!
System.out.printf("Total Cost: $%.2f\n",list.getTotalCost());
System.out.println();
}

public static void fillList(GroceryList list, int numItems) {

Random rand = new Random();
for (int i = 0; i < numItems; i++) {

GroceryItemOrder g = new GroceryItemOrder("g" + (i+1));
g.setQuantity(rand.nextInt(5) + 1);
double unitPrice = rand.nextDouble() * 1000;
unitPrice = (int) unitPrice / 100.0;
g.setUnitPrice(unitPrice);
list.add(g);

}

}


}

Any suggestions would be great!

GroceryList extends ArrayList<GroceryItemOrder>
so
super(name,quantity,unitPrice);
tries to find a constructor in the superclass (ie ArrayList) that takes those 3 params, and there isn't one.

any chance to explain?

I'm not sure how to work with constructors when using the ArrayList<GroceryItemOrder>..

your super statement is based on the idea you extend GroceryItemOrder, but you don't.
in your description, you state that those classes are super- and subclass, but they are not.

IMHO, a better approach would be to keep an ArrayList, instead of extending it.

sorry to be a pest but I really dont understand what you mean, I have tried everything I could think of and it does not help.

public GroceryList(String name, int quantity, double unitPrice){

	  super(name,quantity,unitPrice);
    }

ArrayList doesn't have a constructor which takes a String, an int and a double as parameters.

your subclass is not extending from GroceryItemOrder, it's extending ArrayList.
besides, why are you calling that super-constructor anyway?

I was under the impression that I would need to use super(args) to call the constructor in my super class,

well, yes, but AFAIK you don't really have to call that constructor.
also, as mentioned before: GroceryItemOrder is NOT your super-class.

I think you need to remove the extension of the ArrayList<>.

public class GroceryList {
ArrayList<GroceryItemOrder> list = new ArrayList<GroceryItemOrder>();

    public GroceryList(String name, int quantity, double unitPrice){
       // do something nice with the parameters
    }
 
   public void add()
   {
      GroceryItemOrder tempGIO = new GroceryItemOrder();
      // do init... 
      list.add(tempGIO);
   }
 
   public double getTotalCost()
   {
      double dblTotalCost = 0;
 
      for(int i=0; i< this.size(); i++)
      {
         dblTotalCost += list.get(i).getCost();
      }
 
      return dblTotalCost;
   }
}
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.