Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need answer to Q-4 only please. i have the code for Q1-Q3 posted below. Question: Q1: The first part of the assignment involves crea... Q1:

Need answer to Q-4 only please. i have the code for Q1-Q3 posted below.

Question: Q1: The first part of the assignment involves crea...

Q1: The first part of the assignment involves creating very basic GUI controls: a frame, panel, and slider. Create a JFrame. Add a title to the frame called "Bus Route Reservation". Set the frame size to 475 by 500 pixels. Create a JPanel inside of the frame. You will be adding all of your UI components to this JPanel. [6 pts] Inside of the panel, create a label that says "Choose Color." Right below it, make a slider that changes the background color of the panel when the slider is moved to a particular color. The slider values should be RGB color values from 0 to 255 (or black to white). Assume that the RGB values for red, green, and blue are the same value - for instance: (240, 240, 240). The slider label's text should be replaced with the RGB value that the slider is currently on. [8 pts] Q2: Add functionality to search for an Itinerary. Create labels and text fields that allow the user to enter a bus type, a source bus station, and a destination bus station, as well as the departure time and the arrival time to search for one or more available bus routes in an Itinerary. Also, create a ComboBox (see Chapter 11) that will allow the user to select from a list of bus route Itineraries. Each item in the ComboBox represents an Itinerary element, e.g. the first comboBox element is the first Itinerary in the array. [15 pts] Methods: Your BusFrame class should include the following methods (The methods that check for validation must have message dialogs indicating to the user if the information entered in each text field is invalid): initComponents() : Method used to initialize UI components and controls default constructor : Inside the default constructor, call the initComponents() method convertTime() : converts time string to a Time object and returns the Time object checkValidBusType() : returns true if entered bus type is valid and exists in BusType enum and can be converted to a BusType object checkValidBusStations(String src, String dest) : will return true if both the source and destination bus stations entered in the text fields exist in BusStation enum checkValidTime(String depTime, String arriveTime) : checks if the time is in valid format (hh:mm a) and returns true if it is in the correct format Make sure all text fields have error checking and exception handling for invalid input. For example, if the user enters an integer as a departure time instead of a String (ie 1200 instead of 12:00 PM), a JOptionPane error message should appear stating, "Incorrect format for departure time." If the bus type they entered is not in the BusType enum, then the option pane should say, "Bus type unavailable. Please choose a different bus type." If the bus station they entered is not in the BusStation enum, then the option pane should say, "Unknown city." Make sure the times are in hh:mm a format. [8 pts] Q3: Create a button that says Search and a button that says View. When the user clicks Search, the combobox will get populated. When the user clicks View, if all the fields are filled out and have valid input, a JOptionPane with a message dialog should appear stating, "Bus route search successful!" The frame should open a new JFrame with the title, "Bus Route Information," and a size of 475 by 500 pixels. This frame has the bus route information displayed in a JTextArea. [6 pts] Q4: In the RouteFrame class, you will need to display information for each BusRoute in the Itinerary [7 points]. The addTo() method shown in the UML diagram is used to add information to the JTextArea for both one-way and connecting bus routes (when applicable). It takes in a String val, which is the string of information that needs to be appended to the JTextArea. (For instance, if you would like to append the String "Bus", that is the String that needs to be passed in inside your addTo() method call in the RouteFrame class). You need to display the following information in a JTextArea: Bus Type: Display the bus type Departure Bus Station: Display the source bus station Departure City: Display the departure city based on the departure bus station (use the getBusStationCity() method you implemented in the Unit 2/3 assignment to get the bus station city) Destination Bus Station: Display the destination bus station Destination City: Display the destination city based on the destination bus station (use the getBusStationCity() method again) Departure Time: Display the departure time Arrival Time: Display the arrival time Bus Number: Display the bus number Cost: Display the total cost, which should be computed by the Itinerary object. Here is the base file Bus.Frame.java import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Locale; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.JTextField; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class BusFrame extends JFrame { /* Declare your UI variables and other class level variables at the class level (outside of the main method). These include GUI controls, such as a panel, text fields, combo box, slider, labels, and buttons. */ private JPanel panel1; private Time time; /* Place all of your UI initialization code inside the initComponents() method. Also, add UI components to the panel. (For example: panel = new JPanel(); ) */ public void initComponents() { panel1 = new JPanel(); panel1.setLayout(null); add(panel1); // Create listeners for each button/control and add action listeners to each UI control ActionListener comboListener = new ComboListener(); ActionListener viewListener = new ViewListener(); ActionListener searchListener = new SearchListener(); ChangeListener sliderListener = new SliderListener(); } public BusFrame() { initComponents(); } /** * Converts the time String to a Time object. Uncomment this method and * implement it * */ /* private Time convertTime() { // Modify this method so that it returns the correct time Time time = new Time(hours, minutes); return time; } */ /** * SliderListener class that changes the color of the panel to a grayscale * color when the slider is moved * */ class SliderListener implements ChangeListener { public void stateChanged(ChangeEvent event) { } } /** * ComboListener class used to get the selected option in the itineraries * combobox * */ class ComboListener implements ActionListener { public void actionPerformed(ActionEvent event) { } } /** * SearchListener class is used to populate the itineraries combobox * (validating text input is not required, but recommended) * */ class SearchListener implements ActionListener { public void actionPerformed(ActionEvent event) { } } /** * ViewListener class validates bus type, source and destination bus * stations, departure and arrival times, creates an instance of RouteFrame * and opens the route frame that shows the results of the bus route search * */ class ViewListener implements ActionListener { public void actionPerformed(ActionEvent event) { } } /** * Checks if the bus type entered in the bus type text field can be * converted to a busType object * * @return true if entered bus type is valid and exists in busType enum * */ private boolean checkValidBusType() { return false; } /** * Checks if source and destination bus stations entered in the text fields * exist in busStation enum * * @param src The entered source (departure) bus station * @param dest The entered destination bus station * @return true if they are both valid bus station * */ private boolean checkValidBusStations(String src, String dest) { return false; } /** * Checks if the time is in valid format (hh:mm a) * * @param depTime The departure time text field * @param arriveTime The arrival time text field * */ private boolean checkValidTime(String depTime, String arriveTime) { return false; } // Initialize BusFrame and set its properties in the main method (size, whether it is resizable, title, default close operation, and visibility) public static void main(String[] args) { BusFrame frame = new BusFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }

Code for Q1-Q3

import java.awt.EventQueue; import java.awt.Color;

import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.JLabel;

public class Itinerary {

private JFrame frame; private JSlider sliderR,sliderG,sliderB; private JPanel panel; private JLabel lblChooseColor;

/** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Itinerary window = new Itinerary(); window.frame.setTitle("Bus Route Reservation"); window.frame.setSize(475,500); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }

/** * Create the application. */ public Itinerary() { initialize(); }

/** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); panel = new JPanel(); panel.setBounds(0, 0, 475, 500); panel.setBackground(new Color(0, 0, 0)); Color color = new Color(0,0,0); // panel.setBackground(color); frame.getContentPane().add(panel); panel.setLayout(null); sliderR = getSlider(0, 255, 0, 50, 5); sliderR.setBounds(35, 63, 200, 50); panel.add(sliderR); sliderG = getSlider(0, 255, 0, 50, 5); sliderG.setBounds(245, 63, 200, 50); panel.add(sliderG); sliderB = getSlider(0, 255, 0, 50, 5); sliderB.setBounds(35, 119, 200, 50); panel.add(sliderB); lblChooseColor = new JLabel("Choose Color"); lblChooseColor.setBounds(35, 23, 131, 14); panel.add(lblChooseColor); // // JSlider slider = new JSlider(); // slider.setBounds(35, 63, 200, 26); // panel.add(slider); // // JSlider slider_1 = new JSlider(); // slider_1.setBounds(245, 63, 200, 26); // panel.add(slider_1); // // JSlider slider_2 = new JSlider(); // slider_2.setBounds(35, 119, 200, 26); // panel.add(slider_2); } public JSlider getSlider(int min, int max, int init, int mjrTkSp, int mnrTkSp) { JSlider slider = new JSlider(JSlider.HORIZONTAL, min, max, init); slider.setBounds(21, 107, 200, 45); slider.setPaintTicks(true); slider.setMajorTickSpacing(mjrTkSp); slider.setMinorTickSpacing(mnrTkSp); slider.setPaintLabels(true); slider.addChangeListener(new SliderListener()); return slider; } class SliderListener implements ChangeListener { public void stateChanged(ChangeEvent e) { JSlider slider = (JSlider) e.getSource();

if (slider == sliderR) { panel.setBackground(new Color(sliderR.getValue(),sliderG.getValue(),sliderB.getValue())); lblChooseColor.setText(Integer.toString(new Color(sliderR.getValue(),sliderG.getValue(),sliderB.getValue()) .getRGB() & 0xffffff, 16)); } else if (slider == sliderG) { panel.setBackground(new Color(sliderR.getValue(),sliderG.getValue(),sliderB.getValue())); lblChooseColor.setText(Integer.toString(new Color(sliderR.getValue(),sliderG.getValue(),sliderB.getValue()) .getRGB() & 0xffffff, 16)); } else if (slider == sliderB) { panel.setBackground(new Color(sliderR.getValue(),sliderG.getValue(),sliderB.getValue())); lblChooseColor.setText(Integer.toString(new Color(sliderR.getValue(),sliderG.getValue(),sliderB.getValue()) .getRGB() & 0xffffff, 16)); } } } }

Sample out in the end:

image text in transcribed

Bus Route Reservation Bus Type: Departure Bus Station: Destination Bus Station: Departure Time: Arrival Time: Select Itinerary object BoltBus SAN 07:10 AM 11:00 AM Search Success Bus route search successful! OK Bus Route Information Bus Route Information Bus Type: BoltBus Departure Bus Station: SAN Departure City: San Diego Destination Bus Station: LAX Destination City: Los Angeles Departure Time: 7:10AM Arrival Time: 10:22AM Cost: $50.00

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Statistical And Scientific Database Management International Working Conference Ssdbm Rome Italy June 21 23 1988 Proceedings Lncs 339

Authors: Maurizio Rafanelli ,John C. Klensin ,Per Svensson

1st Edition

354050575X, 978-3540505754

More Books

Students also viewed these Databases questions

Question

Explain methods of metal extraction with examples.

Answered: 1 week ago

Question

Does it avoid use of underlining?

Answered: 1 week ago