Sorting Arrays & Subclasses

Is it possible to sort multiple arrays in a particular fashion

Page 1 of 1

5 Replies - 1742 Views - Last Post: 29 October 2007 - 05:17 PM Rate Topic: -----

#1 ITXavier25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 27-October 07

Sorting Arrays & Subclasses

Posted 27 October 2007 - 04:46 PM

Hello. I having some problems with my Java homework assignment. The assignment requires that I sort my Inventory program by name and also create a subclass with a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.

Here's problem # 1: My program has multiple arrays respectively containing int, double, and String variables. I was able to sort the String array that contained the names of the product; however, I do not know of a method that allows the other arrays to follow the same sort. Because of my problem, when the name array is sorted, the inventory information becomes missed mismatched. I want to know if it is possible to have the other arrays follow the same sort. I read a little about multi-dimensional arrays; however, I do not think that will help my situation because I think that the arrays cannot have more than one variable type.

Here's problem # 2: I have no idea what a subclass is or how to make it perform what my assignment requires. The wording for the assignment is confusing and I hope someone can give me an idea of what it's about. I do know how to create a way to add 5% to the restocking fee; by multiplying 0.05 to the value, then adding that product to the value. However, I do not think that's what the assignment wants.

My program is in two classes. TransformersInventory2 is the main class, TransformersProduct is the product class.

Please someone help! This assignment is due tomorrow and I no one experienced in Java. What's worst is that I am learning online. I cannot go to my teacher for help or get help from my classmates (I mean you can post to them like how I am doing here; however, it doesn't appear they want to help or they probably lost as well.

Below are the codes:

import java.util.Arrays;

// class declaration for TransformersInventory
public class TransformersInventory2
{
   // main method begins program execution
   public static void main( String args[] ) 
   {
   	  // declare array name autobotNameArray
	  String autobotNameArray[] = { "Hidden Autobot", "Optimus Prime", "BumbleBee", 
									"Ironhide", "Ratchet", "Jazz" };
			Arrays.sort( autobotNameArray, String.CASE_INSENSITIVE_ORDER );
	  
	  int autobotProductArray[] = { 0, 7, 16, 25, 34, 43 }, // product # array
		  //autobotUnitArray[] = { 0, 52, 61, 106, 115, 124 }; // array of autobot toy units
	  
	  double autobotPriceArray[] = { 0.00, 160.99, 97.99, 88.99, 79.99,70.99 }, // array of autobot toy prices
			 autobotStockArray[] = { autobotUnitArray[0] * autobotPriceArray[0], 
									 autobotUnitArray[1] * autobotPriceArray[1], 
									 autobotUnitArray[2] * autobotPriceArray[2], 
									 autobotUnitArray[3] * autobotPriceArray[3], 
									 autobotUnitArray[4] * autobotPriceArray[4], 
									 autobotUnitArray[5] * autobotPriceArray[5] }; // array of autobot stock values
		   		
	  TransformersProduct myTransformers = new TransformersProduct( "Transformers Autobot Toy Inventory" , 
																	autobotNameArray, autobotProductArray, 
																	autobotUnitArray, autobotPriceArray, 
																	autobotStockArray ); 
	  myTransformers.displayMessage();
	  myTransformers.processInventory();		
	} //end main
} // end class TransformersInventory2






import static java.lang.System.out;
 

// class declaration for TransformersProduct
public class TransformersProduct
{
   private String inventoryName; // name of inventory
   
   private String autobotName[]; // array of autobot toy names
   
   private int product[]; // product # array
   private int unit[]; // array of autobot toy units
   
   private double price[]; // array of autobot prices
   private double stock[]; // array of autobot stock values
   
   /* six-argument constructor initializes name inventoryName, 
	*autobotName, product unit, price and stock arrays. */
   public TransformersProduct ( String name, String autobotNameArray[], 
								int autobotProductArray[], int autobotUnitArray[], 
								double autobotPriceArray[], double autobotStockArray[] )   
   {
   	inventoryName = name; // initialize inventoryName
   	  autobotName = autobotNameArray; // initialize autobotName
   	  product = autobotProductArray; // initialize product
   	  unit = autobotUnitArray; // initialize unit
   	  price = autobotPriceArray; // initialize price
   	  stock = autobotStockArray; // initialize stock
   } // end six-argument TransformersProduct constructor

   // method to set the inventory name
   public void setInventoryName ( String name ) 
   {
	  inventoryName = name; // store inventory name
   } // end method setInventoryName
   
   // method to retrieve the inventory name
   public String getInventoryName()
   {
   	   return inventoryName; //
   } // end method getInventoryName
   
   // display a welcome message to the TransformersInventory2 user
   public void displayMessage()
   {
	  // getInventoryName gets the name of the inventory
	  out.printf( "Welcome to the\n%s!\n\n", 
						 getInventoryName() );
   } // end method displayMessage   
   
   //method to display the invertory arrays output
   public void processInventory()
   {
	  // output inventory arrays
	  outputInventory();
   } // end method processInventory
   
   // output inventory arrays
   public void outputInventory()
   {
	  // displays available inventory message
	  out.println( "Below is the available inventory:\n" ); 
															 
	  // create column headings
	  out.print( "Index\tProduct #\t\t\tName\t\tUnits\t\tPrice\t\t" );
	  
	  out.println( "Stock Value" ); // create Stock Value column
	   
	  // output each arrays' elements' value
	  for ( int toyCounter = 0; toyCounter < autobotName.length; toyCounter++)
			out.printf( "\n%5d%12d%27s%17d		$%7.2f		   $%10.2f\n", 
						toyCounter, product[ toyCounter ], autobotName[ toyCounter ], 
						unit[ toyCounter ], price[ toyCounter ], stock[ toyCounter ] );
	 
	  // calculate entire inventory value 
	  double totalStockValue = stock[1] + stock[2] + stock[3] + stock[4] + stock[5];
	  out.printf( "\nThe value of the entire inventory is $ %.2f\n", totalStockValue );		   
   }  // end outputInventory
} // end class TransformersProduct
~edit code tags PB

This post has been edited by PennyBoki: 28 October 2007 - 05:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Arrays & Subclasses

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5908
  • View blog
  • Posts: 12,814
  • Joined: 16-October 07

Re: Sorting Arrays & Subclasses

Posted 27 October 2007 - 05:17 PM

I believe you may have misinterpreted this somewhat. I think TransformersProduct should probably refer to a single object in inventory. e.g.

public class TransformersProduct {
   private String inventoryName; // name of inventory
   private int product; // product # array
   private int unit; // array of autobot toy units
   private double price; // array of autobot prices
   private double stock; // array of autobot stock values
   
   /* six-argument constructor initializes name inventoryName, 
	*autobotName, product unit, price and stock arrays. */
	public TransformersProduct (String name, int product, int unit, double price) {
		this.inventoryName = name; // initialize inventoryName
		this.product = product; // initialize product
		this.unit = unit; // initialize unit
		this.price = price; // initialize price
	}

,,,



Note, I left out stock. It could then have methods like:

public double getStockPrice() {
	return this.unit * this.price; 
}



Your TransformersInventory class would contain an array of TransformersProduct and manipulate it accordingly.

Does this make sense?

This post has been edited by baavgai: 27 October 2007 - 05:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 ITXavier25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 27-October 07

Re: Sorting Arrays & Subclasses

Posted 29 October 2007 - 01:48 AM

Perhaps you're correct doing it the way you example; however, I didn't really understand your example. I tried to implement it into my code and I couldn't get it to compile. I'm just to new and dumb at Java. Luckily, with some help, I managed to sort the list alphabetically by name. Below is the modified codes.

/* Xavier Downing - IT 307 - October 28, 2007
 *Week Six CheckPoint:Inventory Program Part 3
 *Description: This program displays an inventory
 *of a Transformer Autobot collection.This program
 *also has the ability to sort. */
import java.text.Collator;
import java.util.Locale;

// class declaration for TransformersInventory3
public class TransformersInventory3
{
   // declare array name autobotNameArray
   static String autobotNameArray[] = { "No Sale Autobot", "Optimus Prime", "BumbleBee", 
									"Ironhide", "Ratchet", "Jazz" };
													
   static int autobotProductArray[] = { 0, 7, 16, 25, 34, 43 }; // product # array
   static int autobotUnitArray[] = { 1, 52, 61, 106, 115, 124 }; // array of autobot toy units
	  
   static double autobotPriceArray[] = { 0.00, 160.99, 97.99, 88.99, 79.99,70.99 }; // array of autobot toy prices

   static double autobotRestockArray[] = { 0.05 * autobotPriceArray[0] + autobotPriceArray[0], 
										   0.05 * autobotPriceArray[1] + autobotPriceArray[1], 
										   0.05 * autobotPriceArray[2] + autobotPriceArray[2], 
										   0.05 * autobotPriceArray[3] + autobotPriceArray[3], 
										   0.05 * autobotPriceArray[4] + autobotPriceArray[4], 
										   0.05 * autobotPriceArray[5] + autobotPriceArray[5]}; // array of autobot stock values
										   
   static double autobotStockArray[] = { autobotUnitArray[0] * autobotPriceArray[0], 
										 autobotUnitArray[1] * autobotPriceArray[1], 
										 autobotUnitArray[2] * autobotPriceArray[2], 
										 autobotUnitArray[3] * autobotPriceArray[3], 
										 autobotUnitArray[4] * autobotPriceArray[4], 
										 autobotUnitArray[5] * autobotPriceArray[5] }; // array of autobot stock values
										 
   static double autobotTotalRestockArray[] = { 0.05 * autobotStockArray[0] + autobotStockArray[0], 
													 0.05 * autobotStockArray[1] + autobotStockArray[1], 
													 0.05 * autobotStockArray[2] + autobotStockArray[2], 
													 0.05 * autobotStockArray[3] + autobotStockArray[3], 
													 0.05 * autobotStockArray[4] + autobotStockArray[4], 
													 0.05 * autobotStockArray[5] + autobotStockArray[5] }; // array of autobot stock values
										 
   // main method begins program execution
   public static void main( String args[] ) 
   {										 
	  inventorySorter();
	  TransformersProduct myTransformers = new TransformersProduct( "Transformers Autobot Toy Inventory", "5%",  
																	autobotNameArray, autobotProductArray, 
																	autobotUnitArray, autobotPriceArray,
																	autobotRestockArray, autobotStockArray,
																	autobotTotalRestockArray ); 
	  myTransformers.displayMessage();
	  myTransformers.displayMessage2();
	  myTransformers.processInventory();		
	  
   } //end main
   
   // inventorySorter method
   public static void inventorySorter()
   {
	  String sorter;
		
	  int sorterAutobotProductArray,
		  sorterAutobotUnitArray;
		
	  double sorterAutobotPriceArray,
			 sorterAutobotRestockArray,
			 sorterAutobotStockArray,
			 sorterAutobotTotalRestockArray;
	
	  Locale loc = Locale.ENGLISH;
		
	  Collator col = Collator.getInstance(loc);
		
	  for (int i = 0; i < autobotNameArray.length; i++) 
	  {
		 for (int j = i + 1; j < autobotNameArray.length; j++) 
		 {
			if( col.compare(autobotNameArray[i], autobotNameArray[j] ) > 0 ) 
			{
			   sorter = autobotNameArray[i];
			   sorterAutobotProductArray = autobotProductArray[i];
			   sorterAutobotUnitArray = autobotUnitArray[i];
			   sorterAutobotPriceArray = autobotPriceArray[i];
			   sorterAutobotRestockArray = autobotRestockArray[i];
			   sorterAutobotStockArray = autobotStockArray[i];
			   sorterAutobotTotalRestockArray = autobotTotalRestockArray[i];
				
			   autobotNameArray[i] = autobotNameArray[j];
			   autobotProductArray[i]=autobotProductArray[j];
			   autobotUnitArray[i] = autobotUnitArray[j];
			   autobotPriceArray[i] = autobotPriceArray[j];
			   autobotRestockArray[i] = autobotRestockArray[j];
			   autobotStockArray[i] = autobotStockArray[j];
			   autobotTotalRestockArray[i]= autobotTotalRestockArray[j];
				
			   autobotNameArray[j] = sorter;
			   autobotProductArray[j]=sorterAutobotProductArray;
			   autobotUnitArray[j] = sorterAutobotUnitArray;
			   autobotPriceArray[j] = sorterAutobotPriceArray;
			   autobotRestockArray[j] = sorterAutobotRestockArray;
			   autobotStockArray[j] = sorterAutobotStockArray;
			   autobotTotalRestockArray[j]  = sorterAutobotTotalRestockArray;				
			} // end 2nd inner if
		 } // end 1st inner if
	  } // end for
   } // end inventorySorter
} // end class TransformersInventory3

/* Xavier Downing - IT 307 - October 28, 2007
 *Week Six CheckPoint:Inventory Program Part 3
 *Description: This is the product class for 
 *the TransformersInventory3 class. */
import static java.lang.System.out;
 

// class declaration for TransformersProduct
public class TransformersProduct
{
   private String inventoryName; // name of inventory
   private String restockRate; // restock rate percentage
	  
   private String autobotName[]; // array of autobot toy names
   
   private int product[]; // product # array
   private int unit[]; // array of autobot toy units
   
   private double price[]; // array of autobot prices
   private double restock[];
   private double stock[]; // array of autobot stock values
   private double totalRestock[];
   
   /* eight-argument constructor initializes name inventoryName, 
	*autobotName, product unit, price and stock arrays. */
   public TransformersProduct ( String name, String rate, String autobotNameArray[],
								int autobotProductArray[], int autobotUnitArray[], 
								double autobotPriceArray[], double autobotRestockArray[],
								double autobotStockArray[], double autobotTotalRestockArray[] )   
   {
   	inventoryName = name; // initialize inventoryName
   	restockRate = rate; // initialize restock rate
   	autobotName = autobotNameArray; // initialize autobotName
   	product = autobotProductArray; // initialize product
   	unit = autobotUnitArray; // initialize unit
   	price = autobotPriceArray; // initialize price
   	restock = autobotRestockArray;
   	stock = autobotStockArray;
   	totalRestock = autobotTotalRestockArray; // initialize stock
   } // end eight-argument TransformersProduct constructor

   // method to set the inventory name
   public void setInventoryName ( String name ) 
   {
	  inventoryName = name; // store inventory name
   } // end method setInventoryName
   
   // method to retrieve the inventory name
   public String getInventoryName()
   {
   	   return inventoryName; // 
   } // end method getInventoryName
   
   // method to set the inventory name
   public void setRestockRate ( String rate ) 
   {
	  restockRate = rate; // store restock rate
   } // end method setInventoryName
   
   // method to retrieve the restock rate
   public String getRestockRate()
   {
   	   return restockRate; //
   } // end method getInventoryName

   // display a welcome message to the TransformersInventory2 user
   public void displayMessage()
   {
	  // getInventoryName gets the name of the inventory
	  out.printf( "Welcome to the\n%s!\n\n", 
				  getInventoryName() );
   } // end method displayMessage
   
   // display a welcome message to the TransformersInventory2 user
   public void displayMessage2()
   {
	  // getInventoryName gets the name of the inventory
	  out.printf( "The restock interest rate is at %s.\n\n",
				  getRestockRate() );
   } // end method displayMessage2	
   
   //method to display the invertory arrays output
   public void processInventory()
   {
	  // output inventory arrays
	  outputInventory();
   } // end method processInventory
   
   // output inventory arrays
   public void outputInventory()
   {
	  // displays available inventory message
	  out.println( "Below is the available inventory:\n" ); 
															 
	  // create column headings
	  out.print( "Index\tProduct #\t\tName\t\tUnits\t\t\tPrice\t\t" );

	  out.print( " Unit Restock Value\t" ); // create Stock Value column
	  
	  out.print( "Stock Value\t" ); // create Stock Value column
	  
	  out.print( "Total Product Restock\n" ); // create Stock Value column
	   
	  // output each arrays' elements' value
	  for ( int toyCounter = 0; toyCounter < autobotName.length; toyCounter++)
			out.printf( "\n%5d%12d%19s%17d			 $%10.2f				   $%10.2f	 $%10.2f			   $%10.2f\n", 
						toyCounter, product[ toyCounter ], autobotName[ toyCounter ], 
						unit[ toyCounter ], price[ toyCounter ], restock[ toyCounter ],
						stock[ toyCounter ], totalRestock[ toyCounter ] );
	 
	  // calculate entire inventory value 
	  double totalStockValue = stock[1] + stock[2] + stock[3] + stock[4] + stock[5];
	  
	  // calculate the restocking cose
	  double totalRestockValue = 0.05 * totalStockValue + totalStockValue;
	  
	  out.printf( "\nThe value of the entire inventory is $ %.2f\n", totalStockValue );
	  out.printf( "\nThe restocking cost for the entire inventory is $ %.2f\n", totalRestockValue );		   
   }  // end outputInventory
} // end class TransformersProduct
~edit code tags please use them PB

This post has been edited by PennyBoki: 29 October 2007 - 12:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5908
  • View blog
  • Posts: 12,814
  • Joined: 16-October 07

Re: Sorting Arrays & Subclasses

Posted 29 October 2007 - 05:44 AM

You have two distinct classes. However, I find having the main in one of those classes makes the logic harder to follow. I'll off your three classes as an example of what I'm on about and maybe that will point you to an elegant solution.

Here's the main:
public class Program {
	public static void main(String[] args)	{
		TransformersProduct[] products = new TransformersProduct[] {
			new TransformersProduct("Optimus Prime", 7, 61, 160.99),
			new TransformersProduct("BumbleBee", 16, 106, 97.99)
		};
		TransformersInventory myTransformers = new TransformersInventory("Transformers Autobot Toy Inventory", 0.5, products);
		myTransformers.printProductList();
	}
}


I copyied two of your products here. Now, for the item class:
public class TransformersProduct {
	public String name; // name of inventory
	public int product; // product # array
	public int unit; // array of autobot toy units
	public double price; // array of autobot prices

	public TransformersProduct(String name, int product, int unit, double price) {
		this.name = name; // initialize inventoryName
		this.product = product; // initialize product
		this.unit = unit; // initialize unit
		this.price = price; // initialize price
	}

	public double getStockPrice() {
		return this.unit * this.price;
	}
	public double getRestockPrice(double rate) {
		return this.unit * (this.price * (1 - rate));
	}
}


I made everything public because I'm being lazy, but Java style getters and setters would be better her.

Now, where the work is done:
public class TransformersInventory {
	public String name;
	public double rate;
	public TransformersProduct [] products;

	public TransformersInventory(String name, double rate, TransformersProduct[] products) {
		this.name = name;
		this.rate = rate;
		this.products = products;
	}

	public void sort() {
		// you need to write this;
	}

	public String getInventoryName() { return this.name; }
	public double getRestockRate() { return this.rate; }

	// calculate entire inventory value
	public double getInventoryValue() {
		double total = 0;
		for (int i = 0; i < this.products.length; i++) {
			total += this.products[i].getStockPrice();
		}
		return total;
	}

	// calculate the restocking cost
	public double getRestockValue() {
		// you need to write this;
		return 0.0;
	}


	public void printProductList() {
		for (int i = 0; i < this.products.length; i++) {
			TransformersProduct item = this.products[i];
			out.printf( "\n%5d%12d%19s%17d $%10.2f $%10.2f $%10.2f $%10.2f\n",
				i, item.product, item.name, item.unit, item.price, item.getStockPrice(), item.getRestockPrice(this.rate)
				);
		}
	}
}


I'd love to write more, but I don't want to take away any challenge ;) Hope this will be enough to point you in the direction I see, or at least help you in finding your own.

Good Luck.
Was This Post Helpful? 0
  • +
  • -

#5 ITXavier25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 27-October 07

Re: Sorting Arrays & Subclasses

Posted 29 October 2007 - 03:42 PM

Thanks a bunch. I love the examples, the more the better. I will take a look at your code once I get home from work (I'm currently stealing the Internet from the company). They stopped me from using IE; however, I was able to install Firefox via my USB (before the IT guy disabled the ports). I copied the Firefox files into multiple directories in the server, made them have all different name, and made them hidden files. If the IT guy catches one....he will have to catch the others a different way. Since the files are not on my hard files, my computer is clean and there's no cookies either since Firefox clears them automatically. :)

Any advice on how to keep my Internet going at my job or ways to prevent me from being caught would be nice too! :)

Since my college is directly online, I need to have access as much as possible!
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5908
  • View blog
  • Posts: 12,814
  • Joined: 16-October 07

Re: Sorting Arrays & Subclasses

Posted 29 October 2007 - 05:17 PM

View PostITXavier25, on 29 Oct, 2007 - 06:42 PM, said:

They stopped me from using IE; however, I was able to install Firefox via my USB (before the IT guy disabled the ports)...
Any advice on how to keep my Internet going at my job or ways to prevent me from being caught would be nice too! :)


LOL, interesting. Ok, off topic then. ;)

If firefox works and IE doesn't, sounds like you're on a Windows Domain and they're pushing policies at you. It also sounds like they're not proxying traffic, but just locking clients.

I'd take a look at the current network config on the box and try a few traceroutes out to internet sites. You want to know the gateway and the subnet that currently works. If it's a Windows shop then you're in luck, Windows sysadmins are weak on the seven network layers. Microsoft only tends to teach the first four from the application down.

Chances are, that gateway is open from the inside and the switch will let you out if you can get there. Armed with your previous recon, try booting up on a Knoppix CD and see if you can get there. If that works, you can get a copy of DSL and run it inside windows, from a USB, using QEMU.

If you can get to that gateway from a different subnet, one the current admins don't use, chances are they won't see you.

Ultimately, you can't hide your traffic, only what it is with a proxy. They can see you if they know what they're doing. You're passing through their hardware.

Enjoy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1