Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


interfaces help filter_list
Author
Message
RE: interfaces help #31
@blackeagle:

There are two problems in your toString() method.

The first one is that you can't call a method on a primitive value. Primitives are all types starting with a lowercase letter, like int, double, float. These are no objects, so they have no method. But you try to call the method toString() on an int.

The second problem is using several consecutive return statements. return means: "return that value and leave the method". So your code after the first return is not reachable (will never be executed).

What you really should do is create a single string that contains all of your information. Use the concatentation operator + to build up your string.

Example:

Code:
int price = 10;
String item = "rubics cube";
return "price: " + price + ", item: " + item;

Primitive values like price will be converted to a string automatically.



(11-27-2013, 02:07 AM)darthjames226 Wrote: The toString() returns a string equivalent. For example:

int value = 30;
System.out.println(value.toString());

This code snippet would print out 30. Notice that value will be displayed as a String and not as an integer.

This code snippet is wrong and the problems above actually came from your example. If you provide snippets to programming beginners, make sure they are working and do not create more problems than helping.

(11-27-2013, 03:36 AM)darthjames226 Wrote: Only thing I can see that could be wrong is this:
@Override
public String toString(){
return name;

return purchasePrice.toString(); //calls your override method
return salePrice.toString(): //uses colon instead of semicolon
//calls your override method


}

You used a colon instead of a semicolon on your last statement. Since you did a override on toString(), you are calling your method on the statements inside of your method.

Don't override toString(). Change its name to something else and try to compile. That will let you know if that's the problem.

That is nonsense.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: interfaces help #32
@blackeagle:

There are two problems in your toString() method.

The first one is that you can't call a method on a primitive value. Primitives are all types starting with a lowercase letter, like int, double, float. These are no objects, so they have no method. But you try to call the method toString() on an int.

The second problem is using several consecutive return statements. return means: "return that value and leave the method". So your code after the first return is not reachable (will never be executed).

What you really should do is create a single string that contains all of your information. Use the concatentation operator + to build up your string.

Example:

Code:
int price = 10;
String item = "rubics cube";
return "price: " + price + ", item: " + item;

Primitive values like price will be converted to a string automatically.



(11-27-2013, 02:07 AM)darthjames226 Wrote: The toString() returns a string equivalent. For example:

int value = 30;
System.out.println(value.toString());

This code snippet would print out 30. Notice that value will be displayed as a String and not as an integer.

This code snippet is wrong and the problems above actually came from your example. If you provide snippets to programming beginners, make sure they are working and do not create more problems than helping.

(11-27-2013, 03:36 AM)darthjames226 Wrote: Only thing I can see that could be wrong is this:
@Override
public String toString(){
return name;

return purchasePrice.toString(); //calls your override method
return salePrice.toString(): //uses colon instead of semicolon
//calls your override method


}

You used a colon instead of a semicolon on your last statement. Since you did a override on toString(), you are calling your method on the statements inside of your method.

Don't override toString(). Change its name to something else and try to compile. That will let you know if that's the problem.

That is nonsense.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: interfaces help #33
@Deque thx for making this clear Deque but you didn't answer my last question about interfaces !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: interfaces help #34
@Deque thx for making this clear Deque but you didn't answer my last question about interfaces !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: interfaces help #35
(11-27-2013, 02:33 PM)blackeagle Wrote: @Deque thx for making this clear Deque but you didn't answer my last question about interfaces !!

I didn't because your question appeared after I had submitted my answer.

(11-27-2013, 02:18 PM)blackeagle Wrote: i just have 1 question in my mind for now maybe @Deque could give me a good explanation to this !!
why do i need to make interfaces then inplement them couldnt i just make those methodes inside the class ?

Yes, you could just implement these methods right away, but interfaces will help you to create more flexible code.

I.e. look at the List of Java. A list can have several implementations. If you look at the standard library implementations, it can be an ArrayList or a LinkedList. Both have their strengths and weaknesses, so depending on the situation you might want to use one or prefer the other.

Now imagine further you decide for the ArrayList and you create methods that are working with ArrayLists, take them as parameter and so on.
And then you want decide to use a LinkedList too and all of these methods that take an ArrayList become unusable for this list implementation.

But actually a List is a List and can be handled the same, no matter how the implementation is. So you would rather use the interface as type instead.

I.e. this method can only sort ArrayList:

Code:
public void sort(ArrayList list)

But this can sort both, ArrayList and LinkedList, because they implement the List interface:

Code:
public void sort(List list)

You only need one method for both.


The other advantage is a better communication. If you give someone an interface and tell him to implement it, he knows exactly what methods he has to create.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: interfaces help #36
(11-27-2013, 02:33 PM)blackeagle Wrote: @Deque thx for making this clear Deque but you didn't answer my last question about interfaces !!

I didn't because your question appeared after I had submitted my answer.

(11-27-2013, 02:18 PM)blackeagle Wrote: i just have 1 question in my mind for now maybe @Deque could give me a good explanation to this !!
why do i need to make interfaces then inplement them couldnt i just make those methodes inside the class ?

Yes, you could just implement these methods right away, but interfaces will help you to create more flexible code.

I.e. look at the List of Java. A list can have several implementations. If you look at the standard library implementations, it can be an ArrayList or a LinkedList. Both have their strengths and weaknesses, so depending on the situation you might want to use one or prefer the other.

Now imagine further you decide for the ArrayList and you create methods that are working with ArrayLists, take them as parameter and so on.
And then you want decide to use a LinkedList too and all of these methods that take an ArrayList become unusable for this list implementation.

But actually a List is a List and can be handled the same, no matter how the implementation is. So you would rather use the interface as type instead.

I.e. this method can only sort ArrayList:

Code:
public void sort(ArrayList list)

But this can sort both, ArrayList and LinkedList, because they implement the List interface:

Code:
public void sort(List list)

You only need one method for both.


The other advantage is a better communication. If you give someone an interface and tell him to implement it, he knows exactly what methods he has to create.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: interfaces help #37
@Deque ok thank you very much still have 1 more question on my mind if i have 3 classes is it possible to use the same method name inside the 3 classes !!
i.e:in first class
public void info(){

}
second class
public void info(){
super.info()
and i add something more}

i want to do this but netbeans tell me in the second class that the method already exist and i need to override it!!
if i do so i wont be able to use the first 1 ?
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: interfaces help #38
@Deque ok thank you very much still have 1 more question on my mind if i have 3 classes is it possible to use the same method name inside the 3 classes !!
i.e:in first class
public void info(){

}
second class
public void info(){
super.info()
and i add something more}

i want to do this but netbeans tell me in the second class that the method already exist and i need to override it!!
if i do so i wont be able to use the first 1 ?
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: interfaces help #39
i was able to do this part !! i did everything and i guess everything is fine !!

Spoiler:
Code:
Write an application to manage the stock of a store.

Here is a specification of the program in terms of classes and interfaces:

interfaces
Salable per kilogram : the interface for selling items per kilogram
methods:
Sale : This method receives the quantity sold of the item and change the stock
Salable piece : the interface for items that sell by pieces
methods:
Sale : This method receives the number of rooms sold the item and change the stock
Likely to be sold in balance
methods:
start balance : This method lowers the price of the item by the given percentage
complete the balance : This method increases the price of the item by the given percentage

classes

Class Articles: general class of all items
Properties: number (automatically generated during the creation of an article) , purchase price , sale price and name.
Methods ( other than the manufacturer):
calculating the rate of return ( ( prixVente - prixAchat ) / prixAchat )
toString (): return the name and price of the item
This class does not implement any interface.

Two derived class Articles classes:
Class artciles Appliance
Aditional property : the number of items in stock
Additional methods ( other than the manufacturer):
Add a number of pieces in stock
toString (): return the name , the price of the item and the number of parts
You must implement the corresponding interfaces this class .
Class articles futures
Additional property : quantity in stock
Additional methods:
Add the stock quantity
toString (): return the name , the price of the item and the quantity in stock
You must implement the corresponding interfaces this class , knowing that the items futures can not be sold on sale.

now i have a new part
Spoiler:
Code:
class Store
Properties: a vector (Vector)
Methods (other than the manufacturer):
       Add product to the vector
Save name, purchase price and selling price of all items in the futures vector "Primeurs.data" file properties
Save all items appliances (as instances) of the vector in the "Electro.data" file
Save the information generated by toString all items in the vector file "info.data"
Print file information "info.java"
Read "Primeurs.data" file and print the name and the performance of each primary article.
Read "Electro.data" file and print the number and performance of each article appliances.
can some1 give me and exemple n how to do this ? i know how to save to a file and read from a file i have it in a course thats not my problem but what i did here is :
Code:
import java.util.Vector;

public class Store {
  Vector vecteur = new Vector();
  public Store(){
      vecteur=null;
  }
  public void addArticle(Article article){
      vecteur.addElement(article);
  }
i need to know how should i do this step: Save name, purchase price and selling price of all items in the futures vector "Primeurs.data" file properties

hope any1 can help Sad and i'm sry i feel like i'm a pain to every1 here Sad
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: interfaces help #40
i was able to do this part !! i did everything and i guess everything is fine !!

Spoiler:
Code:
Write an application to manage the stock of a store.

Here is a specification of the program in terms of classes and interfaces:

interfaces
Salable per kilogram : the interface for selling items per kilogram
methods:
Sale : This method receives the quantity sold of the item and change the stock
Salable piece : the interface for items that sell by pieces
methods:
Sale : This method receives the number of rooms sold the item and change the stock
Likely to be sold in balance
methods:
start balance : This method lowers the price of the item by the given percentage
complete the balance : This method increases the price of the item by the given percentage

classes

Class Articles: general class of all items
Properties: number (automatically generated during the creation of an article) , purchase price , sale price and name.
Methods ( other than the manufacturer):
calculating the rate of return ( ( prixVente - prixAchat ) / prixAchat )
toString (): return the name and price of the item
This class does not implement any interface.

Two derived class Articles classes:
Class artciles Appliance
Aditional property : the number of items in stock
Additional methods ( other than the manufacturer):
Add a number of pieces in stock
toString (): return the name , the price of the item and the number of parts
You must implement the corresponding interfaces this class .
Class articles futures
Additional property : quantity in stock
Additional methods:
Add the stock quantity
toString (): return the name , the price of the item and the quantity in stock
You must implement the corresponding interfaces this class , knowing that the items futures can not be sold on sale.

now i have a new part
Spoiler:
Code:
class Store
Properties: a vector (Vector)
Methods (other than the manufacturer):
       Add product to the vector
Save name, purchase price and selling price of all items in the futures vector "Primeurs.data" file properties
Save all items appliances (as instances) of the vector in the "Electro.data" file
Save the information generated by toString all items in the vector file "info.data"
Print file information "info.java"
Read "Primeurs.data" file and print the name and the performance of each primary article.
Read "Electro.data" file and print the number and performance of each article appliances.
can some1 give me and exemple n how to do this ? i know how to save to a file and read from a file i have it in a course thats not my problem but what i did here is :
Code:
import java.util.Vector;

public class Store {
  Vector vecteur = new Vector();
  public Store(){
      vecteur=null;
  }
  public void addArticle(Article article){
      vecteur.addElement(article);
  }
i need to know how should i do this step: Save name, purchase price and selling price of all items in the futures vector "Primeurs.data" file properties

hope any1 can help Sad and i'm sry i feel like i'm a pain to every1 here Sad
[Image: blackeagle_zps6ad86521.gif]

Reply







Users browsing this thread: 2 Guest(s)