My project is as follows:

2. Modify the class NumberTest.java so that:
a) The user is prompted to enter an integer and a double and the following information is displayed:
a. adding the two number,
b. subtracting the double from the int, c. dividing the int by the double, d. dividing the int by 2, e. finding the remainder of the int divided by 2, and f. dividing the double by 2.
b) c) **Note the code to perform these steps can be placed within main().
The output is formatted so that only two decimal places are displayed. The comments are up-to-date with your information

So far my code is like this:

public static void main (String[] args)
    {
        JFrame frame = new JFrame ("Number Tester");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        NumberPanel panel = new NumberPanel();

        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);

        double intNum;
        double doubleNum;

        /** Now use regular System.out and Scanner commands to prompt the user to
         * enter the two numbers and then display the results.  Again, the results
         * should be formatted to two decimal places.
         */
        Scanner sc = new Scanner(System.in);  //creates new scanner

        System.out.print("Enter an integer: ");  //gets first number
        intNum = sc.nextInt(); 

        System.out.print("Enter a double: ");  //gets second number
        doubleNum = sc.nextDouble();
        

        System.out.print(intNum + doubleNum + " ");
        System.out.print(intNum - doubleNum + " ");
        System.out.print(intNum / doubleNum + " ");
        System.out.print(intNum / 2 + " ");
        System.out.print(intNum % 2 + " ");
        System.out.print(doubleNum / 2 + " ");


    } //end main
} //end class

Recommended Answers

All 6 Replies

But I want the output to look like this:
intNum + doubleNum = sum etc. etc.

Right now it only prints out the answers in the console, so I just have six digits that are the six results. Can anyone help? I am really stumped on how to get this to work.

as a reference, I also had to do the same thing but in a frame in a panel. I already got the code for that working. It is here.

import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.*;

public class NumberPanel extends JPanel implements ActionListener{
    private JLabel intInputLabel, doubleInputLabel;
    private JLabel addRes, subRes, divRes;
    private JLabel intDivRes, intModRes, doubleDivRes;


    private JTextField intInp, doubleInp;
    private JButton done;

    //-----------------------------------------------------------------
    //  Constructor: Sets up the main GUI components.
    //-----------------------------------------------------------------
    public NumberPanel() //constructor
    {
        intInputLabel = new JLabel ("Enter an integer:"); //
        doubleInputLabel = new JLabel ("Enter a double:");//


        addRes = new JLabel();        //creates a label for you, no color, no text, results of adding
        subRes = new JLabel();        // results of subtracting
        divRes = new JLabel();        //results of dividing
        intDivRes = new JLabel();        //results of dividing int by 2
        intModRes = new JLabel();
        doubleDivRes = new JLabel();    
        // use string.valueOf, it will turn a value into a string

        intInp = new JTextField (5);
        doubleInp = new JTextField (5);

        done = new JButton("Done!");
        done.addActionListener(this);


        add (intInputLabel);
        add(intInp);
        add(doubleInputLabel);
        add(doubleInp);
        add(done);


        add(addRes);
        add(subRes);
        add(divRes);
        add(intDivRes);
        add(intModRes);
        add(doubleDivRes);

        setPreferredSize (new Dimension(400, 200));
        setBackground (Color.yellow);
    }

    //*****************************************************************
    //  Represents an action listener for the number input field.
    //*****************************************************************

    public void actionPerformed(ActionEvent e) { //this is what always is called with a GUI panel to get something to happen after pressing a button or something
        // when working with panels you always have to read in what the user wrote as text and also print out the result as text
        // 

        /** SI 543 -- Add any necessary variables/objects here.
        // Don't forget that you will need to format the output
        // as a DecimalFormat object 
         **/
        DecimalFormat fmt = new DecimalFormat ("0.##");
        //integer = Integer.parseInt (text); //this text will change the text to an int



        //resultLabel.setText (Integer.toString (celsiusTemp)); //change integer to string (number in parentheses is what you want to change)
        //}


        if (e.getSource() == done)
        {
            String intNumS;
            int intNum;
            double doubleNum;
            double sum;
            double sub;
            double div;
            int intDRes;
            int intMRes;
            double doubleDRes;
            

            intNumS = intInp.getText();
            intNum = Integer.parseInt(intNumS);
            doubleNum = Double.parseDouble(doubleInp.getText());
            sum = intNum + doubleNum;
            sub = intNum - doubleNum;
            div = intNum / doubleNum;
            intDRes = intNum / 2;
            intMRes = intNum % 2;
            doubleDRes = doubleNum / 2;
            
            addRes.setText(intNum + "+" + doubleNum + " = "  + fmt.format(sum));
            subRes.setText(intNum + "-" + doubleNum + " = "  + fmt.format(sub));
            divRes.setText(intNum + "/" + doubleNum + " = " + fmt.format(div));
            intDivRes.setText(intNum + "/ 2 = " + fmt.format(intDRes));
            intModRes.setText(intNum + " % 2 = " + fmt.format(intMRes));
            doubleDivRes.setText(doubleNum + " / 2" + " = " + fmt.format(doubleDRes));
        
            
            
            //subRes.setText();

            
            //read in the two numbers
            //either calculate and then update
            //or update each label as you calculate
            /** 
             * Put the code for updating the result labels in here.
             * Format any output to 2 decimal places.
             */
            repaint();
        }//end if
    }//end actionPerformed

}//end class

But I want the output to look like this:
intNum + doubleNum = sum

This is too simple but here's what you are asking for:
System.out.println("intNum + doubleNum = sum");
or is it this:
System.out.println(intNum + " + " + doubleNum + " = " + (intNum + doubleNum));

Perhaps your explanation could be a little more detailed.

Please show the current output with comments describing how you want to change it and also show what you want the output to look like.

I think that what you are recommending is what I need to do. Here is what I input in the console:
Enter an integer: 10
Enter a double: 2.5
12.5 7.5 4.0 5.0 0.0 1.25

and it just displays the results of 10+2.5, 10-2.5, 10/2.5, 10/2, 10%2.5 & 2.5/2
but I also want to print out the actual action that is taking place so
10 + 2.5 = 12.5 etc.

I don't understand your problem. If you can print out the 6 or so numbers so far, what is the problem with printing out more the same way?

Can you show examples of what you want to see printed out?

My problem was that only the results were being printed out, but I followed the second piece of advice that you gave and it worked perfectly. The program is now doing what I intended. Thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.