This site requires JavaScript, please enable it in your browser!
Greenfoot back
Nosson1459
Nosson1459 wrote ...

2016/12/12

Conversion

Nosson1459 Nosson1459

2016/12/12

#
How can I convert a double variable that gets increased by 1000 a second (or 1 every second but has three decimal points) into an int (or whatever type variable works but preferably an int) that will increase by one hundred every second and after it increased by one hundred (the second) I do what I want and reset it to 0? (My problem with my way of doing it, is that my double variable is not being equal to every of those 1000 numbers it's probably jumping over a lot, so I can't just do
1
if (oneThousand%10.0==0) hundred++;
. Just in case here is a little of what you might ask for:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
import java.text.*;
 
/**
 * This class should be added in to the world when needed, and will continue displaying the amount of seconds that elapsed since the addition to the world.
 *
 * @author Nosson1459
 * @version 12/6/2016
 */
public class Timer extends Actor
{
    private long startTime; // this is the starting time of the scenario
    private long stopTime; // this is for figuring out the diffrence in time from beginning to now
    private double elapsedTime=0.0000; // this is what timer is equal
    private double millis=0.0000;
    private int hun=00;
    private int second=00;
    private int minute=00;
    private int hour=00;
    private static final Color transparent = new Color(0,0,0,0);
    private int a=0;
    private String prefix;
    private GreenfootImage time;
    public Timer(String prefix)
    {
        this.prefix=prefix;
        updateTime();
    }
    /**
     * Act - do whatever the Timer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (a==0)
        {
            startTime=System.currentTimeMillis();
            a=1; // this "if" is so that startTime is initialized only after "Run" is pressed but still only initialized once
        }
        reset();
        updateTime();
    }   
    /**
     * Update the image on screen to show seconds stopwatch
     */
    private void updateTime()
    {
        Stopwatch world=(Stopwatch)getWorld();
        if (a != 0)
        {
            if (world.stopped==true)
            {
                startTime+=(world.startedTime-world.stoppedTime);
                world.stopped=false;
            }
            if (world.click==true && world.middleStop==true && world.buttonStopped==false)
            {
                startTime+=(world.startedMiddle-world.stoppedMiddle);
                world.click=false;
                world.middleStop=false;
            }
            if (world.buttonStopped==false)
            {
                stopTime=System.currentTimeMillis(); //gets current time in milliseconds
                elapsedTime=(stopTime-startTime); //converts time to seconds
            }
            if (elapsedTime%10.0==0)
            {
                hun++;
            }
            if (hun==100)
            {
                second++;
                hun=00;
            }
            if (second==60)
            {
                minute++;
                second=00;
            }
            if (minute==60)
            {
                hour++;
                minute=00;
            }
        }
        time=new GreenfootImage(prefix + new DecimalFormat("00").format(hour) + ":" + new DecimalFormat("00").format(minute) + ":" + new DecimalFormat("00").format(second) + ":" + new DecimalFormat("00").format(hun), 25, Color.BLACK, transparent);
        // the above line makes the timer image, the decimal format makes the timer only have one decimal place (0.0) (you could do more if you do (0.00) etc.
        setImage(time);
    }
    private void reset()
    {
        Stopwatch world=(Stopwatch)getWorld();
        if (world.reset==true)
        {
            a=0;
            elapsedTime=0.0000;
            millis=0.0000;
            hun=00;
            second=00;
            minute=00;
            hour=00;
        }
    }
    // new DecimalFormat("00").format(hun)
}
WillyRed WillyRed

2016/12/12

#
So the problem is that it is skipping the 1000 mark?
danpost danpost

2016/12/12

#
You do not need all these fields -- elapsed, millis, hun, second, minute and hour. The one you need is startTime. Everything else should be local to the method ('updateTime') to determine what is currently to be displayed. Determine what to display every act cycle and you will not have to worry about skipping the 1000 marks. Calculate elapsed time first and make it an int value:
1
int elapsedTime = (int)(System.currentTimeMillis()-startTime);
Then determine each part and subtract the results as you process them. For a start:
1
2
3
4
int hours = elapsedTime/3600000;
elapsedTime -= hours*3600000;
int minutes = elapsedTime/60000;
// etc.
The 'a' field will only have two values '1' and '0'. It would be better to use a boolean value for it, call it 'running'.
Nosson1459 Nosson1459

2016/12/13

#
Thanks a lot. It works but it had to be like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
import java.text.*;
 
/**
 * This class should be added in to the world when needed, and will continue displaying the amount of seconds that elapsed since the addition to the world.
 *
 * @author Nosson1459
 * @version 12/6/2016
 */
public class Timer extends Actor
{
    private long startTime; // this is the starting time of the scenario
    private int elapsedTime=00; // this is what timer is equal
    int hour=00;
    int minute=00;
    int second=00;
    int hun=00;
    private static final Color transparent = new Color(0,0,0,0);
    private boolean running=false;
    private String prefix;
    private GreenfootImage time;
    public Timer(String prefix)
    {
        this.prefix=prefix;
        updateTime();
    }
    /**
     * Act - do whatever the Timer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (running==false)
        {
            startTime=System.currentTimeMillis();
            running=true; // this "if" is so that startTime is initialized only after "Run" is pressed but still only initialized once
        }
        reset();
        updateTime();
    }   
    /**
     * Update the image on screen to show seconds stopwatch
     */
    private void updateTime()
    {
        Stopwatch world=(Stopwatch)getWorld();
        if (running==true)
        {
            if (world.stopped==true)
            {
                startTime+=(world.startedTime-world.stoppedTime);
                world.stopped=false;
            }
            if (world.click==true && world.middleStop==true && world.buttonStopped==false)
            {
                startTime+=(world.startedMiddle-world.stoppedMiddle);
                world.click=false;
                world.middleStop=false;
            }
            if (world.buttonStopped==false)
            {
                elapsedTime=(int)(System.currentTimeMillis()-startTime); //converts time to seconds
            }
            if (world.buttonStopped==false && world.middleStop==false && world.click==false)
            {
                hour=elapsedTime/3600000;
                elapsedTime-=hour*3600000;
                minute=elapsedTime/60000;
                elapsedTime-=minute*60000;
                second=elapsedTime/1000;
                elapsedTime-=second*1000;
                hun=elapsedTime/10;
                elapsedTime-=hun*10;
            }
        }
        time=new GreenfootImage(prefix + "\n" + new DecimalFormat("00").format(hour) + ":" + new DecimalFormat("00").format(minute) + ":" + new DecimalFormat("00").format(second) + ":" + new DecimalFormat("00").format(hun), 40, Color.BLACK, transparent);
        // the above line makes the timer image, the decimal format makes the timer only have one decimal place (0.0) (you could do more if you do (0.00) etc.
        setImage(time);
    }
    /**
     * Reset the stopwatch to 00:00:00:00
     */
    private void reset()
    {
        Stopwatch world=(Stopwatch)getWorld();
        if (world.reset==true)
        {
            running=false;
            elapsedTime=00;
            hun=00;
            second=00;
            minute=00;
            hour=00;
        }
    }
}
If I make the variables local to the method then the 'if' from the reset() method has to be in updateTime() and that gave me a nullPointerException, so I did it as shown above and it works (I hope/think).
You need to login to post a reply.