Question

In: Computer Science

Given the variable as coefficient, exponent and input. Write methods that will convert the input into...

Given the variable as coefficient, exponent and input. Write methods that will convert the input into String in the following format:

coefficient = 1,
exponent=1
input=-3

Method 1 does not take any input, (just coefficient and exponent only) so it should return output as: "1.0 * x^1" (with inverted comma)
Method 2 takes given input, so it should return output as: "1.0 * -3.0^1" (with inverted comma) (

Note coefficient 1 and exponent 3 are now 1.0 and 3.0 in string output.

If exponent=0, then just return "1.0" in both cases.

Also, No extra inbuilt functions are allowed.

P.S: I have tried my best to make question clear. If still not clear please ask me where you did not understand. But there are some spammers who only comment "TEXT NOT CLEAR" (even text in photo is clear), "QUESTION NOT SUFFICIENT" or paste some random answers. Please don't spam this question.
Edit: Thanks a lot. Please do it on Java.

Solutions

Expert Solution

import java.util.Scanner;

public class Solution {
        public static void main(String[] args) {
                System.out.println("----- Method 1 -------");
                method1();
                System.out.println();

                System.out.println("----- Method 2 -------");
                method2();
                System.out.println();
        }

        public static void method1() {
                float coefficient;
                int exponent;

                // Take Inputs from User
                Scanner sc = new Scanner(System.in);
                System.out.print("Coefficient: ");
                coefficient = sc.nextFloat();
                System.out.print("Exponent: ");
                exponent = sc.nextInt();

                // Print Result
                if (exponent != 0)
                        System.out.println(String.format("\"%f * x^%d\"", coefficient, exponent));
                else
                        System.out.println(String.format("\"%f\"", coefficient));
        }

        public static void method2() {
                double coefficient, input;
                int exponent;

                // Take Inputs from User
                Scanner sc = new Scanner(System.in);
                System.out.print("Coefficient: ");
                coefficient = sc.nextDouble();
                System.out.print("Exponent: ");
                exponent = sc.nextInt();
                System.out.print("Input: ");
                input = sc.nextDouble();

                // Print Result
                if (exponent != 0)
                        System.out.println(String.format("\"%f * %f^%d\"", coefficient, input, exponent));
                else
                        System.out.println(String.format("\"%f\"", coefficient));
        }
}

I have attached the solution for both the methods, method1 is without taking input and method2 is with input.


Related Solutions

Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
The strength coefficient (K) and the work hardening exponent (n) for a stainless steel were determined...
The strength coefficient (K) and the work hardening exponent (n) for a stainless steel were determined during the analysis of the data from a tensile test using a standard tensile specimen. The flow curve parameters for the stainless steel are K= 1250 MPa and n = 0.4. A compression test is performed on a specimen of the stainless steel. If the starting height and diameter of the specimen are 80 mm and 18 mm, respectively and the final height of...
Task 1: Class Term The class Term encapsulates the coefficient and exponent of a single term....
Task 1: Class Term The class Term encapsulates the coefficient and exponent of a single term. Exponents are limited in range from 0 to 99. public class Term : IComparable { private double coefficient; private integer exponent; // Creates a term with the given coefficient and exponent public Term (double coefficient, integer exponent) { … } // Evaluates the current term at x public double Evaluate (double x) { … } // Returns -1, 0, or 1 if the exponent...
A polynomial can be represented using linked list, storing coefficient and exponent of each component of...
A polynomial can be represented using linked list, storing coefficient and exponent of each component of polynomial in a node of it. Write a class in C++ to implement polynomials in three variables x, y and z (a polynomial may also have a constant term). Your class should implement methods for following operations. • Add two polynomials. • Multiply two polynomials. Use a two way header list for implementation. Your program should print the coefficient and exponent of each component...
Task 1: Class Term The class Term encapsulates the coefficient and exponent of a single term....
Task 1: Class Term The class Term encapsulates the coefficient and exponent of a single term. Exponents are limited in range from 0 to 99. public class Term : IComparable { private double coefficient; private integer exponent; // Creates a term with the given coefficient and exponent public Term (double coefficient, integer exponent) { … } // Evaluates the current term at x public double Evaluate (double x) { … } // Returns -1, 0, or 1 if the exponent...
Convert the following UML diagram into the Java code. Write constructor, mutator and accessor methods for...
Convert the following UML diagram into the Java code. Write constructor, mutator and accessor methods for the given class. Create an instance of the class in a main method using a Practice class. Note: The structure of the class can be compiled and tested without having bodies for the methods. Just be sure to put in dummy return values for methods that have a return type other than void.      
Write application in C# that enables a user to: Use Methods for user input and calculations...
Write application in C# that enables a user to: Use Methods for user input and calculations input the grade and number of credit hours for any number of courses. Calculate the GPA on a 4.0 scale using those values. Grade point average (GPA) is calculated by dividing the total amount of grade points earned, sometimes referred to as quality points, by the total number of credit hours attempted. For each hour, an A receives 4 grade or quality points, a...
Write a program in java that uses methods to input data for calculation, calculate, and display...
Write a program in java that uses methods to input data for calculation, calculate, and display the results of the calculations. That is, there are at least three methods. The problem is to write a program that calculates the area of a rectangle. This action should be repeatable.
Convert 0.875 to an IEEE 754 single-precision floating-point number. Show the sign bit, the exponent, and...
Convert 0.875 to an IEEE 754 single-precision floating-point number. Show the sign bit, the exponent, and the fraction. Convert -3.875 to an IEEE 754 double-precision floating-point number. Show the sign bit, the exponent, and the fraction Convert the IEEE 754 single-precision floating-point numbers 42E4800016 and 0080000016 to their corresponding decimal numbers.
In this project students will write a very simply program using Java’s input and output methods....
In this project students will write a very simply program using Java’s input and output methods. The program will prompt the user to enter responses to several questions, displaying the answer each time. The project is designed to give students an opportunity to practice very basic input and output via a simple project. Specification When the Java program starts it should prompt the user for input and print a response (including that input) as follows: Hello. What is your name?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT