Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Applet, use of decimals

  1. Feb 2, 2017 #1
    Hello,

    I am writing a program that takes a users input of how many credits they're taking, and the number of dollars each credit is. The book and parking fee is a constant number (no change). I'm using JOptionPane so users can enter 2 values, 1 for number of credits, and the other for dollars per credit. I want to enter a number such as; 135.65 into the input. When I do that I get (you can see in the screenshot) a blank applet. When I use just integers it runs fine and outputs total and everything, except when I enter a decimal it doesn't execute properly. I'm new to Java Applet and was wondering why can't 'double' work. Also, how can I fix it so I can enter decimal numbers? I'm using BlueJ if you're wondering.

    Code (Java):

    import java.awt.*;
    import java.applet.Applet;
    import javax.swing.*;
    public class Tuition extends JApplet {
     
        int number1, number2, number3, number4;
        int sum;
        int NOC; // Number of credits
        double DPC; // dollars per credit
        int PF; // Parking fee
        int BF; // Book(s) fee
        int Multiply;
        int total;
     
        String numberInStringFormat;
     
        String Message = "Tuition Fee for school";
     
        public void init() {
            String input;
     
            input =JOptionPane.showInputDialog (null, numberInStringFormat, "Enter Number of Credits",  JOptionPane.QUESTION_MESSAGE);
            number1 =Integer.parseInt(input);
            input =JOptionPane.showInputDialog (null, numberInStringFormat, "Enter cost Per Credit",  JOptionPane.QUESTION_MESSAGE);
            number2 =Integer.parseInt(input);
       
            sum = 60 + 400; // parking fee + book fee
            Multiply = number1 * number2; // number of credits * dollars per credit
            total = Multiply + sum;
       
            NOC = number1;
            DPC = number2;
       
            JOptionPane.showMessageDialog(null, "Total Fee = " +total);
       
        }
       
            public void paint(Graphics g) {
            Font f = new Font("Serif", Font.PLAIN, 16);
            Font fb = new Font("TimesRoman", Font.BOLD, 18);
       
            g.setFont(f);  //set new font
            g.drawString("Number of Credits entered: " + NOC, 70, 220);
            g.drawString("Dollars per credit: " + DPC, 70, 240);
            g.drawString("Parking fee amount: 60", 70, 260);
            g.drawString("Book(s) fee: 400", 70, 280);
            g.setFont(fb);
            g.drawString("Total cost: $"+total, 70, 310); //total Number of credits * dollars per credit + Book/parking fee.
        }
    }
     
     

    Attached Files:

    Last edited: Feb 2, 2017
  2. jcsd
  3. Feb 2, 2017 #2

    Mark44

    Staff: Mentor

    From your code:
    Code (Java):
    input =JOptionPane.showInputDialog (null, numberInStringFormat, "Enter cost Per Credit",  JOptionPane.QUESTION_MESSAGE);
            number2 =Integer.parseInt(input);
       
            sum = 60 + 400; // parking fee + book fee
            Multiply = number1 * number2; // number of credits * dollars per credit
            total = Multiply + sum;
       
            NOC = number1;
            DPC = number2;
    number2 is type int. If in the first line above you entered something like 123.45, the call to Integer.parseInt(input); will return the truncated version of the input string that is appropriate to store in an int variable. IOW, input will be the string "123" in this case, and number2 will be the integer value 123. The fact that DPC is type double is really irrelevant here, since number2 can't contain the fractional part of the input number. What gets stored in DPC is just 123.0.

    Instead of using Integer.parseInt(input), use Double.parseDouble(input).

    BTW, your choices for variable names are not very good. number1, number2, number3, and number4 should have names that suggest what they are going to hold. Multiply is an awful name. A better name would be something that suggests it will contain the total cost of however many credits are being taken. sum is also a terrible name. A better name would indicate that this is being used to hold the total fees, which should not be hard-coded into your program. It's a safe bet that these fees won't remain constant over time, so you should have these as constants, which in Java is done by declaring them as final double BOOK_FEE = 400, and final double PARKING_FEE = 60.
    NOC and DPC aren't awful, but better names would be NoOfCredits and CostPerCredit.
     
  4. Feb 2, 2017 #3
    Okay, I took your suggestion in renaming the variables. Also, I changed it to Double.parseDouble which helped a lot. In my assignment, it wants the output to be formatted to 2 decimals. If you see in my picture, it formats to one decimal. I added this to my code to see if it works

    At the top in public class tuition extends JApplet
    Code (Text):
    DecimalFormat myFormat = new DecimalFormat(".0");
    And this, which is in public void paint(Graphics g)
    Code (Text):
     String formattedTotal;
            formattedTotal = myFormat.format(total);


    Code (Java):
    import java.awt.*;
    import javax.swing.*;
    import java.text.*;
    import java.applet.Applet;

    public class Tuition extends JApplet
    {
        String welcomeMessage = "Welcome!";
        String numberInStringFormat;
     
        private Image NHCC = null;
     
        double totbookparking;
        double NumofCreds;
        double DollarsperCred;
        double ParkingFee;
        double BookFee;
        double NumofCredsnDollarsperCred;
        double total;

        DecimalFormat myFormat = new DecimalFormat(".0");

        public void init() {
            String input;
     
            input =JOptionPane.showInputDialog (null, numberInStringFormat, "Enter Number of Credits",  JOptionPane.QUESTION_MESSAGE);
            NumofCreds =Double.parseDouble(input);
            input =JOptionPane.showInputDialog (null, numberInStringFormat, "Enter Number of Credits",  JOptionPane.QUESTION_MESSAGE);
            DollarsperCred =Double.parseDouble(input);
     
            final double BookFee = 400;
            final double ParkingFee = 60;
     
            total = (NumofCreds * DollarsperCred) + BookFee + ParkingFee;
     
            JOptionPane.showMessageDialog(null, "Total Fee = " +total);
        }
     
        public void paint(Graphics g) {
            super.paint(g);
            Font f = new Font("Serif", Font.PLAIN, 16);
            Font fb = new Font("TimesRoman", Font.BOLD, 18);
     
            g.setFont(f);
            g.drawString("Number of Credits entered: " + NumofCreds, 70, 220);
            g.drawString("Dollars per credit: " + DollarsperCred, 70, 240);
            g.drawString("Parking fee amount: 60", 70, 260);
            g.drawString("Book(s) fee: 400", 70, 280);
     
            String formattedTotal;
            formattedTotal = myFormat.format(total);
     
            g.setFont(fb);
            g.drawString("Total tuition fee: $" + calculateTuition(60, 400), 70, 320);
     
        }
     
        public double calculateTuition(double books, double parking){
         
            double totalfee = (NumofCreds * DollarsperCred)+ books + parking; //double totalfee: 60 + 400
            return totalfee;
     
        }

     
    }
    Edit: Made a few updates to code, created a method class called calculateTuition but still can't add a leading zero at the end of the total.
     

    Attached Files:

    Last edited: Feb 2, 2017
  5. Feb 2, 2017 #4

    Mark44

    Staff: Mentor

    That's not a leading zero--it's a trailing zero.
    Try this:
    Code (Java):
    NumberFormat numberFormat = new DecimalFormat("#,###.00");
    This format pattern will give you two decimal places and will add a comma separator at the thousands place. For more info, see http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html.

    The new variable names look a lot better.
    totbookparking
    NumofCreds
    DollarsperCred
    ParkingFee
    BookFee
    NumofCredsnDollarsperCred

    Some suggestions:
    The variables are easier to read if each word that makes up the variable is capitalized. E.g., NumOfCreds (or even NumCreds -- 'of' isn't really necessary)
    CostPerCred instead of DollarsperCred
    ParkingFee and BookFee are fine
    NumofCredsnDollarsperCred should be changed. I have no idea what it is supposed to represent. Is it the number of credits or the cost per credit?

    Edit: I suspected that you might have listed the last one in error. Sure enough, I can't find it being used in your code. If a variable isn't being used, it should be deleted.
     
    Last edited: Feb 2, 2017
  6. Feb 2, 2017 #5
    Alright, thanks. I'll keep that in mind for creating variables in my next assignment. Thank you, I got it working as you can see in the snapshot.
     

    Attached Files:

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Java Applet, use of decimals
  1. Java Applet Issues (Replies: 12)

  2. Programming using Java (Replies: 3)

Loading...