•
•
•
•
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
![]() |
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
\**
* 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 CDPlease 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.
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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
\**
* 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?
>Is this the correct way?
Absolutely. You can see the difference it makes for yourself.
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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
>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.
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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
>System.out.printf( "The total value of the inventory is $%.2f\n\n", total );
Same as above.
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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
>formatString += "Units: $%.d\n";
should be
I think this will remove the following errors:
Try compiling it after the above stated changes.
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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,625
Reputation:
Rep Power: 12
Solved Threads: 311
As error say there is problem with line 166 which is part of your method toString
You calling variables that are return by get methods instead of calling methods to get this variables
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 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
Publilius Syrus
(~100 BC)
LJC - London Java Community, JAVAWUG (Java Web User Group), Coding the Architecture
•
•
•
•
>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
----------------------------------------------
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
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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
api apple asm assembly x86 programming hla demo blogger blogging c c++ ccna cocoa code coding com competition compiler computer debugging developer development errors evaluation gdata google high-performance innovation java linerider mcse microsoft sdk for java microsystems networking news next object online open oriented planning platform practices programming python rss software step steps sun tools tutorials web xml
- Java Programming Query (Java)
- Linux Java IDEs (Java)
- Java Programming (Java)
- java programming language (Java)
- Programming in Linux? (*nix Software)
- Installing Java (Web Browsers)
- Java Expert (Needed) (Java)
Other Threads in the Java Forum
- Previous Thread: Terminating a program with a sentinel
- Next Thread: Math Unicode inputted into Swing JTextField



Linear Mode