/***************************************************************/ /* Program Name: Lab 4 Experiment Code */ /* */ /* */ /* Program Overview: */ /* Experiment code for Lab 4 */ /* */ /* */ /***************************************************************/ import java.awt.*; // import ---> View classes import java.awt.event.*; // Import --> Controller Classes import javax.swing.*; public class Lab4ExperimentCode extends JFrame implements ActionListener // A roman numeral converter JFrame { // objects in the class JTextField inputData = new JTextField(15); JTextField outputData = new JTextField(15); JLabel inputLabel = new JLabel("Year (between 1000 & 3000 only) ",Label.LEFT); JLabel outputLabel = new JLabel("In Roman",Label.LEFT); JButton submitB = new JButton("OK"); JButton clearB = new JButton("Reset"); JPanel displayPanel = new JPanel(new GridLayout(2,3)); // remember to have the main method public static void main(String args[]) { new Lab4ExperimentCode(); } public Lab4ExperimentCode() { System.out.println("here the program begins"); // display in the console displayPanel.setBackground(Color.cyan); displayPanel.add(inputLabel); displayPanel.add(inputData); displayPanel.add(submitB); displayPanel.add(outputLabel); displayPanel.add(outputData);displayPanel.add(clearB); add(displayPanel); outputLabel.setFont(new Font("Helvetica", Font.BOLD,16)); inputLabel.setFont(new Font("Helvetica", Font.BOLD,16)); // Report the buttons to be listened submitB.addActionListener(this); clearB.addActionListener(this); // setTitle("Roman Number Application"); setBounds(200,400,700,100); setVisible(true); } // Fullfill the commitments of an ActionListener public void actionPerformed(ActionEvent e) { String whichButton = e.getActionCommand(); // determines which button generated the event evento if (whichButton.equals("OK")) processSubmitB(); if (whichButton.equals("Reset")) processResetB(); validate();repaint(); inputData.requestFocus(); // pay attention to this textfield } // end action performed public void processSubmitB() { // define localvariables String roman; int year; String stringValue; stringValue = inputData.getText(); // read value year = Integer.parseInt(stringValue); roman = numberConvert(year); outputData.setText(roman); // desplay value System.out.println("The final roman number is " + roman); // display in console } // end processSummitB private void processResetB() { String stringValue = null; outputData.setText(stringValue); // display cleared inputData.setText(stringValue); // display cleared System.out.println("Both textfields are cleared " ); // display in console } // end process Reset public String numberConvert(int year) { int rest, unit,decade,hundred,thousand; String roman=""; if(year>=1000 && year<=3000) { thousand=year / 1000; rest=year % 1000; hundred=rest / 100; rest=rest % 100; decade=rest / 10; unit=rest % 10; System.out.println("the number is "+ thousand +" thousand " + hundred +" hundred "+ decade + " decade " + " and "+ unit +" units " ); // display in console // do the roman if(thousand==1) roman = roman +"M"; else if(thousand==2) roman = roman+"MM"; else if(thousand==3) roman = roman+"MMM"; System.out.println("the roman so far is = " + roman ); // attaching the strings //for hundreds if(hundred==1) roman=roman+"C"; else if(hundred==2) roman=roman+"CC"; else if(hundred==3)roman=roman+"CCC"; else if(hundred==4) roman=roman+"CD"; else if(hundred==5)roman=roman+"D"; else if(hundred==6) roman=roman+"DC"; else if(hundred==7) roman=roman+"DCC"; else if(hundred==8) roman=roman+"DCCC"; else if(hundred==9) roman=roman+"CM"; System.out.println("the roman so far is = " + roman ); //for decades if(decade==1) roman=roman+"X"; else if(decade==2)roman=roman+"XX"; else if(decade==3) roman=roman+"XXX"; else if(decade==4) roman=roman+"XL"; else if(decade==5) roman=roman+"L"; else if(decade==6)roman=roman+"LX"; else if(decade==7)roman=roman+"LXX"; else if(decade==8) roman=roman+"LXXX"; else if(decade==9) roman=roman+"XC"; System.out.println("the roman so far is = " + roman ); ///for units if(unit==1)roman=roman+"I"; else if(unit==2) roman=roman+"II"; else if(unit==3) roman=roman+"III"; else if(unit==4) roman=roman+"IV"; else if(unit==5) roman=roman+"V"; else if(unit==6) roman=roman+"VI"; else if(unit==7) roman=roman+"VII"; else if(unit==8) roman=roman+"VIII"; else if(unit==9) roman=roman+"IX"; System.out.println("the roman so far is = " + roman ); } else { System.out.println("The value is not the 1,000 to 3,000 range, please re submmit"); roman = "Not in 1,000-3,000 range"; } return roman ; } } // end of class