Question

Create a File menu in your GUI. Add a file menu to your clockGUI with options...

Create a File menu in your GUI. Add a file menu to your clockGUI with options to open any file for reading ( and displaying the file as in Project2) and one to Quit the

program.You will need a FileMeu Handler classto handle the events from FileMenu. Be sure to use getAbsolutepath() when getting the file from JFileChooser, not getName().

Handle Exceptions

A data file will be provided that will contain errors (eg- hours>23,minutes>59, seconds>59). Create an exception called IllegalClockException (by extending IllegalArgumentException as shown in lecture) and have the constructor of the Clock throw it.Use a try/catch statement to catch this exception in your program and print the erroneous clock

to the console (do not add it to linked lists).

Create a jar file called Project3.jar and submit that to the Blackboard by the due date for full credit.

Language- Java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Clock.java

public class Clock {
private int hours, minutes, seconds;
  
public Clock()
{
this.hours = this.minutes = this.seconds = 0;
}

public Clock(int hours, int minutes, int seconds) {
if(hours > 23 || minutes > 59 || seconds > 59)
throw new IllegalClockException(hours + ":" + minutes + ":" + seconds);
  
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}

public int getHours() {
return hours;
}

public int getMinutes() {
return minutes;
}

public int getSeconds() {
return seconds;
}
  
@Override
public String toString()
{
return(this.hours + ":" + this.minutes + ":" + this.seconds);
}
}

Project3.java (Main class)

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Project3 {
  
private static JFrame mainFrame;
private static JPanel mainPanel, leftPanel, rightPanel;
private static JTextArea errorRes, noErrorRes;
private static JMenuBar menuBar;
private static JMenu menu;
private static JMenuItem openMenu, quitMenu;
  
private static ArrayList<Clock> clocks;
private static ArrayList<String> errorClocks;
  
public static void main(String[] args)
{
clocks = new ArrayList<>();
errorClocks = new ArrayList<>();
  
mainFrame = new JFrame("Clock GUI");
mainPanel = new JPanel(new BorderLayout());
  
menuBar = new JMenuBar();
menu = new JMenu("File");
openMenu = new JMenuItem("Open");
quitMenu = new JMenuItem("Quit");
menu.add(openMenu);
menu.add(quitMenu);
menuBar.add(menu);
mainFrame.setJMenuBar(menuBar);
  
leftPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
noErrorRes = new JTextArea(20, 30);
noErrorRes.setEditable(false);
Border leftBorder = BorderFactory.createTitledBorder("No Error");
JScrollPane leftScroll = new JScrollPane(noErrorRes);
noErrorRes.setBorder(leftBorder);
leftPanel.add(leftScroll);
  
rightPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
errorRes = new JTextArea(20, 30);
errorRes.setEditable(false);
Border rightBorder = BorderFactory.createTitledBorder("Error");
JScrollPane rightScroll = new JScrollPane(errorRes);
errorRes.setBorder(rightBorder);
rightPanel.add(rightScroll);
  
mainPanel.add(leftPanel, BorderLayout.WEST);
mainPanel.add(rightPanel, BorderLayout.EAST);
  
mainFrame.add(mainPanel);
mainFrame.setSize(660, 420);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
  
// action listeners for the menu items
openMenu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("*.txt", "txt"));
int status = fileChooser.showOpenDialog(null);
if(status == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
String filePath = file.getAbsolutePath();
readClockData(filePath);
  
if(clocks.isEmpty())
{
JOptionPane.showMessageDialog(null, "No clock data found in the list!");
return;
}
for(Clock c : clocks)
noErrorRes.append(c.toString() + System.lineSeparator());
  
if(errorClocks.isEmpty())
{
JOptionPane.showMessageDialog(null, "All clock data are good!");
return;
}
for(String s : errorClocks)
errorRes.append(s + System.lineSeparator());
}
}
});
  
quitMenu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
  
private static void readClockData(String filename)
{
Scanner fileReader;
  
try
{
fileReader = new Scanner(new File(filename));
while(fileReader.hasNextLine())
{
String line;
try
{
line = fileReader.nextLine().trim();
String[] data = line.split(":");
if(data.length != 3)
errorClocks.add(line);
else
{
int hours = Integer.parseInt(data[0]);
int minutes = Integer.parseInt(data[1]);
int seconds = Integer.parseInt(data[2]);

clocks.add(new Clock(hours, minutes, seconds));
}
}catch(IllegalClockException ice){
errorClocks.add(ice.getMessage());
}
}
fileReader.close();
}catch(FileNotFoundException fnfe){
JOptionPane.showMessageDialog(null, filename + " could not be found!");
System.exit(0);
}
}
}

********************************************************** SCREENSHOT ******************************************************

Clock GUI File Error 2:30 29:30:59 6:64:34 27:80:75 No Error 2:25:24 1:30:0 13:30:0 15:28:39 5:15:45 2:30:59 8:15:12 6:56:34

Add a comment
Know the answer?
Add Answer to:
Create a File menu in your GUI. Add a file menu to your clockGUI with options...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one me...

    Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...

  • Simple Java Notepad application using Swing GUI Create a help menu with shortcut ctrl+H: -Help me...

    Simple Java Notepad application using Swing GUI Create a help menu with shortcut ctrl+H: -Help menu includes two menu items: 1. About 2. Visit Homepage. Add a separator between these menu items. 1. Create a menu item which is About. Add a short cut for the menu item. It is ctrl+A. -When a user clicks it (an action event occurs), display a show message dialog box. Display the message shown in the figure. Display information icon. 2. Create a menu...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • Please write this in Java, please write comments as you create it, and please follow the...

    Please write this in Java, please write comments as you create it, and please follow the coding instructions. As you are working on this project, add print statements or use the debugger to help you verify what is happening. Write and test the project one line at a time. Before you try to obtain currency exchange rates, obtain your free 32 character access code from this website: https://fixer.io/ Here's the code: 46f27e9668fcdde486f016eee24c554c Choose five international source currencies to monitor. Each...

  • CSC 130 Lab Assignment 8 – Program Menu Create a C source code file named lab8.c...

    CSC 130 Lab Assignment 8 – Program Menu Create a C source code file named lab8.c that implements the following features. Implement this program in stages using stepwise refinement to ensure that it will compile and run as you go. This makes it much easier to debug and understand. This program presents the user a menu of operations that it can perform. The choices are listed and a prompt waits for the user to select a choice by entering a...

  • create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in...

    create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below. The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain...

  • /** * File: GradeCalculator . java * Description: Instances of this class are used to calculate...

    /** * File: GradeCalculator . java * Description: Instances of this class are used to calculate * a course average and a letter grade. In order to calculate * the average and the letter grade, a GradeCalculator must store * two essential pieces of data: the number of grades and the sum * of the grades. Therefore these are declared as object properties * (instance variables). * Each time calcAverage (grade) is called, a new grade is added to *...

  • Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src...

    Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src folder, create a package named: edu.ilstu · Import the following files from T:\it168\Labs\lab13. Note that the .txt file must be in the top level of your project, not inside your src folder. o Student.java o StudentList.java o students.txt Carefully examine the Student.java and StudentList.java files. You are going to complete the StudentList class (updating all necessary Javadoc comments), following the instruction in the code....

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT