Question

In: Computer Science

Create a UI that has a field in which to enter text, a field to display...

Create a UI that has a field in which to enter text, a field to display read-only text and a button. When the button is pressed, take the value of the text box and try to create a month object. If a number was entered, use the constructor that accepts an integer. If alphabetic characters were entered, use the constructor that accepts a string. If nothing is entered, use the no-argument constructor. If a valid month object is created, use the toString function on the month object to display info about the month. Otherwise, display an appropriate error message.

Create a UI with two drop-down lists, a button, and a field to display read-only text. One drop-down has the values 1-12. The other has the names of the months. When the button is pressed, create two-month objects using the appropriate constructors, then display if the months are equal, if month 1 > month 2 or if month 2 > month 1.

TWO SEPERATE USER INTERFACES.

THIS IS TO BE DONE USING JAVA

Solutions

Expert Solution

//Java code

import javax.swing.*;

public class Month {

    private final static String[] monthsName ={"January","February","March","April","May","June","July","August","September","October","November","December"};

    private String monthName;

    public  Month()
    {

        this.monthName = monthsName[0];

    }
    public Month(int monthNumber)
    {
        if(monthNumber>=1 && monthNumber<=12)
        {
            monthName = monthsName[monthNumber-1];
        }
        else
        {

            errorMessage("Invalid month name","MONTH NAME ERROR!");
            return;
        }
    }
    public Month(String monthName)
    {
        setMonthName(monthName);
    }



    public String getMonthName() {
        return monthName;
    }

    public void setMonthName(String monthName)
    {
        boolean isValid = false;
        for (int i = 0; i <monthsName.length ; i++) {
            if(monthsName[i].equalsIgnoreCase(monthName)) {
                isValid = true;
                this.monthName = monthsName[i];
            }
        }

        if(!isValid)
        {
            errorMessage("Invalid month name","MONTH NAME ERROR!");
            return;
        }
    }


    public static  void errorMessage(String message, String title)
    {
        JOptionPane.showMessageDialog(null,message,title,JOptionPane.ERROR_MESSAGE);
    }

}

//========================================

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;

public class MonthGUI extends JFrame {
    JLabel lblPrompt;
    JTextField txtMonth;
    JButton btnDisplay;
    JLabel lblResult;
    public MonthGUI()
    {
        setTitle("Month GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        setSize(400,150);
        setVisible(true);

        lblPrompt = new JLabel("Enter month");
        txtMonth = new JTextField(10);
        btnDisplay = new JButton("Display Month");
        lblResult = new JLabel();
        //Add all components to frame
        add(lblPrompt);
        add(txtMonth);
        add(btnDisplay);
        add(lblResult);

        //add action listener to button
        btnDisplay.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Month object
                Month month;
                String text = txtMonth.getText();
                if(text.equals(""))
                {
                    month = new Month();
                    lblResult.setText("Month is "+month.getMonthName());
                }
                else if(text.matches("[0-9]+"))
                {
                    month = new Month(Integer.parseInt(text));
                    lblResult.setText("Month is "+month.getMonthName());
                }
                else
                {
                    month = new Month(text);
                    lblResult.setText("Month is "+month.getMonthName());
                }
            }
        });
    }
    public static void main(String[] args) throws InvocationTargetException, InterruptedException {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                new MonthGUI();
            }
        });
    }
}

//Output

//Java code 2

import javax.swing.*;

public class Month {

    private final static String[] monthsName ={"January","February","March","April","May","June","July","August","September","October","November","December"};

    private String monthName;

    public  Month()
    {

        this.monthName = monthsName[0];

    }
    public Month(int monthNumber)
    {
        if(monthNumber>=1 && monthNumber<=12)
        {
            monthName = monthsName[monthNumber-1];
        }
        else
        {

            errorMessage("Invalid month name","MONTH NAME ERROR!");
            return;
        }
    }
    public Month(String monthName)
    {
        setMonthName(monthName);
    }



    public String getMonthName() {
        return monthName;
    }

    public void setMonthName(String monthName)
    {
        boolean isValid = false;
        for (int i = 0; i <monthsName.length ; i++) {
            if(monthsName[i].equalsIgnoreCase(monthName)) {
                isValid = true;
                this.monthName = monthsName[i];
            }
        }

        if(!isValid)
        {
            errorMessage("Invalid month name","MONTH NAME ERROR!");
            return;
        }
    }

    public int getIndex(String month)
    {
        for (int i = 0; i <monthsName.length ; i++) {
            if(monthsName[i].equalsIgnoreCase(month))
                return i;
        }
        return 0;
    }
    public static  void errorMessage(String message, String title)
    {
        JOptionPane.showMessageDialog(null,message,title,JOptionPane.ERROR_MESSAGE);
    }

}

//=====================================

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;

public class MonthGUI extends JFrame {
    JComboBox<Integer>cmbMonthNumbers;
    JComboBox<String>cmbMonthName;
    JButton btnDisplay;
    JLabel lblResult;
    Integer[]monthsNumber = {1,2,3,4,5,6,7,8,9,10,11,12};
    String[] monthsName ={"January","February","March","April","May","June","July","August","September","October","November","December"};
    public MonthGUI()
    {
        setTitle("Month GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        setSize(400,150);
        setVisible(true);
        cmbMonthNumbers = new JComboBox<>();
        cmbMonthNumbers.setModel(new DefaultComboBoxModel<Integer>(monthsNumber));

        cmbMonthName = new JComboBox<>();
        cmbMonthName.setModel(new DefaultComboBoxModel<>(monthsName));


        btnDisplay = new JButton("Display Month");
        lblResult = new JLabel();
        //Add all components to frame
        add(cmbMonthName);
        add(cmbMonthNumbers);
        add(btnDisplay);
        add(lblResult);

        //add action listener to button
        btnDisplay.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Month object
                Month month,month1;
                String text1 = cmbMonthName.getSelectedItem().toString();
                String text2  = cmbMonthNumbers.getSelectedItem().toString();
                if(text1.equals(""))
                {
                    month = new Month();

                }
                else if(text1.matches("[0-9]+"))
                {
                    month = new Month(Integer.parseInt(text1));

                }
                else
                {
                    month = new Month(text1);

                }
                if(text2.equals(""))
                {
                    month1 = new Month();

                }
                else if(text2.matches("[0-9]+"))
                {
                    month1 = new Month(Integer.parseInt(text2));

                }
                else
                {
                    month1 = new Month(text2);

                }
                if(month.getIndex(month.getMonthName())> month1.getIndex(month1.getMonthName()))
                {
                    lblResult.setText("Month1 is greater than Month2");
                }
                else  if(month.getIndex(month.getMonthName())< month1.getIndex(month1.getMonthName()))
                {
                    lblResult.setText("Month1 is smaller than Month2");
                }
                else
                {
                    lblResult.setText("Month1 and Month2 are equal.");
                }
            }
        });
    }
    public static void main(String[] args) throws InvocationTargetException, InterruptedException {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                new MonthGUI();
            }
        });
    }
}

//Output

//If you need any help regarding this solution ......... please leave a comment ........ thanks


Related Solutions

Ex. Create a GUI program to fill in the text field the performance time if each...
Ex. Create a GUI program to fill in the text field the performance time if each alignment method is used for 50,000 random integers. Each sort method is indicated by a button:Selection Sort ,Bubble Sort ,Insertion Sort ,Mergesort ,Quicksort ,Counting Sort ,Radix Sort ,Heapsort Write java code and show me the output. Thank you :)
Question 1: Write an AVR program to display text on a 2-line WH2002 LCD display based...
Question 1: Write an AVR program to display text on a 2-line WH2002 LCD display based on the HD44780 controller. Apply the following: The LCD instruction/data bus is connected to PORTD, and control bus is connected to PORTB (RS is connected to PB0, R/W to PB1, E to PB2). Use the #define command to name PORTD as Display_port. Use the #define command to name PORTB as Control_port. The displayed information is stored in the microcontroller Flash ROM (maximum size of...
Allow the user to enter the number of people in the party. Calculate and display the...
Allow the user to enter the number of people in the party. Calculate and display the amount owed by each person if the bill were to be split evenly among the party members. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mdc.tippcalcula"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> ------------------------------- <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="2" android:useDefaultMargins="true" tools:context=".MainActivity"> <EditText android:id="@+id/amountEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:hint="" android:digits="0123456789" android:inputType="number"...
Explain the 3 Emissive flat-panel devices: electroluminescent, plasma display and field-emission display with detail to the...
Explain the 3 Emissive flat-panel devices: electroluminescent, plasma display and field-emission display with detail to the use of phosfur if used
Create an app that stores a set of names. The UI should contain a EditText widget...
Create an app that stores a set of names. The UI should contain a EditText widget and two Buttons (one for adding a name and one for editing a name).   When the app starts up, the first name of the set is used to initialize the EditText widget. The app uses ViewPager so that the user can swipe left to visit the previous name in the set, or swipe right to visit the next name in the set, similar to...
Create a C++ program which will prompt the user to enter a password continually until the...
Create a C++ program which will prompt the user to enter a password continually until the password passes the following tests. Password is 6 chars long Password has at least 1 number If the input does not match the tests, it will input a specific error message. "Pass must be 6 chars long." If the input is allowed, print a message saying "Correct Password Criteria."
Swift - Implement a simple text editor program that includes a text input field (with several...
Swift - Implement a simple text editor program that includes a text input field (with several lines), a text label, and two buttons, called "Open" and "Save". When the button "Save" is touched, the app will save the contents of the text input field to a data file (sample.txt). When the button "Open" is touched, the app will read the content from the data file, (sample.txt)) and feed the content into the text label. Thank you.
Create and display an XML file in Android studio that has -One edittext box the does...
Create and display an XML file in Android studio that has -One edittext box the does NOT use material design -One edittext box that DOES use material design Provide a “hint” for each, and make sure the string that comprises the hint is referenced from the strings.xml file. Observe the differences in how the hints are displayed. Here is sample code for a material design editText: <android.support.design.widget.TextInputLayout         android:id="@+id/input_layout_price1Text"         android:layout_width="80dp"         android:layout_height="40dp"            >     <EditText             android:id="@+id/price1Text"             android:importantForAutofill="no"...
Write a program that will read a line of text. Display all the letters that occure...
Write a program that will read a line of text. Display all the letters that occure in the text, one per line and in alphabetical order, along with the number of times each letter occurs in the text. Use an array of base type int of length 26, so that the element at index 0 contains the number of a’s, the element at index 1 contains the number of b’s, and so forth, Alloow both upperCase and lower Case. Define...
Write a program that asks the user to enter a number. Display the following pattern by...
Write a program that asks the user to enter a number. Display the following pattern by writing lines of asterisks. The first line will have one asterisk, the next two, and so on, with each line having one more asterisk than the previous line, up to the number entered by the user.For example, if the user enters 5, the output would be: * *   * *   *   * *   *   *   * *   *   *   *   * short codes please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT