Question

Help to fix Java GUI CALCULATOR code: This is a partial part of the program. When...

Help to fix Java GUI CALCULATOR code: This is a partial part of the program.

When i divide 16/6, i get 3. I don't want it to round up but I want it in decimal eg: 16/6 = 2.66666667

Code:

public class Calculator1 extends javax.swing.JFrame {

   double firstnum = 0.0;

   double secondnum = 0.0;

   double result = 0.0;

   

   String operation;

   

   public Calculator1() {

       initComponents();

   }

   

   private void jBtnEqualActionPerformed(java.awt.event.ActionEvent evt) {                                           

       secondnum = Double.parseDouble(jtxtDisplay.getText());

       String answer;

       switch(operation)

       {

           case "+":

               result = firstnum + secondnum;

               answer = String.format("%.0f", result);

               break;

               

           case "-":

               result = firstnum - secondnum;     

               answer = String.format("%.0f", result);

               break;

               

           case "*":

               result = firstnum * secondnum;

               answer = String.format("%.0f", result);

               break;

               

           case "/":

               result = firstnum / secondnum;

               answer = String.format("%.0f", result);

               break;

       }

       answer = String.format("%.0f", result);

       jtxtDisplay.setText(answer);

   }                                       

   private void jBtnPlusMinusActionPerformed(java.awt.event.ActionEvent evt) {                                              

   if(!jtxtDisplay.getText().equals(""))

   {

       double value = Double.parseDouble(jtxtDisplay.getText());

       value *= -1;

       jtxtDisplay.setText(String.format("%.0f", value));

   }

   }                                          

   private void jBtnMinus1ActionPerformed(java.awt.event.ActionEvent evt) {                                           

       firstnum = Double.parseDouble(jtxtDisplay.getText());

       jtxtDisplay.setText("");

       operation = "-";

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please use 16.0/6. or (double)16/6  

because 16 and 6 both are integers if you divide it will give only integer part only.. so convert one of the number to double so that you will get the result in the decimal

if you face any issues please post complete program

Note : If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Help to fix Java GUI CALCULATOR code: This is a partial part of the program. When...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static...

    In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...

  • IN JAVA: Write a program that displays a menu as shown in the sample run. You...

    IN JAVA: Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1 is...

  • This is my current output for my program. I am trying to get the output to...

    This is my current output for my program. I am trying to get the output to look like This is my program Student.java import java.awt.GridLayout; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFileChooser; public class Student extends javax.swing.JFrame {     BufferedWriter outWriter;     StudentA s[];     public Student() {         StudentGUI();            }     private void StudentGUI() {         jScrollPane3 = new javax.swing.JScrollPane();         inputFileChooser = new javax.swing.JButton();         outputFileChooser = new javax.swing.JButton();         sortFirtsName = new...

  • LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR...

    LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR CODE: import java.util.Scanner; public class CalculatorProject { public static void main (String[] args) { Scanner yurr = new Scanner(System.in);    String mode; double i; String choice; String answer = "Y"; while (answer.equals("Y")) { System.out.println("Enter the calculator mode: Standard/Scientific?"); mode = yurr.next(); if(mode.equals("Scientific")) { System.out.print("Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x,...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label;...

    I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 {    public TextArea courseInput;    public Label textAreaLabel;    public JButton addData;    public Dialog confirmDialog;    HashMap<Integer, ArrayList<String>> students;    public Fall_2017(){    courseInput = new TextArea(20, 40);    textAreaLabel = new Label("Student's data:");    addData = new JButton("Add...

  • Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for...

    Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for their inventory management system. The program needs the following classes: A main class (SpaceCarDealer.java) that includes the main method and GUI. Two instantiable classes: CarDealer.java o Variables to store the dealer name, and the carsOnLot array in Car type. o A constructor to initialize the name variable with the given dealer name. o An accessor method to return the name of the dealer. o...

  • Help! Not sure how to create this java program to run efficiently. Current code and assignment...

    Help! Not sure how to create this java program to run efficiently. Current code and assignment attached. Assignment :Write a class Movies.java that reads in a movie list file (movies.txt). The file must contain the following fields: name, genre, and time. Provide the user with the options to sort on each field. Allow the user to continue to sort the list until they enter the exit option. ---------------------------------------------------------- import java.util.*; import java.io.*; public class Movies { String movieTitle; String movieGenre;...

  • IT Java code In Lab 8, we are going to re-write Lab 3 and add code...

    IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows:                 bmi = kilograms / (meters2)                 where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...

  • This is the java GUI program. I want to add an icon to a button, but...

    This is the java GUI program. I want to add an icon to a button, but it shows nothing. Can you fix it for me please? import java.awt.BorderLayout; import java.awt.Container; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; class Icon extends JFrame{ public static void main(String args[]){ Icon frame = new Icon("title"); frame.setVisible(true); } Icon(String title){ setTitle(title); setBounds(100, 100, 300, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); ImageIcon icon1 = new ImageIcon("https://static.thenounproject.com/png/610387-200.png"); JButton button1 = new JButton(icon1); p.add(button1); Container contentPane...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT