Design and implement the class Day that implements the day of the week in a prog
ID: 664512 • Letter: D
Question
Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:
A. Set the day.
B. Print the day.
C. Return the day.
D. Return the next day.
E. Return the previous day.
F. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
G. Add the appropriate constructors.
H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G.
I. Write a program to test various operations on the class Day.
I have most of the code written for what I want to do with this program; however I need the previous day and next day to be off the slected day and not the result day. also my result day errors out if I go further than a week.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PaineProg8 extends JFrame
{
private JLabel SelectDay, addDays, resultDay, nextDay, previousDay, emptyLabel, emptyLabel2;
private JTextField addDaysTo, resultDayText, nextDayText, previousDayText;
String[] weekDays = new String[] {"Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"};
JComboBox comboWeekDays = new JComboBox(weekDays);
String selectedDay = (String) comboWeekDays.getSelectedItem();
int selectedIndex = comboWeekDays.getSelectedIndex();
private JButton exitB, calculateB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
//set size of the window
private static final int WIDTH = 300;
private static final int HEIGHT = 300;
public PaineProg8()
{
//assign the respective labels
SelectDay = new JLabel("Select a day",
SwingConstants.LEFT);
addDays = new JLabel("Add Days",
SwingConstants.LEFT );
resultDay = new JLabel("Result day",
SwingConstants.LEFT);
nextDay = new JLabel("Next Day ",
SwingConstants.LEFT);
previousDay = new JLabel("Previous Day ",
SwingConstants.LEFT);
emptyLabel = new JLabel("",
SwingConstants.LEFT);
emptyLabel2 = new JLabel("",
SwingConstants.LEFT);
//assign the respective text fields
addDaysTo = new JTextField(10);
resultDayText = new JTextField(10);
nextDayText = new JTextField(10);
previousDayText =new JTextField(10);
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
setTitle("Days of the Week Calculator");
Container pane = getContentPane();
pane.setLayout(new GridLayout(7, 2));
pane.add(SelectDay);
pane.add(comboWeekDays);
pane.add(addDays);
pane.add(addDaysTo);
pane.add(emptyLabel);
pane.add(calculateB);
pane.add(resultDay);
pane.add(resultDayText);
pane.add(nextDay);
pane.add(nextDayText);
pane.add(previousDay);
pane.add(previousDayText);
pane.add(emptyLabel2);
pane.add(exitB);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int resultDay1, addingDays, onePrev, nxWk;
resultDay1 = Integer.parseInt(addDaysTo.getText());
int ind = selectedIndex + resultDay1;
while (resultDay1 >= 7){
resultDay1 = resultDay1-7;}
resultDay1 = resultDay1 + 1;
String resDay = weekDays[resultDay1];
resultDayText.setText(resDay);
addingDays = resultDay1;
while (addingDays >= 7){
addingDays = addingDays-7;}
int addOndays = addingDays;
String nxDay = weekDays[addOndays];
nextDayText.setText(" " + nxDay);
onePrev = ind -1;
while (onePrev >= 7){
>
int thePrevDay = onePrev;
String prevDay = weekDays[thePrevDay];
previousDayText.setText("" + prevDay );
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PaineProg8 extends JFrame
{
private JLabel SelectDay, addDays, resultDay, nextDay, previousDay, emptyLabel, emptyLabel2;
private JTextField addDaysTo, resultDayText, nextDayText, previousDayText;
String[] weekDays = new String[] {"Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"};
JComboBox comboWeekDays = new JComboBox(weekDays);
String selectedDay = (String) comboWeekDays.getSelectedItem();
int selectedIndex = comboWeekDays.getSelectedIndex();
private JButton exitB, calculateB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
//set size of the window
private static final int WIDTH = 300;
private static final int HEIGHT = 300;
public PaineProg8()
{
//assign the respective labels
SelectDay = new JLabel("Select a day",
SwingConstants.LEFT);
addDays = new JLabel("Add Days",
SwingConstants.LEFT );
resultDay = new JLabel("Result day",
SwingConstants.LEFT);
nextDay = new JLabel("Next Day ",
SwingConstants.LEFT);
previousDay = new JLabel("Previous Day ",
SwingConstants.LEFT);
emptyLabel = new JLabel("",
SwingConstants.LEFT);
emptyLabel2 = new JLabel("",
SwingConstants.LEFT);
//assign the respective text fields
addDaysTo = new JTextField(10);
resultDayText = new JTextField(10);
nextDayText = new JTextField(10);
previousDayText =new JTextField(10);
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
setTitle("Days of the Week Calculator");
Container pane = getContentPane();
pane.setLayout(new GridLayout(7, 2));
pane.add(SelectDay);
pane.add(comboWeekDays);
pane.add(addDays);
pane.add(addDaysTo);
pane.add(emptyLabel);
pane.add(calculateB);
pane.add(resultDay);
pane.add(resultDayText);
pane.add(nextDay);
pane.add(nextDayText);
pane.add(previousDay);
pane.add(previousDayText);
pane.add(emptyLabel2);
pane.add(exitB);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int resultDay1, addingDays, onePrev, nxWk;
resultDay1 = Integer.parseInt(addDaysTo.getText());
int ind = selectedIndex + resultDay1;
while (resultDay1 >= 7){
resultDay1 = resultDay1-7;}
resultDay1 = resultDay1 + 1;
String resDay = weekDays[resultDay1];
resultDayText.setText(resDay);
addingDays = resultDay1;
while (addingDays >= 7){
addingDays = addingDays-7;}
int addOndays = addingDays;
String nxDay = weekDays[addOndays];
nextDayText.setText(" " + nxDay);
onePrev = ind -1;
while (onePrev >= 7){
>
int thePrevDay = onePrev;
String prevDay = weekDays[thePrevDay];
previousDayText.setText("" + prevDay );
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.