CS1301 Lab 08-Loops; One-Dimensional Arrays Solved

35.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

5/5 - (1 vote)

Good programmers think before they begin coding. Part I of this assignment involves brainstorming with a group of peers with no computers to talk about a strategy for solving this week’s lab. Breakup into groups based on your seating (3-4 people per group) and brainstorm about how to solve the problems below. Make sure everyone understands the problem and sketch out potential ways to move toward a solution.  You may find it helpful to look over the required readings for this week.

Note: Brainstorms are to help you get started with the lab and get you in the habit of designing before you code.  You won’t submit them to eLC.

Introduction

This lab assignment continues to give you practice using loops, particularly loops with variable termination conditions, and it also provides you an opportunity to use one-dimensional arrays.

Recall that an array is used to store a collection of data. The data can be values of Java primitive data types or else objects (for instance, String objects), but the data stored must all be of the same type. An array is created in Java using a statement of the form

BaseType[] arrayName = new BaseType[length];

where BaseType is the underlying type of data stored in the array, arrayName is just a variable name used to refer to the array, and length is a positive integer indicating the number of elements in the array.  An array element can be referred to using arrayName[i], where i is an integer index (starting with 0). The length of the array is stored in the variable arrayName.length.

The arrays used in this assignment will all be one-dimensional (that is, they won’t be arrays of arrays), and they will all use Java primitive data types as the base type.

For the assignment, you are to create a program using two arrays of type double to store multiple values for x and y, where y is defined as the below function.

𝑦𝑦 = 20.0 ∗ |sin 𝑥𝑥|

It is assumed that the angles for the sine function are measured in radians. In the program, both the values for x and y are computed based on input from the user. Once the values are found, the program should print them out as indicated by the examples. The program should also print out a graphical representation of the function, using a sequence of ‘*’s to indicate the magnitude of y.

Lab Objectives

By the end of the lab, you should be able to:

  • create nested loops and loops with variable termination conditions;
  • create and manipulate one-dimensional arrays.

 

Prerequisites

The lab deals with material from Chapter 4 (loops) and Section 7.1 (arrays).

What to Submit

The file StarGraph.java should be submitted to eLC for grading.

Instructions

  1. The class should be called
  2. You must include a comment stating your name, the date, program purpose, and containing a statement of academic honesty as shown in Lab 02. When writing, you must also abide by the Java formatting and naming conventions.
  3. The program should prompt the user for the following information:

o the size N of the array (that is, the number values of x for which y will be computed); o xmin, a minimum value for x; o an increment for x (a value by which x is increased).

The messages to display to the user are shown in the examples below.

  1. Note that the size of the arrays must be a positive integer. If the user enters 0 or a negative integer, then the program should print out an error message and terminate the program, as indicated in the examples.
  2. The increment value should be a positive floating point value (a double). If the user enters 0 or a negative number, then the program should print an error message and terminate.
  3. Once N, xmin, and the increment are established, the program should create two double arrays, one to hold the x values and one to hold the y The size of both arrays will be N.

The first element of the x-array will hold xmin, and each successive element will be incremented by the value provided by the user. The values for the y-array will be defined using the corresponding value from the x-array and the function defined earlier.  To compute the value for y, the program should use the Math.abs and Math.sin methods.

Observe that in order to populate the arrays with values, loops must be used.

  1. Once computed, the values of x and y should be printed to the console. Each pair of values for x and y stored in the arrays should be printed. Format the values of x and y to have 3 digits after the decimal point.
  2. After the values have been printed, a simple graph of the function should be plotted using asterisks (*). Each successive line corresponds to an x value, and the number of ‘*’s on the line corresponds to the whole-number part of y (the fractional part is ignored/truncated). To compute the number of ‘*’s, you should not round the value for y (instead, truncate it).
  • lab08-goiye9.zip