Marshall Mathers

Ranch Hand
+ Follow
since Dec 05, 2015
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
27
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Marshall Mathers

Thank you for all the suggestions guys. I did end up changing it a little bit from last time mainly due to the fact that I have to use inheritance and I wasn't sure how to implement a 'ModelConstants' class (Which I need now and wasn't aware of it then) with the switch statement so I ended up using the if loop again. It does provide random results now too, I tested it multiple times and data is always different so that's good.

I do have another question though regarding the simulation of a single step/steps where the objects will move on the screen to the next available field near them and I'm not sure how to make it work with inheritance as well as some additional requirements such as some objects not being allowed to be placed next to other objects (Detailed below).

Let me give you a quick overview of what classes I have and what each one more or less is supposed to do without posting lines and lines of code.

Base classes given to me:
Counter: "Provide a counter for a participant in the simulation. This includes an identifying string and a count of how many participants of this type currently exist within the simulation."
Field: "Represent a rectangular grid of field positions. Each position is able to store a single person."
FieldStats: "This class collects and provides some statistical data on the state of a field. It is flexible: it will create and maintain a counter for any class of object that is found within the field."
Location: "Represent a location in a rectangular grid."
SimulatorView: GUI
RandomGenerator: Posted above
Randomizer:

And in addition to them I have a People class which is supposed to be used as a super class for Host, Artist, Engineer and Scientist. As well as the Simulator (below) and Model Constants (also below).

Now to the question. Like I said above, I have to allow the user to be able to simulate the simulation for one step or more and I'm not really sure how to do that given the requirements:
1. Artists chat only to other artists
2. Scientists like other scientists and artists
3. Engineers like all other engineers, scientists and artists.
4. Hosts don't have preferences and everyone else can move next to the hosts.

So let me show you what I have so far:
Simulator


I know there is a lot of commented out code and most likely some duplication but I just wanted to include it too just in case I have to reuse something so just didn't want to delete it.

ModelConstants


Finally, I thought I'd mention that 'Field' has methods such as getObjectAt, place (as in placing an object) and freeAdjacentLocation so I guess they might be of use but not going to post it now as it's already a lot but if you need them or anything else then just ask.
Thanks in advance!
4 years ago

Carey Brown wrote:

Marshall Mathers wrote:

By the way, this was already given to me and I'm not supposed to change anything here.



On line 17 you are seeding it with 27. When you seed a random number generator it GUARANTEES that you'll get the same result.

You'd have to replace it with



I see, that makes sense. It's working perfectly now! I'm not sure why that was there then...
Either way, thank you so much for your help! If I'm stuck on something else, I'll make sure to add a post on here, instead of creating a new topic.

Thanks again.
4 years ago

Carey Brown wrote:Post the code for RandomGenerator.getRandom()





By the way, this was already given to me and I'm not supposed to change anything here.
4 years ago

Carey Brown wrote:Here's a solution to your "else if" problem. Each case has a 20% probability.



Thank you for this. I haven't used switch statements that much you see, I was used to doing more loops.
Anyway, I think I've done something wrong because now I get a different outcome but it's still the same every time.

Here's the code:



I've commented out that getRandom line too as I wasn't sure whether to include that or not but the outcome doesn't change either way.
I assume it's probably a newbie mistake so I'm sorry about that.

Thanks.
4 years ago

Carey Brown wrote:
This is allocating a reference to an ArrayList but not the ArrayList itself, so it is null. You need

Also, here's a code tag trick. I see you are using code tags when posting your code, which is a good thing. When you lop off the beginning of the file the line numbers no longer match the original. You can fix this by modifying the beginning code tag like this
(code=java:firstline(42))
Where the parenthesis are replaced by square brackets and 42 is replaced by the line number you want for the first line of your posted code.



It works! Thank you soo much!
And thanks for the tip regarding the code formatting, I'll make sure to use that next time.

I have one more question now regarding the generation of the dots on the field.
Every time I run it, I end up with the same amount of people on the screen: 250 hosts, 408 scientists, 327 engineers and 512 artists, in the same place. What am I doing wrong here?

Thanks.
4 years ago

Carey Brown wrote:should be
Also your line numbers are off so it's hard to tell exactly which line caused the error.

Your class has several fields: e.g. width, depth, field, simView, so there's no need to constantly be passing these around. Your methods already have access to these.


Thanks for the reply!

Ok so regarding that 'random' line. There's a Random Generator class that I've to use so that's why that's there. If I only leave 'Random' there, it asks me to create a method so not sure what to do here.
Sorry about the number lines. These are the lines that it highlights:
at Simulator.populate:

at Simulator.partyStatus:

at Simulator.<init>:

at Simulator.main:

Hope that helps.
And when it comes to the fields, I guess you mean that the code is not clean? Sorry, I'm still learning. As long as I can get it to work, I'll make it easier to follow after.

Thanks.
4 years ago
I'm trying to create a simulation for a party. There will be 4 classes: host, engineer, artist and scientist. Each one of those will have to be placed on a 50x50 field. I have to create a random generator in order to do this and I'm really stuck on this part for many hours now since I've never done random generators before so I hope that I can get some help. (I'm a beginner, yes.)
I'm currently receiving this error:
"Exception in thread "main" java.lang.NullPointerException at Simulator.populate(Simulator.java:88) at Simulator.partyStatus(Simulator.java:101) at Simulator.(Simulator.java:56) at Simulator.main(Simulator.java:41) BUILD SUCCESSFUL (total time: 3 seconds)"

If there is a different, better way of doing it then I'd be more than happy to see it and if you need any more details regarding this then just ask.

Thanks in advance!
4 years ago

Knute Snortum wrote:

Marshall Mathers wrote:

Knute Snortum wrote:modifyAssignment has no return type and will not compile because of that. Always post code that can compile (unless the question is about code that won't compile). The method need to return the assignment it modified.



I assume that return assignment (title, day, etc.) would be sufficient in that case?


Just return assignment; should work.

And what about addAssignment since that's actually the most important part.


You need to validate the parameters.



Unfortunately, return assignment doesn't. I tried that before and tried it once more, nothing. It says "Cannot return a value from a method with void result type.", so where do I change that?

And when it comes to the parameters, this is what i thought that I have to do but the thing is, I've got no idea how to do so. I tried various different ways and various different parameters as well as different calling methods to try to get the addAssignment to work. But still nothing.
4 years ago

Knute Snortum wrote:modifyAssignment has no return type and will not compile because of that. Always post code that can compile (unless the question is about code that won't compile). The method need to return the assignment it modified.



I assume that return assignment (title, day, etc.) would be sufficient in that case?
And what about addAssignment since that's actually the most important part.
4 years ago

Knute Snortum wrote:You know the drill by now. Show some effort and post the code you have.



That's not a problem, the question is whether what I post is actually going to be of any value if you don't actually know how the program works.

Part from the Model class:


Assignment class:


Thanks.
4 years ago
Hello, I have been working on an assignment for a while now and it seems as if I was able to do most of the requirements (below), however, I was unable to finish part 7a and 7b. This is why I came here to hopefully get some help since I have no idea where I'm making a mistake which stops me from actually adding an assignment when the program is run, like it should.

I have added my whole program as a zip file so that you can have a look at it yourself as me posting code here, from multiple classes wouldn't make much sense, especially because I do not know where I'm making the mistake. Assignment class was done by me, and some parts of the Model class were done by me, the rest was provided. But if you need any more detail then hopefully the requirements below will help you understand.

Requirements:

You have been provided with a base project that enables you to enter student data
and the modules that they are enrolled in. It needs to be extended in order to
handle assignments and student submissions of those assignments. The project has
two packages (view and model). You must not modify any code in the view
package. You should be adding code to the StudentModel interface and Model
class in the model package and adding new classes to achieve the requirements
below. The system will provide messages if it cannot find what it expects in your
code.
You will be expected to
1. Create an Assignment class that can be used to create assignment objects. The
class should have fields for a reference to the module associated with the
assignment, the title of the assignment, the date when the assignment is due,
the time when the assignment is due, and the percentage of the module grade
that the assignment counts for.
2. The Assignment class should have an appropriate constructor method that
enables all the fields to be set. All fields should also have getters and setter
methods for each of the fields.
3. There should be a factory method in the Assignment class that verifies the data
entered and only creates an assignment object if the supplied data is valid.
4. The class should have an equals(Object o) method that will return true if the
two assignment objects are for the same module and have the same title.
5. The class should have a toString() method that will return a String of the form
“<module code>: <assignment title> due on dd/mm/yyyy at hh:mm” where
<module code> is the module code for the associated module, <assignment
title> is the tile od the assignment, dd/mm/yyyy is the date on which the
assignment is due, and hh:mm is the time when the assignment is due.
6. You should also write a corresponding test class for the Assignment class to
verify the operation of all the methods in the Assignment class.

7. In order for the system to work, the Model class needs a number of methods
added. The view code checks whether these methods exist and reports an error
message if the required method does not exist. These methods are:
a. addAssignment: This method should create an Assignment object
using the currently selected Module object and the parameter data
passed to the method. It should return a reference to the created
Assignment object. The parameters for this method are a String title,
an integer day number, an integer month number, and integer year
number, an integer hour number, and an integer percentage. These
parameter values should be validated according to the rules below and
the object should only be created if the parameters are valid. NOTE:
The view code will use the returned reference to output a message to
say that the object was successfully created.
b. modifyAssignment: This method should modify the current
assignment object if one has been created. If there is no current
assignment object, it should do nothing. This method receives the same
parameters as the addAssignment method and it should validate the
parameter data in the same way as the addAssignment method. This
method should return a reference to the modified object.
c. getCurrentAssignment: This method should return a reference to the
current assignment. There are no parameters for this method. If there is
no current assignment, it should return null.
d. clearAssignment: This method should clear the currently selected
assignment. There are no parameters for this method and no return
value. This will have the effect of clearing the input fields on the
Assignment data entry panel. The following get… methods need to be
implemented to see this effect.
e. getAssignmentTitle: This method should return the title of the current
assignment. There are no parameters for this method. If there is no
current assignment, it should return null.
f. getAssignmentDay: This method should return the day that the current
assignment is due. There are no parameters for this method. If there is
no current assignment, it should return 0.
g. getAssignmentMonth: This method should return the month that the
current assignment is due. There are no parameters for this method. If
there is no current assignment, it should return 0.
h. getAssignmentYear: This method should return the year that the
current assignment is due. There are no parameters for this method. If
there is no current assignment, it should return 0.
i. getAssignmentHour: This method should return the hour that the
current assignment is due. There are no parameters for this method. If
there is no current assignment, it should return 0.
j. getAssignmentMinute: This method should return the minute that the
current assignment is due. There are no parameters for this method. If
there is no current assignment, it should return 0.
k. getAssignmentPercent: This method should return the percentage
assigned to the current assignment. There are no parameters for this
method. If there is no current assignment, it should return 0.

8. Write a test class to verify that the methods added to the Model class work as
expected. You may add additional methods to the model class to support these
methods and your testing.

9. The following data validation rules should be applied:
a. None of the string fields should be blank.
b. Any year fields must be a four digit number (i.e. greater than 999) and
less than or equal to 2016.
c. Any month field should be a number between 1 and 12 inclusive.
d. Any day field should be a number between 1 and 31 inclusive where
the month is January (1), March (3), May (5), July (7), August (8),
October (10), and December (12), between 1 and 30 inclusive where
the month is April (4), June (6), September (9), and November (11),
and between 1 and 28 for February (2). We are ignoring leap years for
this exercise.
e. The hour should be a number between 0 and 23 inclusive.
f. The minute should be a value between 0 and 59 inclusive.
g. The percentage should be a value between 1 and 100 inclusive. In the
next assignment, you will be expected to verify that the total of the
percentages for assignments assigned to a module do not exceed 100.
h. If the data is not valid then you should ensure that you add an error
message saying why the action was not completed. You should also
ensure that the error messages are cleared when the next action is
performed.
i. These validation rules should be verified by your test methods.


Crossed out parts I'm not worrying about for now, as I believe that they can be dealt with later on. But like I said, most parts are done and 7 is mostly what I'm worried about. So if you have any more questions then by all means ask and I'll be happy to answer.
Thanks in advance.


My program (zipped): http://www.filedropper.com/assignment6v2
4 years ago
Update on what I've managed to come up with so far. I think that I more or less know how the date is supposed to work but now I encountered another issue and I am not sure how to solve it so hopefully after posting the code here, I'll be able to get an answer to my problem. In short, I am not sure about a few requirements that I need to implement for my solution to the assignment to work. That's why I'll also post the full requirements below.

Requirements:
1. Create an Assignment class that can be used to create assignment objects. The
class should have fields for a reference to the module associated with the
assignment, the title of the assignment, the date when the assignment is due,
the time when the assignment is due, and the percentage of the module grade
that the assignment counts for.
2. The Assignment class should have an appropriate constructor method that
enables all the fields to be set. All fields should also have getters and setter
methods for each of the fields.
3. There should be a factory method in the Assignment class that verifies the data
entered and only creates an assignment object if the supplied data is valid.
4. The class should have an equals(Object o) method that will return true if the
two assignment objects are for the same module and have the same title.
5. The class should have a toString() method that will return a String of the form
“<module code>: <assignment title> due on dd/mm/yyyy at hh:mm” where
<module code> is the module code for the associated module, <assignment
title> is the tile od the assignment, dd/mm/yyyy is the date on which the
assignment is due, and hh:mm is the time when the assignment is due.
6. You should also write a corresponding test class for the Assignment class to
verify the operation of all the methods in the Assignment class.

7. In order for the system to work, the Model class needs a number of methods
added. The view code checks whether these methods exist and reports an error
message if the required method does not exist. These methods are:
a. addAssignment: This method should create an Assignment object
using the currently selected Module object and the parameter data
passed to the method. It should return a reference to the created
Assignment object. The parameters for this method are a String title,
an integer day number, an integer month number, and integer year
number, an integer hour number, and an integer percentage. These
parameter values should be validated according to the rules below and
the object should only be created if the parameters are valid. NOTE:
The view code will use the returned reference to output a message to
say that the object was successfully created.
b. modifyAssignment: This method should modify the current
assignment object if one has been created. If there is no current
assignment object, it should do nothing. This method receives the same
parameters as the addAssignment method and it should validate the
parameter data in the same way as the addAssignment method. This
method should return a reference to the modified object.
c. getCurrentAssignment: This method should return a reference to the
current assignment. There are no parameters for this method. If there is
no current assignment, it should return null.
d. clearAssignment: This method should clear the currently selected
assignment. There are no parameters for this method and no return
value. This will have the effect of clearing the input fields on the
Assignment data entry panel. The following get… methods need to be
implemented to see this effect.
e. getAssignmentTitle: This method should return the title of the current
assignment. There are no parameters for this method. If there is no
current assignment, it should return null.
f. getAssignmentDay: This method should return the day that the current
assignment is due. There are no parameters for this method. If there is
no current assignment, it should return 0.
g. getAssignmentMonth: This method should return the month that the
current assignment is due. There are no parameters for this method. If
there is no current assignment, it should return 0.
h. getAssignmentYear: This method should return the year that the
current assignment is due. There are no parameters for this method. If
there is no current assignment, it should return 0.
i. getAssignmentHour: This method should return the hour that the
current assignment is due. There are no parameters for this method. If
there is no current assignment, it should return 0.
j. getAssignmentMinute: This method should return the minute that the
current assignment is due. There are no parameters for this method. If
there is no current assignment, it should return 0.
k. getAssignmentPercent: This method should return the percentage
assigned to the current assignment. There are no parameters for this
method. If there is no current assignment, it should return 0.
8. Write a test class to verify that the methods added to the Model class work as
expected. You may add additional methods to the model class to support these
methods and your testing.

9. The following data validation rules should be applied:
a. None of the string fields should be blank.
b. Any year fields must be a four digit number (i.e. greater than 999) and
less than or equal to 2016.
c. Any month field should be a number between 1 and 12 inclusive.
d. Any day field should be a number between 1 and 31 inclusive where
the month is January (1), March (3), May (5), July (7), August (8),
October (10), and December (12), between 1 and 30 inclusive where
the month is April (4), June (6), September (9), and November (11),
and between 1 and 28 for February (2). We are ignoring leap years for
this exercise.
e. The hour should be a number between 0 and 23 inclusive.
f. The minute should be a value between 0 and 59 inclusive.
g. The percentage should be a value between 1 and 100 inclusive. In the
next assignment, you will be expected to verify that the total of the
percentages for assignments assigned to a module do not exceed 100.
h. If the data is not valid then you should ensure that you add an error
message saying why the action was not completed. You should also
ensure that the error messages are cleared when the next action is
performed.
i. These validation rules should be verified by your test methods.

(Tests are crossed out as they can be dealt with later on.)

Code:
Model class (Everything to do with assignment was done by me and the rest was already provided):


Assignment class (done by me):


What I'm asking from you is to (if possible) tell me where I'm going wrong and why when I start up the program, I can't add a new assignment, etc.
If you have any further questions, would like me to clear something up or would like me to provide you with the full file then please ask. Thanks in advance for all the help!
4 years ago

Knute Snortum wrote:java.lang.CharSequence is a good guess for the type of dateString, but it can just be a String.

This is assuming that you're setting the date string yourself. Posting your latest code will help with the context.



Ok so I don't seem to have any errors now when it comes to the formatter, a part from the 'dateTime' not being used.
To make things clearer, I'll post the main requirements for the assignment so that then you can understand it more and I'll show you all the declarations as well as the getters and setters that I've made so far. The only thing that I'm wondering on how to do now is how to use that LocalDateTime to actually retrieve each part of the date? Cos my getMonth is not going to retrieve the month that I set at the top at the moment, which I am aware of but not sure how to fix.

Code:

Main Assignment requirements to do with this part:
a) Create an Assignment class that can be used to create assignment objects. The
class should have fields for a reference to the module associated with the
assignment, the title of the assignment, the date when the assignment is due,
the time when the assignment is due, and the percentage of the module grade
that the assignment counts for.
b) The Assignment class should have an appropriate constructor method that
enables all the fields to be set. All fields should also have getters and setter
methods for each of the fields.

Thanks.

@UP
These are just some of the validations that will have to be put in place though so I don't think it's as simple as you say unfortunately but I am no Java expert as you can tell so if you have any suggestions then I'd be more than happy to discuss them.
4 years ago

Knute Snortum wrote:You should use the parse() method of LocalDateTime. Assuming your date string is in a variable called dateString...



Is this the correct way to do this?



If so, where should I now declare what each of the parameters is (as in day = 10, etc.)?
If not, then where have I gone wrong?

Thanks for all the help and sorry for being such a newbie.
4 years ago

Knute Snortum wrote:If you're going to use Java 8, then things change -- they actually get easier.

* Create a DateTimeFormatter with the static ofPattern(PATTERN) method.
* Create a LocalDateTime object with the static parse(DATE_STRING, FORMATTER) method.
* Retrieve data from the LocalDateTime object, e.g. getYear().

* Your date format string is close. If the String to parse is "23/11/2016 at 23:59", the formatter should be "dd/MM/yyyy 'at' HH:mm". The single quote (') surrounds a literal.



Thank you.
This is what I have so far:


But I am obviously making a mistake somewhere since I'm not linking the formatter with the LDT as this is what I was supposed to do right? Also, I am not entirely sure whether I am declaring the parameters correctly (or even if there is a need to do because maybe this is what the formatter was created for?). A little explanation would be useful here.
And when it comes to the toString and equals, I'll leave them for now and worry about them later. I'll just focus on being able to declare the date and then retrieve all its parts properly.
Once again, thanks.
4 years ago