Question

4 CS136: Computer Science II-Spring 2019 [10 points] Problem #4 A run is a sequence of adjacent repeated values. write a Java

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

Java Code:

import java.io.*;
import java.util.*;
import java.util.Random;
public class Runsprinter {
public static void main(String args[]) {
Random rand = new Random();
int n;
//declaring ArrayList with initial size n
ArrayList<Integer> arrli = new ArrayList<Integer>(20);
  
// Appending the new element at the end of the list
for (int i=0; i<20; i++){
n = rand.nextInt(6) + 1;
arrli.add(n);
}
// Printing elements
System.out.println(arrli);
//Given algorithem starated from here
boolean inRun = false;
for(int i = 0; i < 20; i++)
{
if(inRun){
if(arrli.get(i) != arrli.get(i-1)){
  
System.out.print(")");
inRun = false;
}
}
if(!inRun && i < 19){
if(arrli.get(i) == arrli.get(i+1)){
System.out.print("(");
  
inRun = true;
}
}
System.out.print(arrli.get(i));
}
}
}

Program Screenshot:

Your code 1 import java.1o. 2 import java.util. 3 import java.util.Randon 4 public class Runsprinter f public static void maiC Please Read The InstructionsB Online Jawa Compiler-Online. ArrayList in Java-GeeksforGee HL Online Java Compiler-Online × J

Add a comment
Know the answer?
Add Answer to:
4 CS136: Computer Science II-Spring 2019 [10 points] Problem #4 A run is a sequence of...
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
  • Write a program that generates a sequence of 20 random die tosses in an ArrayList and...

    Write a program that generates a sequence of 20 random die tosses in an ArrayList and that prints the die values, marking only the longest run, like this: 1 2 5 5 3 1 2 4 3 (2 2 2 2) 3 6 5 5 6 3 1 If there is more than one run of maximum length, mark the first one. Must use ArrayList, not an array!

  • need source code for NetBeans here is the file Write a class PrimeSequence that implements the Sequence inte...

    need source code for NetBeans here is the file Write a class PrimeSequence that implements the Sequence interface of Worked Example 10.1* and produces a sequence of prime numbers. * See Files section bj6_ch10_we.pdf Hint: Pseudocode to produce next prime: class Prime Sequence implements Sequence instance variable int p function next ()I isPrime-true While (isPrime) increment p; for (d-2; until d*d > p divides p) isPrime-false; d+) if (d if isPrime) return p; isPrime-true Use your PrimeSequence class to generate...

  • MATLAB Define the following computer programming terms and give an example of each. MATLAB example Sequence...

    MATLAB Define the following computer programming terms and give an example of each. MATLAB example Sequence Selection structure- Repetition structure - Relational operators (True = 1, False = 0) Logical operators (True = 1, False =0) And Or Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to Not ans ans Type the following relational operator commands into MATLAB in the command window one at a time. Write the results to...

  • Problem 5. Indicator variables S points possible (graded) Consider a sequence of n 1 independent tosses...

    Problem 5. Indicator variables S points possible (graded) Consider a sequence of n 1 independent tosses of a biased coin, at times k = 0,1,2,...,n On each toss, the probability of Heads is p, and the probability of Tails is 1 -p {1,2,.., at time for E resulted in Tails and the toss at time - 1 resulted in A reward of one unit is given if the toss at time Heads. Otherwise, no reward is given at time Let...

  • Lab 6 Instructions Objectives: • Executing and experimenting with programs that handle array related topics such...

    Lab 6 Instructions Objectives: • Executing and experimenting with programs that handle array related topics such as declaration, initialization, storing, retrieving, and processing elements. • Effectively manipulating and using ArrayList objects. • Becoming familiar with the use of arrays as method arguments and how array elements are passed to and returned from the called method. • Mastering the use of various debugging tools available on the Eclipse IDU. Important: EVERY one of the following Activities MUST be in a separate...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

  • Question 3 Program Language C++ Problem 3 Fibonacci Numbers 10 points Fibonacci numbers are a sequence...

    Question 3 Program Language C++ Problem 3 Fibonacci Numbers 10 points Fibonacci numbers are a sequence of numbers where each number is represented by the sum of the two preceding numbers, starting with 0 and 1: 0, 1, 1, 2, 3, 5, 8, etc Write a program that repeatedly prompts the user for a positive integer and prints out whether or not that integer is a Fibonacci number. The program terminates when-I is entered. Create a method with the following...

  • Create a class named Program10. Your program should have the following 4 methods (other helper methods...

    Create a class named Program10. Your program should have the following 4 methods (other helper methods are allowed, but these four methods must be implemented as specified). You need to define a global scanner object and only use it inside the main method and the getValues method, since other methods do not get any values from the input. getValues: This method does not have any input parameter and returns an array of integers. This method prompts the user with a...

  • Word Bank: a) python b) computer science c) algorithm d) program e) interpreter f) compiler g)...

    Word Bank: a) python b) computer science c) algorithm d) program e) interpreter f) compiler g) syntax h) semantics i) value J) variable k) operator l) operand m) expression n) statement o) input p)output q)call r) arguments s) library t) bug u) variable scope v) local variable w)global variable x) variable lifetime y) relational operators z) logical operators 1) Compares operands and results in a bool: 2) The duration of a variable's existence: 3) A list of instructions to solve...

  • Part A is done, I do not know how to start these steps.. "In computer science,...

    Part A is done, I do not know how to start these steps.. "In computer science, an abstract data type (ADT) is a mathematical model for data types where a data type is defined by its behavior (semantics) from the point of view of a user of the data, specifically in terms of possible values, possible operations on data of this type, and the behavior of these operations." (Source: "Abstract Data Types." Wikipedia. Wikimedia Foundation, 2 Nov. 2016. Web. 9...

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