Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please write the answer for the below questions, and explain each steps and procedures: (a) Given a string, find out the length of the longest
Please write the answer for the below questions, and explain each steps and procedures:
(a) Given a string, find out the length of the longest consecutive substring without repeated characters. For example, if the input string is "abcace", then one possible longest consecutive substring without repeated characters is "abc", which has a length of 3 . Note that there could be multiple such substrings with the same length. [10 marks] b) Write a method named repeatCheck that accepts two Strings s1 and s2 as parameters. It returns true if s1 is composed entirely of repetitions of s2 and false otherwise. Assume both Strings have at least one character. [10 marks] The following table shows some sample input and output. public static boolean repeatCheck(String s1, String s2) { Question 5 (20 marks) (a) Write a method called mergeArray that merges two integer arrays nums 1 and nums 2 to generate a single non-decreasing array. Assume that the two integer arrays nums1 and nums2 are in non-decreasing order respectively before being processed by the method. [10 marks] (b)Write a method called replaceNumber that accepts two arrays of integers nums1 and nums2, in addition to one integer n. This method replaces any occurrences of nums 2 in nums 1 with the given number n. The sequence of elements in nums 2 may be anywhere in nums 1 but must appear consecutively in the same order. [10 marks] For example, if int [] nums 1={1,2,3,8,5,1,2,3,4,5}, int [ ] nums 2={1,2,3}, the call of replaceNumber(nums 1 , nums 2,9 ) will modify nums 1 with values of {9,9,9,8,5,9,9,9,4, 5}. Replaced numbers are underscored. Note that the pattern could occur many times and consecutively. For example, if int [] nums 1={1,2,1,2,5,1,2,3,4,5}, int [] nums 2={1,2}, the call of replaceNumber(nums 1 , nums 2,0 ) will modify nums 1 with values of {0,0,0,0,5,0,0,3,4,5}. If nums 2 could not be found in nums 1 , nums 1 will not be modified. For example, if int [] nums 1={1,2,4,2,5,1,2,3,4,5}, int [] nums 2={2,1}, the call of replaceNumber(nums 1 , nums2, 0 ) will return with the values of ={1,2,4,2,5,1,2,3,4, 5}. You can assume that both arrays will have lengths larger than 1 . Please note that nums1's values need to be modified in place - don't return a new array. Question 6 (20 marks) Assume the Point class is defined as below: class Point \{ double x; double y; Point(double xIn, double yIn) \{ x=xIn; y=yIn; \} \} (a) Write a class called Triangle that represents a triangle determined by three Points. [6 marks] We need to have the following methods: public Triangle(Point p1, Point p2, Point p3) Construct a new triangle that is determined by the given three points public Point getRemote() Return the point which has the largest distance to the origin (0,0) public String toString() Return a string representation of this triangle, such as "[(1.0,2.0),(3.0,4.2),(10.4,12)]" (b) Add another constructor to Triangle class. Construct a new triangle with the following three points. [3 marks] (c) Add the following method to Triangle class. [2 marks] public double getLongestSide() This method computes the length of each side and returns the longest length. The length of a side between two points (x1,y1) and (x2,y2) could be calculated via (x1x2)2+(y1y2)2 (d) Add the following method to Triangle class. [2marks] public double getArea() This method returns the area of the triangle. The area could be computed with the formula area =s(sside1)(sside2)(sside3) s=2side1+side2+side3 (e) Add the following method to the Triangle class. [2marks] public boolean isAcute() Return true if this triangle is an acute triangle; return false otherwise. Denote c as the length of the longest side and a,b as lengths of other sides, if a2+b2>c2, then this triangle is acute. (f) Add a method sort(Triangle [] t) to sort the triangles according to their areas in an increasing order and display their areas in non-decreasing order to the console. [5marks] Java sorting libraries are not allowed
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started