I need help with this code. I am already a week late on this part because part 2 took to long to code. I am not getting the LeatherBlueDot class to compile. I am getting a message that in my constructor statement saying each variable has private access in class SoftballInv3, but I used the super method to declare them. I am not if I am explaining this correctly. I am very new to Java and struggling. Can anyone help? I am posting three classes needed. The Test class is supposed to display the arrays. Here they are beginning with the Test class.

// Inventory program part two
	// for softball inventories
	// from Als sporting goods


	public class SoftballInvTest3
	{
		// begins main method
		public static void main ( String args[])
		{
                    // creats a welcome message
                    System.out.println( "Welcome to Al's Sporting Goods softball inventory" );

                    // displays headers for the inventory
                    System.out.printf( "\n%4s%26s%10s%5s%9s%11s%13s", "ITEM", "BRAND NAME", "TYPE", "QTY",
							"PRICE", "RESTOCK", "INV VALUE" );

					// lists all of the arrays used in this program
                    int itemNumber[] = {1001,1002,1003,1004,1005,1006, 1007};
                    int ballCount[] = {36, 48, 12, 72, 24, 96, 24 };
                    double ballPrice[] = { 5.99, 4.99, 5.49, 6.99, 7.99, 1.99, 3.99 };
                    String itemName[] = {"Blue Dot-Worth", "Gold Dot-Worth", "Green Dot-Worth", "Super Blue Dot-Worth", 
											"Demarini Evil Ball",	"Rawlings practice balls", "Demarini Core 44" };
                	double subTotal[] = new double [7];
					double reStock[] = new double [7];
					String coverType[] = {"leather", "synthetic", "synthetic", "leather", "synthetic",
									"leather", "leather"};

                    // created new objects of softballinv2 class
                    SoftballInv3 softBall1 = new SoftballInv3();
					LeatherBlueDot softBall2 = new LeatherBlueDot();
                    // uses methods of softballinv public class to determine value

                    softBall1.sortArrayName(itemName);
                    softBall1.displayArrays(itemNumber, ballCount, ballPrice, itemName, subTotal);
                    softBall1.getTotalOfInv(subTotal);
                    softBall2.getReStock(stock);
                    softBall2.getTotalOfInv(subTotal);
					softBall2.sortArrayName( itemName);
					softBall2.displayArrays(itemNumber, ballCount, coverType, ballPrice, reStock, itemName, subTotal);
		} // ends main method
} // ends public class

// This is class of SoftballInvPart3
// this file computes totals and
// creates array

public class SoftballInv3
{

    private int item[]; // array of item numbers
	private String name[];
    private int balls[]; // array for quantity of balls
	private double prices[]; // array for price of each ball
	private double subs[]; // array created for totals of item inventory


        // constructor that initializes arrays for each element of inventory list
	public SoftballInv3(int itemNumber[], String itemName[], int ballCount[], double ballPrice[], double subTotal[])
	{


                item = itemNumber; // stores item number in array
                balls = ballCount; // stores number of balls per for item number
				prices = ballPrice; // stores price of each ball
				subs = subTotal; // stores total price of each item
				name = itemName; // stores brand Names





	}// end public Softball array constructor

		// method to display value of entire inventory
		public void getTotalOfInv(double subs[])
		{

			double total = 0;

			for ( int counter = 0; counter < subs.length; counter++ )
				total += subs[ counter ];
			System.out.printf( "\nTotal of entire inventory is: %.2f\n", total );

		} // end set display value method


       	// method call to displays sorted arrays
	   	public void displayArrays(int item[], int balls[], double prices[], String name[], double subs[])
	   	{
	   		for ( int j = 0; j < name.length; j++)
	   		System.out.printf("%4s%26s%15s%9s%24.2f\n", item[j], name[j], balls[j],
	   					 prices[j], subs[j] = (balls[j] * prices[j]));
		} // end method to display the arrays

		// method to sort the the itemName array alphabetically
       	public void sortArrayName(String itemName[])
		{
			int j;
			boolean flag = true;  //to determine when the sort is finished
			String temp;

			while ( flag ) // loops through array while flag is false
			{
				flag = false;
				for ( j = 0;  j <itemName.length - 1;  j++ )
				{
					if ( itemName[ j ].compareToIgnoreCase( itemName[ j+1 ] ) > 0 )
					{                                             // ascending sort
			     		temp = itemName[ j ];
			        	itemName[ j ] = itemName[ j+1];
			        	itemName [ j+1] = temp;
			        	flag = true;
			  		} // end if
				}
			} // end while
		}





} // end class SoftballInv2

// This class is sub class of
	// of the SoftballInv3 class
	// that uses inheritance to add one more
	// item to products

	public class LeatherBlueDot extends SoftballInv3
	{
		private double stock[]; //re-stock fee for leather balls
		private String cover[]; // initiates array variable

		// five argument constructor
		public LeatherBlueDot(int itemNumber[], String itemName[], String coverType[], int ballCount[],
				double ballPrice[], double subTotal[], double reStock[])
		{

			super( item, name, balls, prices, subs );

			getReStock( stock ); // method to add restocking fee
			System.out.printf( "\nThe new inventory that adds a restocking fee and cover type:\n");
			stock = reStock;
			cover = coverType;

		}// end constructor

		// adds restocking fee to total price of each product
		public void getReStock( double prices[] )
		{
			double stock = 0;

			for ( int counter = 0; counter < prices.length; counter++ )

			stock = prices[ counter ] * 1.05;

		} // end method




		public double getTotalOfInv()
		{
			return getTotalOfInv();
		}


		// method call to displays sorted arrays
		public void displayArrays(int item[], String name[], String cover[], int balls[], double prices[],
								double stock[], double subs[])

		{
			 for ( int j = 0; j < name.length; j++)
			  System.out.printf("%4s%26s%15s%9s%24.2f\n", item[j], name[j], cover[j], balls[j],
			   			prices[j], stock[j], subs[j] = (balls[j] * prices[j]));

		} // end method to display the arrays


	}

Recommended Answers

All 4 Replies

exactly as you said, they are private, make them protected

I had already tried that once before and didn't work but I went ahead and tried again just to see what error I was getting. There are 5 errors, all say the same thing for each variable from softballinv3 class. The messages are "can't reference variable before super type constructor has been called". I don't understand why it says that because the super type constructor is the first statement in the class.

Here is my code again. Nothing has changed, I am just wrapping the code in tags correctly this time.

// Inventory program part two
// for softball inventories
// from Als sporting goods


public class SoftballInvTest3
{
       // begins main method
       public static void main ( String args[])
      {
             // creats a welcome message
             System.out.println( "Welcome to Al's Sporting Goods softball inventory" );

             // displays headers for the inventory
               System.out.printf( "\n%4s%26s%10s%5s%9s%11s%13s", "ITEM", "BRAND                                     NAME", "TYPE", "QTY",
							"PRICE", "RESTOCK", "INV VALUE" );

					// lists all of the arrays used in this program
                    int itemNumber[] = {1001,1002,1003,1004,1005,1006, 1007};
                    int ballCount[] = {36, 48, 12, 72, 24, 96, 24 };
                    double ballPrice[] = { 5.99, 4.99, 5.49, 6.99, 7.99, 1.99, 3.99 };
                    String itemName[] = {"Blue Dot-Worth", "Gold Dot-Worth", "Green Dot-Worth", "Super Blue Dot-Worth", 
											"Demarini Evil Ball",	"Rawlings practice balls", "Demarini Core 44" };
                	double subTotal[] = new double [7];
					double reStock[] = new double [7];
					String coverType[] = {"leather", "synthetic", "synthetic", "leather", "synthetic",
									"leather", "leather"};

                    // created new objects of softballinv2 class
                    SoftballInv3 softBall1 = new SoftballInv3();
					LeatherBlueDot softBall2 = new LeatherBlueDot();
                    // uses methods of softballinv public class to determine value

                    softBall1.sortArrayName(itemName);
                    softBall1.displayArrays(itemNumber, ballCount, ballPrice, itemName, subTotal);
                    softBall1.getTotalOfInv(subTotal);
                    softBall2.getReStock(stock);
                    softBall2.getTotalOfInv(subTotal);
					softBall2.sortArrayName( itemName);
					softBall2.displayArrays(itemNumber, ballCount, coverType, ballPrice, reStock, itemName, subTotal);
		} // ends main method
} // ends public class

// This is class of SoftballInvPart3
// this file computes totals and
// creates array

public class SoftballInv3
{

    private int item[]; // array of item numbers
	private String name[];
    private int balls[]; // array for quantity of balls
	private double prices[]; // array for price of each ball
	private double subs[]; // array created for totals of item inventory


        // constructor that initializes arrays for each element of inventory list
	public SoftballInv3(int itemNumber[], String itemName[], int ballCount[], double ballPrice[], double subTotal[])
	{


                item = itemNumber; // stores item number in array
                balls = ballCount; // stores number of balls per for item number
				prices = ballPrice; // stores price of each ball
				subs = subTotal; // stores total price of each item
				name = itemName; // stores brand Names





	}// end public Softball array constructor

		// method to display value of entire inventory
		public void getTotalOfInv(double subs[])
		{

			double total = 0;

			for ( int counter = 0; counter < subs.length; counter++ )
				total += subs[ counter ];
			System.out.printf( "\nTotal of entire inventory is: %.2f\n", total );

		} // end set display value method


       	// method call to displays sorted arrays
	   	public void displayArrays(int item[], int balls[], double prices[], String name[], double subs[])
	   	{
	   		for ( int j = 0; j < name.length; j++)
	   		System.out.printf("%4s%26s%15s%9s%24.2f\n", item[j], name[j], balls[j],
	   					 prices[j], subs[j] = (balls[j] * prices[j]));
		} // end method to display the arrays

		// method to sort the the itemName array alphabetically
       	public void sortArrayName(String itemName[])
		{
			int j;
			boolean flag = true;  //to determine when the sort is finished
			String temp;

			while ( flag ) // loops through array while flag is false
			{
				flag = false;
				for ( j = 0;  j <itemName.length - 1;  j++ )
				{
					if ( itemName[ j ].compareToIgnoreCase( itemName[ j+1 ] ) > 0 )
					{                                             // ascending sort
			     		temp = itemName[ j ];
			        	itemName[ j ] = itemName[ j+1];
			        	itemName [ j+1] = temp;
			        	flag = true;
			  		} // end if
				}
			} // end while
		}





} // end class SoftballInv2

// This class is sub class of
	// of the SoftballInv3 class
	// that uses inheritance to add one more
	// item to products

	public class LeatherBlueDot extends SoftballInv3
	{
		private double stock[]; //re-stock fee for leather balls
		private String cover[]; // initiates array variable

		// five argument constructor
		public LeatherBlueDot(int itemNumber[], String itemName[], String coverType[], int ballCount[],
				double ballPrice[], double subTotal[], double reStock[])
		{

			super( item, name, balls, prices, subs );

			getReStock( stock ); // method to add restocking fee
			System.out.printf( "\nThe new inventory that adds a restocking fee and cover type:\n");
			stock = reStock;
			cover = coverType;

		}// end constructor

		// adds restocking fee to total price of each product
		public void getReStock( double prices[] )
		{
			double stock = 0;

			for ( int counter = 0; counter < prices.length; counter++ )

			stock = prices[ counter ] * 1.05;

		} // end method




		public double getTotalOfInv()
		{
			return getTotalOfInv();
		}


		// method call to displays sorted arrays
		public void displayArrays(int item[], String name[], String cover[], int balls[], double prices[],
								double stock[], double subs[])

		{
			 for ( int j = 0; j < name.length; j++)
			  System.out.printf("%4s%26s%15s%9s%24.2f\n", item[j], name[j], cover[j], balls[j],
			   			prices[j], stock[j], subs[j] = (balls[j] * prices[j]));

		} // end method to display the arrays


	}

Suggestions:

1. Where your LeatherBlueDot constructor (the one with 6 arguments) calls super, the arguments you have in super don't exist. item, name, balls, prices, and subs are not defined in the LeatherBlueDot class. You probably meant to say super( itemNumber, itemName, ballCount, ballPrice, subTotal );

2. You have the line SoftballInv3 softBall1 = new SoftballInv3(); but you don't have a no argument constructor in your SoftBallInv3 class. You have LeatherBlueDot softBall2 = new LeatherBlueDot(); but you don't have that constructor in the LeatherBlueDot class either.

3. softBall2.getReStock(stock); Where is stock declared?
4. softBall2.displayArrays(itemNumber, ballCount, coverType, ballPrice, reStock, itemName, subTotal); You seem to be calling this wrong (with the wrong number or types of arguments)

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.