Hi. Im working on a problem and Im having trouble with it.

In the class Mymath, write a method called lab51 that reads three doubles a, b and c followed by one non-negative integer k, and prints the values of the function y = ax2 + bx + c

for the first k values. Validate the k value.
Your program should functions as follows:

Enter a:  1.5
Enter b: 2.5
Enter c: 3.5
Enter k: 4

x    y= 1.5x^2 + 2.5x + 3.5

1    7.5
2    14.5
3    24.5
4    37.5

Heres my input:

import java.util.Scanner;

public class MyMath {
    public static void main(String[] args) 
        Scanner input=new Scanner(System.in);
        double a;
        System.out.println("Enter a");
        a = input.nextDouble();

        double b;
        System.out.println("Enter b");
        b = input.nextDouble();

        double c;
        System.out.println("Enter c");
        c = input.nextDouble();

        int k;
        System.out.println("Enter k");
        k = input.nextInt();

        double x;
        x = input.nextInt();

        double y;
        y = (a * x * x) + (b * x) + c;


Im having trouble displaying the output, and Im confused what my professor means by "validate k" and also having trouble with where k belongs. PLEASE HELP!

Edited by mike_2000_17: Fixed formatting

8 Years
Discussion Span
Last Post by Jocamps

What does k refer to in your program. Where is the number held by the variable k used in the program. The formula only needs 3 variables, so I'm a bit confused where k is coming from. Can you please clarify this?
By the way please use code tags when supplying code on this site. Thanks.


Im not sure either. The problem just says "Validate k" and thats where I get confused


this is what I've understand on this problem:

printing the first k values of the function meaning if k is 1, you output one value of y using x=1. The validating part is where you substitute k to x (x=1) . In your example given above if k is 4 then there are four values of y evaluated and using x = k where k = 1, 2, 3, 4

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.