incompatible types

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2495 Views - Last Post: 12 April 2007 - 10:50 AM Rate Topic: -----

#16 tlcoolj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-April 07

Re: incompatible types

Posted 11 April 2007 - 01:33 PM

I don't know if i fully understand what exactly your trying to do but i'll give it a shot..
your method getFullName takes in a char and returns the corresponding full name that is related to that char. Example pass the char 'G' and you want to return "Glycine" or 'F' and get "Phenylalanine" correct???

if that is so then why not try this

public String getFullName(Char L){

String[]fullnames = {"Glycine", "Alanine","Leucine", "Methionine", "Phenylalanine", "Tryptophan", "Lysine", "Serine", "Asparagine", "Aspartic acid"," Proline"," Valine", "Isoleucine", " Cystine","Tyrosine", "Histidine", "Arginine", "Threonine"," Glutamine","Glutamic acid", "stop"};


Character[]letters= {'G','A', 'L','M', 'F', 'W', 'K', 'S', 'N', 'D', 'P', 'V', 'I', 'C', 'Y', 'H','R', 'T','Q', 'E', '*'};

for(int i = 0; i < letters.length; i++){
if(L.equalsIgnoreCase(letters[i])
return fullnames[i];
else
//letter passed to getFullName is not in the letters array and you may want to do something
}}

since the position of the letter is the exact same as the position of the full name in the fullName array then i believe this method will work and require less code/resources
Was This Post Helpful? 0
  • +
  • -

#17 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: incompatible types

Posted 11 April 2007 - 02:35 PM

Ok, one more hint:

public V myMethod(K key) {
   // takes type K, which could be any class
   // returns type V, which could be any class
   // map is a Map<K, V>
   return map.get(key);
}

Was This Post Helpful? 0
  • +
  • -

#18 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

Re: incompatible types

Posted 12 April 2007 - 12:13 AM

Hi
Thansk
As far as I understand from your hint my parameter should be an array (Character[]letters)?
Maybe I am wrong.( I am just starting to learn Java.) But for the purpose of the code I need a single character.????


View Postalcdotcom, on 11 Apr, 2007 - 02:35 PM, said:

Ok, one more hint:

public V myMethod(K key) {
   // takes type K, which could be any class
   // returns type V, which could be any class
   // map is a Map<K, V>
   return map.get(key);
}

Was This Post Helpful? 0
  • +
  • -

#19 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: incompatible types

Posted 12 April 2007 - 02:52 AM

View Poststanka, on 12 Apr, 2007 - 12:13 AM, said:

Hi
Thansk
As far as I understand from your hint my parameter should be an array (Character[]letters)?
Maybe I am wrong.( I am just starting to learn Java.) But for the purpose of the code I need a single character.????


Isn't the point of this method to return the string value corresponding to a single letter; G corresponding to Glycene for instance? If this is the case, then why would your parameter be an array of Characters? Think about it. It's a one to one relationship. when you use the map, one character gets one string. In fact, you can't have duplicate keys in a map. Am I wrong about what your assignment is? Are you clear on how a map works? If not, I can show you some examples.
Was This Post Helpful? 0
  • +
  • -

#20 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

Re: incompatible types

Posted 12 April 2007 - 05:25 AM

Thanks
Well I thought I know how map is working, but now I am not sure anymore. Examples would be good to see.
So in my case the letter is the key. Then how to extract the key from the map?


View Postalcdotcom, on 12 Apr, 2007 - 02:52 AM, said:

View Poststanka, on 12 Apr, 2007 - 12:13 AM, said:

Hi
Thansk
As far as I understand from your hint my parameter should be an array (Character[]letters)?
Maybe I am wrong.( I am just starting to learn Java.) But for the purpose of the code I need a single character.????


Isn't the point of this method to return the string value corresponding to a single letter; G corresponding to Glycene for instance? If this is the case, then why would your parameter be an array of Characters? Think about it. It's a one to one relationship. when you use the map, one character gets one string. In fact, you can't have duplicate keys in a map. Am I wrong about what your assignment is? Are you clear on how a map works? If not, I can show you some examples.

Was This Post Helpful? 0
  • +
  • -

#21 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: incompatible types

Posted 12 April 2007 - 06:50 AM

Well, for starters, Map is just an interface. A specific implementation of the Map is a HashMap, which is what I'll be using in my example. Think of a map as a set of keys, X and a set of values, Y. The set of keys and values can each be any type of object. Think of the relationship between keys and values (set X and set Y) as a function where every key in X is related to exactly one value in Y and every value in Y is related to at least one key in X. So, you can have duplicate values, but not duplicate keys. And every key or value must be related to another value or key, respectively.

So, let's define a set of keys, which are the capital letters of the alphabet. Let's also define a set of values that are the phonetic alphabet string corresponding to a letter. For example

A--> Alpha
B --> Bravo
C --> Charlie
D --> Delta
E --> Echo
F --> Foxtrot
etc...

So, I'd define my HashMap thusly:

HashMap<String, String> myMap = new HashMap<String, String>();


Knowing what you know about maps, hopefully you realize that you cannot just add a key or a value to a map, but that you have to add both. It's not just a collection of objects, but also a collection of relationships between objects. Here are some examples of input and output using my map:

// put some values in the Map
myMap.put("A", "Alpha");
myMap.put("B", "Bravo");
myMap.put("C", "Charlie");

// get some value from the Map
String s = map.get("A"); // s == "Alpha"
s = myMap.get("B") // s == "Bravo"
s = myMap.get("W") // s == null because there is no "W" in the set of keys in my map

// what happens when you try to put a value with an existing key?
s = myMap.put("B", "Baker") // s == "Bravo"
s = myMap.get("B")  // s == "Baker"

// What happened here is that when you
// put a value for an existing key, put
// returns the old value and replaces it 
// with a new value.  (If it weren't an existing
// key, put would have returned null.)  
// Remember that you cannot have duplicate
// keys so there can be only one value 
// corresponding to "B."

// here's an example of the fact that you can
// have duplicate *values*
myMap.put("W", "Whiskey");
myMap.put("G", "Whiskey");

s = myMap.get("W") // s == "Whiskey"
s = myMap.get("G") // s == "Whiskey"

// Although this is not the correct phonetic
// String for "G", the map allows you to
// give it whatever value you like.  You
// could have done this:
myMap.put("G", "Pneumono­ultra­micro­scopic­silico­volcano­coniosis");

// returns a Set that contains all of you keys
// in this instance it is a Set<String> because
// my keys are Strings.  I could have
// made them Characters, like you did
Set<String> keys = myMap.keySet(); 

// returns a Collection of values.  Similar to above.
Collection<String> values = myMap.values();


Hopefully this clears things up. If you want to read more go here: http://java.sun.com/...rfaces/map.html

This post has been edited by alcdotcom: 12 April 2007 - 06:56 AM

Was This Post Helpful? 0
  • +
  • -

#22 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

Re: incompatible types

Posted 12 April 2007 - 08:34 AM

Well,
I think (maybe) I understand the map interface. Anyway being completly stupid I really do not understand how to acess an element from an array, providing that this element can be anyone in the already declared array.
From my code I do not undestand how to define letter from the Character[]letters.



View Postalcdotcom, on 12 Apr, 2007 - 06:50 AM, said:

Well, for starters, Map is just an interface. A specific implementation of the Map is a HashMap, which is what I'll be using in my example. Think of a map as a set of keys, X and a set of values, Y. The set of keys and values can each be any type of object. Think of the relationship between keys and values (set X and set Y) as a function where every key in X is related to exactly one value in Y and every value in Y is related to at least one key in X. So, you can have duplicate values, but not duplicate keys. And every key or value must be related to another value or key, respectively.

So, let's define a set of keys, which are the capital letters of the alphabet. Let's also define a set of values that are the phonetic alphabet string corresponding to a letter. For example

A--> Alpha
B --> Bravo
C --> Charlie
D --> Delta
E --> Echo
F --> Foxtrot
etc...

So, I'd define my HashMap thusly:

HashMap<String, String> myMap = new HashMap<String, String>();


Knowing what you know about maps, hopefully you realize that you cannot just add a key or a value to a map, but that you have to add both. It's not just a collection of objects, but also a collection of relationships between objects. Here are some examples of input and output using my map:

// put some values in the Map
myMap.put("A", "Alpha");
myMap.put("B", "Bravo");
myMap.put("C", "Charlie");

// get some value from the Map
String s = map.get("A"); // s == "Alpha"
s = myMap.get("B") // s == "Bravo"
s = myMap.get("W") // s == null because there is no "W" in the set of keys in my map

// what happens when you try to put a value with an existing key?
s = myMap.put("B", "Baker") // s == "Bravo"
s = myMap.get("B")  // s == "Baker"

// What happened here is that when you
// put a value for an existing key, put
// returns the old value and replaces it 
// with a new value.  (If it weren't an existing
// key, put would have returned null.)  
// Remember that you cannot have duplicate
// keys so there can be only one value 
// corresponding to "B."

// here's an example of the fact that you can
// have duplicate *values*
myMap.put("W", "Whiskey");
myMap.put("G", "Whiskey");

s = myMap.get("W") // s == "Whiskey"
s = myMap.get("G") // s == "Whiskey"

// Although this is not the correct phonetic
// String for "G", the map allows you to
// give it whatever value you like.  You
// could have done this:
myMap.put("G", "Pneumono­ultra­micro­scopic­silico­volcano­coniosis");

// returns a Set that contains all of you keys
// in this instance it is a Set<String> because
// my keys are Strings.  I could have
// made them Characters, like you did
Set<String> keys = myMap.keySet(); 

// returns a Collection of values.  Similar to above.
Collection<String> values = myMap.values();


Hopefully this clears things up. If you want to read more go here: http://java.sun.com/...rfaces/map.html

Was This Post Helpful? 0
  • +
  • -

#23 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: incompatible types

Posted 12 April 2007 - 09:10 AM

You don't need to define "letter." It is the parameter to you function, so it has a value already when the function is called:

getFullName('G');  // say I make this method call then...

public String getFullName(Character letter) {
  // letter == 'G' in this instance
  // so how do you use letter
  // to get the output you want?
  // here's a hint
  return map.?
...
}

Was This Post Helpful? 0
  • +
  • -

#24 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

Re: incompatible types

Posted 12 April 2007 - 09:24 AM

Thanks a lot for your help
Yes return fullname = map.get(letter). I feel very stupid but I learned a lot.
:)

View Postalcdotcom, on 12 Apr, 2007 - 09:10 AM, said:

You don't need to define "letter." It is the parameter to you function, so it has a value already when the function is called:

getFullName('G');  // say I make this method call then...

public String getFullName(Character letter) {
  // letter == 'G' in this instance
  // so how do you use letter
  // to get the output you want?
  // here's a hint
  return map.?
...
}

Was This Post Helpful? 0
  • +
  • -

#25 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: incompatible types

Posted 12 April 2007 - 10:50 AM

Glad to help. Do you think you learned more as a result of my not just posting some code? Or do you think that would have been better for learning purposes?

By the way, you don't need to have a fullName variable, unless you are wanting to store the last full name that was retrieved. You can just return map.get(letter).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2