Question

GetNumber Reuse the GetNumber0 method that you have been refining in the previous two labs Get Array Using your GetNumber0 me

using System; 3 namespace ConsoleApp2 5 public class Program 7 public static void Main(string] args) int[] tmp,res; :res-Exer

34 :; int? length -GetNumber); : while (lengthnull); 36 37 length-GetNumber); :if (length0) 39 40 41 42 return null; array-ne

PrintArray(int[] 67 68 69 70 71 曰: : public static void x,int perLine-20) :: throw new NotImplementedException(); 72 ; public

GetNumber and Getarray parts are working but I need help figuring out part 7_4 and 7_5 (I do not need Printarray). My professor provided the sections in which we need to fill in with code to make them run and pass the test files provides by the professor in Visual Studio.

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

________________

import java.util.Arrays;
import java.util.Scanner;

public class ArraySequence {

public static void main(String[] args) {

int[] a = GetArray();
System.out.print("Printing array: ");PrintArray(a);
int[] longestArray = Excercise7_4(a);
System.out.print("\nLongest sequence: ");PrintArray(longestArray);
}

public static int[] GetArray() {
Scanner scanner = new Scanner(System.in);
System.out.print("How many numbers you want in the array: ");
int arraySize = scanner.nextInt();
int[] numbers = new int[arraySize];
for (int count = 1; count <= arraySize; count++) {
System.out.print("Enter number: ");
numbers[count - 1] = scanner.nextInt();
}
return numbers;
}

public static void PrintArray(int[] array) {

int perLine = 20;
int index = 0;
for (int num : array) {
if (index % perLine == 0) {
System.out.println();
}
System.out.print(num + " ");
index += 1;
}
}

public static int[] Excercise7_4(int[] x) {
int previousElement = x[0];
int previousCount = 1;
int currentElement = x[0];
int currentCount = 1;

for (int index = 0; index < x.length - 1; index++) {
if (x[index] == x[index + 1]) {
currentCount += 1;
currentElement = x[index];
} else {
if (currentCount > previousCount) {
previousElement = currentElement;
previousCount = currentCount;
} else if (currentCount == previousCount && currentElement > previousElement) {
previousElement = currentElement;
previousCount = currentCount;
}
currentCount = 1;
currentElement = x[index + 1];
}

}
if(currentCount>previousCount){
previousElement=currentElement;
previousCount=currentCount;
}
if(previousCount==1 && previousElement<x[x.length-1]){
previousElement=x[x.length-1];
}

int[] newArray = new int[previousCount];
Arrays.fill(newArray, previousElement);
return newArray;
}
}

Add a comment
Know the answer?
Add Answer to:
GetNumber and Getarray parts are working but I need help figuring out part 7_4 and 7_5...
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
  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • Need help with this binary tree program. I will give a thumbs up for anybody who...

    Need help with this binary tree program. I will give a thumbs up for anybody who helps. Source Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class Trees { public static int[] prepareData(int[] data){ /* Data is prepared by inserting random values between 1 and data.length. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ ArrayList list = new ArrayList(); for (int i=0; i< data.length; i++) {...

  • \\ this is the code i need help to get this part working the rest works...

    \\ this is the code i need help to get this part working the rest works but this and im not sure what is missing or how to make it work. this is what they asking for to do and there is two errors the ones shown in the pictures this is all the information I have from zybooks\\ Q3. (54 Points) Complete a public class to represent a Movie as described below. (a-e 4 pts each) (f-h 8 pts...

  • Need help with this Java. I need help with the "to do" sections. Theres two parts...

    Need help with this Java. I need help with the "to do" sections. Theres two parts to this and I added the photos with the entire question Lab14 Part 1: 1) change the XXX to a number in the list, and YYY to a number // not in the list 2.code a call to linearSearch with the item number (XXX) // that is in the list; store the return in the variable result 3. change both XXX numbers to the...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • static public int[] Question() // retrieve an integer 'n' from the console // the first input...

    static public int[] Question() // retrieve an integer 'n' from the console // the first input WILL be a valid integer, for 'n' only. // then read in a further 'n' integers into an array. // in the case of bad values, ignore them, and continue reading // values until enough integers have been read. // this question should never print "Bad Input" like in lab // hint: make subfunctions to reduce your code complexity return null; static public int...

  • Hey, I was wondering if there’s another way to make these methods not reliable to imports...

    Hey, I was wondering if there’s another way to make these methods not reliable to imports such as “import java.util.Arrays” and “import java.util.Hash” I am still new in coding and would like to know if there’s another way to do it! Here is the code! (Thank you in advance and I will really appreciate it :) ) /** This exercise involves implementing several methods. Stubs for each method with documentation are given here. It is your task to fill out...

  • I am having trouble understanding the concept of polymorphism. I know that I only need to...

    I am having trouble understanding the concept of polymorphism. I know that I only need to edit the MathDriver.java (driver) class, but I am not entirely sure how. Here is my MathDriver.java driver class, the one that I need to finish: Here is the parent class, Math.java: Lastly, here are the child classes, Addition, Subtraction, and Multiplication Given the following program, finish the driver class to demonstrate polymorphism. Program: PolyMath Copy and paste your driver class into the textbox for...

  • Please Help! Need in Java C++ Object oriented programming. Thank you! Method 2: public static int...

    Please Help! Need in Java C++ Object oriented programming. Thank you! Method 2: public static int sumCapped(int[] nums, int x) This method will return the largest sum that is less than or equal x found in one pass of the array. This means that you must check each number in succession to determine whether or not you should add it in. For example, for the array {4, 2, 3, 5} with the value of the paramter x set to 7,...

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