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 6 of 6

Thread: calculator program

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post calculator program

    Hi I need help with creating a program that handles various mathematical calculations. When the program must have a SPLASH SCREEN and MAIN MENU screen displayed:
    Splash screeN: name of the. Calculator
    Main Menu: 1 Arithmetic Calculator
    2 Area calculator
    3 volume calculator
    4 help
    I am really confused on how to start it as I am new to java I am still trying to adjust to it and the rules. Please help and guide me. Here is my code so far but it has error on the readLine();


    [ import java.util.Scanner;
    class Calcultor {
    public static void main(String[]args){

    int userChoice;
    Scanner keyboard= new Scanner(System.in);
    System.out.print("enter calculator choice");

    userChoice= keyboard.nextInt();
    switch(userChoice){
    case 1:
    System.out.println("arithmeticCalculator");
    break;
    case 2:
    System.out.println("areaCalculator");
    break;
    case 3:
    System.out.println("volumeCalculator");
    break;
    }
    static int arithmeticCalculator(){ *// this where the error starts don't know what causes it
    int num1= Integer.parseInt(readLine);
    int num2= Integer.parseInt(readLine);
    Srting operation= readLine(); *//this is where the error ends help me fix it please

    if (operation.equals("+"))
    return num1 + num2;
    else if(operation.equals("-"))
    return num1 - num2
    else
    return 0;
    .Your assistance will be appreciated thank you :?:


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: calculator program

    When you have an error, you need to post its full text here.

    Is the new method definition inside of another method?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: calculator program

    Ok I fixed It now can help you help insert a SPLASH SCREEN within the code.
    [import java.util.Scanner;

    public class Calculator
    {
    * *public static void main(String[] args)
    * *{

    * ** *Scanner keyboard= new Scanner(System.in);
    * ** *System.out.print("Enter calculator choice: ");

    * ** *int userChoice = keyboard.nextInt();
    * ** *
    * * * * * * * * //initialise variables for choice 1
    * ** *int num1 = 0;
    * ** *int num2 = 0;
    * ** *String arithmeticOperation = "";
    * ** *
    * * * * * * * * //initialise variables for choices 2 and 3
    * ** *int length = 0;
    * ** *int width = 0;
    * ** *int height = 0;
    * ** *
    * ** *switch(userChoice)
    * ** *{
    * ** *case 1:
    * ** ** *System.out.println("Arithmetic Calculator");
    * ** ** *break;
    * ** *case 2:
    * ** ** *System.out.println("Area Calculator");
    * ** ** *break;
    * ** *case 3:
    * ** ** *System.out.println("Volume Calculator");
    * ** ** *break;
    * ** *}
    * ** *
    * ** *if(userChoice == 1)
    * ** *{
    * ** ** *System.out.print("Enter first number: ");
    * ** ** *num1 = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter second number: ");
    * ** ** *num2 = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter Arithmetic Operation: ");
    * ** ** *arithmeticOperation = keyboard.next();
    * ** ** *
    * ** ** *System.out.println("Answer = " + arithmeticCalculator(num1, num2, arithmeticOperation));
    * ** *}
    * ** *else if(userChoice == 2)
    * ** *{
    * ** ** *System.out.print("Enter Length of the square: ");
    * ** ** *length = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter Width of the square: ");
    * ** ** *width = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.println("Area = " + areaCalculator(length, width) + " Units^2");
    * ** *}
    * ** *else if(userChoice == 3)
    * ** *{
    * ** ** *System.out.print("Enter Length of the cube: ");
    * ** ** *length = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter Width of the cube: ");
    * ** ** *width = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter Height of the cube: ");
    * ** ** *height = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.println("Volume = " + volumeCalculator(length, width, height) + " Units^3");
    * ** *}
    * ** *else
    * ** *{
    * ** ** *System.out.println("Error, choose a number between 1 and 3 inclusive.");
    * ** *}
    * *}
    * *
    * *
    * *public static int arithmeticCalculator(int number1, int number2, String arithmeticOperator)
    * *{
    * ** * if(arithmeticOperator.equals("+"))
    * ** * {
    * ** * * * return number1 + number2;
    * ** * }
    * ** * else if(arithmeticOperator.equals("-"))
    * ** * {
    * ** * * * return number1 - number2;
    * ** * }
    * ** * else
    * ** * {
    * ** * * * return 0;
    * ** * }
    * *}
    * *
    * * * *
    * *public static int areaCalculator(int length, int width)
    * *{
    * ** *return length * width;
    * *}
    * *
    * *
    * *public static int volumeCalculator(int length, int width, int height)
    * *{
    * ** *return length * width * height;
    * *}
    }
    ]

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: calculator program

    Quote Originally Posted by Milestone45 View Post
    Ok I fixed It now can help you help insert a SPLASH SCREEN within the code.
    import java.util.Scanner;

    public class Calculator
    {
    * *public static void main(String[] args)
    * *{

    * ** *Scanner keyboard= new Scanner(System.in);
    * ** *System.out.print("Enter calculator choice: ");

    * ** *int userChoice = keyboard.nextInt();
    * ** *
    * * * * * * * * //initialise variables for choice 1
    * ** *int num1 = 0;
    * ** *int num2 = 0;
    * ** *String arithmeticOperation = "";
    * ** *
    * * * * * * * * //initialise variables for choices 2 and 3
    * ** *int length = 0;
    * ** *int width = 0;
    * ** *int height = 0;
    * ** *
    * ** *switch(userChoice)
    * ** *{
    * ** *case 1:
    * ** ** *System.out.println("Arithmetic Calculator");
    * ** ** *break;
    * ** *case 2:
    * ** ** *System.out.println("Area Calculator");
    * ** ** *break;
    * ** *case 3:
    * ** ** *System.out.println("Volume Calculator");
    * ** ** *break;
    * ** *}
    * ** *
    * ** *if(userChoice == 1)
    * ** *{
    * ** ** *System.out.print("Enter first number: ");
    * ** ** *num1 = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter second number: ");
    * ** ** *num2 = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter Arithmetic Operation: ");
    * ** ** *arithmeticOperation = keyboard.next();
    * ** ** *
    * ** ** *System.out.println("Answer = " + arithmeticCalculator(num1, num2, arithmeticOperation));
    * ** *}
    * ** *else if(userChoice == 2)
    * ** *{
    * ** ** *System.out.print("Enter Length of the square: ");
    * ** ** *length = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter Width of the square: ");
    * ** ** *width = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.println("Area = " + areaCalculator(length, width) + " Units^2");
    * ** *}
    * ** *else if(userChoice == 3)
    * ** *{
    * ** ** *System.out.print("Enter Length of the cube: ");
    * ** ** *length = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter Width of the cube: ");
    * ** ** *width = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.print("Enter Height of the cube: ");
    * ** ** *height = keyboard.nextInt();
    * ** ** *
    * ** ** *System.out.println("Volume = " + volumeCalculator(length, width, height) + " Units^3");
    * ** *}
    * ** *else
    * ** *{
    * ** ** *System.out.println("Error, choose a number between 1 and 3 inclusive.");
    * ** *}
    * *}
    * *
    * *
    * *public static int arithmeticCalculator(int number1, int number2, String arithmeticOperator)
    * *{
    * ** * if(arithmeticOperator.equals("+"))
    * ** * {
    * ** * * * return number1 + number2;
    * ** * }
    * ** * else if(arithmeticOperator.equals("-"))
    * ** * {
    * ** * * * return number1 - number2;
    * ** * }
    * ** * else
    * ** * {
    * ** * * * return 0;
    * ** * }
    * *}
    * *
    * * * *
    * *public static int areaCalculator(int length, int width)
    * *{
    * ** *return length * width;
    * *}
    * *
    * *
    * *public static int volumeCalculator(int length, int width, int height)
    * *{
    * ** *return length * width * height;
    * *}
    }
    ]
    Can you fix your posted code and wrap it in code tags?
    Never done a splash. I think there is a class for that. Look in the API doc
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: calculator program


  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: calculator program

    It depends what kind of splash screen u want. If u wand just a square splash screen its easy:

    JFrame f = new JFrame();
    f.setUndecorated(true);
    f.setResizable(false);
    f.setBackground(Color.THECOLORYOUWANT);
    f.setVisible(true);
    try{
    Thread.sleep(3000);
    }catch(Exception e){}
    f.setVisible(false);
    Ofcourse you can add your own labels and other stuff to it , but i will assume that you know how to do that =)

Similar Threads

  1. Need Beginner Calculator Program help!
    By theJastro in forum What's Wrong With My Code?
    Replies: 18
    Last Post: December 17th, 2011, 07:30 PM
  2. [SOLVED] Calculator Program
    By mwardjava92 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 3rd, 2011, 11:01 AM
  3. Calculator Using AWT
    By Allicat in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 17th, 2011, 06:49 AM
  4. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 09:00 AM
  5. My Algebraic Calculator Program.
    By crazed8s in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2010, 03:07 AM