/    Sign up×
Community /Pin to ProfileBookmark

JAVA, assignment with Arrays

hi,
I am having a hard time getting to grips with JAVA. I am on a course that is an introduction so i am only a novice at this. I have an assignment due in on Friday? but i am struggling to come up with the code. I am wondering if anyone would be able to help me with a few things.
I have to create 3 classes, the main one being MyGArage which should only contain a main method. This method should provide; a default constructor creating a garage for 5 vehicles and a constructor allowin any size of garage to be created.
The second class the Vehicle class i have:

[code]
class Vehicle
{
private static int count =0;
private String rNo;
private boolean petrol;
private int cc;

public static int HowMany()
{
return count;
}
public String regNo()
{
return rNo;
}

public boolean isPetrolEngine()
{
return petrol;
}
public int capacity()
{
return cc;
}
}
[/code]

The third class is the Garage class. This class will contain an aggregation of Vehicle objects; with this possible UML definition:

theVehicles : an array of Vehicle objects references
noOfVehicles : no. of vehicles currently in the garage
park: add a supplied vehicle to the garage
remove : taks a reg. no. (string) and removes the corresponding Vehicle object from the garage; returns the removed object
occupancy: returns the max. size of the garage
contains: returns a string containing the reg. nos. of all vehicles in the garage(seperated by spaces)

Here is the code i have so far, its not much and is only setting the classes out:

[code]
public class MyGarage
{
public static void main(String[] argvs)
{
}
}
class Vehicle
{
private static int count =0;
private String rNo;
private boolean petrol;
private int cc;

public static int HowMany()
{
return count;
}
public String regNo()
{
return rNo;
}

public boolean isPetrolEngine()
{
return petrol;
}
public int capacity()
{
return cc;
}
}
class Garage
{

int[] theVehicles = new int[5];
private int noOfVehicles;

public int park()
{

return noOfVehicles;
}
public int remove()
{
return noOfVehicles;
}
public int occupancy()
{
return noOfVehicles;
}
public int capacity()
{
return noOfVehicles;
}

}
[/code]

Ok now i am not looking for anyone to hand me all the code for this on a plate, i ain’t going to learn it like that so if someone could maybe spoon feed me this then that would be great.
My first question is when i am creating the array of vehicles do i create that out side the methods but still in the garage class?
I think i should be starting with the park method. So once i have my array in this method i would just be adding to the array does any one have an example of adding to an array?
Still in the park method, how do i find out how many references to objects that are already in the array, this is so i can tell it at which point i want to add the new vehicle?
How do i return the total number of vehicles in my array?

Any and i mean any help on this would be great, i am so stuck with this i feel like glue.

to post a comment
Full-stack Developer

10 Comments(s)

Copy linkTweet thisAlerts:
@khalidali63Mar 12.2003 — are you still looking for help on this topic?

Khalid

[email][email protected][/email]
Copy linkTweet thisAlerts:
@RibeyedauthorMar 12.2003 — yes plz, i have until friday.
Copy linkTweet thisAlerts:
@khalidali63Mar 12.2003 — email me your assignment requrements so I can understnd it properly to guide you any further

Khalid
Copy linkTweet thisAlerts:
@RibeyedauthorMar 13.2003 — hi plz find attached my assignment in .html, thank you for this. If refers to a previous example for the Vehicle class, this is the code i posted about so i never sent that document.


[upl-file uuid=3cca6dcc-0404-4fa3-84af-00a2ed1c2f91 size=2kB]assignment.zip[/upl-file]
Copy linkTweet thisAlerts:
@khalidali63Mar 13.2003 — Here is what you do.

define a class MyGarage with main method and a constructor( with String[] parameter)

the declae some global variables for this class to use

Say int capacity =10; to hold max numbers of allowed parking spots

A global instance of Garage that will hold the parked vehicles.

Garage garage;

in the constructor MyGarage make sure that

if(garage==null){

garage = new Garage()

//we want this to be created once.

}

then run aloop through the args array and get a regNo and pass to garage where you create a new instance of vehicle and put it in an array of vehicles in garage class.

Garage class

//declare global array of vehicle class

Vehicle[] vehicles = new Vehicle[capacity];

public int parkCtr=0;//counts the number of vehicles parked

there dif ways of doing it,this is how I have done it...simple

//2 methods to create an instance of vehicle and put it in array and then retrieve value from array

public void parkVehicle(String regNo){

vehicles[parkCtr++] = new Vehicle(regNo);

}

public Vehicle getVehicle(int parkId){
return vehicles[parkId];
}


and finally

Vehicle class

class Vehicle{

public String regNo;

public String ccDesc = "1500cc";

public boolean isPetrol = true;

public Vehicle(String regNo){
this.regNo = regNo;
}

public String getRegNo(){
return this.regNo;
}

public String getCCDesc(){
return this.ccDesc;
}

public boolean getIsPetrol(){
return this.isPetrol;
}

}

I hope this brings you verry close to the solution

?

Cheers

Khalid
Copy linkTweet thisAlerts:
@RibeyedauthorMar 13.2003 — thank you so much. Today in my last do on this so i will have to hammer this tonight. I have only had 3 1hour lessons and am still finding my barings.

I am sttill working on what you gave me, thanks again for this. I am using JBuilder and i seem to have a problem with the syntax for my if statement. Looking through your code am i correct in thinking that the parkID and the bit where you have:
<i>
</i>public Vehicle getVehicle(int parkID);

Th int parkID is thenumber of object refereneces in the array after the new vehicle as been added?

here is the code i have so far, can you point out wher i am going wrong?

<i>
</i>public class MyGarage
{
public static void main(String[] argvs)
{

}
}
class Vehicle
{
private static int count =0;
private String rNo;
private boolean petrol;
private int cc;

public static int HowMany()
{
return count;
}
public String regNo()
{
return rNo;
}

public boolean isPetrolEngine()
{
return petrol;
}
public int capacity()
{
return cc;
}
}
class Garage
{
private int garagecapacity = 10;
Garage garage;
if (Garage==NULL){
garage = new Garage();
}

theVehicle[] tbheVehicle = new Vehicle[garagecapacity]
private int noOfVehicles = 0;

public int park(String regNo)
{
theVehicle[noOfVehicles++]= new theVehicle(int parkid)
return noOfVehicles[parkid];
}
public int remove()
{
return noOfVehicles;
}
public int occupancy()
{
return noOfVehicles;
}
public int capacity()
{
return noOfVehicles;
}

}


Thanks again
Copy linkTweet thisAlerts:
@khalidali63Mar 13.2003 — JBuilder is probably the best..:-)

I use that too.

Now what I 'd recomend that you scrap the code you already have and re think on the lines I have posted earlier,

There seems to be some logical errors andits always better to r-do something instead of trying to fix it.

Let me know if you have any questions..

Khalid
Copy linkTweet thisAlerts:
@RibeyedauthorMar 13.2003 — hi khalidali63,

ok, i hear what you are saying and i agree with your philosophy, but we started this in class and this was the struture we were prompted to use, well my interpetation anyway. I would love to go in to class tomorrow and say here is a better or even easier way to do this but this is not sure if this is what they want. You would also agree that it is hard to think outside the box if you don't even understand the box, ?

A few questions:

what is the correct syntax for adding to an array, can you give me an example, i just want to add 1 object reference?

About the only difference i see whith your code and the code i have is the placement of the Garage class. I would persume mine is wrong but is it wrong becuse of inheritance?

Placement of the array code in the vehicle class:

when i am creating and initalising an array i do i place the code in the vehicle class and after i declaire the variables, is this correct?

any if do you don't mind i will have some more stupid novice questions for you.
Copy linkTweet thisAlerts:
@khalidali63Mar 13.2003 — if (Garage==NULL){

garage = new Garage();

}

should be in the MyGrage constructor.according to the doc,thats where garage should be created

public int park(String regNo)

{

theVehicle[noOfVehicles++]= new theVehicle(int parkid)

return noOfVehicles[parkid];

}

the above is wrong,youare trying toreturna an array value where as you do not have "noOfVehicles" declared as an array

plus you need to implement some functionality in the MyGarage constructor to create vehicles for the command line iput by user.

well I guess here you go ,take a look at the code and see where you need to make what changes.

I am adding .java file as well as .class files so that you can run it and see it as well,use this as reference,if you want to keep working on your code.

:-)

To run this rogram

open the archive ina directory and from dos prompt go to that direcotry where you can see 4 files in it

1 .java file and

3 .class files

from this folder in does prompt type the following line

java MyGarage ABC123 XBC225 BGA9235

and press enter.

Let me know of any other of your questions and btw I love to anser your java questions therefore do not feel bad at all,we all start at 0 at some time.

Khalid

[upl-file uuid=1f6613ec-c987-4d9c-9138-7a7da6f8c813 size=4kB]mygarage.zip[/upl-file]
Copy linkTweet thisAlerts:
@RibeyedauthorMar 13.2003 — hi khalidali63,

thank you very much for all your help what you have did is great. I just need to learn it now. Tomorrow i i'll be getting a course project so no doubt i will have a lot more questions for you. Thanks again,

David
×

Success!

Help @Ribeyed spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...