• Post Reply Bookmark Topic Watch Topic
  • New Topic

Suppose someone can help me really quick  RSS feed

 
Kyle Grady
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's having to do with arrays and exteded

Here is what i'm trying to win at:

Declare an interface called Incrementable which represents items that store an integer that can be incremented in some way. THe interface has a method called increment that increments the value and a method called getValue that returns the value.
Once you have written the interface, wite two classes called SequentialIncrementer begins its value at 0 and increases it by 1 each time it is incremented.
The RandomIncrementer begins its value at a random integer and changes it to a new random integer each time it is incremented.

This is my code


IncrementMain



RandomIncrementer


Incrementable


SequentialIncermenter



When i'm running this code normally for the SequentialIncermenter it returns an array of 0's when i want them to be incremented.

For the random, it is difficult to cast an array size to a random number so that it could pop up on the zero based index location and grow from there.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public int[] inOrder(int[] incrementation){
for(int i = 0; i < incrementation.length; i++){
incrementation[i]++;
}
return incrementation;
}


In this you are incrementing the values but dint storing back to the array. So it is not getting stored anywhere and when you returning the
array it have original values only.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont understand what you want to do with RandomIncrementer. I will help you after you goal is.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Grady wrote:When i'm running this code normally for the SequentialIncermenter it returns an array of 0's when i want them to be incremented.

You're not calling method "inOrder" by supplying an empty array anywhere in your code.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kyle,

I’m a bit puzzled about this assignment. That assignment is, according tou your opening post:

“Declare an interface called Incrementable which represents items that store an integer that can be incremented in some way. THe interface has a method called increment that increments the value and a method called getValue that returns the value.”

I find this description rather vague. Why should an interface represent some item? And how does that “increment” function is supposed to look like?

But apart from that: if I look at your code, I see no interface defined, only a class “Incrementable”, that has no method “increment()”.

So, your answer does not fulfill the assignment.

Let’s look at the mandatory method “increment()”.
Since that method must increment some integer (or more than one integer?) it must have access to some method that tells it how to increment. From the assignment it is clear what must be incremented (is it?).

So, my first bet would be something like:


You see, I run into several problems here. If I define a variable in a Java 8 interface, then that variable is static and final, among others.

That rules out a Java 8 style interface. But in Java < 8 interfaces we have no variables. And we still do not know how to define this ‘incrementer()’ method.

Let’s stick to your implementation, an abstract class. Say:


As you see, we actually still haven no good idea how this incrementer() method shoud look like. And I have an int as member, while you have an array of integers.

It is Monday early in Holland, I’ve just arrived at the office, had no coffee yet, so I might be completely daft and seeing things completely wrong, but can someone shed some lights on my comments? Thanks.

Greetz,
Piet


 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoid doing arithmetic with Math#random, despite waht it say sin the Java™ Tutorials. It is very error‑prone. If you find this old thread, there is a long discussion. I still prefer the instance nextInt method.
You can also get a stream of “random” numbers with the ints longs or doubles methods of Random. They permit you to decide the range of possible outputs and the size of the stream.
 
Kyle Grady
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manage to get this right - with the use of random with the increment, which generates a number for the array and starts at that position.

Tell me what you think?

IncrementMain



Incrementable



RandomIncrementer



SequentialIncermenter

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still do not have an Interface called Incrementable, so you haven't done what the assignment asked you to do.
 
Kyle Grady
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That boggles my mind, can you have a console work with an interface and take the data- and if so how would the interface look like?

I'm using Eclipse as my personal coding program.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this section of the Java™ Tutorials.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!