Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Can anyone see the problem in this code??

  • 25-03-2004 02:16PM
    #1
    Registered Users, Registered Users 2 Posts: 947 ✭✭✭


    Can anyone see this problem?
    The program takes in two numbers, gives a menu, the user selects an option, then the program calls the function, does the calculation and prints it on screen.
    Problem is, it keeps displaying the calculation over and over in a loop for some reason, I cant see the problem?!
    Any help greatly appriciated!!



    //Project Program
    class num{

    //Declaring variables
    static final double pi = 3.1416;
    private int num1;
    private int num2;

    //Constructor
    public num(int a, int b)
    {
    num1 = a;
    num2 = b;
    }


    //Add Function
    public void add()
    {
    Screen.newline();
    Screen.cursorpos(35);
    Screen.writeInt(num1);
    Screen.message(" + ");
    Screen.writeInt(num2);
    Screen.message(" = ");
    Screen.writeInt(num1+num2);
    }

    //Subtract Function
    public void subtract()
    {
    Screen.newline();
    Screen.cursorpos(35);
    Screen.writeInt(num1);
    Screen.message(" - ");
    Screen.writeInt(num2);
    Screen.message(" = ");
    Screen.writeInt(num1-num2);
    }
    //Divion Function
    public void divide()
    {
    Screen.newline();
    Screen.cursorpos(35);
    Screen.writeInt(num1);
    Screen.message(" / ");
    Screen.writeInt(num2);
    Screen.message(" = ");
    Screen.writeInt(num1/num2);
    Screen.message(" . ");
    Screen.writeInt(num1%num2);
    }
    //multiplication Function
    public void multiply()
    {
    Screen.newline();
    Screen.cursorpos(35);
    Screen.writeInt(num1);
    Screen.message(" * ");
    Screen.writeInt(num2);
    Screen.message(" = ");
    Screen.writeInt(num1*num2);
    }

    //area Function
    public void area()
    {
    Screen.newline();
    Screen.cursorpos(35);
    Screen.message(" Area = ");
    Screen.writeDouble(pi*num1*num1);
    }

    //volume Function
    public void volume()
    {
    Screen.newline();
    Screen.cursorpos(35);
    Screen.message(" Volume = ");
    Screen.writeDouble(4*pi*(num1*num1*num1)/3);
    }

    //Circumference Function
    public void Circumference()
    {
    Screen.newline();
    Screen.cursorpos(35);
    Screen.message(" Circumference = ");
    Screen.writeDouble(pi*num1);
    }


    //Menu Options
    public int call()
    {
    int opt1;

    //Telling user the options
    Screen.newline(2);
    Screen.message("********************************************************************************");

    Screen.newline(2);
    Screen.cursorpos(2);
    Screen.message("Option 1: Add Variables -> Press 1 for Addition");

    Screen.newline(2);
    Screen.cursorpos(2);
    Screen.message("Option 2: Subtract Variables -> Press 2 for Subtraction");

    Screen.newline(2);
    Screen.cursorpos(2);
    Screen.message("Option 3: Divide Variables -> Press 3 for Division");

    Screen.newline(2);
    Screen.cursorpos(2);
    Screen.message("Option 4: Multiply Variables -> Press 4 for mulitiplication");

    Screen.newline(2);
    Screen.cursorpos(2);
    Screen.message("Option 5: Area of circle -> Press 5 to find area of a circle");

    Screen.newline(2);
    Screen.cursorpos(2);
    Screen.message("Option 6: Volume of circle -> Press 6 to find volume of a circle");

    Screen.newline(2);
    Screen.cursorpos(2);
    Screen.message("Option 7: Circumference of circle -> Press 7 to find Circumference of a circle");

    Screen.newline(2);
    Screen.cursorpos(2);
    Screen.message("Option 9:Exit program -> Press 9 to exit");

    Screen.newline(2);
    Screen.message("********************************************************************************");

    //statement to acknowlage user selection
    Screen.cursorpos(30);
    Screen.message("Option selected = ");
    opt1 = Keyboard.readInt();
    return opt1;

    }

    }



    //Test Program
    class numTest
    {
    public static void main(String[] args)
    {

    //Declaring Variables
    int a;
    int b;
    int opt1;

    //asking user to enter values into variables
    Screen.cursorpos(30);
    Screen.message("Enter two numbers");
    Screen.newline(2);
    Screen.message(" Number 1 will count as radius and diameter in option 5, 6 and 7 respectively");

    //reading in numbers to variable
    Screen.newline(2);
    Screen.cursorpos(35);
    a = Keyboard.readInt();
    Screen.cursorpos(35);
    b = Keyboard.readInt();


    //creating instance
    num result1 = new num(a,b);

    //value returned from the call function & stored in opt1
    opt1 = result1.call();

    //Exit Function
    while(opt1 !=9)
    {
    //IF function for menu
    if (opt1== 1)
    {
    result1.add();
    }
    else if (opt1== 2)
    {
    result1.subtract();
    }
    else if (opt1== 3)
    {
    result1.divide();
    }
    else if (opt1== 4)
    {
    result1.multiply();
    }
    else if (opt1== 5)
    {
    result1.area();
    }
    else if (opt1== 6)
    {
    result1.volume();
    }
    else if (opt1== 7)
    {
    result1.Circumference();
    }

    }
    }
    }


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    What language is that?

    Looks like C++, but using some extra classes


  • Registered Users, Registered Users 2 Posts: 947 ✭✭✭LanceStorm


    Oh sorry forgot to mention that vital factor!!
    Its Java!


  • Registered Users, Registered Users 2 Posts: 947 ✭✭✭LanceStorm


    And Pat Patterson is always funny!!


  • Registered Users, Registered Users 2 Posts: 225 ✭✭Obscure


    Looks like problem here:


    //value returned from the call function & stored in opt1
    opt1 = result1.call();

    //Exit Function
    while(opt1 !=9)
    {
    // stuff
    }


    ... so if set "opt1" to 1 lets say, then while statement will go check over and over if "opt1 != 9" and it never is so "//stuff" will be executed over and over ad infiniam (?).

    maybe something like:

    //value returned from the call function & stored in
    opt1 = result1.call();

    //Exit Function
    while(opt1 !=9)
    {
    // stuff

    opt1 = result1.call();
    }


    is required.

    Ob


  • Registered Users, Registered Users 2 Posts: 6,342 ✭✭✭OfflerCrocGod


    A) Have you ever used System.out.println("String");?, if so USE IT.
    B) Do not use Keyboard unless you understand what its doing, you are doing a disservice to your self if you dont learn how to use BufferReader.
    C) I believe while(opt1 !=9) is to blame, its just testing opt1 over and over again but opt1 is the same number its never changed, give the user a choice to change it, put the while to encompass PRACTICALLY all of the main method except for the int opt1; part which should be int opt1=0; that way it keeps looping the question to the user asking for more input until it gets a nine.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 6,342 ✭✭✭OfflerCrocGod


    Or what Obscure says, although that means that nothing will be printed from the main class.


  • Registered Users, Registered Users 2 Posts: 947 ✭✭✭LanceStorm


    Thank You Obscure!!
    You are a legend!!

    It works perfectly now!!

    Cannot thank you enough!!
    No longer gonna fail my programming presentation cuz of you!!


  • Registered Users, Registered Users 2 Posts: 16,415 ✭✭✭✭Trojan


    All: please post code samples in [ code ] tags in order to maintain readability - makes things all that easier to debug.

    Cheers,
    Al.


  • Registered Users, Registered Users 2 Posts: 5,333 ✭✭✭Cake Fiend


    Is there no equivalent to a switch statement in Java?


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Originally posted by Sico
    Is there no equivalent to a switch statement in Java?
    There is a switch. Almost exactly the same as a C switch IIRC.


  • Advertisement
Advertisement