Implement a solution to the following problem in a program Class called AnimalOf
ID: 3673756 • Letter: I
Question
Implement a solution to the following problem in a program Class called AnimalOfTheYear.
In Chinese culture, each birth year is associated with a symbolic animal. For example, 1972 is associated with ”rat”, and we say a person born in 1972 was born in the year of “rat”. Here is the algorithm that you must use to calculate the symbolic animal of a year.
1- Assign to value the result of the following expression:
( year -1972) % 12
where % is the modulus or remainder operator and year is the year of interest.
2 - Assign the animal based on the following table:
Value: Animal:
0 Rat
1 Ox
2 Tiger
3 Rabbit
4 Dragon
5 Snake
6 Horse
7 Sheep
8 Monkey
9 Rooster
10 Dog
11 Pig
”
“Your solution should ask the user to enter the year they are interested in finding out the associated animal, and then it should display, in an informative, well formatted manner, both the year entered and the associated animal. Your solution must adhere to the following:
-You are to use the JOptionPane class for all dialogues with the user.
-You are required to use the switch statement. This is the purpose of the lab: to give you experience with this decision structure.
-You may use only one JOptionPane statement associated with the display of the results of the switch statement. In other words, you may not have a JOptionPane.showMessageDialog statement inside each case.
Within the whole source code file, there should be only three JOptionPane statements: one to create the introduction pane, one to create the pane for the entry of the year and one for the pane to display of the results and terminating message.
On user prompt, make sure you tell your user what to enter as well as give them an example and if appropriate, a range.
Examples of a correct program would output the following:
Year Animal
1987 Rabbit
1973 Ox
1995 Pig
2014 Horse
2001 Snake
All in JAVA and JOptionPane
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package animal_year;
/**
*
* @author TANAY
*/
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jOptionPane1 = new javax.swing.JOptionPane();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setText("jTextField1");
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(113, 113, 113))
.addGroup(layout.createSequentialGroup()
.addGap(143, 143, 143)
.addComponent(jButton1)
.addContainerGap(184, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(87, 87, 87)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(64, 64, 64)
.addComponent(jButton1)
.addContainerGap(106, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int year = Integer.parseInt(jTextField1.getText());
int c=( year -1972) % 12 ;
switch(c)
{
case 0:
jOptionPane1.showMessageDialog(jOptionPane1,"Animal: Rat");
break;
case 1:
jOptionPane1.showMessageDialog(jOptionPane1,"Animal: Ox");
break;
case 2:
jOptionPane1.showMessageDialog(jOptionPane1,"Animal: Tiger");
break;
case 3:
jOptionPane1.showMessageDialog(jOptionPane1,"Animal: Rabbit");
break;
case 4:
jOptionPane1.showMessageDialog(jOptionPane1,"Animal: Dragon");
break;
case 5:
jOptionPane1.showMessageDialog(jOptionPane1,"Animal: Snake");
break;
case 6:
jOptionPane1.showMessageDialog(jOptionPane1,"Animal: Horse");
break;
case 7:
jOptionPane1.showMessageDialog(jOptionPane1,"Animal: Sheep");
break;
case 8:
jOptionPane1.showMessageDialog(jOptionPane1,"Animal: Monkey");
break;
case 9:
jOptionPane1.showMessageDialog(jOptionPane1,"Animal: Rooster");
break;
case 10:
jOptionPane1.showMessageDialog(jOptionPane1,"Animal: Dog");
break;
case 11:
jOptionPane1.showMessageDialog(jOptionPane1,"Animal: Pig");
break;
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JOptionPane jOptionPane1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.