Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I will make sure to thumbs up if you can help with my cs homework :) Would appreciate it dearly /* TSA1 */ import

Hello, I will make sure to thumbs up if you can help with my cs homework :) Would appreciate it dearly

image text in transcribed

image text in transcribed

image text in transcribed

/* TSA1 */ import java.util.Random; import java.util.Scanner; import java.util.Arrays; public class TSA1 { public static int[] prepareData(int[] prices){ /* Data is prepared by inserting random values up to 100. Data items may be repeated. Please refer to slides for the problem definiton. */ Random rand = new Random(); for (int i = 0; i

return span; }

public static void main(String[] args) { System.out.println("Enter the no of days:"); Scanner scan = new Scanner(System.in); int days = scan.nextInt(); int[] prices_initialized = new int[days]; int[] prices_populated = prepareData(prices_initialized);

// a set of days and their respective prices. /* does a random distribution of prices. */ /* Implement the computeSoan method, so as to get the correct results.*/ int[] span = computeSpan(prices_populated); /* The two lines below will print the output. Do not uncomment these lines. */ System.out.println("Prices:\t" + Arrays.toString(prices_populated)); System.out.println("Span:\t" + Arrays.toString(span)); } } ------

/* TSA2 */ import java.util.Random; import java.util.Scanner; import java.util.Arrays; public class TSA2 { public static int[] prepareData(int[] prices){ /* Data is prepared by inserting random values up to 100. Data items may be repeated. Please refer to slides for the problem definiton. */ Random rand = new Random(); for (int i = 0; i

return span; }

public static void main(String[] args) { System.out.println("Enter the no of days:"); Scanner scan = new Scanner(System.in); int days = scan.nextInt(); int[] prices_initialized = new int[days]; int[] prices_populated = prepareData(prices_initialized);

// a set of days and their respective prices. /* does a random distribution of prices. */ /* Implement the computeSoan method, so as to get the correct results.*/ int[] span = computeSpan(prices_populated); /* The two lines below will print the output. Do not uncomment these lines. */ System.out.println("Prices:\t" + Arrays.toString(prices_populated)); System.out.println("Span:\t" + Arrays.toString(span)); } }

More info:

computeScan and ADT examples from slides

image text in transcribed

image text in transcribed

Part 01 - A Time Series problem implementation (15 points) Implement the Time Scrics problem using a scries of requirements outlined below. 1. The problem definition is provided below: 2. The span Si of a stock's price on a certain day i is the maximum number of consecutive days (up to the current day) the price of the stock has been less than or equal to its price on day i. Let us suppose we are given with the stock prices for different days. Identify the span S for those days. Price vs. Day IL..Il Span[0,1,2,3,4,5,61 = {1,1,1,2,1,4,6} 3. Please refer to lesson 3 and 4 and your notes for more details from class discussion related to this problem. 4. In order to casily access the Time Series algorithm Version 1 is provided below: Algorithm - ComputeSpan P) Input: an n-element array P of numbers such that Pil is the price of the stock on a day i. of numbers such that S[i] is the span of the stock on a day i. Output: an n-element array 1: for i = 0 ton - 1 do 4 5: while h =P[D.top()) then D.pop() else done = true end if end while if Dis empty then h -1 15: else 16 h = D.top() 17: end if 18: S[iich 19. D.push(i) 20: end for 21: returns 10. The starter code file is provided inside the lab repository in a file named, TSA2. java. Here TSA refers to Time Series Algorithm. 11. Implement the computeSpan method using the approach 2 algorithm provided above by integrating the Stack ADT to the program. The details on how to implement the method are purposefully left out so as to challenge the students to think about the implementation on their own. 12. A sample code file named StackUsage.java is provided in the repository. The sample code may be used as a tool to learn how to use the built-in Stack ADT in Java. 13. For more details on the requirements for this program, please look at the comments in the source code file. 14. To validate the correctness of the output of the program, please pick an example from the slides and/or your notes and validate the correctness of the program. StackUsage.java 1 /* 2 Please refer to the link below for more examples: 3 https://www.geeksforgeeks.org/stack-class-in-java/ 4 */ import java.util.Arrays; import java.util.Scanner; import java.util.stack; public class stackusage { static void printstack(stack s) // If stack is empty then return if (s.isEmpty() return; int x = s.peek; 1/ Pop the top element of the stack s.pop(); // Recursively call the function Printstack printstack(s); 1/ Print the stack element starting // from the bottom System.out.print(x + " "); // Push the same element onto the stack // to preserve the order S.push(x); public static void main(String[] args) { // Initialize the stack System.out.println("Enter the size of the stack:"); Scanner scan = new Scanner(System.in); int stack_size = scan.nextInt(); Stack stack = new Stack ; for (int i = 0; i =P[D.top()) then D.pop() else done = true end if end while if Dis empty then h -1 15: else 16 h = D.top() 17: end if 18: S[iich 19. D.push(i) 20: end for 21: returns 10. The starter code file is provided inside the lab repository in a file named, TSA2. java. Here TSA refers to Time Series Algorithm. 11. Implement the computeSpan method using the approach 2 algorithm provided above by integrating the Stack ADT to the program. The details on how to implement the method are purposefully left out so as to challenge the students to think about the implementation on their own. 12. A sample code file named StackUsage.java is provided in the repository. The sample code may be used as a tool to learn how to use the built-in Stack ADT in Java. 13. For more details on the requirements for this program, please look at the comments in the source code file. 14. To validate the correctness of the output of the program, please pick an example from the slides and/or your notes and validate the correctness of the program. StackUsage.java 1 /* 2 Please refer to the link below for more examples: 3 https://www.geeksforgeeks.org/stack-class-in-java/ 4 */ import java.util.Arrays; import java.util.Scanner; import java.util.stack; public class stackusage { static void printstack(stack s) // If stack is empty then return if (s.isEmpty() return; int x = s.peek; 1/ Pop the top element of the stack s.pop(); // Recursively call the function Printstack printstack(s); 1/ Print the stack element starting // from the bottom System.out.print(x + " "); // Push the same element onto the stack // to preserve the order S.push(x); public static void main(String[] args) { // Initialize the stack System.out.println("Enter the size of the stack:"); Scanner scan = new Scanner(System.in); int stack_size = scan.nextInt(); Stack stack = new Stack ; for (int i = 0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions