Reputation: 45
I'm suppoused to create "clocks" collection and change it's minute Value by 1. I've created Clock class (taking 3 int as parameters) and put them to LinkedList. But when I try to get values of object it results with error... Here is my idea (yes I know i have to add code that will change hour if minute gets bigger than 60):
public static void main (String[] args) throws java.lang.Exception{
Random randomizer = new Random();
List <Clock> Clocks = new LinkedList <Clock>();
for (int i=0; i <randomizer.nextInt(9)+1; i++){
Clock clock = new Clock(randomizer.nextInt(24)+, randomizer.nextInt(60), randomizer.nextInt(60));
Clocks.add(clock);
}
for (Clock clock : Clocks) {
clock.checkTime();//this method from Clock class just prints on the console randomized time separated with ":".
clock.set(clock.getHour(), clock.getMinute()+1, clock.getSecond());
}
}
Is there any way to change just one of these 3 int?
I also considered creating another class "time" that would be then turned to String and the Clock using this String instead of 3 ints. But I stil would need code to extract ints from String and change them... So I decided to not go this way.
Upvotes: 1
Views: 46
Reputation: 150
I tested this out (code at the bottom) and it works without any errors. The only change I had to make to your code was removing the +
from Clock clock = new Clock(randomizer.nextInt(24)+, randomizer.nextInt(60), randomizer.nextInt(60));
Could you provide more information on the error getting the values of the objects?
As to changing just one of the numbers on the clock, you could add one or more methods to the Clock
class to increment the values. (e.g. get/set for each of the 3 ints)
...
public void setH(int h) {
this.h = h;
}
public void setM(int m) {
this.m = m;
}
public void setS(int s) {
this.s = s;
}
...
Since you probably just want to increment the values, you can do something like this in the Clock
class.
...
public void increment(int h, int m, int s) {
this.h += h;
this.m += m;
this.s += s;
}
public void incrementH(){
this.h++;
}
public void incrementM(){
this.m++;
}
public void incrementS(){
this.s++;
}
public void tick() {
/* this.s++;
if (this.s >= 60) {
this.s = 0;
this.m++;
} */
// just the minutes
this.m++;
if (this.m >= 60) {
this.m = 0;
this.h++;
}
}
...
My implementation of your code
import java.util.Random;
import java.util.List;
import java.util.LinkedList;
public class HelloWorld {
public static void main (String[] args) throws java.lang.Exception{
Random randomizer = new Random();
List <Clock> Clocks = new LinkedList <Clock>();
for (int i=0; i <randomizer.nextInt(9)+1; i++){
Clock clock = new Clock(randomizer.nextInt(24), randomizer.nextInt(60), randomizer.nextInt(60));
Clocks.add(clock);
}
for (Clock clock : Clocks) {
clock.checkTime();//this method from Clock class just prints on the console randomized time separated with ":".
clock.set(clock.getHour(), clock.getMinute()+1, clock.getSecond());
}
}
}
class Clock {
private int h, m, s;
public Clock(int h, int m, int s) {
set(h, m, s);
}
public void checkTime() {
System.out.println(h + " " + m + " " + s);
}
public void set(int h, int m, int s) {
this.h = h;
this.m = m;
this.s = s;
}
public int getHour() {
return h;
}
public int getMinute() {
return m;
}
public int getSecond() {
return s;
}
}
Upvotes: 1