0

I have trouble trying to create a clock java program that asks for user's input for hours and minutes.

When it asks the user for their input, the clock program is suppose to set it to that time using the hour hand and the minute hand, but for some reason I keep getting an invalid input message.

I don't really know where the problem is coming from and how to fix it :( Any input/advice/help would be greatly appreciated!!
(I am still a new java programmer so please excuse my horrible structure x.x )

Here is my code:
Clock.java

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class Clock
{
    public Clock()
    {
        this.anHour=12;
        this.aMin=0;
    }

    public Clock(double hr, double min)
    {
        this.anHour=hr;
        this.aMin=min;
    }

    public void draw(Graphics g)
    {
        Graphics2D g2=(Graphics2D)g;

        Ellipse2D.Double clock_face = new Ellipse2D.Double(0, 0, 2 * RADIUS, 
                                        2 * RADIUS);
        ((Graphics2D) g).draw(clock_face);

        Point2D.Double center=new Point2D.Double(RADIUS, RADIUS);
        double angle=Math.PI /2 - 2 * Math.PI * aMin / MINUTES_PER_HOUR;
        Point2D.Double minutePoint=new Point2D.Double(RADIUS + MINUTE_HAND
                                    * Math.cos(angle), RADIUS - MINUTE_HAND * Math.sin(angle));
        Line2D.Double minuteHand=new Line2D.Double(center, minutePoint);
        ((Graphics2D) g).draw(minuteHand);

        angle=Math.PI / 2 - 2 * Math.PI * (anHour * MINUTES_PER_HOUR +
                aMin) / (MINUTES_PER_HOUR * HOURS_PER_DAY);
        Point2D.Double hourPoint=new Point2D.Double(RADIUS + HOUR_HAND *
                                    Math.cos(angle), RADIUS - HOUR_HAND * Math.sin(angle));
        Line2D.Double hourHand=new Line2D.Double(center, hourPoint);
        ((Graphics2D) g).draw(hourHand);

        Clock clock1=new Clock(anHour, aMin);
    }

    private double anHour;
    private double aMin;

    final double RADIUS = 100;
    final double MINUTES_PER_HOUR = 60;
    final double HOURS_PER_DAY = 12;
    final double HOUR_HAND = 75;
    final double MINUTE_HAND = 90;
}

ClockApplet.java

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JApplet;
import javax.swing.JOptionPane;

/**
 * This applet draws the clock shape.
 */

public class ClockApplet extends JApplet
{
    public ClockApplet()
    {
        String input;

        input=JOptionPane.showInputDialog(null, "Please enter the time (hh:mm):","Clock Applet", JOptionPane.QUESTION_MESSAGE);

        if((input==null) || (input.equals("")))
        {
            JOptionPane.showMessageDialog(null, "Invalid input", "Alert", JOptionPane.WARNING_MESSAGE);
            System.exit(0);
        }
        else
        {
            int colon1=input.indexOf(":");
            int colon2=input.lastIndexOf(":");

            double hours=findHour(colon1, input);
            double minutes=findMin(colon2, input);

            if(hours!=0 && minutes!=0)
            {
                Clock clock1=new Clock(hours, minutes);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Invalid input", "Alert", JOptionPane.WARNING_MESSAGE);
                System.exit(0);
            }
        }
    }

    public double findHour(int colon1, String input)
    {
        String userHour=input.substring(colon1+1);
        double aHour=Double.parseDouble(userHour);
        double temp1=0.0;

        if(aHour>0 && aHour<=12)
        {
            temp1=aHour;
        }
        else
        {
            return 0;
        }

        return temp1;
    }

    public double findMin(int colon2, String input)
    {
        String userMin=input.substring(colon2+1);
        double aMin=Double.parseDouble(userMin);
        double temp1=0.0;

        if(aMin>0 && aMin<=60)
        {
            temp1=aMin;
        }
        else
        {
            return 0;
        }

        return temp1;
    }

    public void paint(Graphics g)
    {
        Graphics2D g2=(Graphics2D)g;

        Clock clock1=new Clock(5, 40);

        clock1.draw(g2);

    }

}

ClockApplet.html

<applet code="ClockApplet.class" width="50" height="50"> </applet>
2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0

Put a load of print statements into your code, printing the values of the key variables at each stage so you can see where it's going wrong. Focus on the code that parses the user's input. Once you can see exacty what values are being set and used at each stage in the code you will be able to see where and why it's going wrong.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.