• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Array Help  RSS feed

 
Beth Elliott
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an assignment to write but I am struggling with the code. Any direction would help!!!

Code I have so far:



Screen-Shot-2016-05-21-at-12.00.26-AM.png
[Thumbnail for Screen-Shot-2016-05-21-at-12.00.26-AM.png]
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch! It looks like a good start.

A couple of things about your post: Thanks for using code tags and formatting your code. Screen shots are frowned upon for posting text. Could you copy and paste from your screen and post the text? Also, what specific problems are you having? If it's just getting started, try writing down what one of the methods should so, not in Java but in your native language.

Edit: And you need to change your one screen name to your real name, or at least a "real sounding" name. We encourage real names here.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks pretty good. Some feedback:


The field name says Car, but this is an Object array. Objects are not cars. To continue this program, I advise writing the Car class that the code implies should exist.

Field names should start lowercase, besides constants which you're writing correctly. And if it's an array, probably make it plural (as in, "cars" not "Car"). Also all of those instance variables should be private.

I disagree with making that Scanner an instance variable. Since it's only used in two methods, it could be local to the process method, then passed as an argument to the menu method. Or another possibility, take it out of the menu method and rename it to showMenu(), and then get user input inside the process method.

That entire case statement could be extracted into it's own method, something like checkChoice(int choice) or processChoice(int choice).

That while loop can be changed to a do-while loop to eliminate the initial condition.

(edit) Keep in mind, I don't have your assignment. So if any of this advice contradicts the assignment, don't do it.
 
Beth Elliott
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response.

I'm trying to build my array, and I'm not sure if I need to build a multidimensional array or an array list.

I need to store the Licence plate of up to 20 cars, and the number of hours they stay, and the cost. Both Licence plate and hours are values are entered using Textfield, the cost is calculated.

Again any assistance is appreciated.

Cheers
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beth Elliott wrote:. . . I need to build a multidimensional array or an array list. . . .
Apart from there not actually being multidimensional arrays in Java® ... What look like multidimenstional arrays are actually arrays of arrays ...

Use a List<Car>. As you have been told, you should write yourself a Car class. In fact you should start off by creating a single Car object and testing that, before you try multiple Cars. Multiple arrays are error‑prone and non‑object‑oriented style. A List<Car> is a good object‑oriented object. You could use an array of Cars, but Lists are generally better than arrays because they can change size. Many people here use arrays because they have not been taught how to use Lists yet. And declare it as List
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zachary Griggs wrote:. . . I disagree with making that Scanner an instance variable. . . .
I would make it a final static field ... in a different class, maybe called KeyboardInputs, and you use methods of that class to do all your input. As long as you are running everything in one thread, then you only need one instance of the Scanner, which you never close, per program.

And welcome to the Ranch again, BE.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beth Elliott wrote:Thanks for your feedback.

I have updated my code, but cannot nut the final pieces out - can anyone help?


Unfortunately, updating code after lots of conversations have taken place may break context. In other words, those feedback that you value may make no sense to another rancher following your topic. And why your last edit has been rejected. Sorry.

Perhaps it would be better to add a post to this topic.... one that describes the "final pieces" issues that you are encountering?

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!