Results 1 to 6 of 6
  1. #1
    Sharpedge is offline Lazy
    Join Date
    Nov 2012
    Location
    Sindh
    Posts
    3
    Rep Power
    0

    Default can this java calculator program be modified for my learning purpose?

    This works just fine, but as you can see their is no "constructor" in this program! And their is only a scanner object! Can this program be more simplified in a way it has constructors and methods, as an example for me to learn ?



    import java.util.Scanner;
    public class calc {
    public static void main(String args[]){
    Scanner calculator = new Scanner(System.in);
    double fnum, snum, answer;

    System.out.println("Enter first number: ");
    fnum = calculator.nextDouble();
    System.out.println("Enter second number: ");
    snum = calculator.nextDouble();
    System.out.println("enter any operator: ");
    String op = calculator.next();
    switch (op){
    case ("x") :
    answer = fnum * snum;
    System.out.println(answer);
    break;

    case ("/") :
    answer = fnum / snum;
    System.out.println(answer);
    break;

    case ("+") :
    answer = fnum + snum;
    System.out.println(answer);
    break;

    case ("-") :
    answer = fnum - snum;
    System.out.println(answer);
    break;


    }

    }





    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,690
    Rep Power
    8

    Default Re: can this java calculator program be modified for my learning purpose?

    Please use code tags when posting code.

    Sure, you could use OO principles to improve this code. What have you tried?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Sharpedge is offline Lazy
    Join Date
    Nov 2012
    Location
    Sindh
    Posts
    3
    Rep Power
    0

    Default Re: can this java calculator program be modified for my learning purpose?

    Im sorry ... im new to this forum world
    If u can give me an example ... easy for me to understand or change my code adding constructors and methods, it would really be appreciated ... im trying to learn about methods , objects and constructors using simple examples, a calculator in above case ..

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,690
    Rep Power
    8

    Default Re: can this java calculator program be modified for my learning purpose?

    Recommended reading: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

    Why don't you read through that and try to apply the ideas yourself, then we'll go from there.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Sharpedge is offline Lazy
    Join Date
    Nov 2012
    Location
    Sindh
    Posts
    3
    Rep Power
    0

    Default Re: can this java calculator program be modified for my learning purpose?

    I am already following those lessons , but the practical examples given are not very helpful because they are limited ..
    Thats why i posted this simple calculator program , which i learned from Bucky's java video tutorials..
    I just want a simple program to demonstrate , "objects" , "methods" and "constructors" ... !!
    so that i can make my logic clear on them ... my theory on these 3 is clear although ..

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,690
    Rep Power
    8

    Default Re: can this java calculator program be modified for my learning purpose?

    Well, you could create a Calculator class that has methods for doing the different operations, and then call those methods based on user input.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Help with a java program, I'm just learning...
    By pittypatsfan in forum New To Java
    Replies: 1
    Last Post: 10-12-2012, 04:46 AM
  2. general purpose calculator
    By j3sr2010 in forum NetBeans
    Replies: 3
    Last Post: 06-27-2011, 07:19 PM
  3. Getting Backspace on a java calculator program to work
    By marylanddem in forum New To Java
    Replies: 1
    Last Post: 12-09-2010, 01:01 AM
  4. unreachable statement - Java calculator program
    By V2001Gordon in forum New To Java
    Replies: 3
    Last Post: 12-13-2008, 12:57 AM
  5. Replies: 0
    Last Post: 04-04-2008, 02:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •