qpMONKEYMIKEqp's Profile
Reputation: -1
Dishonored
- Group:
- Members
- Active Posts:
- 22 (0.09 per day)
- Joined:
- 16-October 12
- Profile Views:
- 100
- Last Active:
Feb 14 2013 07:14 AM- Currently:
- Offline
Previous Fields
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: Lottery winner
Posted 15 Dec 2012
Here is what i think you are trying to do.
What i changed was inside your for loop you made it to where it chose if the number was right or wrong. The first number is going to be right or wrong. So then it pushes it out of the loop
so what i did was only ask if its right inside the for loop. but outside ask if the value 'win' has been changed. If it was not changed in the for loop then you did not win.
int main(void) { int playerNumbers [10] = { 77777, 13579, 62483, 26792, 93121, 79422, 85647, 26791, 33445, 55555}; int i = 0; int win = 0; int results = 0; int lott = 0; printf("Enter the winning number: "); fflush(stdout); scanf("%d", &lott); if (lott > 0 || lott < 99999) { for(i=0;i<10;i++) { if(lott==playerNumbers[i]) { printf(" is a winner!!! :"); fflush(stdout); win = 1; } } if (win == 0) { printf("Sorry you did not win"); fflush(stdout); } }//end if else { printf("You have entered a wrong number "); fflush(stdout); return 0; } return 0; }//end main -
In Topic: JAVA - GUI metric and temp conversion program - help with output!
Posted 15 Dec 2012
It is complete
This is the criteria that i was working off of. Does it look like i implemented everything that was needed so far i think i did but a second pair of eyes always helps?
1. Start with the Conversion class. Task 1 is to extend this class using inheritance and create a subclass called 'TempConversion'. The new class needs methods for converting Fahrenheit to Celsius and Celsius to Fahrenheit. You will use the parent class' roundoff() method to insure the results returned are two decimal places.
2. Task 2 is to create a GUI. The first textbox is Fahrenheit and the second is Celsius. When input is entered into the first box, the F to C formula is used and the results placed in the second textbox. If the input value is in the second textbox, the C to F formula is used and the result is placed in the first textbox. Your GUI will include functioning menus and a HELP menu that displays a dialog box that lists the name of the program and the programmers name.
3. Finally for Task 3, You will use Exception Handling to insure the input is correct before attempting to convert the input.
Here is my final code
TestConverter.java
import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.*; public class TestConverter extends JFrame implements ActionListener {//TestConverter private JLabel title = new JLabel("Temperature Converter" ); //label for title private JPanel titlePanel = new JPanel( ); //panel for title private JLabel fLabel = new JLabel( "Fahrenheit: " ); //label for fahrenheit input private JTextField fField = new JTextField( 10 ); //textbox for fahrenheit input private JPanel fPanel = new JPanel( ); //panel for fahrenheit label+textbox private JPanel cPanel = new JPanel( ); //panel for celsius label+textbox private JTextField cField = new JTextField( 10 ); //textbox for celsius input private JLabel cLabel = new JLabel( "Celsius: " ); //label for celsius input private JPanel converterPanel = new JPanel( ); //panel for fpanel+cpanel private JButton convertBtn = new JButton("Convert"); //button to execute conversion private JButton clearBtn = new JButton("Clear"); //button to clear all textboxs private JPanel buttonPanel = new JPanel(); //panel for convertBtn+clearBtn private JMenuBar menuBar = new JMenuBar(); public TestConverter( ) { //constructor super("Temperature Converter"); //text above menuBar createTitlePanel(); //constructor for createTitlePanel getContentPane().add(titlePanel, BorderLayout.NORTH); //organize titlePanel to the top createConverterPanel( ); //constructor for createConverterPanel getContentPane().add(converterPanel, BorderLayout.WEST); //organize converterPanel to the left createButtonPanel(); //constructor for createButtonPanel getContentPane().add(buttonPanel, BorderLayout.EAST); //organize buttonPanel to the right createMenuBar(); //constructor for createMenuBar }//end of constructor private void createTitlePanel( ) {//createTitlePanel titlePanel.add( title ); //add title to titlePanel }//end of createTitlePanel private void createfPanel( ) {//createfPanel fPanel.setLayout(new GridLayout(1,2)); //create layout for fPanel fPanel.add( fLabel ); //add fLabel to fPanel fPanel.add( fField ); //add fField to fPanel }//end of createfPanel private void createcPanel( ) {//createcPanel cPanel.setLayout(new GridLayout(1,2)); //create layout for cPanel cPanel.add( cLabel ); //add cLabel to cPanel cPanel.add( cField ); //add cField to cPanel }//end of createcPanel private void createConverterPanel( ) {//createConverterPnel converterPanel.setLayout(new GridLayout(2,1)); //create layout for converterPanel createfPanel( ); //constructor for createfPanel converterPanel.add( fPanel ); //add fPanel to converterPanel createcPanel( ); //constructor for createcPanel converterPanel.add(cPanel); //add cPanel to converterPanel }//end of createConverterPanel private void createButtonPanel( ) {//createButtonPanel buttonPanel.setLayout(new GridLayout(2,1)); //create layout for buttonPanel buttonPanel.add(convertBtn); //add convertBtn to buttonPanel buttonPanel.add(clearBtn); //add clearBtn to buttonPanel convertBtn.addActionListener(this); //add actionListener to converterBtn clearBtn.addActionListener(this); //add actionListener to clearBtn }//end of createButtonPanel private void createMenuBar( ) {//createMenuBar class exitAction implements ActionListener {//exitAction public void actionPerformed (ActionEvent ae) {//actionPerformed System.exit(0); //exit app }//end of actionPerformed }//end of exitAction class aboutAction implements ActionListener {//aboutAction public void actionPerformed (ActionEvent ae) {//actionPerformed JOptionPane.showMessageDialog(null, //popup dialog box with app information "Author: Michael Rauch\nTitle: Temperature\nLast Edit: 12/14/12", //message contents "About", //title JOptionPane.INFORMATION_MESSAGE); //type of message icon }//end of actionPerformed }//end of aboutAction setJMenuBar(menuBar); // create menuBar JMenu file = new JMenu("File"); //create file menu menuBar.add(file); //add file JMenuItem exit = new JMenuItem("Exit"); //create exit menuItem file.add(exit); //add exit exit.addActionListener(new exitAction()); //add actionListener to exit JMenu help = new JMenu("Help"); //create help menu menuBar.add(help); //add help JMenuItem about = new JMenuItem("About"); //create about menuItem help.add(about); //add about about.addActionListener(new aboutAction()); //add actionListener to about }//end of createMenuBar public void actionPerformed(ActionEvent ae) {//actionPerformed if ( ae.getSource( ) == convertBtn ) { //if convertBtn is pressed double d = 0.0; //create double d int error = 0; //create int error if ( cField.getText().isEmpty() && fField.getText().isEmpty() ) {//if cField & fField are empty JOptionPane.showMessageDialog(null, //popup dialog box with error "Both fields are empty, try again.", //contents "Input Error", //title JOptionPane.ERROR_MESSAGE); //icon type }//end if else if ( cField.getText().isEmpty() != true && fField.getText().isEmpty() != true ) {//else if cField & fField are NOT empty JOptionPane.showMessageDialog(null, //popup dialog box with error "Both fields are full, try again.", //contents "Input Error", //title JOptionPane.ERROR_MESSAGE); //icon type fField.setText(""); //clear fField cField.setText(""); //clear fField }//end else if else if( cField.getText().isEmpty() && fField.getText().isEmpty() != true ) {//else if cField is empty & fField is NOT empty try {//begin try d = Double.parseDouble( fField.getText( )); //converts fFields text to a double and sets it = to d }//end of try catch (Exception e) {//catch if error d = 0; //set d = to 0 JOptionPane.showMessageDialog(null, //popup dialog box with error "Fahrenheit input is not a number, try again.", //contents "Input Error", //title JOptionPane.ERROR_MESSAGE); //icon type error = 1; //set error = to 1 (to show an error has happened) fField.setText(""); //clear fField }//end catch if (error == 0) {// if there were no errors d = TempConversion.f2c( d ); //send the value of d to TempConversion.f2c, then set d to its return value cField.setText( Double.toString( d ) ); //set the cField to the text value of d }//end if error = 0; //reset error counter }//end else if else if( cField.getText().isEmpty() != true && fField.getText().isEmpty() ) { //else if cField is NOT empty & fField is empty try {//begin try d = Double.parseDouble( cField.getText( )); //convert cFields text to double and set it = to d }//end of try catch (Exception e) {//catch if error d = 0; //set d = to 0 JOptionPane.showMessageDialog(null, //popup dialog box with error "Celsius input is not a number, try again.", //contents "Input Error", //title JOptionPane.ERROR_MESSAGE); //icon type error = 1; //set error = to 1 (to show an error has happened) cField.setText(""); //clear cField }//end catch if (error == 0) {//if there were no errors d = TempConversion.c2f( d ); //send the value of d to TempConversion.c2f, then set d to its return value fField.setText( Double.toString( d ) ); //set the cField to the text value of d }//end if error = 0; //reset error counter }//end else if }//end if else if ( ae.getSource( ) == clearBtn ) {//if clearBtn is pressed fField.setText(""); //clear fField cField.setText(""); //clear cField }// end if } //end actionPerformed method public static void main( String[ ] args ) {//main final int FRAMEWIDTH = 400; //set FRAMEWIDTH final int FRAMEHEIGHT = 130; //set FRAMEHEIGHT TestConverter tc = new TestConverter( ); //create new TestConverter tc.setSize( FRAMEWIDTH, FRAMEHEIGHT ); //set size of tc tc.pack(); //compress tc to remove white space tc.setVisible( true ); //set tc to be visible }//end of main method }//end of class
TempConversion
public class TempConversion extends Conversion {//TempConversion public static double f2c( double f ) {//f2c return roundOff((f - 32.) * 5./9.); //convert f to c then send to roundOff then return the value } //end f2c method public static double c2f( double c ) {//c2f return roundOff(c * 9./5. + 32.); //convert c to f then send to roundOff then return the value } //end c2f method }//end of TempConversion
Conversion.java
public class Conversion {//conversion public static double m2k( double m ) {//m2k return roundOff( m*1.6 ); //convert m to k then send the value to roundOff then return the value } //end m2k method public static double k2m( double k ) {//k2m return roundOff( k*0.62 ); //convert k to m then send the value to roundOff then return the value } //end k2m method protected static double roundOff( double d ) {//roundOff d = d * 100; //multiply by 100 d = Math.round( d ); //round to 0.00 d = d / 100; //divide by 100 return d; //return rounded off value } // end of roundOff method } //end of class -
In Topic: JAVA - GUI metric and temp conversion program - help with output!
Posted 15 Dec 2012
I now have a menu bar and i changed the GUI around. I removed the radio buttons and now it works without them.
Next i will be creating exception handlers so that when any errors occur like if both text fields are full or both are empty or if its not a number then it will throw a pop up to the user stating what the error was.
current code (Only changes from here on out are to the metricConverter)
metricConverter.java
import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.*; public class MetricConverter extends JFrame implements ActionListener { private JLabel title = new JLabel("Temperature Converter" ); private JPanel titlePanel = new JPanel( ); private JLabel fLabel = new JLabel( "Fahrenheit: " ); private JTextField fField = new JTextField( 10 ); private JPanel fPanel = new JPanel( ); private JPanel cPanel = new JPanel( ); private JTextField cField = new JTextField( 10 ); private JLabel cLabel = new JLabel( "Celsius: " ); private JPanel converterPanel = new JPanel( ); private JButton convertBtn = new JButton("Convert"); private JButton clearBtn = new JButton("Clear"); private JPanel buttonPanel = new JPanel(); public MetricConverter( ) { super("Metric Converter"); createTitlePanel(title); getContentPane().add(titlePanel, BorderLayout.NORTH); createConverterPanel( ); getContentPane().add(converterPanel, BorderLayout.CENTER); createButtonPanel(); getContentPane().add(buttonPanel, BorderLayout.EAST); createMenuBar(); }//end of constructor private void createTitlePanel( JLabel t ) { titlePanel.add( t ); } private void createInputPanel( ) { fPanel.setLayout(new GridLayout(1,2)); fPanel.add( fLabel ); fPanel.add( fField ); } private void createResultPanel( ) { cPanel.setLayout(new GridLayout(1,2)); cPanel.add( cLabel ); cPanel.add( cField ); } private void createConverterPanel( ) { converterPanel.setLayout(new GridLayout(2,1)); createInputPanel( ); converterPanel.add( fPanel ); createResultPanel( ); converterPanel.add(cPanel); } private void createButtonPanel( ){ buttonPanel.setLayout(new GridLayout(2,1)); buttonPanel.add(convertBtn); buttonPanel.add(clearBtn); convertBtn.addActionListener(this); clearBtn.addActionListener(this); } private void createMenuBar( ) { class exitAction implements ActionListener { public void actionPerformed (ActionEvent ae) { System.exit(0); } } class aboutAction implements ActionListener { public void actionPerformed (ActionEvent ae) { JOptionPane.showMessageDialog(null,"Author: Michael Rauch\nTitle: Temperature\nLast Edit: 12/14/12","About",JOptionPane.PLAIN_MESSAGE); } } JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu file = new JMenu("File"); menuBar.add(file); JMenuItem exit = new JMenuItem("Exit"); file.add(exit); exit.addActionListener(new exitAction()); JMenu help = new JMenu("Help"); menuBar.add(help); JMenuItem about = new JMenuItem("About"); help.add(about); about.addActionListener(new aboutAction()); } public void actionPerformed(ActionEvent ae) { if ( ae.getSource( ) == convertBtn ) { System.out.println(fField.getText() + "\n" + cField.getText()); double d = 0.0; //f = Double.parseDouble( fField.getText( ) ); //c = Double.parseDouble( cField.getText( ) ); if ( cField.getText().isEmpty() && fField.getText().isEmpty() ) { System.out.println("Error: emply fields"); fField.setText(""); cField.setText(""); } if( cField.getText().isEmpty() && fField.getText().isEmpty() != true ) { d = Double.parseDouble( fField.getText( )); d = TempConversion.f2c( d ); cField.setText( Double.toString( d ) ); } else if( cField.getText().isEmpty() != true && fField.getText().isEmpty() ) { d = Double.parseDouble( cField.getText( )); d = TempConversion.c2f( d ); fField.setText( Double.toString( d ) ); } else if ( cField.getText().isEmpty() != true && fField.getText().isEmpty() != true ) { System.out.println("Error: full fields"); fField.setText(""); cField.setText(""); } } else if ( ae.getSource( ) == clearBtn ){ fField.setText(""); cField.setText(""); } } //end actionPerformed method public static void main( String[ ] args ) { final int FRAMEWIDTH = 400; final int FRAMEHEIGHT = 130; MetricConverter mc = new MetricConverter( ); mc.setSize( FRAMEWIDTH, FRAMEHEIGHT ); //mc.pack(); mc.setVisible( true ); }//end of main method }//end of class -
In Topic: JAVA - GUI metric and temp conversion program - help with output!
Posted 15 Dec 2012
Thank you both! i implemented the changes and its working like a charm. I can see the redundancy in having the two classes but the book that i am following is wanting me to stick to it.
Next what i am going to try and do is remove the radio buttons and just have two text boxes and two buttons. the two text boxes are Celsius and Fahrenheit. If if Fahrenheit's text box is full it converts it into Celsius and puts the result in the Celsius text box and in reverse. I also want to try and implement exception handling so that if two are in at the same time a error pops up and clears both boxes. I will be updating my code soon with my attempted changes.
Here is my current code:
Conversion.java
public class Conversion { public static double m2k( double m ) { return roundOff( m*1.6 ); } //end m2k method public static double k2m( double k ) { return roundOff( k*0.62 ); } //end k2m method protected static double roundOff( double d ) { d = d * 100; d = Math.round( d ); d = d / 100; System.out.println(d); return d; } // end of roundOff method } //end of class
MetricConverter.java
import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.*; public class MetricConverter extends JFrame implements ActionListener { private JLabel title = new JLabel("Metric Converter Form" ); private JPanel titlePanel = new JPanel( ); private JLabel inputLabel = new JLabel( "Input:" ); private JTextField inputField = new JTextField( 10 ); private JPanel inputPanel = new JPanel( ); private JPanel resultPanel = new JPanel( ); private JTextField resultField = new JTextField( 10 ); private JLabel resultLabel = new JLabel( "Results:" ); private JPanel converterPanel = new JPanel( ); private JButton convertBtn = new JButton("Convert"); private JButton clearBtn = new JButton("Clear"); private JPanel buttonPanel = new JPanel(); private JPanel controlPanel = new JPanel(); private ButtonGroup bg = new ButtonGroup(); private JPanel typePanel = new JPanel(); private JRadioButton m2kButton = new JRadioButton("Miles to KM"); private JRadioButton k2mButton = new JRadioButton("KM to Miles"); private JRadioButton f2cButton = new JRadioButton("Fahrenheit to Celsius"); private JRadioButton c2fButton = new JRadioButton("Celsius to Fahrenheit"); public MetricConverter( ) { super("Metric Converter"); createTitlePanel(title); getContentPane().add(titlePanel, BorderLayout.NORTH); createConverterPanel( ); getContentPane().add( converterPanel, BorderLayout.CENTER); createControlPanel(); getContentPane().add(controlPanel, BorderLayout.SOUTH); }//end of constructor private void createTitlePanel( JLabel t ) { titlePanel.add( t ); } private void createInputPanel( ) { inputPanel.setLayout(new GridLayout(1,2)); inputPanel.add( inputLabel ); inputPanel.add( inputField ); } private void createResultPanel( ) { resultPanel.setLayout(new GridLayout(1,2)); resultPanel.add( resultLabel ); resultPanel.add( resultField ); } private void createConverterPanel( ) { converterPanel.setLayout(new GridLayout(2,1)); createInputPanel( ); converterPanel.add( inputPanel ); createResultPanel( ); converterPanel.add(resultPanel); } private void createControlPanel( ){ controlPanel.setLayout(new GridLayout(1,2)); bg.add(m2kButton); bg.add(k2mButton); bg.add(f2cButton); bg.add(c2fButton); typePanel.setLayout(new GridLayout(4,1)); createTypeBorder( ); m2kButton.setSelected(true); typePanel.add(m2kButton); typePanel.add(k2mButton); typePanel.add(f2cButton); typePanel.add(c2fButton); controlPanel.add(typePanel); buttonPanel.setLayout(new GridLayout(1,2)); buttonPanel.add(convertBtn); buttonPanel.add(clearBtn); controlPanel.add(buttonPanel); convertBtn.addActionListener(this); clearBtn.addActionListener(this); } public void createTypeBorder(){ Border etchedBorder = BorderFactory. createEtchedBorder(EtchedBorder.LOWERED); TitledBorder titleBorder = BorderFactory.createTitledBorder( etchedBorder, "convert:"); titleBorder.setTitleJustification(TitledBorder.LEFT); titleBorder.setTitleColor(Color.GRAY); typePanel.setBorder(titleBorder); } public void actionPerformed(ActionEvent ae) { if ( ae.getSource( ) == convertBtn ) { double d = 0.0; d = Double.parseDouble( inputField.getText( ) ); if( m2kButton.isSelected( ) ) { d = Conversion.m2k( d ); } else if( k2mButton.isSelected( ) ) { d = Conversion.k2m( d ); } else if( f2cButton.isSelected() ) { d = TempConversion.f2c( d ); } else if( c2fButton.isSelected() ) { d = TempConversion.c2f( d ); } resultField.setText( Double.toString(d ) ); } else if ( ae.getSource( ) == clearBtn ){ inputField.setText(""); resultField.setText(""); } } //end actionPerformed method public static void main( String[ ] args ) { final int FRAMEWIDTH = 500; final int FRAMEHEIGHT = 400; MetricConverter mc = new MetricConverter( ); mc.setSize( FRAMEWIDTH, FRAMEHEIGHT ); mc.pack(); mc.setVisible( true ); }//end of main method }//end of class
TempConversion.java
public class TempConversion extends Conversion { public static double f2c( double f ) { return roundOff((f - 32.) * 5./9.); } //end f2c method public static double c2f( double c ) { return roundOff(c * 9./5. + 32.); } //end c2f method } -
In Topic: Dynamically allocate an array and find the mode.
Posted 2 Dec 2012
Here is my addition so far, I changed a few things so that what i added would work. I think i have the right idea for the Mode for loop but its not exactly working?
#include <stdio.h> #include <stdlib.h> #include <malloc/malloc.h> int * getIntegers(int *); void displayIntegers(int *,int); void sortIntegers(int *integers, int size); void modeIntegers(int *integers, int size); int main() { int i; int *testIntegers = NULL; int numberIntegers = 0; int sizep = 0; printf("How many Integers do you want? "); fflush(stdout); scanf(" %d",&sizep); testIntegers = getIntegers(&sizep); sortIntegers(testIntegers, numberIntegers); displayIntegers(testIntegers,numberIntegers); modeIntegers(testIntegers, sizep); free (testIntegers); return 0; } int *getIntegers(int *sizep) { int i; int *myArray; int *iptr; myArray = calloc(*sizep , sizeof(int)); /* using array notation*/ for (i = 0; i < *sizep; i++) { printf("\tIntegers %d: ",i+1); fflush(stdout); scanf(" %d",&myArray[i]); } return myArray; } void displayIntegers(int *integers, int size) { /* array */ int i; for (i = 0; i < size; i++) printf("%d\n",integers[i]); fflush(stdout); } void sortIntegers(int *integers, int size) { /* Selection sort as an array */ int inner, outer, hold; for (outer = 0; outer < size -1; outer++) for (inner = outer + 1; inner < size; inner++) { if (integers[outer] > integers[inner]) { // swap them hold = integers[outer]; integers[outer] = integers[inner]; integers[inner] = hold; } } } void modeIntegers(int *integers, int size) { int mode = 0; int i = 0; int counter = 0; int largestCount = 0; int currentCheck = 0; for (i = 0; i < size; i++) { printf("loop"); if (i == 0) { currentCheck = integers[i]; } if (integers[i] == integers[i+1]) { counter = (counter + 1); if (counter > largestCount) { largestCount = counter; mode = currentCheck; } } else { currentCheck = integers[i+1]; } } printf("The mode is: %d", mode); }
My Information
- Member Title:
- New D.I.C Head
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
Contact Information
- E-mail:
- Click here to e-mail me
Friends
qpMONKEYMIKEqp hasn't added any friends yet.
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
qpMONKEYMIKEqp has no profile comments yet. Why not say hello?