Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Loosing Grey Matter  RSS feed

 
Sue Hunt
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.
Well, started my new course, which said it wasn't going to be heavy into Java, but it's scared me into a mental block again....
And as I had such great help here last time, thought I would come back for some more gentle coaching. Mind you, probably shouldn't have left it till the nth hour.
I've been given a skeleton code where I have to put in some missing line.

Have also been given a full Tester class.
Question, should I be looking at the Tester class to give me some clues as to what to put into the different methods?
If it would help for me to put in the tester class, I'll try and modify it slightly so as to protect the original author.
Thanks for any and all help.
Cheers
Sue
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sue,
Please try not to get any grey matter on the floor here, I just swept up.
The Tester class may indeed give you some clues as to how this class ought to behave, but it's pretty obvious to me what it's supposed to do just looking at it.
Let's try to exercise the Rubber Duck effect, shall we? I will play the part of the duck. Why don't you explain to me what NamingService class is supposed to do, right now, without thinking about it too much. You might speak in general terms, or you might explain each method one at a time.
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sue,
The exercise you have been assigned requires you to become familiar with the java.util.HashTable class. This class maps keys to values and contains all the methods you require for the exercise. Have a look at the javadocs for this class and post again if you still have problems.

HTH
Nigel
 
Sue Hunt
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest do like it when you help out...I'll try not to spill any more grey matter.
Well, when I first looked at it, I thought it wanted me to create a new entry...but then I got to thinking...dangerous first thing in the morning.
As the Tester (code below) has the names of the computers and their respective IP address, it looks as though I have to populate the hashtable with that.
So I'm hoping, after looking at the hashTable info on Java.Sun that the addName method will look something similar to this
nameStore.put(nService.addName);
The second method, wants me to remove a name from the hashtable.
nameStore.remove(nService.removeName);
The third method, wants me to retrieve an ip address of a name supplied by the tester.
not sure, but think it will be a get ?
The forth, wants to know the number of entries in the hashTable
then the final one, will return a true value if ths names supplied by the tester is in the hashtable....
You know when I said it was the midnight hour? Tomorrow....unless I can get an extension.... who said studying part time and working full time works....*grin*

Living recklessly and putting code in.
 
Sue Hunt
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to think I'm on the right track...how's this so far?

I know I still have to do the last method, however, when I compile it, I get these command results
..\..\..\Java\NamingService.java:29: missing return statement
{
^
..\..\..\Java\NamingService.java:34: missing return statement
{
^
..\..\..\Java\NamingService.java:39: missing return statement
{
^
3 errors

Thanks for any and all help...sometimes, me think, I should just stick to VBScript, ASP, XML and HTML....
Cheers
Sue
Tool completed with exit code 1
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sue,
The last three methods must return the appropriate type indicated by the method. For example, all you need to do with retrieveAddress is:

Note that you must typecast the return to a String since all Maps return Object on the get method. There is one other potential problem here as well in that a Hashtable doesn not allow null keys, so if the parameter returnedAddress is null, a NullPointerException will be thrown. According to your overall design, this may not be an issue.
You can fix the noOfEntries method in a similar manner, but no typecast is necessary there. For isInService, take a look at the API docs for java.util.Map, there is a method that will allow you to return a boolean defined there for this situation.
 
Sue Hunt
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael
Thanks for that.
Will also go have a look at the api.
Funny, how I can stare at it for so long and not get anywhere, and then come on here and you guys just have to say a few words and the grey matter kicks in a lot faster, then trying to read a text book (don't get it man, I just don't)
I'm simultaneously working on another question, once I figure out what it's trying to ask, I'll definitely come here.
Cheers
Sue
 
Sue Hunt
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again.
..\..\..\Java\NamingService.java:35: inconvertible types
found : int
required: java.lang.String
return (String) nameStore.size(); //Returns number of pairs in naming service
^
1 error
I got this in relation to this...


And I've been looking at the API, as suggested and I'm still unsure as to which one to use...

but that didn't work.
I feel like I should be using an if statement...or am I barking up the wrong tree?
Thanks for not giving up on me....
Sue
 
eammon bannon
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at what you are doing to the return type of your noOfEntries() method. Its expecting a primative int, but you are making an explicit cast.
You shouldn't need an if statement for your isInService(String computerName) method - java.util.Hashtable gives you a ways to test values and keys in itself. Look at the contains(Object value), containsKey(Object key) and containsValue(Object value) methods of Hashtable - think about how you are storing data in your Hashtable, and it should become clear which you need. Good luck.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you do a get() from the hashtable, you get something that is an Object (note: capital O, so this is the class type). the method is supposed to return a string. you know that what you put into the hash is a string, so you can cast it to a String (that's what Michael was telling you).
now, your noOfEntries method should return an int [from the line "public int noOfEntries() ]
the size() method of a Hashmap returns an int. you don't want to cast that to a String, you just want to return it to the calling methos. so you should change

to

......
the isInService should return a boolean. you want to CALL the containsValue() method, similar to how you called the size() method, but pass into it the computer name as a string..., then return the boolean you get...
 
Sue Hunt
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AAAHHHHH...I think.
note to self: I really must learn not to leave it till the last minute...
Thanks guys, I've now managed to get the NameServer to compile.
This is what I put in.

Now in Tester, which was supplied by the course, I'm getting an error reading on line 14, which is

The command result tells me it doesn't like
..\..\..\Java\Tester.java:14: cannot resolve symbol
symbol : variable Service
location: class Tester
System.out.println("There are " + Service.noOfEntries() +
^
1 error
Tool completed with exit code 1
Is this because I'm using different names with in the Size method.?
I will NOT let Java beat me, like Pascal did....
Cheers
Sue
[ March 17, 2004: Message edited by: Sue Hunt ]
 
eammon bannon
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Think about where you are instantiating the "Service" object you call methods on - or indeed if you are
 
Sue Hunt
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Honest, I'm not trying to be thick.....
huh?
 
eammon bannon
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way that line of code works: you have a variable called "Service" on which you try to call the noOfEntries() method. This will work only if either
1) You have somewhere in your code the line NamingService Service = new NamingService(); or
2) You have defined a static class called "Service" somewhere that includes a noOfEntries() method.
Now, since you didn't include any code for a static "Service" class, I'm guessing this is a typo and you meant to write "nService.numberOfEntries()" . Am I right?
[ March 17, 2004: Message edited by: eammon bannon ]
 
Sue Hunt
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, you know sometimes when you want to hit your head against a brick wall...well this might be one of those times....the air was blue, with all the names I was calling myself just now.
Thanks Eammon....
Now, provided I get an extension, I'll hit you all with another one...
Have to develop a server, which sends back the average, largest number or smallest number from a set of data that it stores. The server then should also respond to a quit message from the client. We have been given the client class codes....
Thanks alot everyone. You saved me from feeling like I was going to fail the first assignment...
Cheers
Sue
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
re: leaving it to the last minute...
here's some unsolicited advice. feel free to ignore.
it's hard (i'm a procrastinator too), but in programming, you REALLY need to not wait. EVERYTHING takes longer than you think, and until you actually start writing code, you never realize how many "gotcha's" there can be. I usually try and just do one or two pieces a day, and make SURE they work. so, monday, you could do the addName method, tuesday the removeName, etc.
that way it's not quite so overwhelming, and you can really focus on the one peice at a time. even if i'm going to do a marathon session of writing, try and make sure ONE method works, before you write code for all 10, and get 30 compiler errors. the fewer changes between compiling, the easier it is to find any mistakes.
also, if you're like me, and you try and do 8 things at once, you'll forget to test 3 of them. and that's inevitably the first thing the tester/grader checks.
i'll shut up now. you're doing great, and people here are always happy to help!!!
 
Sue Hunt
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred, advice taken in the manner in which it was given
I have been given an extension for the final programming question, so I shall try and get my head wrapped around and figure out what it wants us to do. And try to take some of your suggestions on board. Plus it doesn't help that I'm a crunch producer, rather than a plan and plod.
However, I shall start a new topic string for it, as it's to do with Server stuff.
Thanks again to everyone for the patience and encouragement.
Sue
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!