Learning Java

Trying to write a class Item with the following, im at a loss and having issues with my compiler, request som guidance on structure and format?

Fields:
Item name (IrishSpring)
Item type (BodySoap)
Price (Example: 2.25)
Quantity (3)

Constructors:
No-arg constructor
3-arg constructor (Default quantity 1)
4-arg constructor

Methods:

getInvoice()  Computes the total cost. Does not print anything.
Display()  displays all the values of the item. Calls getInvoice() method

Write class MedicineItem that extends Item:

Fields:

Patients name
Doctors name
Healthcare name
Patients age

Methods:

Override methods

Write class NurseryItem that extends Item:

Fields:

Plant family name
Sun requirement (full sun, shade, etc)
Warranty

Methods:

Override methods

Write a main class to create one object of each class and print the values. I must
use proper access modifiers (public/private/protected).
USN4x4Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
What do you have code-wise so far and where exactly are you stuck?
0
Kevin CrossChief Technology OfficerCommented:
I can't just write all the code for you, but can help you through issues you are having with your own code assisting you to get it to compile.
0
USN4x4Author Commented:
Here is my code thus far, confused to say the least
public class ClassItem {
	
{
public static void main(String[] args)
{
Item name IrishSpring;
Item type BodySoap;
Price 2.25;
Quantity 3;
}
}
class MedicineItem
{
private Patient's name;
private Doctor's name;
private Healthcare name;
private Patient's age;
}
}
class NurseryItem
{
 
protected Plant family name;
protected Sun requirement;
protected Warranty;
}
}
    

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Kevin CrossChief Technology OfficerCommented:
Here, start with the java tutorials:
http://java.sun.com/docs/books/tutorial/

I would start with base class "Item"

Fields:
Item name (IrishSpring)
Item type (BodySoap)
Price (Example: 2.25)
Quantity (3)

Constructors:
No-arg constructor
3-arg constructor (Default quantity 1)
4-arg constructor

Methods:

getInvoice()  Computes the total cost. Does not print anything.
Display()  displays all the values of the item. Calls getInvoice() method

Store each class in a file named for the class, so Item.java in this case.

The values in the paranthesis above are example values that you don't necessary need to use in code for definition of Item class.  You would probably want to set those as the values in your test class that will have the main method described in last step of assignment.
public class Item {
	String item_name;
	String item_type;
	double price;
	int quantity;
	
	public Item() {
		// set values to some defaults
		// can use the values given as examples or blanks/zeros
	}
	
	// 3-arg constructor
	
	// 4-arg constructor
	
	// getInvoice() code here
	
	// Display() code here
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kevin CrossChief Technology OfficerCommented:
When you do go to set values, they should be like this:

price = 2.25;

Price 2.25 won't compile.
0
Kevin CrossChief Technology OfficerCommented:
To create the child classes, you use the extends keyword like the instructions elude to:

public class NuseryItem extends Item

Once you have built the three different classes, you just need to build a main class something similar to this:
public class ItemTester {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Item i = new Item();
	}
 
}

Open in new window

0
Kevin CrossChief Technology OfficerCommented:
This is how an overrided method would look:
@Override
public double getInvoice() {
	double baseGetInvoiceValue = super.getInvoice();
	// TODO: do something different to value
	return baseGetInvoiceValue;
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.