473,499 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Learning Java Help please!

2 New Member
Hi im learning java through blue j and im having trouble doing an exercise if anyone can help that would be great.
the question is:

Rewrite the getLot method so it doesn't rely on a lot with a particular number being stored at index (number -1) in the collection.You may assume that lots are always stored in increasing order of their lot number.

The method is written ass :

Expand|Select|Wrap|Line Numbers
  1. public Lot getLot(int lotNumber)
  2.     {
  3.         if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
  4.             // The number seems to be reasonable.
  5.             Lot selectedLot = lots.get(lotNumber - 1);
  6.             // Include a confidence check to be sure we have the
  7.             // right lot.
  8.             if(selectedLot.getNumber() != lotNumber) {
  9.                 System.out.println("Internal error: Lot number " +
  10.                                    selectedLot.getNumber() +
  11.                                    " was returned instead of " +
  12.                                    lotNumber);
  13.                 // Don't return an invalid lot.
  14.                 selectedLot = null;
  15.             }
  16.             return selectedLot;
  17.         }
  18.         else {
  19.             System.out.println("Lot number: " + lotNumber +
  20.                                " does not exist.");
  21.             return null;
  22.         }
  23.  
  24.  
Any help would be much appreciated again
Feb 20 '09 #1
3 2423
JosAH
11,448 Recognized Expert MVP
Would a Map<Integer, Lot> be of help? The Integer is the lot number, stored as the key in the Map, the associated value is the Lot itself.

kind regards,

Jos
Feb 20 '09 #2
Speckydodge
2 New Member
maybe but i havent come across maps yet...im just a wee beginner ha :)

thanks for the help though bud.
Feb 20 '09 #3
JosAH
11,448 Recognized Expert MVP
@Speckydodge
Well, then you're stuck with your arrays then. The index number of the array has a functional dependency on the Lot number. There's no way to get rid of it.

kind regards,

Jos
Feb 20 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
5529
by: Rick | last post by:
I know this is an open-ended question. But, can someone recommend a path for learning Java. I have some minimal background in programming, C and C++. Time constraints ony allow me a few hours in...
3
2064
by: Ralph H. Stoos Jr. | last post by:
All, I need suggestions for learning Java. I have purchased several books (Java Essentials, Beginning Java 2) and have tried to read and write code. Apparently, I am not easily able to read a...
0
5577
by: Donovan | last post by:
Hello. I am interested in learning about and using the Eclipse framework and Java -- both of which are new to me. I have prior programming experience with VB.NET and c#.NET; but not Java. As...
1
9582
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
7
1572
by: Max | last post by:
On monday I start a semester course in Python (the alternative was Java). I was looking through the course outline and noticed the following: 1) UserDict is used. This is deprecated, right? 2)...
7
2362
by: Hal Vaughan | last post by:
I have a problem with port forwarding and I have been working on it for over 2 weeks with no luck. I have found C programs that almost work and Java programs that almost work, but nothing that...
7
4333
by: Richard Phillips | last post by:
Hello, Does anyone else out there have a copy of this? I'm working through my copy and have discovered that it has no chapter 11... Pages 341-388 are just not there! Anyone else able to check...
26
9265
by: K.J.Williams | last post by:
Hello, A friend and I want to learn PHP but we have two totally different programming backgrounds. I have experience with procedural programming in C, and he has experience with Visual BASIC....
2
1313
by: Matt Bitten | last post by:
Hi, all. I'm in a situation where I need to be writing a bunch of quick-y (hopefully) self-contained programs that anyone can run from a web page. Java applets are the obvious way to do this....
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.