Click to See Complete Forum and Search --> : Java Help


stephendudra
11-05-2003, 08:56 PM
I am a Network administrator that is enrolled in the University of Phoenix IT program. My current class is Java programming. Needless to say, I suck at this. I can follow along in the book, but cannot produce what the homework assignment is asking me to.

I am not asking for anyone to do my homework, I am merly asking for a push in the right direction.

basically, it should contain the cost. Use 2 classes. first class contains the item data and methods to get and set the item name, quantitiy and price. 2 creats objects for the items and uses the objects to call the set and get methods.

I am doing the directive study class and this is a lot more difficult than the classroom environment.

If someone could offer some advice to me, please email me at stephendudra@yahoo.com.

Thanks,

Stephen

Khalid Ali
11-05-2003, 10:51 PM
Although your question is not pertaining to JavaScript, I can help you with your assign.....have you written any code at all?? if yes show me that and I'll explain/guide next steps for you

and I will move this post to general forum as well.

stephendudra
11-06-2003, 07:01 AM
Would you like me to do this by email or through here???

Please let me know. My email at work is Stephen.dudra@wosupply.com.

To answer your question, I have written very little code. I can forward that to you.

Stephen

Khalid Ali
11-06-2003, 07:22 AM
me writiing all the code for you might not be a very good idea,since you want to learn.So post your code here and ask me a specific question about any part of the code that you have problem understanding it, and we'll go from there.

stephendudra
11-06-2003, 07:40 AM
public class Invoice

{

// Inv
public String lineItem1Description;
public double lineItem1Cost;

public String lineItem2Description;
public double lineItem2Cost;


}
}

This is what I got so far. I have other things written, but they are on my laptop at home. Actually, the one that I have at home is much better than this.


This is what it is asking for....

1st class "Inv" contain - Item data, methods to get and set the item name, quantity and price.

2nd class - creates objects for items, uses objects to call the set and get methods.

If I had an example to go from that has these things in it, it would be easy to see and understand what is being asked.

Suggestion???

Khalid Ali
11-06-2003, 08:34 AM
Hunmm..Here is the inventory class's description.

we want this class to store the items name as well as its quantity or any other values there may be.

First we need to decide about the name of the class(which in this case is "Inv").
Make sure that class name and the file name on disk is same

1. declare the class
public class Inv{
//declare the variables that will store the data
public String itemDescription;
public double itemCost;

//now create constructor for this class to set the above values.Constructors are always of the same name as a class itself.We will pass 2 parameters to it,one for description and one for cost

public Inv(String desc, double cost){
//set the values
this.itemDescription=desc;
this.itemCost=cost;
//now you can use getter methods to retrieve these values.
}

//alternatively you can set setter methods instead of using a constructor.

public void setItemDescription(String desc){
this.itemDescription=desc;
}

public void setItemCost(double cost){
this.itemCost=cost;
}

//see both the setter methods and the constructor are essentially performing the same task in a different way.Once above is done you can use getters to get data out for this object
public String getItemDescription(){
return this.itemDescription;
}

public double getItemCost(){
return this.itemCost;
}
}

and this is how you may use the above class to set items

first instantiate an instance of this class

Inv invItem1 = new Inv();
//you can use above declaration to set values for items by using setter methods.

invItem1.setItemDescription("Irish Creme Coffee");
invItem1.setItemCost(25.99);

or you can use the constructor to avoid this much code
Inv invItem1 = new Inv("Irish Creme Coffee",25.99);

either way it will work(usually chose the design where you have to write the least amount of code.

now you will need to get these values
just use the object reference which in this case is
invItem1
to get values
System.out.printl("Description = "+invItem1.getItemDescription()+"\nPrice = "+invItem1.getItemCost());
I am hopping there are not many mistakes,since the above is not tested,but it will show you how this may work.Make sure to remove all the comment text.

stephendudra
11-06-2003, 08:53 AM
//declare the variables that will store the data
public String itemDescription;
public double itemCost;


**Why does the first line have "String" and the second line have "double"?

What if you had a thrid line? What would you put on there then????

Stephen

Khalid Ali
11-06-2003, 09:04 AM
The first line is a type of String,because (obviodly) you don't want product name to be of type integer or float,it should be string so you can use as many characters.
Second line is double because presumabley a price will be of a type double or float something like
price = 18.99

it will be pretty strange to set a price like this

price="eighteen dollars and ninety nine cents"

dont ya think..:D

and yes you can add as many variables as you want,these varaibles in Object Oriented Designs are called properties of this class.there may be cost,description,category,width,height and so on...

stephendudra
11-06-2003, 09:47 AM
. declare the class
public class Inv{
//declare the variables that will store the data
public String itemDescription;
public double itemCost;


//Would it be ok to add the following?
public double itemQuantity:
public double itemName;


Please see above for additions.

Khalid Ali
11-06-2003, 10:11 AM
yes it all ok to add those or replace the ones I posted in my example with the one you mentioned.just make sure that if you replace these with the ones I have,you will need to make this change in all intances wher ethey are used.
And if you want to add them in addition to the ones I have,make sure that you are setting their values just as I did in the example

stephendudra
11-06-2003, 03:25 PM
//Filename Invoice.java
//Written by Stephen L. Dudra
//Written on 11/03/03


public class Invoice

//declare the variables that will store the data

{
public String itemDescription;
public double itemPrice;
public double itemName;
public double itemQuantity;

}

public Inv(String desc, double price, double name, double quantity)

{
//set the values

this.itemDescription=desc;
this.itemPrice=price;
this.itemName=name;
this.itemQuantity;
}

public void setItemDescription(String desc)

{
this.itemDescription=desc;


public void setItemPrice(double price)


this.itemPrice=price;


public void setItemName(double name)

this.itemName=name;

public void setItemQuantity(double quantity)

this.itemQuantity=quantity;
}

public String getItemDescription()

{
return this.itemDescription;


public double getItemPrice()


return this.itemPrice;



public double getItemName()

return this.itemName;


public double getItemQuantity()


return this.itemQuantity;
}

}


The above is what I got so far. I am getting 3 errors.
One is pointing to the I in Public Inv at the top of the program. Saying a class what expected.

The others are pointing to the } bracket.

Any suggestions???

Khalid Ali
11-06-2003, 03:40 PM
Originally posted by stephendudra
//Filename Invoice.java
//Written by Stephen L. Dudra
//Written on 11/03/03


public class Invoice

//declare the variables that will store the data

{
public String itemDescription;
public double itemPrice;
public double itemName;
public double itemQuantity;

}

text in the bold below should be name of your class which in here is Invoice

public Inv(String desc, double price, double name, double quantity)

{
//set the values

this.itemDescription=desc;
this.itemPrice=price;
this.itemName=name;
this.itemQuantity;
}

public void setItemDescription(String desc){
this.itemDescription=desc;
missing a bracket here

public void setItemPrice(double price) missing a bracket here
this.itemPrice=price;
missing a bracket here

public void setItemName(double name) missing a bracket here
this.itemName=name;
missing a bracket here

public void setItemQuantity(double quantity) missing a bracket here
this.itemQuantity=quantity;
}

public String getItemDescription(){
return this.itemDescription;
missing a bracket here

public double getItemPrice() missing a bracket here
return this.itemPrice;
missing a bracket here

public double getItemName() missing a bracket here
return this.itemName;
missing a bracket here

public double getItemQuantity() missing a bracket here
return this.itemQuantity;
}

}


There may be some other erros but lets fix those and see

stephendudra
11-06-2003, 03:56 PM
//Filename Invoice.java
//Written by Stephen L. Dudra
//Written on 11/03/03


public class Invoice

//declare the variables that will store the data

{
public String itemDescription;
public double itemPrice;
public double itemName;
public double itemQuantity;

}

public Invoice(String desc, double price, double name, double quantity)

{
//set the values

this.itemDescription=desc;
this.itemPrice=price;
this.itemName=name;
this.itemQuantity;
}
public void setItemDescription(String desc){
this.itemDescription=desc;

}
public void setItemPrice(double price){

this.itemPrice=price;

}
public void setItemName(double name){

this.itemName=name;
}
public void setItemQuantity(double quantity){

this.itemQuantity=quantity;
}

public String getItemDescription(){

return this.itemDescription;

}
public double getItemPrice(){

return this.itemPrice;

}

public double getItemName(){

return this.itemName;

}
public double getItemQuantity(){


return this.itemQuantity;
}

}

This is what I have above. Still get the class error (Invoice at the top) and bracket messages still.

Khalid Ali
11-06-2003, 04:13 PM
public double itemName;
typpe for this should be string

public String itemName;

and make changes in all methods where its the paramter and update the return types in getter methods

stephendudra
11-09-2003, 05:23 PM
This is the response I got from my instructor. I have a little bit of understanding of what he is saying to me. Could you help clarify it a little better for me????

If I can find a way to put notes in my code. Such as:

//first class. Defines Methods.

code here
code here

//second class. Manipulation of methods.

code here
code here


What I dont understand is the term that he wrote below, "create an instance for the first class."

What does that mean???

Thanks for your help!


Instructor wrote:

"You have major problems in organizing your methods. Some of them are defined inside other ones. Please pay attention to the bracies. First, create a class that defines all your methods. No method in this class. Then create another class with a main method. In the second class, create an instance for the first class. Now, you can manipulate all methods that are described in the first class."

Khalid Ali
11-09-2003, 07:23 PM
don't take it personally,but had yo seen my post above I have outlined where you needed the braces and stuff...

by "instantiating an instance of a class" means this

suppose you have a class Invoice..then you have another class where you test stuf in that class you use "new" keyword to create an instance or piece of a class

Invoice invoice = new Invoice();

in the above example,its pretty much self explanatory.

create a variable of type
Invoice by this text

Invoice invoice

and then you get the referance of the class by using an assignement operator ( = ).

at this point you have access to all of the public methods in the Invoice class as well its public or static properties(variables)

stephendudra
11-09-2003, 07:37 PM
If you look at what I had typed, you would have seen that I added the brackets where you indicated.

In all honesty, this is not helping me at all. I dont know why this is so difficult to pick up.

Khalid Ali
11-09-2003, 08:14 PM
ok take a look at the code below,I even went as far as testing it,it compiles correctly...and take a look at the structure,
Java does not allow errors that someother loosely typed languages do(such as scripting language javascript).

and there were some brackets still missing a one at wrong place..


//Filename Invoice.java
//Written by Stephen L. Dudra
//Written on 11/03/03


public class Invoice{//bracket was required here

//declare the variables that will store the data
//no bracket required here
public String itemDescription;
public double itemPrice;
public double itemName;
public double itemQuantity;
//and no bracket required here
public Invoice(String desc, double price, double name, double quantity){
//set the values
this.itemDescription=desc;
this.itemPrice=price;
this.itemName=name;
this.itemQuantity = quantity;//missed assignment
}

public void setItemDescription(String desc){
this.itemDescription=desc;
}

public void setItemPrice(double price){
this.itemPrice=price;
}

public void setItemName(double name){
this.itemName=name;
}

public void setItemQuantity(double quantity){
this.itemQuantity=quantity;
}

public String getItemDescription(){
return this.itemDescription;
}

public double getItemPrice(){
return this.itemPrice;
}

public double getItemName(){
return this.itemName;
}

public double getItemQuantity(){
return this.itemQuantity;
}
}

stephendudra
11-09-2003, 08:53 PM
Do you have to put these on seperate brackets????

public String getItemDescription(){
return this.itemDescription;
}

public double getItemPrice(){
return this.itemPrice;
}

public double getItemName(){
return this.itemName;
}

public double getItemQuantity(){
return this.itemQuantity;
}

Khalid Ali
11-09-2003, 09:22 PM
yes each method starts with
{
and ends with a
}

bracket.And so does a class...

look at teh code above ..its a complete working example....

PeOfEo
11-09-2003, 09:27 PM
kalid, want to help me master my constructors and returns? :D

Khalid Ali
11-09-2003, 09:57 PM
lol...sure....what can I do to help?

PeOfEo
11-09-2003, 10:08 PM
Actually I would know them by now If I would just do my freaken homework in that class lol. If I read that chapter in the book I would have it down :o. But thats way too much like work. I would enjoy java if I were not forced to do it. This is why I think I would never be able to work for a large sortware firm, I do not want to get stuck making the next version of ms word :rolleyes: It looses its fun at that point.

stephendudra
11-10-2003, 08:12 AM
Well I took your advice. It compiled correctly, but will not run.

I get the following message.

Exception in thread "main" java.lang.NoSuchMethodError: Main

What next?????:confused:

Khalid Ali
11-10-2003, 08:43 AM
First thing that comes to mind is,you will always need a class that has a main method in it to start it up.

The invoice class does not have a amin method in it.

Now you need to create another class with a main method and use invoice inthat class (this is assuming you have your JDK setup properly)