0% found this document useful (0 votes)
12 views

Write Java Code For Class Produce That Meets The Listed Requirements

. Write Java code for class Produce that meets the listed requirements

Uploaded by

Adel Hassan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Write Java Code For Class Produce That Meets The Listed Requirements

. Write Java code for class Produce that meets the listed requirements

Uploaded by

Adel Hassan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Programming Assignment 01

Review of Classes, Objects, Arrays, and Enhance for Looping


Given: UML Diagram of class Produce
Test Harness code: ProcessProduce.java
These instructions
Tasks:
1. Write Java code for class Produce that meets the listed requirements
2. Complete code for the ProcessProduce.java test harness as instructed below
Requirements:
Class Produce
1. Code Java class Produce according to the UML provided and the instructions included here.
2. The class Produce will require two constructors
a. The default constructor (accepting no parameters) will instantiate a Produce object with the
following values:
prodName = Unspecified
prodPrice = 0.0
prodInv = 0.0
b. The second constructor will accept a String object and two double type parameters and
instantiate a Produce object based on those parameters:
c. Constructors will call the appropriate “set” methods to assign values to the instance variables.
NOTE: Direct assignment of instance variable values effectively invalidates the constructor, i.e.,
the student will score a zero for that portion of the assignment.
3. “Set” methods will be written for all instance variables
a. Each “set” method will accept the appropriate data type value and assign it to the appropriate
instance variable
b. “Set” methods will not return any value
c. “Set” methods will not contain any code other than that required to set a value for the given
instance variable
4. “Get” methods will be written for all instance variables
a. Each “get” method will have a return type appropriate to the data type of the instance variable
b. “Get” methods do not accept any input parameters or values
c. “Get” methods will not contain any code other than that required to return the value of the
given instance variable
5. Method toString
a. Will return a String object containing all object instance variable values and formatted as
follows:
%f pounds of %s at a price of %f per pound/n
where the first “%f” represents the inventory in pounds, the “%s” represents the name of the
produce type, and the second “%f” represents the price per pound for the produce type
b. The toString method will call other class methods to retrieve or derive the needed values.
NOTE: Direct reference to instance variable values effectively invalidates the constructor, i.e.,
the student will score a zero for that portion of the assignment.
c. The toString method will use method format to format the returned String object
6. There must be no unneeded (not related to one of the requirements) code.
7. As always, all guidelines in the Basic Coding Standards will be followed. Please note that this
specifically includes required code comments.
ProcessProduce Java Code
The basic code provided to you will allow you to test your Produce class. You will add code to
complete the following functions:
1. Each instantiated Produce object will be stored in an array. To accomplish this, you will need
to do the following:
a. Declare an Array structure, one-dimensional, to hold the instantiated objects.
b. Set the size of the array to contain 5 elements.
c. Add each new Produce object to the array as it is instantiated. (Hint: this can be accomplished
in a single statement that instantiates the object and assigns the new object to a position in the
array structure.)
d. You will instantiate 5 Produce objects and insert them into the array structure.
e. NOTE: It is not necessary to prompt a user for input – you may simply include the test data in
the object instantiation statements
2. Once all Produce objects have been instantiated and inserted into the Array structure, use an
enhanced for loop structure to display the toString results for each object to the screen. NOTE:
Failure to use the enhanced for loop here will result in losing all points for this portion of the
assignment.
3. There must be no unneeded (not related to one of the requirements) code.
4. As always, all guidelines in the Basic Coding Standards will be followed. Please note that this
specifically includes required code comments.
Testing Your Code
Test your code carefully.
Submitting Your Work
Submit only your Java code for class Produce and the modified code for ProcessProduce.java.
Do not submit zipped files. Submissions are only accepted through Blackboard; do not email or
provide hard copy – these will not be accepted. Late submissions are not accepted and will earn
an assignment grade of zero.

Answer

public class Produce


{

private String prodName ;


private double prodPrice ;
private double prodInv ;

public Produce() {

setprodName (null);
setprodPrice(0.0);
setprodInv (0.0);
}

public Produce(String prodName,double


prodPrice,double prodInv) {

setprodName (prodName);
setprodPrice(prodPrice);
setprodInv (prodInv);
}
// setprodName will have a set the value to prodName.

public void setprodName(String prodName) {

this.prodName = prodName;

// setprodPrice will have a set the value to prodPrice

public void setprodPrice(double prodPrice) {

this.prodPrice = prodPrice;

// setprodInv will have a set the value to prodInv


public void setprodInv(double prodInv) {

this.prodInv = prodInv;

// getprodName will have a return the value of


prodName

public String getprodName() {

return prodName;

// getprodPrice will have a return the value of prodPrice

public double getprodPrice() {


return prodPrice;

// getprodInv will have a return the value of prodInv

public double getprodInv() {

return prodInv;

//tostring method Will return a String object containing


all object instance variable values

public String toString() {


return String.format("inventory %f, produce %s,
price %f\n", getprodInv() ,getprodName() ,
getprodPrice()) ;
}

} // end class ProcessProd

/**
* @(#)ProcessProduce.java
*
*
* @Terri Davis
* @version 1.00 2014/05/12
*
* This code will exercise class Produce
*
*/
public class ProcessProduce
{

public static void main( String[] args )


{

String[] prodName = {"Bok Choy", "Choy


Bok","Bhoy Cok","YhoB Zok","John smith"};
double[] prodPrice =
{3.59,12.3,82.47,69.17,16.46};
double[] prodInv =
{100.5,101.3,109.03,91.34,71.09};

for(int i =0 ; i< 5 ; i++){


Produce myVeggie = new Produce( prodName[i],
prodPrice[i], prodInv[i]);
System.out.println( myVeggie.toString( ) );

} // end main

} // end class ProcessProd

You might also like