In this chapter, the class Date was designed to implement the date in a program,
ID: 3584334 • Letter: I
Question
In this chapter, the class Date was designed to implement the date in a program, but the method setDate and the constructor with parameters do not check whether the date is valid before storing the date in the data member. Rewrite the definition of the method setDate and the constructor with parameters so that the values o month, day, and year are checked before storing the date into the data members. Add a method isLeapYear to check whether a year is a leap year. Then, write a test program to test your clock.Explanation / Answer
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
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(10,10);
clock1.draw(g2);
}
}
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;
}
to determine if it was a leap year, add this class to program
public class DetermineLeapYearExample {
public static void main(String[] args) {
int year = 2004;
if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
System.out.println("Year " + year + " is a leap year");
else
System.out.println("Year " + year + " is not a leap year");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.