Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Can you help me to make this calculator better

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Can you help me to make this calculator better

    package com.company;

    import javax.swing.*;

    class Calculator {
    static void Addition() {
    Float sum = Float.parseFloat(JOptionPane.showInputDialog("Ente r First value:-"));
    Float sum2 = Float.parseFloat(JOptionPane.showInputDialog("Ente r second value"));
    Float answer = sum + sum2;
    System.out.println("\nThis is your answer:-");
    System.out.println("\n " + answer);
    }

    static void Subtraction() {
    Float sub = Float.parseFloat(JOptionPane.showInputDialog("Ente r First value:-"));
    Float sub2 = Float.parseFloat(JOptionPane.showInputDialog("Ente r second value:-"));
    Float answer = sub - sub2;
    System.out.println("\nThis is your Answer:-");
    System.out.println("\n " + answer);
    }

    static void Multiplication() {
    Float mul = Float.parseFloat(JOptionPane.showInputDialog("Ente r first value:-"));
    Float mul2 = Float.parseFloat(JOptionPane.showInputDialog("Ente r second value:-"));
    Float answer = mul * mul2;
    System.out.println("\nThis is your answer:-");
    System.out.println(" " + answer);
    }

    static void Division() {
    Float div = Float.parseFloat(JOptionPane.showInputDialog("Ente r first value:-"));
    Float div2 = Float.parseFloat(JOptionPane.showInputDialog("Ente r second value"));
    Float answer = div / div2;
    System.out.println("\nThis is your answer:-");
    System.out.println("\n " + answer);
    }

    static void exponents(){
    Float base = Float.parseFloat(JOptionPane.showInputDialog("Ente r base:-"));
    Float power = Float.parseFloat(JOptionPane.showInputDialog("Ente r power:-"));
    double answer = Math.pow(base,power);
    System.out.println("\nThis is your answer:-");
    System.out.println(" "+answer);
    }
    static void percentage(){
    Float number = Float.parseFloat(JOptionPane.showInputDialog("Ente r number:-"));
    Float percentage = Float.parseFloat(JOptionPane.showInputDialog("Ente r percentage you want to calculate:-"));
    Float total = 1/100F*percentage;
    Float total2 = total*number;
    System.out.println("\nThis is your answer:-");
    System.out.println(" "+total2);
    }
    static void sum_of_factors(){
    int n = Integer.parseInt(JOptionPane.showInputDialog("Plea se enter a number to find sum of divisors:-"));
    int i,sum = 0;
    for (i=1;i<=n;i++)
    {
    if (n%i==0) {
    sum = sum+i;

    }

    }
    System.out.println("Sum of divisors of "+n+" = "+sum);
    }
    static void product_of_divisors(){
    int n = Integer.parseInt(JOptionPane.showInputDialog("Ente r a number to find product of divisors"));
    int i,product = 1;
    for (i = 1;i<=n;i++){
    if (n%i==0){
    product = product*i;
    }
    }
    System.out.println("Product of divisors of "+n+" = "+product);
    }
    public static void main(String[]args){
    String arthimeticoperation;
    char selection;

    System.out.println("Possible arithmetic operations:-\n a addition \n b subtraction \n c Multiplication \n d Division \n e Exponents \n f percentage \n g sum of divisors of numbers \n h product of divisors");
    arthimeticoperation = JOptionPane.showInputDialog("What would you like to initiate?:-");
    selection = arthimeticoperation.charAt(0);
    switch (selection){
    case 'a':
    Addition();
    break;
    case 'b':
    Subtraction();
    break;
    case 'c':
    Multiplication();
    break;
    case 'd':
    Division();
    break;
    case 'e':
    exponents();
    break;
    case 'f':
    percentage();
    break;
    case 'g':
    sum_of_factors();
    break;
    case 'h':
    product_of_divisors();
    break;
    default:
    System.out.println("Please enter a valid variable");
    break;
    }

    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can you help me to make this calculator better

    First suggestion is to move the prompts for user input to a separate method that gets and validates the values from the user and returns them in a custom class.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2021
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can you help me make this ATM better?????

    package com.company;

    import javax.swing.*;

    class ATM{
    static void Balance(){
    String restart = "Y";
    Float balance = 77000.54F;
    System.out.println("This is you Balance:- "+balance);
    restart = String.join(JOptionPane.showInputDialog("Would you like to go back?));
    if (restart == "no") {
    System.out.println("Thank you");
    }
    }
    static void withdrawl(){
    Float balance = 77000.54F;
    Float withdrawl = Float.parseFloat(JOptionPane.showInputDialog("How much do you want to withdraw?:- "));
    if (withdrawl <= balance){
    balance = balance - withdrawl;
    System.out.println("\nNow,This is your Balance:- "+balance);
    }else{
    System.out.println("\nPlease enter desired amount");
    }
    }
    static void Pay_in(){
    Float balance = 77000.54F;
    Float Pay_in = Float.parseFloat(JOptionPane.showInputDialog("\nHo w much do you want to Pay in?));
    if (Pay_in == Pay_in){
    balance = balance + Pay_in;
    System.out.println("Now,This is your balance:- "+balance);
    }else {
    System.out.println("\nPlease enter a desired amount);
    }
    }
    static void return_card(){
    System.out.println("Please wait while your card is returned");
    System.out.println("Thank you for your service");
    }
    public static void main(String[]args){
    int chances = 4;
    while (chances >= 0){
    System.out.println("Welcome to Bank of America");
    Integer pin = Integer.parseInt(JOptionPane.showInputDialog("Plea se enter your 4 digit pin));
    if (pin == 1234){
    System.out.println("Congratulations you have entered your pin sucessfully");
    String options;
    char selection;
    System.out.println("\nPress 1 to see your Balance \nPress 2 to Withdrawl \nPress 3 to Pay in \nPress 4 to return card");
    options = JOptionPane.showInputDialog("\nWhat would you like to choose);
    selection = options.charAt(0);

    switch (selection){
    case '1':
    Balance();
    break;
    case '2':
    withdrawl();
    break;
    case '3':
    Pay_in();
    break;
    case '4':
    return_card();
    break;
    default:
    System.out.println("Please enter a valid option");
    }
    }else {
    System.out.println("Incorrect pin please try again");
    chances = chances - 1;
    if (chances == 3) {
    System.out.println("3 more tries");
    }else if (chances == 2){
    System.out.println("2 more tries");
    }else if (chances == 1){
    System.out.println("This is your last try");
    }
    }
    }
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can you help me make this ATM better?????

    Use the equals method to compare the contents of String objects, not the == operator.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Incredibly new at java... trying to make area calculator
    By shain in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 19th, 2014, 03:56 PM
  2. how do I add Action Listener to my code to make the calculator work
    By alex1994 in forum What's Wrong With My Code?
    Replies: 58
    Last Post: March 8th, 2014, 09:32 AM
  3. Replies: 3
    Last Post: January 15th, 2013, 02:18 PM
  4. How can I make a JDialog to make main Frame to "sleep"
    By piulitza in forum AWT / Java Swing
    Replies: 1
    Last Post: May 11th, 2012, 08:00 AM
  5. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM

Tags for this Thread