User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 455,967 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,741 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1672 | Replies: 20
Reply
Join Date: Nov 2007
Posts: 3
Reputation: summersa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
summersa summersa is offline Offline
Newbie Poster

New to Java Programming

  #1  
Nov 21st, 2007
\**
* InventoryProgramPart3.java
* @ author Amy Summers
* Inventory Program which uses an array to store items and creates
* a subclass of the CD class
*
*/
import java.util.Arrays;//program uses Arrays

class InventoryPart3
{
	//main method begins execution of Java program
	public static void main (String[] args)
	{	
		
		double total = 0.0;
		
		CD inventory [ ] = new CD [8];//allocates 8 integers
		
		//create object
		inventory[0] = new CD("Fantasia","Fantasia",00012, 10, 13.00);
		inventory[1] = new CD("Tamia","Tamia", 00015, 10, 15.00);
		inventory[2] = new CD("Love","Alicia Keys", 00016, 10, 16.00);
		inventory[3] = new CD("Always","Keisha Cole", 00025, 10, 15.00);
		inventory[4] = new CD("Al","Al Green", 00022, 10, 12.00);
		inventory[5] = new CD("Smile","Marvin Gaye", 00026, 10, 13.00);
		inventory[6] = new CD("Jump","DJ Quick", 00036, 10, 13.00);
		inventory[7] = new CD("Graduation","Kanye West", 00017, 10, 16.00);
		


	for (int counter = 0; counter < 8; counter++)
      {
 
         System.out.printf(inventory[counter].toString());
 
      } // end for
      
       System.out.printf( "The total value of the inventory is $%.2f\n\n", total );

				
	}//end main
}// end class InventoryPart3


/**
* CD class that uses one additional unique feature of the CD
* Artist is the additional feature
*/

class CD
{
	 String NameIn;
	 String ArtistIn;
	 double  NumberIn;
	 double UnitsIn;//number of units in stock
	 double PriceIn;//price of each unit
	 double ValueIn;//value of inventory
	
	//five-argument constructor initializes CD
	public CD( String NameIn, String ArtistIn, double NumberIn, double UnitsIn, double PriceIn )
             {
		//implicit call to Object constructor occurs here
			String product = NameIn;// initialize name
			String Artist = ArtistIn;//initialize name
			double number = NumberIn;// initialize number
			double units = UnitsIn;// initialize units
			double price = PriceIn;// initialize price
	 }//end five-argument constructor

	// set product name
	public void setNameIn (String product)
	{
	  product = NameIn;//store product name
	}//end method setNameIn

	//return product name
	public String getNameIn()
	{
	  return NameIn;
	}//end method getNameIn

	//set artist name
	public void setArtistIn (String artist)
	{
	artist = ArtistIn;//store artist name
	}//end method setArtistIn
	
	//return artist name
	public String getArtistIn()
	{
	return ArtistIn;
	}//end method getArtistIn

	
	// set number
	public void setNumberIn (double number)
	{

	  number = (NumberIn > 0.0) ? 0.0: number;//store number
	}//end method setNumberIn

	//return number
	public double getNumberIn()
	{
	  return NumberIn;
	}//end method getNumberIn

	//set units
	public void setUnitsIn ( double units)
	{
	  units = (UnitsIn > 0.0) ? 0.0: units;//store units
	}//end method setUnitsIn

	//return units
	public double getUnitsIn ()
	{
	return UnitsIn;
	}//end method getUnitsIn

	//set price
	public void setPriceIn ( double price) 
	{
	 price = (PriceIn > 0.0) ? 0.0: price;//store price
	}//end method setPriceIn

	//return price
	public double getPriceIn()
	{
	  return PriceIn;
	}//end method getPriceIn
	
	//getValue
	public double getValue()
	{
	 	return (UnitsIn * PriceIn);
	}//end getValue
	
	
	  // method getInventoryValue
   	public static double getInventoryValue ( CD inventory3[] )
   {
      double total = 0.0;
      total = (inventory3[ 0 ].getValue() +
               inventory3[ 1 ].getValue() +
               inventory3[ 2 ].getValue() +
               inventory3[ 3 ].getValue() +
               inventory3[ 4 ].getValue());
 
      System.out.printf( "The total value of this entire inventory is: $%.2f\n\n", total );
      return ( total );
 
   } // End method getInventoryValue
    
     
   //toString 
   public String toString ()       
   {
      String formatString    = "Name: %s\n";
      formatString          += "Artist: %s\n";
      formatString          += "Number: %d\n";
      formatString          += "Units: $%.d\n";
      formatString	   		+= "Price:$%.2f\n";
      formatString          += "Value: $%.2f\n\n";
      
      return (String.format ( formatString, NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn, getValue()) ); 
         
   } // End toString method
    
}//End class CD


/**
 * The Artist is a subclass of CD. A method is created
 * to calculate the value of the inventory and a
 * 5% restocking fee is added, also. 
*/

class Artist extends CD {
    
	private double BasePriceIn;
   	private double BaseInventoryValue;
    private double RestockFee;
    
    {
      // variables
      RestockFee = 0.05;
    }
 	

		//five-argument constructor
        public Artist(String NameIn, String ArtistIn, double NumberIn, double UnitsIn, double PriceIn)
        {	
        // explicit call to superclass CD constructor
		super( NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn );	
        	
        
      	 } // End five-argument constructor
 
   		// set price
   		public void setBasePriceIn( double getPriceIn)
   		{
      	BasePriceIn = ( getPriceIn < 0.0 ) ? 0.0 : getPriceIn;
   		} // end method setPriceIn
 
  		 // return  price
   		public double getBasePriceIn()
   		{
      	return BasePriceIn;
   		} // end getBasePriceIn
 
   		// calculate, return price, and the restocking fee
   		public double getBasePriceInPlusFee()
   		{
      	return  BasePriceIn + (BasePriceIn * RestockFee);
   		} // End getBasePriceInPlusFee
   
      	// set base inventory value
   		public void setBaseInventoryValue( double getInventoryValue)
  		 {
      	BaseInventoryValue = ( getInventoryValue < 0.0 ) ? 0.0 : getInventoryValue;
   		} // end setInventoryValue
 
   		// return base inventory value
   		public double getBaseInventoryValue()
   		{	
      	return BaseInventoryValue;
   		} // end getBaseInventoryValue
 
   // calculate, return value, and the restocking fee
   public double getValuePlusFee()
   {
      return  BaseInventoryValue + (BaseInventoryValue * RestockFee);
   }
  //Stringto
  public String toString(){
      String formatString    = "Name: %s\n";
      formatString          += "Artist: %s\n";
      formatString          += "Number: %d\n";
      formatString          += "Units: $%.d\n";
      formatString	    	+= "Price:$%.2f\n";
      formatString          += "Value : $%.2f\n\n";
      formatString          += "Value with Restock Fee: $%.2f\n\n";
   
   
    return (String.format ( formatString, NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn, getBasePriceInPlusFee(), getValuePlusFee()) ); 
   } // End toString 
	   
}//end class Artist extends CD



Please forgive me if I did not code my program correctly. If you could let me know if I did it right or not I would greatly appreciate it.

I am able to complie my program, but I get run errors that state the following:

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '.'
at java.util.Formatter.checkText(Formatter.java:2502)
at java.util.Formatter.parse(Formatter.java:2466)
at java.util.Formatter.format(Formatter.java:2413)
at java.util.Formatter.format(Formatter.java:2366)
at java.lang.String.format(String.java:2770)
at CD.toString(InventoryPart3.java:166)
at InventoryPart3.main(InventoryPart3.java:35)


I know I am doing something strangely wrong, but can not figure it out.
Last edited by cscgal : Nov 23rd, 2007 at 2:29 pm. Reason: Fixed code tags.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 5
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: New to Java Programming

  #2  
Nov 21st, 2007
This is your first post, but now onwards, use code tags to display your code in the posts.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote  
Join Date: Nov 2007
Posts: 3
Reputation: summersa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
summersa summersa is offline Offline
Newbie Poster

Re: New to Java Programming

  #3  
Nov 21st, 2007
\**
* InventoryProgramPart3.java
* @ author Amy Summers
* Inventory Program which uses an array to store items and creates
* a subclass of the CD class
*
*/
import java.util.Arrays;//program uses Arrays

class InventoryPart3
{
	//main method begins execution of Java program
	public static void main (String[] args)
	{	
		
		double total = 0.0;
		
		CD inventory [ ] = new CD [8];//allocates 8 integers
		
		//create object
		inventory[0] = new CD("Fantasia","Fantasia",00012, 10, 13.00);
		inventory[1] = new CD("Tamia","Tamia", 00015, 10, 15.00);
		inventory[2] = new CD("Love","Alicia Keys", 00016, 10, 16.00);
		inventory[3] = new CD("Always","Keisha Cole", 00025, 10, 15.00);
		inventory[4] = new CD("Al","Al Green", 00022, 10, 12.00);
		inventory[5] = new CD("Smile","Marvin Gaye", 00026, 10, 13.00);
		inventory[6] = new CD("Jump","DJ Quick", 00036, 10, 13.00);
		inventory[7] = new CD("Graduation","Kanye West", 00017, 10, 16.00);
		


	for (int counter = 0; counter < 8; counter++)
      {
 
         System.out.printf(inventory[counter].toString());
 
      } // end for
      
       System.out.printf( "The total value of the inventory is $%.2f\n\n", total );

				
	}//end main
}// end class InventoryPart3


/**
* CD class that uses one additional unique feature of the CD
* Artist is the additional feature
*/

class CD
{
	 String NameIn;
	 String ArtistIn;
	 double  NumberIn;
	 double UnitsIn;//number of units in stock
	 double PriceIn;//price of each unit
	 double ValueIn;//value of inventory
	
	//five-argument constructor initializes CD
	public CD( String NameIn, String ArtistIn, double NumberIn, double UnitsIn, double PriceIn )
             {
		//implicit call to Object constructor occurs here
			String product = NameIn;// initialize name
			String Artist = ArtistIn;//initialize name
			double number = NumberIn;// initialize number
			double units = UnitsIn;// initialize units
			double price = PriceIn;// initialize price
	 }//end five-argument constructor

	// set product name
	public void setNameIn (String product)
	{
	  product = NameIn;//store product name
	}//end method setNameIn

	//return product name
	public String getNameIn()
	{
	  return NameIn;
	}//end method getNameIn

	//set artist name
	public void setArtistIn (String artist)
	{
	artist = ArtistIn;//store artist name
	}//end method setArtistIn
	
	//return artist name
	public String getArtistIn()
	{
	return ArtistIn;
	}//end method getArtistIn

	
	// set number
	public void setNumberIn (double number)
	{

	  number = (NumberIn > 0.0) ? 0.0: number;//store number
	}//end method setNumberIn

	//return number
	public double getNumberIn()
	{
	  return NumberIn;
	}//end method getNumberIn

	//set units
	public void setUnitsIn ( double units)
	{
	  units = (UnitsIn > 0.0) ? 0.0: units;//store units
	}//end method setUnitsIn

	//return units
	public double getUnitsIn ()
	{
	return UnitsIn;
	}//end method getUnitsIn

	//set price
	public void setPriceIn ( double price) 
	{
	 price = (PriceIn > 0.0) ? 0.0: price;//store price
	}//end method setPriceIn

	//return price
	public double getPriceIn()
	{
	  return PriceIn;
	}//end method getPriceIn
	
	//getValue
	public double getValue()
	{
	 	return (UnitsIn * PriceIn);
	}//end getValue
	
	
	  // method getInventoryValue
   	public static double getInventoryValue ( CD inventory3[] )
   {
      double total = 0.0;
      total = (inventory3[ 0 ].getValue() +
               inventory3[ 1 ].getValue() +
               inventory3[ 2 ].getValue() +
               inventory3[ 3 ].getValue() +
               inventory3[ 4 ].getValue());
 
      System.out.printf( "The total value of this entire inventory is: $%.2f\n\n", total );
      return ( total );
 
   } // End method getInventoryValue
    
     
   //toString 
   public String toString ()       
   {
      String formatString    = "Name: %s\n";
      formatString          += "Artist: %s\n";
      formatString          += "Number: %d\n";
      formatString          += "Units: $%.d\n";
      formatString	   		+= "Price:$%.2f\n";
      formatString          += "Value: $%.2f\n\n";
      
      return (String.format ( formatString, NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn, getValue()) ); 
         
   } // End toString method
    
}//End class CD


/**
 * The Artist is a subclass of CD. A method is created
 * to calculate the value of the inventory and a
 * 5% restocking fee is added, also. 
*/

class Artist extends CD {
    
	private double BasePriceIn;
   	private double BaseInventoryValue;
    private double RestockFee;
    
    {
      // variables
      RestockFee = 0.05;
    }
 	

		//five-argument constructor
        public Artist(String NameIn, String ArtistIn, double NumberIn, double UnitsIn, double PriceIn)
        {	
        // explicit call to superclass CD constructor
		super( NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn );	
        	
        
      	 } // End five-argument constructor
 
   		// set price
   		public void setBasePriceIn( double getPriceIn)
   		{
      	BasePriceIn = ( getPriceIn < 0.0 ) ? 0.0 : getPriceIn;
   		} // end method setPriceIn
 
  		 // return  price
   		public double getBasePriceIn()
   		{
      	return BasePriceIn;
   		} // end getBasePriceIn
 
   		// calculate, return price, and the restocking fee
   		public double getBasePriceInPlusFee()
   		{
      	return  BasePriceIn + (BasePriceIn * RestockFee);
   		} // End getBasePriceInPlusFee
   
      	// set base inventory value
   		public void setBaseInventoryValue( double getInventoryValue)
  		 {
      	BaseInventoryValue = ( getInventoryValue < 0.0 ) ? 0.0 : getInventoryValue;
   		} // end setInventoryValue
 
   		// return base inventory value
   		public double getBaseInventoryValue()
   		{	
      	return BaseInventoryValue;
   		} // end getBaseInventoryValue
 
   // calculate, return value, and the restocking fee
   public double getValuePlusFee()
   {
      return  BaseInventoryValue + (BaseInventoryValue * RestockFee);
   }
  //Stringto
  public String toString(){
      String formatString    = "Name: %s\n";
      formatString          += "Artist: %s\n";
      formatString          += "Number: %d\n";
      formatString          += "Units: $%.d\n";
      formatString	    	+= "Price:$%.2f\n";
      formatString          += "Value : $%.2f\n\n";
      formatString          += "Value with Restock Fee: $%.2f\n\n";
   
   
    return (String.format ( formatString, NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn, getBasePriceInPlusFee(), getValuePlusFee()) ); 
   } // End toString 
	   
}//end class Artist extends CD

Is this the correct way?
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 5
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: New to Java Programming

  #4  
Nov 22nd, 2007
>Is this the correct way?

Absolutely. You can see the difference it makes for yourself.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 5
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: New to Java Programming

  #5  
Nov 22nd, 2007
>System.out.printf(inventory[counter].toString());

I do not think there is any function like printf in Java. It should be either print or println.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 5
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: New to Java Programming

  #6  
Nov 22nd, 2007
>System.out.printf( "The total value of the inventory is $%.2f\n\n", total );

Same as above.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 5
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: New to Java Programming

  #7  
Nov 22nd, 2007
>formatString += "Units: $%.d\n";

should be

formatString          += "Units: $%d\n";

I think this will remove the following errors:
at CD.toString(InventoryPart3.java:166)
at InventoryPart3.main(InventoryPart3.java:35)

Try compiling it after the above stated changes.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,625
Reputation: peter_budo is just really nice peter_budo is just really nice peter_budo is just really nice peter_budo is just really nice 
Rep Power: 12
Solved Threads: 311
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: New to Java Programming

  #8  
Nov 22nd, 2007
As error say there is problem with line 166 which is part of your method toString
public String toString ()       
   {
      String formatString    = "Name: %s\n";
      formatString          += "Artist: %s\n";
      formatString          += "Number: %d\n";
      formatString          += "Units: $%.d\n";
      formatString	   		+= "Price:$%.2f\n";
      formatString          += "Value: $%.2f\n\n";
      
      return (String.format ( formatString, NameIn, ArtistIn, NumberIn, UnitsIn, PriceIn, getValue()) ); 
         
   } // End toString method
You calling variables that are return by get methods instead of calling methods to get this variables
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, JAVAWUG (Java Web User Group), Coding the Architecture
Reply With Quote  
Join Date: Feb 2006
Posts: 1,508
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is online now Online
Posting Virtuoso

Re: New to Java Programming

  #9  
Nov 22nd, 2007
Originally Posted by Jishnu View Post
>System.out.printf(inventory[counter].toString());

I do not think there is any function like printf in Java. It should be either print or println.



As of 1.5, yes there is, but it is not used in this manner. You need to pass it an optional locale object, a format pattern string, and an array of objects to match the arguments in the pattern string.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 5
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: New to Java Programming

  #10  
Nov 22nd, 2007
Ok. I was not aware of that. Thank You!
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 9:07 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC