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

a. Add code to AgeCalculator.java to get current date and then use the current y

ID: 3649918 • Letter: A

Question

a. Add code to AgeCalculator.java to get current date and then use the current year to validate the birth year the user enters.
b. Add code to create, format and print user

Explanation / Answer

//Made at www.mycoding.net import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Date; import javax.swing.JOptionPane; public class MyProject implements ActionListener{ static TextField dayT,monthT,yearT,age; //Creating TextFields static Frame frame; //Creating a frame static Label dayL,monthL,yearL; //Creating Label //This will be our constructor to Initialize the frame and adding components to it. public MyProject(int x,int y,int h, int br){ Button b= new Button("Tell me my age!"); //Initializing button Button exit = new Button("Exit"); //Initializing button Button clear = new Button("Clear"); //Initializing button dayL = new Label("Day: "); //Initializing Label dayT = new TextField ("Day"); //Initializing TextField monthL = new Label("Month: "); //Initializing Label monthT = new TextField ("Month"); //Initializing TextField yearL = new Label("Year: "); //Initializing Label yearT = new TextField("Year"); //Initializing TextField age = new TextField(); //Initializing TextField frame = new Frame("Age Calculator - MyCoding.net"); //Initializing our frame and setting the tittle. frame.setVisible(true); //Setting frame visibility to true. frame.setLayout(new FlowLayout()); //Setting LayOut frame.setBounds(x, y,h, br); //Setting bounds, i.e screen location, height,breadth frame.add(dayL); // Adding Label dayL to frame frame.add(dayT); // Adding TextField dayT to frame frame.add(monthL); // Adding Label monthL to frame frame.add(monthT); // Adding TextField monthT to frame frame.add(yearL); //Adding Label yearL to frame frame.add(yearT); // Adding TextField yearT to frame frame.add(age); //Adding TextField age to frame frame.add(b); //Adding button b frame.add(exit); //Adding button exit frame.add(clear); //Adding button clear //Adding ActionListeners to the buttons in the frame. clear.addActionListener(new Clear()); exit.addActionListener(new Exit()); b.addActionListener(this); } //When clear button is clicked, this class will be called. public class Clear implements ActionListener{ public void actionPerformed(ActionEvent ae) { dayT.setText(""); monthT.setText(""); yearT.setText(""); } } //When exit button is clicked, this class will be called. public class Exit implements ActionListener{ public void actionPerformed(ActionEvent e) { frame.dispose(); //Exits frame } } //When calculate button is clicked, this class will be called. public void actionPerformed(ActionEvent e) { try{String a = dayT.getText(); //Gets date entered by user String b = monthT.getText(); //Gets month entered by user String c = yearT.getText(); //Gets year entered by user //Converts all strings to ints. int day = Integer.parseInt(a); int month = Integer.parseInt(b); int year = Integer.parseInt(c); if(day>31 || month > 12 || day == 0 || month==0) { JOptionPane.showMessageDialog(null,"Please Enter a valid date!"); } else{ int answer = CalculateAge(day,month,year); //Calling the function which will calculate age for us. String ans_op = String.valueOf(answer); //Converting answer from int to string. age.setText(ans_op);} }//Setting age textfield to the calculated answer catch(NumberFormatException ae){ JOptionPane.showMessageDialog(null,"Please Enter a valid date!"); //Prints error message if user inputs non-int value } } //Function to calculate age public int CalculateAge(int day,int month,int year){ Date d = new Date(); int current_day = d.getDate();//Gets current date int current_month = d.getMonth()+1; //Gets current month int current_year = d.getYear()+1900; //Gets current year int answer = (current_year - year); //Calculates age if(current_month>month) { answer--; } if(current_month == month && current_day
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote