In: Computer Science
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.
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.