Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write code for a JOptionPane dialog that will ask whether the user is at leas

ID: 3912404 • Letter: 1

Question

1. Write code for a JOptionPane dialog that will ask whether the user is at least 18 years of age. Then set the boolean variable adult equal to either true, if the user is at least 18, or false otherwise. Include the declaration of the variable adult. 2 Write a code to draw a colored frowning face. 3. Use showConfirmDialog box to take user confirmation for finding the factorial of a number. If the user confirms Yes, Use dialog boxes to take the input of a number and displaying it's factorial 4. Write a program that inputs two strings that represents a time of day using the format HH:MM:SS AM or PM and then outputs the time elapsed from the first to the second time in minutes and seconds. For example, given the strings: 1. 11:58:1O PM 2. 12:02:15 AM The program should output that the time elapsed is 4 minutes and 5 seconds. Use dialog boxes.

Explanation / Answer

I have made 4 files for each question: smiley.java, Adult.java, Factorial.java,Time.java

EDIT: I have gone over the Time.java file and made some changes in this answer. Hope it helps!

smiley.java:

import java.awt.*;
public class smiley extends Frame
{
public smiley()
{
    setTitle("Smiley Face");
    setSize(500, 500);
    setVisible(true);       
}
public void paint(Graphics g)
{
    g.setColor(Color.pink);      
    g.fillOval(10,40,250,250);      // pinky round face
    g.setColor(Color.black);         // lines will be in black
           
    g.fillOval(75, 80, 32, 32);       // left eye
    g.fillOval(180, 80, 32, 32);     // right eye
              
g.drawArc(110,150,100,100,0,180);

g.drawArc(70,50,50,20,180,180);

g.drawArc(170,50,50,20,180,180);

}
public static void main(String args[])
{
    new smiley();
}
}

Adult.java:

    import javax.swing.*;
    public class Adult {
    JFrame f;
    Boolean adult=false;
    Adult(){
        f=new JFrame();
            String name=JOptionPane.showInputDialog(f,"Enter Age");
            if(Integer.parseInt(name)>=18){
                adult=true;
            }    
        JOptionPane.showMessageDialog(f,"Adult? "+adult);                
    }
    public static void main(String[] args) {
        new Adult();
    }
    }

Factorial.java:

    import javax.swing.*;
    import java.awt.event.*;
    public class Factorial extends WindowAdapter{
    JFrame f;
    Factorial(){
        f=new JFrame();

        f.setSize(300, 300);
         int a=JOptionPane.showConfirmDialog(f,"Do you want to calculate the factorial of a number?");
        if(a==JOptionPane.YES_OPTION){
            String number=JOptionPane.showInputDialog(f,"Enter number:");
          
            try{int fact = factorial(Integer.parseInt(number));

            JOptionPane.showMessageDialog(f,"Factorial of "+number+" is "+fact);                

            }
            catch(Exception e){
            JOptionPane.showMessageDialog(f,"Enter a valid number");
            }

        }
        else{
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    static int factorial(int n)
    {
        int res = 1, i;
        for (i=2; i<=n; i++)
            res *= i;
        return res;
    }
    public void windowClosing(WindowEvent e) {
        int a=JOptionPane.showConfirmDialog(f,"Are you sure?");
    if(a==JOptionPane.YES_OPTION){
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
    public static void main(String[] args) {
        new Factorial();
    }   
    }

Time.java:

   

import javax.swing.*;
import java.awt.event.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class Time extends WindowAdapter{
    JFrame f;
   int check = 0;
   Time(){
   f=new JFrame();
   f.setSize(300, 300);
   while(check==0){
      String time1=JOptionPane.showInputDialog(f,"Enter first time in "HH:MM:SS AM"(or PM) format :");
       if(time1 == null || (time1 != null && ("".equals(time1)))) {
       break;
       }

       if(!isValidFormat("hh:mm:ss a",time1)){
           JOptionPane.showMessageDialog(f,"Enter time in valid format"); continue;
       }
       String time2=JOptionPane.showInputDialog(f,"Enter second time in "HH:MM:SS AM"(or PM) format :");
       if(time2 == null || (time2 != null && ("".equals(time2)))) {
           break;
       }
       if(!isValidFormat("hh:mm:ss a",time2)){
       JOptionPane.showMessageDialog(f,"Enter time in valid format"); continue;
       }
       try{
           SimpleDateFormat date12Format = new SimpleDateFormat("hh:mm:ss a");
       SimpleDateFormat date24Format = new SimpleDateFormat("HH:mm:ss");
       String date1 = date24Format.format(date12Format.parse(time1));
       String date2 = date24Format.format(date12Format.parse(time2));
       Date d1 = date24Format.parse(date1);
       Date d2 = date24Format.parse(date2);
       getTimeDiff(d1,d2);
       check=1;
       } catch(Exception e){
       // e.printStackTrace();
       }
       }
       }
   public boolean isValidFormat(String format, String value) {
   Date date = null;
   try {
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      date = sdf.parse(value);
      if (!value.equals(sdf.format(date)))
       {
       date = null;
       // getTimeDiff(d1,d2);
       }
       }
          catch (Exception ex) { // ex.printStackTrace();
          }
           return date != null;
       }
   public void getTimeDiff(Date dateOne, Date dateTwo) {
    // String diff = "";
    long diff = dateTwo.getTime() - dateOne.getTime();
    long diffSeconds = diff / 1000 % 60;
       long diffMinutes = diff / (60 * 1000);
       // long diffHours = diff / (60 * 60 * 1000) % 24;
       String message = "The time elapsed is: ";
       // if(diffHours>0){
       //    message = message + diffHours + " hours ";
       // }
       if(diffMinutes>0){
       message = message + diffMinutes + " minutes ";
       }
       if(diffSeconds>0){
       message = message + diffSeconds + " seconds ";
       }
       JOptionPane.showMessageDialog(f,message);
       }
  
       public static void main(String[] args) {
           new Time();
           System.exit(0);
       }
   }