Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Java by (13.1k points)

Can anyone help me with me code for menu driven program as I got stuck for the menu part. Here is my code:

import java.util.Scanner;

public class Polynomial2 {

    public static void main(String[] args) {

        int i, coef, x, deg, count = 2;

        double total = 0.0, result;

        char opt;

        Scanner sc = new Scanner(System.in);

        do {

            System.out.println("Enter Degree");

            deg = sc.nextInt();

            System.out.println("Enter x");

            x = sc.nextInt();

            for (i = 0; i <= deg; i++) {

                System.out.println("Enter Coefficent for " + i);

                coef = sc.nextInt();

                total = total + coef * Math.pow(x, i);

            }

            count--;

        } while (count > 0);

        System.out.println("Total=" + total);

        {

            coef = sc.nextInt();

            result = sc.nextDouble();

            opt = sc.next().charAt(0);

            switch (opt) {

                case 'a':

                    result = total + coef * Math.pow(x, i);

                    System.out.println("Evalute Polynomial 1 at x");

                    break;

                case 'b':

                    result = total + coef * Math.pow(x, i);

                    System.out.println("Evaluate Polynomial 2 at x");

                    break;

                case 'c':

                    result = total + total;

                    System.out.println("Add Polynomial 1 and 2");

                    break;

                case 'd':

                    result = total - total;

                    System.out.println("Subtract Polynomial 1 from Polynomial 2");

                    break;

                case 'e':

                    result = total - total;

                    System.out.println("Subtract Polynomial 2 from Polynomial 1");

                    break;

                case 'f':

                    result = total * x;

                    System.out.println("Multiply Polynomial 1 by a constant");

                    break;

                case 'g':

                    result = total * x;

                    System.out.println("Multiple Polynomial 2 by a constant");

                    break;

            }

        }

    }

}

Any help would be appreciated.

1 Answer

0 votes
by (26.7k points)

You can take the below code as a reference for your menu driven program:

String menuPrompt = "Please select from the following options:\n\n" +

    "a - Evaluate Polynomial 1 at x\n" +

    "b - Evaluate Polynomial 2 at x\n" +

    "c - Add Polynomial 1 and 2\n" +

    "d - Subtract Polynomial 1 from Polynomial 2\n" +

    "e - Subtract Polynomial 2 from Polynomial 1\n" +

    "f - Multiply Polynomial 1 by a constant\n" +

    "g - Multiple Polynomial 2 by a constant\n\n" +

    "Enter selection: ";

System.out.print(menuPrompt);

char opt = sc.nextChar();

switch(opt) {

    case 'a':

        result = total + coef * Math.pow(x, i);

        break;

    case 'b':

    //...etc...

}

I hope this will help.

Want to become a Java Expert? Join Java Certification now!!

Want to know more about Java? Watch this video on Java Course | Java Tutorial for Beginners:

Related questions

0 votes
1 answer
asked Dec 15, 2020 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Mar 9, 2021 in Java by Jake (7k points)
0 votes
1 answer

Browse Categories

...