Question

I should use the array and loop to create a java program according to the instruction, but I have no idea how to do it.Introduction This lab assignment continues to give you practice using loops, particularly loops with variable termination con3. The program should prompt the user for the following information: o the size N of the array (that is, the number values ofSample Input and Output These are sample program runs. Input is underlined. Your output should be consistent with what is shoPlease enter the number of x values to process: 9 Enter a minimum value for x: -1.5707963267 Enter the amount to increment x:Please enter the number of x values to process: 10 Enter a minimum value for x: -5 Enter the amount to increment x: 0.20 ValuPlease enter the number of x values to process: 0 The number of x values must be an integer greater than 0. Please enter the

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Scanner class is used to get input from the user (from keyboard).

PROGRAM:

PlotSine.java :

package HomeworkLib.answers;

import java.util.Scanner;

public class PlotSine {
    public static void main(String[] args) {
        double[] x,y;
        Scanner sc = new Scanner(System.in);

        // asking user to input the length of array x
        System.out.print("Please enter the number of x values to process: ");
        // getting value from user
        int n = sc.nextInt();
        //checking if length is <= zero, if true then printing error msg and exiting
        if(n <= 0){
            System.out.println("The number of x values must be an integer greater than 0.");
            // exiting the program if condition is true with exit status as 1
            System.exit(1);
        }

        // this portion will be executed only if the above condition is false
        System.out.print("Enter a minimum value for x: ");
        // getting the starting value of x
        double min_val = sc.nextDouble();

        System.out.print("Enter the amount to increment x: ");
        //getting the increment value
        double inc = sc.nextDouble();
        //checking if increment value is <= zero, if true then printing error msg and exiting
        if(inc <= 0){
            System.out.println("The increment must be a decimal number greater than 0.");
            System.exit(1);
        }

        // this portion will be executed only if the above condition is false
        // creating two arrays of double type with length entered by user
        x = new double[n];
        y = new double[n];

        // initializing the 1st element of both the arrays
        x[0] = min_val;
        y[0] = 20.0 * Math.abs(Math.sin(x[0]));

        // calculating the angles and storing them in array y and also printing the values to the screen
        // It is done in the same loop in order to reduce the computation time of the program
        System.out.println("\nValues");

        // printf takes a formatted string
        // ".3f": 'f' signifies that it will be replaced by a double,
        // '.3' signifies that number of digits will be three after the decimal point
        System.out.printf("x: %.3f, y: %.3f\n",x[0], y[0]);

        // 1st for loop
        for(int i = 1; i < n; i++){
            // incrementing the value of array x by user input
            x[i] = x[i-1] + inc;
            // calculating the angle and storing in array y
            y[i] = 20.0 * Math.abs(Math.sin(x[i]));
            // printing the output to the screen
            System.out.printf("x: %.3f, y: %.3f\n",x[i], y[i]);
        }

        // printing the graph
        System.out.println("\nGraph");
        //2nd for loop
        // outer for loop will loop through the entire array y
        for (int i = 0; i < n ; i++) {
            // printing ":" without changing line
            System.out.print(":");
            //inner for loop will execute the whole-number part of array-y times
            // whole-number part is calculated by taking the floor of the floating point number
            for (int j = 0; j < Math.floor(y[i]); j++) {
                // printing "*" without changing line
                System.out.print("*");
            }
            // printing a new line
            System.out.println();
        }
    }
}

OUTPUT:

case 1:

Please enter the number of x values to process: 10
Enter a minimum value for x: 0
Enter the amount to increment x: 0.25

Values
x: 0.000, y: 0.000
x: 0.250, y: 4.948
x: 0.500, y: 9.589
x: 0.750, y: 13.633
x: 1.000, y: 16.829
x: 1.250, y: 18.980
x: 1.500, y: 19.950
x: 1.750, y: 19.680
x: 2.000, y: 18.186
x: 2.250, y: 15.561


Graph
:
:****
:*********
:*************
:****************
:******************
:*******************
:*******************
:******************
:***************

case 2:

Please enter the number of x values to process: 9
Enter a minimum value for x: -1.5707963267
Enter the amount to increment x: 0.78539816339

Values
x: -1.571, y: 20.000
x: -0.785, y: 14.142
x: 0.000, y: 0.000
x: 0.785, y: 14.142
x: 1.571, y: 20.000
x: 2.356, y: 14.142
x: 3.142, y: 0.000
x: 3.927, y: 14.142
x: 4.712, y: 20.000

Graph
:********************
:**************
:
:**************
:********************
:**************
:
:**************
:********************

case 3:

Please enter the number of x values to process: 10
Enter a minimum value for x: -5
Enter the amount to increment x: 0.20

Values
x: -5.000, y: 19.178
x: -4.800, y: 19.923
x: -4.600, y: 19.874
x: -4.400, y: 19.032
x: -4.200, y: 17.432
x: -4.000, y: 15.136
x: -3.800, y: 12.237
x: -3.600, y: 8.850
x: -3.400, y: 5.111
x: -3.200, y: 1.167

Graph
:*******************
:*******************
:*******************
:*******************
:*****************
:***************
:************
:********
:*****
:*

case 4:

Please enter the number of x values to process: 0
The number of x values must be an integer greater than 0.

case 5:

Please enter the number of x values to process: 100
Enter a minimum value for x: -50
Enter the amount to increment x: 0
The increment must be a decimal number greater than 0.

SCREENSHOTS:

PlotSine.java:

PlotSine.java package HomeworkLib.answers; import java.util.Scanner; public class PlotSine { public static void main(String[] args)

PlotSine.java System.out.println(the increment must be a decrILIAI number greater than 0.77 System.exit( status: 1); 0 00 Ji

c PlotSine.java ISL TOT 100 for(int i = 1; i<n; i++) { // incrementing the value of array x by user input x[i] = x[i-1] + inc

Outputs:

case 1:

Run: PlotSinex C:\Program Files\Java\jdk1.8.0 144\bin\java.exe ... Please enter the number of x values to process: 10 Enter

case 2:

Run: PlotSinex C:\Program Files\Java\jdk1.8.0 144\bin\java.exe ... Please enter the number of x values to process: 9 Enter

case 3:

Run: + → If PlotSinex C:\Program Files\Java\jdk1.8.0_144\bin\java.exe ... Please enter the number of x values to process: 1

case 4:

Run: PK PlotSine C:\Program Files\Java\jdk1.8.0_144\bin\java.exe ... Please enter the number of x values to process: 0 The

case 5:

Run: PlotSinex C:\Program Files\Java\jdk1.8.0_144\bin\java.exe ... Please enter the number of x values to process: 100 Ente

REMOVE "PACKAGE CHEGG.ANSWERS;" LINE BEFORE EXECUTING!

HOPE THIS HELPS!

Add a comment
Know the answer?
Add Answer to:
I should use the array and loop to create a java program according to the instruction,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that...

    Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that holds an array for the integer values 1-10. Pass the array to a method that will calculate the values to the power of 2 of each element in the array. Then return the list of calculated values back to the main method and print the values

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • in java / You will: // 1- create a square 2 dimensional array of random size...

    in java / You will: // 1- create a square 2 dimensional array of random size (less than 11) // 2- fill the array with random integers from 1 to the size limit // 3- print the array // 4- prompt to see if the user wants to do another //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // 1- The square can use the same random value for x and y. Don't allow 0 size arrays. // 2- Maybe a nested set of for loops? to...

  • Create a java program that with an integer array that has ten numbers in it. Ask...

    Create a java program that with an integer array that has ten numbers in it. Ask the user for an integer. Search your array and if found it tell them at what position it was found. If not found tell them the number was not found.

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

  • Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a...

    Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a one-dimensional array with the first 30 Fibonacci numbers using a calculation to generate the numbers. Note: The first two Fibonacci numbers 0 and 1 should be generated explicitly as in -long[] series = new long[limit]; //create first 2 series elements series[0] = 0; series[1] = 1; -But, it is not permissible to fill the array explicitly with the Fibonacci series’ after the first two...

  • This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to...

    This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to Using Individual Variables One of the main advantages of using arrays instead of individual variables to store values is that the program code becomes much smaller. Write two versions of a program, one using arrays to hold the input values, and one using individual variables to hold the input values. The programs should ask the user to enter 10 integer values, and then it...

  • Please complete this code for java Lab Write a program as follows: Create an array with...

    Please complete this code for java Lab Write a program as follows: Create an array with 10 elements. The array should contain numbers generated randomly between 1 and 100 Ask the user to enter any number - Search the array to see if the user entered number is in the array If the user-entered number is in the array, print a 'match found' message. Also print at which index the match was found, else print a 'match not found' message...

  • IN JAVA 1. Create a program that prompts the user for an integer and then displays...

    IN JAVA 1. Create a program that prompts the user for an integer and then displays a message indicating whether or not the number is a perfect square. This can be determined by finding the square root of a number, truncating it (by casting the double result), and then squaring that result 2. Write a program that prompts the user for two numbers. The first number is a minimum value and the second number is a maximum value. RandomNum then...

  • Create a C program that: Within main, declares a linear array consisting of 10 double values....

    Create a C program that: Within main, declares a linear array consisting of 10 double values. You can assign values to the array when it is declared or fill them items manually using scanf () - your choice. Also, declare three doubles: min, max, and average. Then from within main, a function is called. The function should take a pointer to the array declared above and then finds the maximum value, the minimum value, and calculates the average of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT